STACK Data Structure
A Linear Abstract Data type
*Stack
*Basic principles
*Operation of stack
*Stack using Array
*Stack using Linked List
*Applications of stack
What is Stack?
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
A stack is a linear data structure in which the insertion of a new
element and removal of an existing element takes place at the same
end represented as the top of the stack.
Stack as ADT
Stack is abstract Data Type
because,
1) It is organizing data
(Collection of Data)
2) It has set of operations to
manipulate the data
Push: To insert element
into stack
Pop: To remove Element
from the stack
Implementation of Stack
• We can implement stack using ,
• Array(Static Memory Allocation)
• Linked List(Dynamic Memory Allocation)
Implementation of Stack using Array(Static Memory
Allocation)
Step 1: initialization of Stack
#define MAX 5 //to define constant size of stack
//set of operations to be perform on stack(Function
Prototype)
void push(int data); // to insert data
void pop(); // to remove data or fetch data
void stackTop(); // to examine data on top of the stack
void display(); // to display all elements from stack
int tos=-1,stack[MAX];
5
4
3
2
1
0
MAX
tos -1
stack[MAX]
STACK Operations: push() and pop()
void push(int data)
{
if(tos==(MAX-1))
printf("n Stack is Full cant Push Element...");
else
{
tos++;
stack[tos]=data;
printf("n After Push Operation Stack is:");
}
}
void pop()
{
if(tos==-1)
printf("n Stack is Underflow POP Operation is not
possible");
else
{
printf("n Element Pooped is: %d",stack[tos]);
tos--;
}
}
STACK Operations: stackTop() and display()
void display()
{
int i;
if(tos==-1)
printf("n Stack is Empty.");
else
{
for(i=tos;i>=0;i--)
{
printf("n%d",stack[i]);
}
}
}
void stackTop()
{
if(tos==-1)
printf("n Stack is Empty.");
else
printf(“Top of the stack is: %d",stack[tos]);
}
Stack using Array Implementation
Output:

Operations on Stack, Stack using Array and Linked List

  • 1.
    STACK Data Structure ALinear Abstract Data type
  • 2.
    *Stack *Basic principles *Operation ofstack *Stack using Array *Stack using Linked List *Applications of stack
  • 3.
    What is Stack? •A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real- world stack, for example – a deck of cards or a pile of plates, etc. A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes place at the same end represented as the top of the stack.
  • 4.
    Stack as ADT Stackis abstract Data Type because, 1) It is organizing data (Collection of Data) 2) It has set of operations to manipulate the data Push: To insert element into stack Pop: To remove Element from the stack
  • 5.
    Implementation of Stack •We can implement stack using , • Array(Static Memory Allocation) • Linked List(Dynamic Memory Allocation)
  • 6.
    Implementation of Stackusing Array(Static Memory Allocation) Step 1: initialization of Stack #define MAX 5 //to define constant size of stack //set of operations to be perform on stack(Function Prototype) void push(int data); // to insert data void pop(); // to remove data or fetch data void stackTop(); // to examine data on top of the stack void display(); // to display all elements from stack int tos=-1,stack[MAX]; 5 4 3 2 1 0 MAX tos -1 stack[MAX]
  • 7.
    STACK Operations: push()and pop() void push(int data) { if(tos==(MAX-1)) printf("n Stack is Full cant Push Element..."); else { tos++; stack[tos]=data; printf("n After Push Operation Stack is:"); } } void pop() { if(tos==-1) printf("n Stack is Underflow POP Operation is not possible"); else { printf("n Element Pooped is: %d",stack[tos]); tos--; } }
  • 8.
    STACK Operations: stackTop()and display() void display() { int i; if(tos==-1) printf("n Stack is Empty."); else { for(i=tos;i>=0;i--) { printf("n%d",stack[i]); } } } void stackTop() { if(tos==-1) printf("n Stack is Empty."); else printf(“Top of the stack is: %d",stack[tos]); }
  • 9.
    Stack using ArrayImplementation
  • 10.