VIOLATION DE LA PARITE Gaetano Barone Séminaire dorientation.
C++ Programming: Program Design Including Data Structures,...
Transcript of C++ Programming: Program Design Including Data Structures,...
Lesson 4
Stacks
Readings: chapter 18
Stacks
• Stack: a data structure in which elements are added and removed from one end only
– Addition/deletion occur only at the top of the stack
– Last in first out (LIFO) data structure
• Operations:
– Push: to add an element onto the stack
– Pop: to remove an element from the stack
– isEmpty: to check if the stack is empty
CSE 143 2
Stacks (cont’d.)
CSE 143 3
Stacks (cont’d.)
4 CSE 143
Implementation of Stacks as Arrays
• First element goes in first array position, second in the second position, etc.
• Top of the stack is index of the last element added to the stack
• Stack elements are stored in an array, which is a random access data structure – Stack element is accessed only through top
• To track the top position, use a variable called stackTop
CSE 143 5
Implementation of Stacks as Arrays
• The stack elements are stored in an array
– The stack uses a prefix of the array
– The size of the array is the maximum number of elements in the stack
• class stackType implements the functions of the abstract class stackADT
CSE 143 6
Implementation of Stacks as Arrays
• C++ arrays begin with the index 0
• If stackTop is 0, stack is empty
• If stackTop is nonzero, stack is not empty
– Top element is given by stackTop – 1
– stackTop is the size of the stack
CSE 143 7
Implementation of Stacks as Arrays
CSE 143 8
Initialize Stack
CSE 143 9
Push
• Store the newItem in the array component indicated by stackTop
• Increment stackTop
• Overflow occurs if we try to add a new item to a full stack
CSE 143 10
Push
CSE 143 11
Push
CSE 143 12
Return the Top Element
• Returns the top element of the stack
• Does not change the state of the stack
• Overflow condition: only allowed if the stack is not empty
CSE 143 13
Pop
• Removes the top element and returns it
• To remove an element from the stack, decrement stackTop by 1
• Underflow condition: trying to remove an item from an empty stack
CSE 143 14
Pop
CSE 143 15
Pop
CSE 143 16
Constructor and Destructor
• Constructor:
– Creates array to store stack elements (with default array size)
– Sets stackTop to 0
– Sets stack size to 0
• Destructor:
– Deallocates memory occupied by the array
– Sets stackTop to 0
CSE 143 17
Stack Header File
• Place definitions of class and function profiles (stack operations) together in a file
– called ArrayStack.h
• Place implementation of class and function bodies together in a file
– called ArrayStack.cpp
CSE 143 18
Linked Implementation of Stacks
• Array only allows fixed number of elements
• If number of elements to be pushed exceeds array size, the program may terminate
• Linked lists can dynamically organize data
• In a linked representation, stackTop is pointer to memory address of top element in stack
CSE 143 19
Linked Implementation of Stacks
CSE 143 20
Default Constructor
• Initializes the stack to an empty state when a stack object is declared
– Sets stackTop to NULL
– Sets size to 0
CSE 143 21
Empty Stack and Full Stack
• In a linked implementation of stacks, function isFullStack does not apply
– Logically, the stack is never full
• Stack is empty if stackTop is NULL
CSE 143 22
Initialize Stack
• initializeStack: reinitializes stack to an empty state
– Must deallocate memory occupied by current element
– Sets stackTop to NULL
CSE 143 23
Push
• newNode is added at the beginning of the linked list pointed to by stackTop
CSE 143 24
Push
CSE 143 25
Return the Top Element
• top operation
– returns top element of stack
– returns the element in the node pointed by stackTop
CSE 143 26
Pop
• Node pointed to by stackTop is removed
– Second element becomes top element
CSE 143 27
Pop
CSE 143 28