0% found this document useful (0 votes)
25 views20 pages

Stack

Uploaded by

kannangr8523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views20 pages

Stack

Uploaded by

kannangr8523
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

UNIT 2

STACK
STACK
• A Stack is a linear data structure that holds a linear, ordered sequence
of elements.

• A stack is an ordered collection of items where the addition of new


items and the removal of existing items always takes place at the same
end.

• A Stack works on the LIFO process (Last In First Out)


EXAMPLES OF STACK
OPERATIONS IN STACK
• push( ) : Adds an element to the top of the stack.
• pop( ) : Removes the topmost element from the stack.
• peek( ) : returns the value of the top node without removing it from
the stack.
• isEmpty( ) : Checks whether the stack is empty.
• isFull( ) : Checks whether the stack is full.
• top( ) : Displays the topmost element of the stack.
Conditions of Stack:
Two Conditions:

Overflow

Underflow
Advantages of stack
• Easy implementation

• Efficient memory utilization

• Fast access time

• Helps in function calls

• Supports backtracking

• Enables undo/redo operations


Disadvantage of stack
• Limited capacity

• No random access

• Memory management

• Stack overflow and underflow

• Recursive function calls limitations

• Not suitable for certain applications


Applications of stack
• CD/DVD stand.

• Stack of books in a book shop.

• Call center systems.

• Undo and Redo mechanism in text editors.

• The history of a web browser is stored in the form of a stack.

• Call logs, E-mails, and Google photos in any gallery are also stored in
form of a stack.
Applications of stack in data structure
• Function calls and recursion

• Undo/Redo operations

• Expression evaluation

• Balanced Parentheses

• Backtracking Algorithms
Two ways to implement stack

Static -
Array Based Implementation

Dynamic
Linked list Based Implementation
Quizzzzz
1. In a sequence of operations – push(1), push(2), pop(),push(1),
push(2), pop(), pop(), pop(),push(2), pop() are performed on a
stack, the sequence of popped out values are:

a) 2,2,1,2,2

b) 2,1,2,1,2

c) 2,2,1,1,2

d) 2,1,2,2,1
2. Consider the following operations performed on a stack of size 5:
push(a) , pop(), push(b),push(c), pop(), push(d), pop(), pop(), push(e).

Which of the following statements is correct?

a) Underflow occurs

b) Overflow Occurs

c) Stack operations are performed smoothly

d) None of the mentioned


Stack – Array based Implementation
Program to Implement
// Preprocessor Directives
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 5 // macro definition

// User defined function


void push();
void pop();
void display();

int stack[N];
int top=-1;
int main() case 2:
{ pop();
int choice; break;
clrscr();
case 3:
while(choice!=5)
display();
{
break;
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
printf("\nEnter your choice: "); case 4:
scanf("%d",&choice); exit(0);
switch(choice) default:
{ printf("wrong choice");
case 1: }
push(); getch();
break;
}
}
Push function
void push()
{
int x;
printf("\n Enter data: ");
scanf("%d",&x);
if(top==N-1)
{
printf("stack overflow");
}
else
{
top=top+1;
stack[top]=x;
}
}
Pop function
void pop()
{
int item;
if(top==-1)
{
printf("\n Stack Underflow");
}
else
{
item=stack[top];
top=top-1;
printf("The deleted Item is %d",item);
}
}
Peek function
void peek()
{
if(top==-1)
{
printf("\n Stack Underflow");
}
else
{
printf("The top most element is %d",stack[top]);
}
}
Display Function
void display()
{
int i;
if(top==-1)
{
printf("Stack is Empty");
}
else
{
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}

You might also like