SlideShare a Scribd company logo
1 of 4
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

Similar to stack.pptx (20)

C basics
C basicsC basics
C basics
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
 
Array menu
Array menuArray menu
Array menu
 
Circular queue
Circular queueCircular queue
Circular queue
 
Single linked list
Single linked listSingle linked list
Single linked list
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
C program to implement linked list using array abstract data type
C program to implement linked list using array abstract data typeC program to implement linked list using array abstract data type
C program to implement linked list using array abstract data type
 
One dimensional operation of Array in C- language
One dimensional operation of Array in C- language One dimensional operation of Array in C- language
One dimensional operation of Array in C- language
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Array imp of list
Array imp of listArray imp of list
Array imp of list
 
Array list
Array listArray list
Array list
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
week-15x
week-15xweek-15x
week-15x
 
C
CC
C
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 

More from MeghaKulkarni27

Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////MeghaKulkarni27
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power pMeghaKulkarni27
 
Different string operations....................
Different string operations....................Different string operations....................
Different string operations....................MeghaKulkarni27
 
virtual reality...............................
virtual reality...............................virtual reality...............................
virtual reality...............................MeghaKulkarni27
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxMeghaKulkarni27
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxMeghaKulkarni27
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxMeghaKulkarni27
 

More from MeghaKulkarni27 (12)

Note for Java Programming////////////////
Note for Java Programming////////////////Note for Java Programming////////////////
Note for Java Programming////////////////
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
 
Different string operations....................
Different string operations....................Different string operations....................
Different string operations....................
 
virtual reality...............................
virtual reality...............................virtual reality...............................
virtual reality...............................
 
positive.pptx
positive.pptxpositive.pptx
positive.pptx
 
linkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptxlinkedlist-130914084342-phpapp02.pptx
linkedlist-130914084342-phpapp02.pptx
 
circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
 
linkedlist.pptx
linkedlist.pptxlinkedlist.pptx
linkedlist.pptx
 
queueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptxqueueppt-191018053228 (1).pptx
queueppt-191018053228 (1).pptx
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 
DS_PPT.pptx
DS_PPT.pptxDS_PPT.pptx
DS_PPT.pptx
 
DS_PPT.ppt
DS_PPT.pptDS_PPT.ppt
DS_PPT.ppt
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

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