cse1303 part a data structures and algorithms lecture a3 – basic data structures (stacks)
Post on 19-Dec-2015
218 views
TRANSCRIPT
![Page 1: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/1.jpg)
CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms
Lecture A3 – Basic Data Lecture A3 – Basic Data Structures (Stacks)Structures (Stacks)
![Page 2: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/2.jpg)
2
Basic Data StructuresBasic Data Structures
• Stacks
• Queues
• Lists
![Page 3: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/3.jpg)
3
Overview of StacksOverview of Stacks
• What is a Stack?
• Operations on a Stack– push, pop– initialize– status: empty, full
• Implementation of a Stack.
• Example: Reversing a sequence.
![Page 4: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/4.jpg)
4
A StackA Stack
Top
Items on the Stack
![Page 5: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/5.jpg)
5
PushPush
Top
After
Top
Before
![Page 6: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/6.jpg)
6
PopPop
Top
Before After
Top
This comes off the stack
![Page 7: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/7.jpg)
7
OperationsOperations
• Initialize the Stack.
• Pop an item off the top of the stack.
• Push an item onto the top of the stack.
• Is the Stack empty?
• Is the Stack full?
• Clear the Stack
• Determine Stack Size
![Page 8: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/8.jpg)
8
Stack PropertiesStack Properties
• Sequence of items, where insertions and deletions are done at the top.
• Main operations are pop and push.
• Last-In First Out (LIFO).
• Used when calling functions.
• Used when implementing recursion.
![Page 9: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/9.jpg)
9
ImplementationImplementation
Top
index0
1
2
:
array(upside-down)
int top;
float entry[MAXSTACK];
![Page 10: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/10.jpg)
10
#define MAXSTACK 20
struct StackRec{ int top; float entry[MAXSTACK];};
typedef struct StackRec Stack;
Stack:
.
.
.
entry:
top:
![Page 11: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/11.jpg)
11
#ifndef STACKH#define STACKH#include <stdbool.h>#define MAXSTACK 20
struct StackRec{ int top; float entry[MAXSTACK];};
typedef struct StackRec Stack;
void intializeStack(Stack* stackPtr);bool stackEmpty(const Stack* stackPtr);bool stackFull(const Stack* stackPtr);void push(Stack* stackPtr, float item);float pop(Stack* stackPtr);
#endif
![Page 12: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/12.jpg)
12
#include <stdio.h>#include <stdlib.h>#include “stack.h”
void initializeStack(Stack* stackPtr){ stackPtr -> top = -1;}
top:
.
.
.
entry:
Stack:
addr of Stack
stackPtr:
-1
![Page 13: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/13.jpg)
13
bool stackEmpty(const Stack* stackPtr){ if (stackPtr-> top < 0) { return true; } else { return false; }}bool stackFull(const Stack* stackPtr){ if (stackPtr -> top >= MAXSTACK-1) { return true; } else { return false; }}
![Page 14: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/14.jpg)
14
void push(Stack* stackPtr, float item){ if (stackFull(stackPtr)) { fprintf(stderr, “Stack is full\n”); exit(1); } else { stackPtr-> top++; stackPtr-> entry[stackPtr-> top] = item; }}
![Page 15: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/15.jpg)
15
float pop(Stack* stackPtr){ float item;
if (stackEmpty(stackPtr)) { fprintf(stderr, “Stack is empty\n”); exit(1); } else { item = stackPtr-> entry[stackPtr-> top]; stackPtr-> top--; }
return item;}
![Page 16: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/16.jpg)
16
Take a sequence:
Then pop each number off the stack
Reversing a SequenceReversing a Sequence
-1.5 2.3 6.7
Push onto a stack one number at a time
Top
![Page 17: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/17.jpg)
17
2.3 6.7
Push onto a stack one number at a time
-1.5
Reversing a SequenceReversing a Sequence
TopThen pop each number off the stack
![Page 18: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/18.jpg)
18
6.7
Push onto a stack one number at a time
2.3-1.5
Reversing a SequenceReversing a Sequence
Top Then pop each number off the stack
![Page 19: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/19.jpg)
19
2.3-1.5
6.7
Reversing a SequenceReversing a Sequence
Top
Then pop each number off the stack
Push onto a stack one number at a time
![Page 20: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/20.jpg)
20
2.3-1.5
Reversing a SequenceReversing a Sequence
Top
6.7
Then pop each number off the stack
Push onto a stack one number at a time
![Page 21: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/21.jpg)
21
-1.5
Reversing a SequenceReversing a Sequence
Top
6.7 2.3
Then pop each number off the stack
Push onto a stack one number at a time
![Page 22: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/22.jpg)
22
Reversing a SequenceReversing a Sequence
Top
6.7 2.3 -1.5
Then pop each number off the stack
Push onto a stack one number at a time
![Page 23: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/23.jpg)
23
module reverse (){ initialize the Stack loop{ input nextNumber if (end of input) then {exit loop} if (Stack is not full) then {push nextNumber onto Stack} } loop { if (Stack is empty) then {exit loop} pop nextNumber off the Stack output nextNumber }}
![Page 24: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/24.jpg)
24
#include <stdio.h>#include “stack.h”int main(){ Stack theStack; float next;
initializeStack(&theStack); printf(“Enter number sequence: ”); while (scanf(“%f”, &next) != EOF) { if (!stackFull(&theStack)) { push(&theStack, next); } } while (!stackEmpty(&theStack)) { next = pop(&theStack); printf(“%f”, next); } printf(“\n”);}
![Page 25: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/25.jpg)
25
RevisionRevision• Stack
• Operations on a Stack– push, pop– initialize– status: empty, full– others: clear, size
• Example: Reversing a sequence.
• Implementation.
![Page 26: CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)](https://reader031.vdocuments.us/reader031/viewer/2022032309/56649d375503460f94a0fb8b/html5/thumbnails/26.jpg)
26
Next LectureNext Lecture• Queue
• Main Operations
• Implementation
Revision: ReadingRevision: Reading• Kruse - Chapters 3.1.1 – 3.1.5• Deitel & Deitel – Chapter 12.5• Langsam - Chapter 2• Standish – Chapters 7.1 – 7.6
PreparationPreparationNext lecture: Queues• Read 4.1 to 4.2 in Kruse et al.
• Deitel & Deitel 12.6