Download Data Structure & Algo and more Exercises Data Analysis & Statistical Methods in PDF only on Docsity!
Stack
Engr. Usman Raza
Stack
- Suppose that you have functions A, B, C, and D in your program.
- Now suppose that function A calls function B, function B calls function C,
and function C calls function D.
- When function D terminates, control goes back to function C; when function
C terminates, control goes back to function B; and when function B
terminates, control goes back to function A.
- During program execution, how do you think the computer keeps track of the
function calls? What about recursive functions? How does the computer
keep track of the recursive calls?
- This section discusses the data structure called the stack, which the
computer uses to implement function calls.
- Stacks have numerous applications in computer science.
- After developing the tools necessary to implement a stack, we will examine
some applications of stacks.
Stack (cont.)
- A Stack is a special kind of list in which all insertions and deletions take
place at one end, called the Top ”.
- It is also known as LIFO or Push-down List:
- The elements at the bottom of the stack have been in the stack the longest.
- The top element of the stack is the last element added to the stack.
- Because the elements are added and removed from one end (that is, the top), it follows that the item that is added last will be removed first.
- For this reason, a stack is also called a Last In First Out (LIFO) data structure.
• Operations:
1. MAKENULL(S): Make Stack S be an empty stack.
2. TOP( S ): Return the element at the top of stack S.
3. POP( S ): Remove the top element of the stack.
4. PUSH( S ): Insert the element x at the top of the stack.
5. ISEMPTY( S ): Return true if S is an empty stack; return false otherwise.
6. ISFULL( S ): If the stack is full, returns true; otherwise, it returns false.
push, top, and pop operations
Stack Implementation and Operations
Engr. Usman Raza
Stack Implementation
- For simplicity, assume the data is an integer type.
- Main stack operations
- Push (int data): Inserts data onto stack.
- int Pop(): Removes and returns the last inserted element from the stack.
- Auxiliary stack operations
- int Top(): Returns the top element without removing it.
- int Size(): Returns the number of elements stored in the stack.
- int IsEmptyStack(): Indicates whether any elements are stored in the stack or not.
- int IsFullStack(): Indicates whether the stack is full or not.
- Exceptions
- Attempting the execution of an operation may sometimes cause an error condition, called an exception. Exceptions are said to be “thrown” by an operation that cannot be executed.
- In the Stack ADT, operations pop and top cannot be performed if the stack is empty.
- Underflow : Attempting the execution of pop() on an empty stack throws an exception.
- Overflow : Trying to push an element in a full stack throws an exception.
Initialize Stack
• Let us consider the initializeStack operation. Because the value
of stackTop indicates whether the stack is empty or not, we can
simply set stackTop to - 1 to initialize the stack.
An Array Implementation of Stacks
Anchor the bottom of the stack at the bottom of the array.
Let the stack grow towards the top of the array.
Top indicates the current position of the first stack element.
Defining Stack Class
• Constructor
stack(){
top = - 1 ;
Defining Stack Class (cont.)
- Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will hold the data items.
- TOP points to the top most element of the stack.
- Item is the pushed (or inserted) data item on the top of the stack. Push (item)
1. IF (TOP == MAX-1)
a) PRINT “ The Stack is FULL” b) EXIT
2. ELSE
a) TOP=TOP+ b) STACK[TOP]=item c) EXIT
Defining Stack Class (cont.)
- Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will hold the data items.
- TOP points to the top most element of the stack.
- Item is the popped (or deleted) data item from the top of the stack. POP (item)
1. IF (TOP < 0)
a) PRINT “ The Stack is EMPTY” b) EXIT
2. ELSE
a) Item=STACK[TOP] b) TOP=TOP - 1 c) EXIT
POP Implementation
void pop(int &item){ if(top==-1) print "Stack Is Empty. No item to pop"<<endl; else{ item=a[top]; top--; print item<<" is Popped"<<endl; } }
Defining Stack Class (cont.)
- Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will hold the data items.
- TOP points to the top most element of the stack.
- Implement it yourself as a practice Size (item)
1. IF (TOP < 0)
a) PRINT “ The
Stack is EMPTY”
b) EXIT
2. ELSE
a) RETURN TOP+
b) EXIT
Defining Stack Class (cont.)
- Suppose STACK[SIZE] is a one dimensional array for implementing the stack, which will hold the data items.
- TOP points to the top most element of the stack.
- Implement it yourself as a practice IsFull (item)
1. IF (TOP == SIZE - 1)
a) RETURN TRUE
2. ELSE
a) RETURN FALSE