SlideShare a Scribd company logo
STACK CONCEPT BY DIVYA
LIFO-Last in first out
Operations of stack:
1. Insertion - push
2. Deletion -pop
3. Peek - returns topmost element of the stack
STACK CONCEPT BY DIVYA
Parsing- compiler
if, else,main,switch-32 keywords in C, identifiers
int a_12;
if()
{
statement 1;
…….
if()
STACK CONCEPT BY DIVYA
{
statement 2;
.….
Parsing -stack: no of open parenthesis=no. of closing
parenthesis
Whenever Opening brace is encountered -push element
Whenever Closing brace is encountered-pop element
{
Preprocessing
#include<stdio.h>
#define max 90
main()
{
int a;
if(a>max)
}
At the end stack is non-empty, means there is
syntax error in above code snippet and the
parenthesis are unmatched
STACK CONCEPT BY DIVYA
Memory representation of a process
Stack Representation using Array
Consider the number of elements the stack can store is N.
Following four conditions are possible depending on the value
of top( which points to the topmost position in the stack):
STACK CONCEPT BY DIVYA
1. If top=-1, it means the stack is empty and there are no
elements present in the stack. If we want to pop an element
from stack, it will result in underflow condition.
2. If top=N, it means the stack is full. If we want to push an
element inside stack, it will result in the overflow condition.
3. If the value of top is incremented by 1, means an element has
been pushed(inserted) inside the stack.
4. If the value of top is decremented by 1, means an element has
been popped(deleted) from the stack.
OPERATIONS ON STACK
I. Push operation
Algorithm
PUSH(STACK, TOP, N, ITEM)
1. Start
2. IF TOP = N THEN
DISPLAY Overflow
EXIT
END IF
3. TOP = TOP + 1
4. STACK[TOP] = ITEM [Insert Item into new TOP Position]
5. Stop
Static allocation
Program:
#include<stdio.h>
#define N 3
int top=-1,stack[N];
void push(int item)
{
if(top==N-1)
{
STACK CONCEPT BY DIVYA
printf("nOverflow Conditionn");
return;
}
top=top+1;
stack[top]=item;
}
void display (int stack[])
{
int i;
for(i=0;i<=top;i++)
printf(" %d ",stack[i]);
}
int main()
{
int item,num_elements,i;
printf("n No. of elements you want to insert inside the stack");
scanf("%d",&num_elements);
for(i=0;i<num_elements;i++)
{
printf("n Enter the element to be inserted ");
scanf("%d",&item);
push(item);
}
printf("nDisplay elements inside the stack");
display(stack);
printf("nDelete an element fron the stack");
//pop();
//printf("nDisplay elements inside the stack after deletion");
//display(stack);
return 0;
}
II. Pop operation
Algorithm
STACK CONCEPT BY DIVYA
POP(STACK, TOP, ITEM)
1. Start
2. IF TOP = 0 THEN
DISPLAY Underflow
EXIT
END IF
3. SET ITEM = STACK[TOP] [Call by reference]
4. TOP = TOP -1
5. Stop
Program: Pop function definition
void pop()
{
int Popped_Item;
if(top==-1)
{
printf("n underflow Condition");
return;
}
Popped_Item=stack[top];
printf("nDeleted element is%d",Popped_Item);
top=top-1;
}
STACK CONCEPT BY DIVYA
STACK REPRESENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*top;
void push(int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(!temp)
{
printf("nOverflow condition");
return;
}
temp->info=item;
temp->link=top;
STACK CONCEPT BY DIVYA
top=temp;
}
void display()
{
struct node *temp;
if(top==NULL)
printf("nstack is empty");
else
{
temp=top;
while(temp!=NULL)
{
printf(" %d ",temp->info);
temp=temp->link;
}
}
}
void pop()
{
struct node *temp;
if(top==NULL){
printf("nstack underflow");
return;
}
temp=top;
top=top->link;
temp->link=NULL;
free(temp);
}
int main()
{
int num_elem,i,item;
printf("nenter the number of elements you want to push inside
stack");
STACK CONCEPT BY DIVYA
scanf("%d",&num_elem);
for(i=0;i<num_elem;i++)
{
printf("nenter the element you want to push inside the
stack") ;
scanf("%d",&item);
push(item);
}
printf("ndisplay elements");
display();
printf("ndelete an element from stack");
pop();
printf("nDisplay after deleting an element form stack");
display();
return 0;
}

More Related Content

PDF
Recursion concepts by Divya
PPT
Doublylinklist
PPTX
Stack using Linked List
PDF
Data structure lab manual
PDF
C++ Question on References and Function Overloading
PPTX
Linked list2
PDF
6. function
PPTX
Stack - Data Structure
Recursion concepts by Divya
Doublylinklist
Stack using Linked List
Data structure lab manual
C++ Question on References and Function Overloading
Linked list2
6. function
Stack - Data Structure

What's hot (20)

DOC
Ds lab manual by s.k.rath
PPTX
Application of Stack - Yadraj Meena
DOCX
New microsoft office word document (2)
PDF
Introduction to Computer and Programing - Lecture 04
DOCX
Matlab project
PPTX
Function recap
PDF
Programming Fundamentals Arrays and Strings
PPTX
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
PDF
Array notes
PPTX
C Programming Language Part 9
PPTX
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
PPTX
Functions
DOCX
Data Structure in C (Lab Programs)
PDF
Class 6 2ciclo
PPT
Queue implementation
PPSX
C programming pointer
PPTX
C Programming Language Part 7
Ds lab manual by s.k.rath
Application of Stack - Yadraj Meena
New microsoft office word document (2)
Introduction to Computer and Programing - Lecture 04
Matlab project
Function recap
Programming Fundamentals Arrays and Strings
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
Array notes
C Programming Language Part 9
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
Functions
Data Structure in C (Lab Programs)
Class 6 2ciclo
Queue implementation
C programming pointer
C Programming Language Part 7
Ad

Similar to Stack concepts by Divya (20)

PDF
04 stacks
PPTX
Stack.pptx
PPTX
Stack and its applications
PPTX
Stack and Queue.pptx university exam preparation
PPTX
6 - STACKS in Data Structure and Algorithm.pptx
PPTX
Stack & Queue in Data Structure and Algorithms
PPTX
U3.stack queue
PDF
Data structures stacks
DOCX
Stack array
PDF
DSU C&C++ Practical File Diploma
PDF
DATA STRUCTURE USING C & C++
PPTX
STACKS implimentarions AND stack applications .pptx
PPT
Data structure lecture7
PPTX
Stack operation algorithms with example
PPTX
Abscddnddmdkwkkstack implementation.pptx
PPT
Lecture2
DOCX
Stack - Operations and Applications
PDF
Stack push pop
PPTX
Stack data structure
PPTX
Stacks in c++
04 stacks
Stack.pptx
Stack and its applications
Stack and Queue.pptx university exam preparation
6 - STACKS in Data Structure and Algorithm.pptx
Stack & Queue in Data Structure and Algorithms
U3.stack queue
Data structures stacks
Stack array
DSU C&C++ Practical File Diploma
DATA STRUCTURE USING C & C++
STACKS implimentarions AND stack applications .pptx
Data structure lecture7
Stack operation algorithms with example
Abscddnddmdkwkkstack implementation.pptx
Lecture2
Stack - Operations and Applications
Stack push pop
Stack data structure
Stacks in c++
Ad

Recently uploaded (20)

PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
OOP with Java - Java Introduction (Basics)
PDF
Well-logging-methods_new................
PPTX
Sustainable Sites - Green Building Construction
PDF
composite construction of structures.pdf
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Digital Logic Computer Design lecture notes
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
web development for engineering and engineering
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
UNIT 4 Total Quality Management .pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
OOP with Java - Java Introduction (Basics)
Well-logging-methods_new................
Sustainable Sites - Green Building Construction
composite construction of structures.pdf
Internet of Things (IOT) - A guide to understanding
R24 SURVEYING LAB MANUAL for civil enggi
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Automation-in-Manufacturing-Chapter-Introduction.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Lecture Notes Electrical Wiring System Components
Digital Logic Computer Design lecture notes
UNIT-1 - COAL BASED THERMAL POWER PLANTS
web development for engineering and engineering
Model Code of Practice - Construction Work - 21102022 .pdf
UNIT 4 Total Quality Management .pptx

Stack concepts by Divya

  • 1. STACK CONCEPT BY DIVYA LIFO-Last in first out Operations of stack: 1. Insertion - push 2. Deletion -pop 3. Peek - returns topmost element of the stack
  • 2. STACK CONCEPT BY DIVYA Parsing- compiler if, else,main,switch-32 keywords in C, identifiers int a_12; if() { statement 1; ……. if()
  • 3. STACK CONCEPT BY DIVYA { statement 2; .…. Parsing -stack: no of open parenthesis=no. of closing parenthesis Whenever Opening brace is encountered -push element Whenever Closing brace is encountered-pop element { Preprocessing #include<stdio.h> #define max 90 main() { int a; if(a>max) } At the end stack is non-empty, means there is syntax error in above code snippet and the parenthesis are unmatched
  • 4. STACK CONCEPT BY DIVYA Memory representation of a process Stack Representation using Array Consider the number of elements the stack can store is N. Following four conditions are possible depending on the value of top( which points to the topmost position in the stack):
  • 5. STACK CONCEPT BY DIVYA 1. If top=-1, it means the stack is empty and there are no elements present in the stack. If we want to pop an element from stack, it will result in underflow condition. 2. If top=N, it means the stack is full. If we want to push an element inside stack, it will result in the overflow condition. 3. If the value of top is incremented by 1, means an element has been pushed(inserted) inside the stack. 4. If the value of top is decremented by 1, means an element has been popped(deleted) from the stack. OPERATIONS ON STACK I. Push operation Algorithm PUSH(STACK, TOP, N, ITEM) 1. Start 2. IF TOP = N THEN DISPLAY Overflow EXIT END IF 3. TOP = TOP + 1 4. STACK[TOP] = ITEM [Insert Item into new TOP Position] 5. Stop Static allocation Program: #include<stdio.h> #define N 3 int top=-1,stack[N]; void push(int item) { if(top==N-1) {
  • 6. STACK CONCEPT BY DIVYA printf("nOverflow Conditionn"); return; } top=top+1; stack[top]=item; } void display (int stack[]) { int i; for(i=0;i<=top;i++) printf(" %d ",stack[i]); } int main() { int item,num_elements,i; printf("n No. of elements you want to insert inside the stack"); scanf("%d",&num_elements); for(i=0;i<num_elements;i++) { printf("n Enter the element to be inserted "); scanf("%d",&item); push(item); } printf("nDisplay elements inside the stack"); display(stack); printf("nDelete an element fron the stack"); //pop(); //printf("nDisplay elements inside the stack after deletion"); //display(stack); return 0; } II. Pop operation Algorithm
  • 7. STACK CONCEPT BY DIVYA POP(STACK, TOP, ITEM) 1. Start 2. IF TOP = 0 THEN DISPLAY Underflow EXIT END IF 3. SET ITEM = STACK[TOP] [Call by reference] 4. TOP = TOP -1 5. Stop Program: Pop function definition void pop() { int Popped_Item; if(top==-1) { printf("n underflow Condition"); return; } Popped_Item=stack[top]; printf("nDeleted element is%d",Popped_Item); top=top-1; }
  • 8. STACK CONCEPT BY DIVYA STACK REPRESENTATION USING LINKED LIST #include<stdio.h> #include<stdlib.h> struct node { int info; struct node *link; }*top; void push(int item) { struct node *temp; temp=(struct node*)malloc(sizeof(struct node)); if(!temp) { printf("nOverflow condition"); return; } temp->info=item; temp->link=top;
  • 9. STACK CONCEPT BY DIVYA top=temp; } void display() { struct node *temp; if(top==NULL) printf("nstack is empty"); else { temp=top; while(temp!=NULL) { printf(" %d ",temp->info); temp=temp->link; } } } void pop() { struct node *temp; if(top==NULL){ printf("nstack underflow"); return; } temp=top; top=top->link; temp->link=NULL; free(temp); } int main() { int num_elem,i,item; printf("nenter the number of elements you want to push inside stack");
  • 10. STACK CONCEPT BY DIVYA scanf("%d",&num_elem); for(i=0;i<num_elem;i++) { printf("nenter the element you want to push inside the stack") ; scanf("%d",&item); push(item); } printf("ndisplay elements"); display(); printf("ndelete an element from stack"); pop(); printf("nDisplay after deleting an element form stack"); display(); return 0; }