CSC 221 DSA Stack Arrays - Linked List Implementation 27022023 111135am
CSC 221 DSA Stack Arrays - Linked List Implementation 27022023 111135am
2
STACK
Stack is a linear data structure in which data insertion and deletion is done
at one end.
Order may be LIFO(Last In First Out) or FILO(First In Last Out).
Stacks in real life: stack of books, stack of plates
Add new items at the top
Remove an item at the top
Stack data structure similar to real life: collection of elements arranged in a
linear order.
3
4
INSERTION & DELETION ON STACK
5
6
STACK OPERATIONS
7
8
RELATED TERMINOLOGY
Top
A pointer that points the top element in the stack.
Stack Underflow
When there is no element in the stack, we cannot pop an element: stack underflow
Stack Overflow
When the stack contains equal number of elements as per its capacity and no more
elements can be added: stack overflow 9
IMPLEMENTATION OF STACK
10
STACK IMPLEMENTATION
Implementation can be done in two ways
Static implementation
Dynamic Implementation
Static Implementation
Stacks have fixed size, and are implemented as arrays
It is also inefficient for utilization of memory
Dynamic Implementation
Stack grow in size as needed, and implemented as linked lists
Dynamic Implementation is done through pointers
11
The memory is efficiently utilized with Dynamic Implementations
PUSH OPERATION
The following steps should be taken to push (insert) data into a stack −
Step 1 − Check if the stack is full.
Step 2 − If the stack is full, produce overflow error and exit.
Step 3 − If the stack is not full, increment top to point the next empty
space.
Step 4 − Add data element to the stack location, where the top is pointing.
Step 5 − return success.
12
POP OPERATION
3. S.Push(‘C’);
4. S.Pop();
5. S.Pop();
6. S.Push(‘D’);
7. S.Push(‘E’);
8. S.Pop();
14
9. S.Pop();
Stack Implementation
using Arrays
15
STACK USING ARRAY
#include <iostream>
using namespace std;
int n=5, top=-1 ;
int stack_array[n];
16
PUSH OPERATION
}
PUSH OPERATION USING IS FULL FUNCTION
}
POP OPERATION USING IS EMPTY FUNCTION
}
TRAVERSING STACK
22
Stack Implementation
using Linked List
23
STACK USING LINKED LIST
24
THE STACK CLASS
Class Stack
{
public:
Node *Top = NULL;
void push(int);
int pop();
void displayStack();
int Top();
};
*Same node class will be used as list 25
PUSH OPERATION USING LIST
void stack::push(int value)
{
Node *ptr;
ptr=new Node();
ptr->data=value;
//ptr->next=NULL;
if(top==NULL)
{ptr->next=NULL;}
else
{ptr->next=top; }
top=ptr; 26
}
POP OPERATION
int stack::pop()
{
if(top==NULL)
{
cout<<"The stack is empty!!!";
return -1;
}
Node *temp = top;
top=top->next;
int retValue = temp->data;
delete temp;
return retValue; 27
}
DISPLAY TOP
28
DISPLAY OR TRAVERSING STACK
void stack::displayStack()
{
if(top==NULL)
cout<<"stack is empty";
else
{
Node *ptr = top;
cout<<"Stack elements are: ";
}
while (ptr != NULL)
{
cout<< ptr->data <<" ";
ptr = ptr->next;
} 29
}
STACK IMPLEMENTATION USING LINKED LIST
10
Push(20);
20 10
top
Push(30); top
30 20 10
30
top
STACK IMPLEMENTATION USING LINKED LIST
30 20 10
top
Pop();
20 10
top 31
STACK USING LINKED LIST
32
REAL LIFE APPLICATIONS OF STACK