SlideShare ist ein Scribd-Unternehmen logo
1 von 10
Downloaden Sie, um offline zu lesen
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;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaDipayan Sarkar
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)Syed Umair
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04hassaanciit
 
Function recap
Function recapFunction recap
Function recapalish sha
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CSAAKASH KUMAR
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CSAAKASH KUMAR
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointerargusacademy
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 

Was ist angesagt? (20)

week-16x
week-16xweek-16x
week-16x
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Application of Stack - Yadraj Meena
Application of Stack - Yadraj MeenaApplication of Stack - Yadraj Meena
Application of Stack - Yadraj Meena
 
Introduction to c part 2
Introduction to c   part  2Introduction to c   part  2
Introduction to c part 2
 
New microsoft office word document (2)
New microsoft office word document (2)New microsoft office word document (2)
New microsoft office word document (2)
 
Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04Introduction to Computer and Programing - Lecture 04
Introduction to Computer and Programing - Lecture 04
 
Matlab project
Matlab projectMatlab project
Matlab project
 
Function recap
Function recapFunction recap
Function recap
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 1) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Array notes
Array notesArray notes
Array notes
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
2 d array(part 2) || 2D ARRAY FUNCTION WRITING || GET 100% MARKS IN CBSE CS
 
Functions
FunctionsFunctions
Functions
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
Class 6 2ciclo
Class 6 2cicloClass 6 2ciclo
Class 6 2ciclo
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
C programming pointer
C  programming pointerC  programming pointer
C programming pointer
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Chap 9(functions)
Chap 9(functions)Chap 9(functions)
Chap 9(functions)
 

Ähnlich wie Stack concepts by Divya

Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applicationsAhsan Mansiv
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationRAtna29
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7Kumar
 
Stack operation algorithms with example
Stack operation algorithms with exampleStack operation algorithms with example
Stack operation algorithms with exampleNamanKikani
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arraystameemyousaf
 

Ähnlich wie Stack concepts by Divya (20)

04 stacks
04 stacks04 stacks
04 stacks
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
U3.stack queue
U3.stack queueU3.stack queue
U3.stack queue
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stack array
Stack arrayStack array
Stack array
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
Data structure lecture7
Data structure lecture7Data structure lecture7
Data structure lecture7
 
Stack operation algorithms with example
Stack operation algorithms with exampleStack operation algorithms with example
Stack operation algorithms with example
 
Lecture2
Lecture2Lecture2
Lecture2
 
Stack - Operations and Applications
Stack - Operations and ApplicationsStack - Operations and Applications
Stack - Operations and Applications
 
Stack push pop
Stack push popStack push pop
Stack push pop
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Stacks
StacksStacks
Stacks
 
STLStack.pdf
STLStack.pdfSTLStack.pdf
STLStack.pdf
 
03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays03 stacks and_queues_using_arrays
03 stacks and_queues_using_arrays
 

Kürzlich hochgeladen

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 

Kürzlich hochgeladen (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

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; }