SlideShare a Scribd company logo
3
Most read
5
Most read
7
Most read
STACK ADT:
• Stack is a linear data structure in which the
insertion and deletion operations are
performed at only one end.
• In a stack, adding and removing of elements
are performed at a single position which is
known as "top".
• In stack, the insertion and deletion operations
are performed based on LIFO (Last In First
Out) & FILO (First In Last Out) principle.
• In a stack, the insertion operation is performed
using a function called "push" and deletion
operation is performed using a function
called "pop".
Operations on a Stack
• The following operations are performed on the
stack.
1. Push (To insert an element on to the stack)
2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)
• Stack data structure can be implemented in
two ways. They are as follows.
1. Using Array
2. Using Linked List
• When a stack is implemented using an array,
that stack can organize an only limited number
of elements.
• When a stack is implemented using a linked
list, that stack can organize an unlimited
number of elements.
Implementation of Stack using Array:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
int value, choice;
clrscr();
while(1)
{
printf("nn***** MENU *****n");
printf("1.Push 2.Pop 3.Display 4.Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to insert: ");
scanf("%d",&value);
push(value); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("nWrong selection!");
}
} }
void push(int value)
{
if(top == SIZE-1)
printf("nStack is Full!");
else
{
top++;
stack[top] = value;
printf("nInsertion success!!!");
}
}
void pop()
{
if(top == -1)
printf("nStack is Empty!");
else
{
printf("nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
if(top == -1)
printf("nStack is Empty!!!");
else
{
int i;
printf("nStack elements are:n");
for(i=top; i>=0; i--)
printf("%dn",stack[i]);
} }
Implementation of Stack using Linked List:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*top;
void push();
void pop();
void display();
void main()
{
int choice;
clrscr();
while(1)
{
printf("nn***** MENU *****n");
printf("1.Push 2.Pop 3.Display 4.Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("nWrong selection!");
}
}
}
void push ()
{
int val;
struct node *new =(struct node*)malloc(sizeof
(struct node));
printf("Enter the value");
scanf("%d",&val);
if(top == NULL)
{
new->data = val;
new->next = NULL;
top=new;
}
else
{
new->data = val;
new->next = top;
top=new;
}
}
void pop()
{
struct node *temp=top;
if (temp== NULL)
printf("Underflow");
else
{
printf(“Deleted element %d”,temp->data);
top = temp->next;
free(temp);
} }
void display()
{
struct node * temp =top;
if(temp == NULL)
printf("Stack is emptyn");
else
{
printf("Printing Stack elements n");
while(temp!=NULL)
{
printf("%dn",temp->data);
temp = temp ->next;
}
}
}
Application of Stack:
• Evaluating arithmetic expression
• Balancing the symbols
• Towers of Hanoi
• Function calls
• 8 Queen problem

More Related Content

What's hot (19)

PPTX
Stack1
Iqrazb
 
DOCX
Array imp of list
Elavarasi K
 
PDF
Stack Data Structure
Er. Ganesh Ram Suwal
 
PPTX
Grokking TechTalk #16: Maybe functor in javascript
Grokking VN
 
DOCX
New microsoft word document
Abhishek Arya
 
TXT
Data Structures : array operations in c program
Raghavendra Narayan
 
PPTX
Python basic Program
nuripatidar
 
PPTX
C Programming Language Step by Step Part 5
Rumman Ansari
 
PDF
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
SAKSHISINGH486
 
PPTX
C Programming Language Part 8
Rumman Ansari
 
DOCX
One dimensional operation of Array in C- language
9096308941
 
PPTX
C Programming Language Part 4
Rumman Ansari
 
DOCX
Qprgs
Ssankett Negi
 
TXT
Luhn sh
Ben Pope
 
PDF
Functional Programming on Android: is it possible?
Lucas Albuquerque
 
PDF
CalculateLoanPayments
William Rutherford
 
Stack1
Iqrazb
 
Array imp of list
Elavarasi K
 
Stack Data Structure
Er. Ganesh Ram Suwal
 
Grokking TechTalk #16: Maybe functor in javascript
Grokking VN
 
New microsoft word document
Abhishek Arya
 
Data Structures : array operations in c program
Raghavendra Narayan
 
Python basic Program
nuripatidar
 
C Programming Language Step by Step Part 5
Rumman Ansari
 
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
SAKSHISINGH486
 
C Programming Language Part 8
Rumman Ansari
 
One dimensional operation of Array in C- language
9096308941
 
C Programming Language Part 4
Rumman Ansari
 
Luhn sh
Ben Pope
 
Functional Programming on Android: is it possible?
Lucas Albuquerque
 
CalculateLoanPayments
William Rutherford
 

Similar to CS8391-Data Structures Unit 2 (20)

DOCX
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
PPTX
Stack and its applications
Ahsan Mansiv
 
PPTX
STACKS implimentarions AND stack applications .pptx
Dr. Amna Mohamed
 
PDF
04 stacks
Rajan Gautam
 
PPTX
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
PPSX
Stack-data-structure.ppsxErwewwwrrterewewew
RamaKrishnaErroju
 
PPTX
Stack and Queue.pptx university exam preparation
RAtna29
 
PPTX
Stack,queue and linked list data structure.pptx
yukti266975
 
PDF
Stack
Amrutha Rajan
 
PPTX
Stack and its operation implemented with array new - Copy.pptx
Shivam Kumar
 
PPTX
STACK1.pptx
MouDhara1
 
PDF
Chapter 4 stack
jadhav_priti
 
PPTX
Stack of Data structure
Sheikh Monirul Hasan
 
PDF
Datastructures asignment
sreekanth3dce
 
PPTX
Stack.pptx
SherinRappai
 
PPT
Data Structures and algorithms using c .ppt
RaviKumarChavali1
 
PPTX
stacks and queues
EktaVaswani2
 
PPT
Stack data structures with definition and code
bansidharj11
 
PPTX
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
PPTX
introduction of the Stacks and Queues.pptx
kavitashingi123
 
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
Stack and its applications
Ahsan Mansiv
 
STACKS implimentarions AND stack applications .pptx
Dr. Amna Mohamed
 
04 stacks
Rajan Gautam
 
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Stack-data-structure.ppsxErwewwwrrterewewew
RamaKrishnaErroju
 
Stack and Queue.pptx university exam preparation
RAtna29
 
Stack,queue and linked list data structure.pptx
yukti266975
 
Stack and its operation implemented with array new - Copy.pptx
Shivam Kumar
 
STACK1.pptx
MouDhara1
 
Chapter 4 stack
jadhav_priti
 
Stack of Data structure
Sheikh Monirul Hasan
 
Datastructures asignment
sreekanth3dce
 
Stack.pptx
SherinRappai
 
Data Structures and algorithms using c .ppt
RaviKumarChavali1
 
stacks and queues
EktaVaswani2
 
Stack data structures with definition and code
bansidharj11
 
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
introduction of the Stacks and Queues.pptx
kavitashingi123
 
Ad

More from SIMONTHOMAS S (20)

PPTX
Cs8092 computer graphics and multimedia unit 5
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 4
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 3
SIMONTHOMAS S
 
PPT
Cs8092 computer graphics and multimedia unit 2
SIMONTHOMAS S
 
PPTX
Cs8092 computer graphics and multimedia unit 1
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-5
SIMONTHOMAS S
 
PPT
Mg6088 spm unit-4
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-3
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-2
SIMONTHOMAS S
 
PPTX
Mg6088 spm unit-1
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 5
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 4
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 3
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 2
SIMONTHOMAS S
 
PPTX
IT6701-Information Management Unit 1
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 4
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 3
SIMONTHOMAS S
 
PPTX
CS8391-Data Structures Unit 1
SIMONTHOMAS S
 
PPT
SPC Unit 5
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 5
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 4
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 3
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 2
SIMONTHOMAS S
 
Cs8092 computer graphics and multimedia unit 1
SIMONTHOMAS S
 
Mg6088 spm unit-5
SIMONTHOMAS S
 
Mg6088 spm unit-4
SIMONTHOMAS S
 
Mg6088 spm unit-3
SIMONTHOMAS S
 
Mg6088 spm unit-2
SIMONTHOMAS S
 
Mg6088 spm unit-1
SIMONTHOMAS S
 
IT6701-Information Management Unit 5
SIMONTHOMAS S
 
IT6701-Information Management Unit 4
SIMONTHOMAS S
 
IT6701-Information Management Unit 3
SIMONTHOMAS S
 
IT6701-Information Management Unit 2
SIMONTHOMAS S
 
IT6701-Information Management Unit 1
SIMONTHOMAS S
 
CS8391-Data Structures Unit 5
SIMONTHOMAS S
 
CS8391-Data Structures Unit 4
SIMONTHOMAS S
 
CS8391-Data Structures Unit 3
SIMONTHOMAS S
 
CS8391-Data Structures Unit 1
SIMONTHOMAS S
 
SPC Unit 5
SIMONTHOMAS S
 
Ad

Recently uploaded (20)

PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Knowledge Representation : Semantic Networks
Amity University, Patna
 
PDF
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
PDF
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
PDF
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPT
Testing and final inspection of a solar PV system
MuhammadSanni2
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Knowledge Representation : Semantic Networks
Amity University, Patna
 
SERVERLESS PERSONAL TO-DO LIST APPLICATION
anushaashraf20
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Footbinding.pptmnmkjkjkknmnnjkkkkkkkkkkkkkk
mamadoundiaye42742
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
Design Thinking basics for Engineers.pdf
CMR University
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Electrical Machines and Their Protection.pdf
Nabajyoti Banik
 
aAn_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Water Resources Engineering (CVE 728)--Slide 3.pptx
mohammedado3
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Testing and final inspection of a solar PV system
MuhammadSanni2
 

CS8391-Data Structures Unit 2

  • 1. STACK ADT: • Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. • In a stack, adding and removing of elements are performed at a single position which is known as "top". • In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) & FILO (First In Last Out) principle.
  • 2. • In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop". Operations on a Stack • The following operations are performed on the stack. 1. Push (To insert an element on to the stack) 2. Pop (To delete an element from the stack) 3. Display (To display elements of the stack)
  • 3. • Stack data structure can be implemented in two ways. They are as follows. 1. Using Array 2. Using Linked List • When a stack is implemented using an array, that stack can organize an only limited number of elements. • When a stack is implemented using a linked list, that stack can organize an unlimited number of elements.
  • 4. Implementation of Stack using Array: #include<stdio.h> #include<conio.h> #define SIZE 10 void push(int); void pop(); void display(); int stack[SIZE], top = -1;
  • 5. void main() { int value, choice; clrscr(); while(1) { printf("nn***** MENU *****n"); printf("1.Push 2.Pop 3.Display 4.Exit"); printf("nEnter your choice: "); scanf("%d",&choice);
  • 6. switch(choice) { case 1: printf("Enter the value to insert: "); scanf("%d",&value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("nWrong selection!"); } } }
  • 7. void push(int value) { if(top == SIZE-1) printf("nStack is Full!"); else { top++; stack[top] = value; printf("nInsertion success!!!"); } }
  • 8. void pop() { if(top == -1) printf("nStack is Empty!"); else { printf("nDeleted : %d", stack[top]); top--; } }
  • 9. void display() { if(top == -1) printf("nStack is Empty!!!"); else { int i; printf("nStack elements are:n"); for(i=top; i>=0; i--) printf("%dn",stack[i]); } }
  • 10. Implementation of Stack using Linked List: #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }*top; void push(); void pop(); void display();
  • 11. void main() { int choice; clrscr(); while(1) { printf("nn***** MENU *****n"); printf("1.Push 2.Pop 3.Display 4.Exit"); printf("nEnter your choice: "); scanf("%d",&choice);
  • 12. switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("nWrong selection!"); } } }
  • 13. void push () { int val; struct node *new =(struct node*)malloc(sizeof (struct node)); printf("Enter the value"); scanf("%d",&val); if(top == NULL) { new->data = val; new->next = NULL;
  • 15. void pop() { struct node *temp=top; if (temp== NULL) printf("Underflow"); else { printf(“Deleted element %d”,temp->data); top = temp->next; free(temp); } }
  • 16. void display() { struct node * temp =top; if(temp == NULL) printf("Stack is emptyn"); else { printf("Printing Stack elements n");
  • 18. Application of Stack: • Evaluating arithmetic expression • Balancing the symbols • Towers of Hanoi • Function calls • 8 Queen problem