SlideShare a Scribd company logo
Write a C program to implement a stack using arrays
#include<stdio.h>
#define SIZE 5
int s[SIZE],top=-1;
void push();
void pop();
void display();
int main()
{
int ch;
for(;;)
{
printf("n1.Pushn2.Popn3.Shown4.Exit");
printf("n Enter your choice n");
scanf("%d",&ch);
switch(ch){
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
default: printf(" Invalidn”); exit(0);}
}
return 0;
}
void push()
{
int item;
if(top==SIZE-1)
{
printf(“STACK IS FULL”);
return;
}
printf(“enter an element to be inserted”);
scanf(“%d”,&item);
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s[top--]);
}
void display()
{
int i;
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Elements of the stack are:n”);
for(i=top;i>=0;i--)
printf(“%dn”,s[i]);
}
Write a C program to implement a stack using structures
#define MAX 10
struct stack
{ int a[SIZE]; int top; };
struct stack s;
s.top=-1;
void main()
{// same as previous code}
void pop()
{
if(s.top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s.a[s.top--]);
}
void push()
{
int item;
if(s.top==SIZE-1)
{ // same as previous push fush() }
printf(“Enter an item:”);
scanf(“%d”,&item);
s.top++;
s.a[s.top]=item;
}
void display()
{
int i;
if(s.top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Elements of the stack are:n”);
for(i=s.top;i>=0;i--)
printf(“%dn”,s.a[i]);
}
Write a C program to implement a stack using dynamic arrays
#include<stdio.h>
#include<stdlib.h>
int *s;
int stackSize=1;
int top=-1;
s=(int*)malloc(stackSize * sizeof(int));
void push()
{
if(top == stackSize-1)
{printf(“stack full allocate extra memory”);
stackSize++;
s=(int*)realloc(s,stackSize * sizeof(int));
s[++top]=item;
}
void pop()
{
if(top==-1)
{
printf(“STACK IS EMPTY”);
return;
}
printf(“Element deleted is %d”,s[top--]);
printf(“stack size decreased”);
stackSize--;
s=(int*)realloc(s,stackSize * sizeof(int));
}
MAIN FUNCTION
REMAINS SAME

More Related Content

PPTX
Stack using Array
Sayantan Sur
 
PDF
Datastructures asignment
sreekanth3dce
 
PDF
data structure and algorithm.pdf
Asrinath1
 
PDF
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
DOCX
DataStructures notes
Lakshmi Sarvani Videla
 
PPTX
Double linked list
Sayantan Sur
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
PDF
Data Structure using C
Bilal Mirza
 
Stack using Array
Sayantan Sur
 
Datastructures asignment
sreekanth3dce
 
data structure and algorithm.pdf
Asrinath1
 
VTU Data Structures Lab Manual
Nithin Kumar,VVCE, Mysuru
 
DataStructures notes
Lakshmi Sarvani Videla
 
Double linked list
Sayantan Sur
 
Practical File of C Language
RAJWANT KAUR
 
Data Structure using C
Bilal Mirza
 

Similar to stack.pptx (20)

DOC
C basics
MSc CST
 
DOCX
Data structure output 1
Balaji Thala
 
PPTX
Array menu
Sayantan Sur
 
DOCX
Circular queue
ShobhaHiremath8
 
PPTX
Single linked list
Sayantan Sur
 
PPTX
Circular linked list
Sayantan Sur
 
DOCX
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
DOCX
One dimensional operation of Array in C- language
9096308941
 
DOCX
ADA FILE
Gaurav Singh
 
PPTX
Stack using Linked List
Sayantan Sur
 
DOCX
Cpds lab
praveennallavelly08
 
PDF
Data Structure in C Programming Language
Arkadeep Dey
 
PDF
C Programming Example
PRATHAMESH DESHPANDE
 
DOCX
Array imp of list
Elavarasi K
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
DOCX
C lab manaual
manoj11manu
 
PDF
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
C basics
MSc CST
 
Data structure output 1
Balaji Thala
 
Array menu
Sayantan Sur
 
Circular queue
ShobhaHiremath8
 
Single linked list
Sayantan Sur
 
Circular linked list
Sayantan Sur
 
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
One dimensional operation of Array in C- language
9096308941
 
ADA FILE
Gaurav Singh
 
Stack using Linked List
Sayantan Sur
 
Data Structure in C Programming Language
Arkadeep Dey
 
C Programming Example
PRATHAMESH DESHPANDE
 
Array imp of list
Elavarasi K
 
Basic c programs updated on 31.8.2020
vrgokila
 
C lab manaual
manoj11manu
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
KavyaSharma65
 
Ad

More from MeghaKulkarni27 (12)

PPTX
Note for Java Programming////////////////
MeghaKulkarni27
 
PPTX
data structures and applications power p
MeghaKulkarni27
 
PPTX
Different string operations....................
MeghaKulkarni27
 
PPTX
virtual reality...............................
MeghaKulkarni27
 
PPTX
positive.pptx
MeghaKulkarni27
 
PPTX
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
PPTX
circularlinklist-190205164051.pptx
MeghaKulkarni27
 
PPTX
linkedlist.pptx
MeghaKulkarni27
 
PPTX
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
PPTX
queue_final.pptx
MeghaKulkarni27
 
PPTX
DS_PPT.pptx
MeghaKulkarni27
 
PPT
DS_PPT.ppt
MeghaKulkarni27
 
Note for Java Programming////////////////
MeghaKulkarni27
 
data structures and applications power p
MeghaKulkarni27
 
Different string operations....................
MeghaKulkarni27
 
virtual reality...............................
MeghaKulkarni27
 
positive.pptx
MeghaKulkarni27
 
linkedlist-130914084342-phpapp02.pptx
MeghaKulkarni27
 
circularlinklist-190205164051.pptx
MeghaKulkarni27
 
linkedlist.pptx
MeghaKulkarni27
 
queueppt-191018053228 (1).pptx
MeghaKulkarni27
 
queue_final.pptx
MeghaKulkarni27
 
DS_PPT.pptx
MeghaKulkarni27
 
DS_PPT.ppt
MeghaKulkarni27
 
Ad

Recently uploaded (20)

PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
The-Invisible-Living-World-Beyond-Our-Naked-Eye chapter 2.pdf/8th science cur...
Sandeep Swamy
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
A Smarter Way to Think About Choosing a College
Cyndy McDonald
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 

stack.pptx

  • 1. Write a C program to implement a stack using arrays #include<stdio.h> #define SIZE 5 int s[SIZE],top=-1; void push(); void pop(); void display(); int main() { int ch; for(;;) { printf("n1.Pushn2.Popn3.Shown4.Exit"); printf("n Enter your choice n"); scanf("%d",&ch); switch(ch){ case 1: push(); break; case 2: pop(); break; case 3: display(); break; default: printf(" Invalidn”); exit(0);} } return 0; }
  • 2. void push() { int item; if(top==SIZE-1) { printf(“STACK IS FULL”); return; } printf(“enter an element to be inserted”); scanf(“%d”,&item); s[++top]=item; } void pop() { if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s[top--]); } void display() { int i; if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Elements of the stack are:n”); for(i=top;i>=0;i--) printf(“%dn”,s[i]); }
  • 3. Write a C program to implement a stack using structures #define MAX 10 struct stack { int a[SIZE]; int top; }; struct stack s; s.top=-1; void main() {// same as previous code} void pop() { if(s.top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s.a[s.top--]); } void push() { int item; if(s.top==SIZE-1) { // same as previous push fush() } printf(“Enter an item:”); scanf(“%d”,&item); s.top++; s.a[s.top]=item; } void display() { int i; if(s.top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Elements of the stack are:n”); for(i=s.top;i>=0;i--) printf(“%dn”,s.a[i]); }
  • 4. Write a C program to implement a stack using dynamic arrays #include<stdio.h> #include<stdlib.h> int *s; int stackSize=1; int top=-1; s=(int*)malloc(stackSize * sizeof(int)); void push() { if(top == stackSize-1) {printf(“stack full allocate extra memory”); stackSize++; s=(int*)realloc(s,stackSize * sizeof(int)); s[++top]=item; } void pop() { if(top==-1) { printf(“STACK IS EMPTY”); return; } printf(“Element deleted is %d”,s[top--]); printf(“stack size decreased”); stackSize--; s=(int*)realloc(s,stackSize * sizeof(int)); } MAIN FUNCTION REMAINS SAME