SlideShare a Scribd company logo
DATA STRUCTURES
STACK IMPLEMENTATION USING
ARRAY
Santhiya S
Assistant Professor
Department of AI
Kongu Engineering College
STACK AS ADT
Basic Operations:
Insertion from top Deletion from top
Push Pop
Last in First out (LIFO) or First in Last out (FILO).
4
3
2
1
0
Size=5
TOP=-1
(At initially)
top++
push(6)
insert 6
top--
pop()
now there we have
garbage value. If you
insert next element, it
will be overwritten
top++
push(3)
insert 3
top--
pop()
top++
push(5)
insert 5
TOP
TOP
5
GARBAGE
6
3
push(5)
push(6)
pop()
push(3)
pop()
STACK
WORKING OF STACK
IMPLEMENTATION OF STACK using
Array
#define N 5;
int stack [N];
int top = -1;
void push()
{
int x;
printf(“Enter data”);
scanf(“%d”, &x);
if(top= =N-1)
printf(“Overflow”);
else
{
top++;
stack[top]=x;
}
}
void pop()
{
int item;
if(top= =-1)
printf(“underflow”);
else
{
item=stack[top];
top--;
printf(“pop element is %d”, item);
}
}
void peek()
{
if(top= =-1)
printf(“underflow”);
else
printf(“%d”, stack[top]);
}
void display()
{
int i;
for(i=top; i >=0;i--)
{
printf(“%d”, stack[i]);
}
}
void main()
{
push();
push();
pop();
peek();
display();}
4
3
2
1
0
stack 5
top=-1
STACK
top
top
int x 10
-1==4, False
20
10
0==4, False
20
int item
1==-1, False
20
garbage
pop element is 20
10
10
0==-1, False
i=0;0>=0,true
i=-1

More Related Content

PPTX
Binary search tree.pptx
Santhiya S
 
PPTX
Expression tree conversion.pptx
Santhiya S
 
PPTX
Queue implementation using Linked list.pptx
Santhiya S
 
PPTX
Queue implementation using Array.pptx
Santhiya S
 
PPTX
Circular linked list.pptx
Santhiya S
 
PPTX
Doubly linked list.pptx
Santhiya S
 
PPTX
Singly linked list.pptx
Santhiya S
 
PPTX
Linked list memory allocation and its types.pptx
Santhiya S
 
Binary search tree.pptx
Santhiya S
 
Expression tree conversion.pptx
Santhiya S
 
Queue implementation using Linked list.pptx
Santhiya S
 
Queue implementation using Array.pptx
Santhiya S
 
Circular linked list.pptx
Santhiya S
 
Doubly linked list.pptx
Santhiya S
 
Singly linked list.pptx
Santhiya S
 
Linked list memory allocation and its types.pptx
Santhiya S
 

Recently uploaded (19)

PPTX
MODUL PEMB CANVA-ULFA RHOMAISA BASAR-2281130172-A4 FIX.pdf.pptx
UlfaRhomaisaBasar
 
PDF
مواقع للذاكرة والتركيز مناسب لحصص الاشغال.pdf
anwaarabusafa
 
PDF
pelatihan rencana evakuasi team darurat.PDF
willys241
 
PPTX
fiqh thaharoh kajian umum selasa malam.pptx
robytyo
 
PPTX
Saint Peter Chrysologus, Bishop of Ravenna, Doctor of Homilies (Russian).pptx
Martin M Flynn
 
PDF
Exercise 1(a) Hindi medium Class 6 Math Uttar Pradesh Board Exercise 1(b)
Mera School Online Academy
 
PPTX
bahan ajar PPT Pembahasan soal Fungsi kuadrat.pptx
lutfi882098
 
PPTX
tambah tolak darab dan bahagi tahun 3.pptx
Noraim Ismail
 
PPTX
HANAFI'S PPT.pptx HANAFI'S PPT HANAFI'S PPY HANAFI'S PP
trianda04
 
PPTX
Conocer las características del animal, pero todo en quechua.
LeydeVictoriaDazaHil
 
PPTX
bahan ajar materi latihan PPT STATISTIKA.pptx
lutfi882098
 
PPTX
PRONUNCIATION DRILL in English subject.pptx
Nerissa Poldo
 
PDF
نظم الياقوت فى سر الكهنوت
Smiling Lungs
 
PDF
分科原得總分與級分對照表.pdf分科原得總分與級分對照表.pdf分科原得總分與級分對照表.pdf
中 央社
 
PPTX
perang diponegoro antara pasukan Pangeran Diponegoro dan pemerintah kolonial ...
shaqiltankian3
 
PDF
Handbook of Critical Care Medicine 1st Edition Senaka Rajapakse
bujljivk7348
 
PPTX
bagi pemula menggunmakan operasi power point
shaqilanauvalynalynr
 
PPTX
Irodori japan foundation Pemula Bab 10.pptx
Nursalam63
 
PPTX
Program bicara Kecemerlangan tahun 6.pptx
g48401826
 
MODUL PEMB CANVA-ULFA RHOMAISA BASAR-2281130172-A4 FIX.pdf.pptx
UlfaRhomaisaBasar
 
مواقع للذاكرة والتركيز مناسب لحصص الاشغال.pdf
anwaarabusafa
 
pelatihan rencana evakuasi team darurat.PDF
willys241
 
fiqh thaharoh kajian umum selasa malam.pptx
robytyo
 
Saint Peter Chrysologus, Bishop of Ravenna, Doctor of Homilies (Russian).pptx
Martin M Flynn
 
Exercise 1(a) Hindi medium Class 6 Math Uttar Pradesh Board Exercise 1(b)
Mera School Online Academy
 
bahan ajar PPT Pembahasan soal Fungsi kuadrat.pptx
lutfi882098
 
tambah tolak darab dan bahagi tahun 3.pptx
Noraim Ismail
 
HANAFI'S PPT.pptx HANAFI'S PPT HANAFI'S PPY HANAFI'S PP
trianda04
 
Conocer las características del animal, pero todo en quechua.
LeydeVictoriaDazaHil
 
bahan ajar materi latihan PPT STATISTIKA.pptx
lutfi882098
 
PRONUNCIATION DRILL in English subject.pptx
Nerissa Poldo
 
نظم الياقوت فى سر الكهنوت
Smiling Lungs
 
分科原得總分與級分對照表.pdf分科原得總分與級分對照表.pdf分科原得總分與級分對照表.pdf
中 央社
 
perang diponegoro antara pasukan Pangeran Diponegoro dan pemerintah kolonial ...
shaqiltankian3
 
Handbook of Critical Care Medicine 1st Edition Senaka Rajapakse
bujljivk7348
 
bagi pemula menggunmakan operasi power point
shaqilanauvalynalynr
 
Irodori japan foundation Pemula Bab 10.pptx
Nursalam63
 
Program bicara Kecemerlangan tahun 6.pptx
g48401826
 
Ad
Ad

Stack implementation using Array.pptx

  • 1. DATA STRUCTURES STACK IMPLEMENTATION USING ARRAY Santhiya S Assistant Professor Department of AI Kongu Engineering College
  • 2. STACK AS ADT Basic Operations: Insertion from top Deletion from top Push Pop Last in First out (LIFO) or First in Last out (FILO).
  • 3. 4 3 2 1 0 Size=5 TOP=-1 (At initially) top++ push(6) insert 6 top-- pop() now there we have garbage value. If you insert next element, it will be overwritten top++ push(3) insert 3 top-- pop() top++ push(5) insert 5 TOP TOP 5 GARBAGE 6 3 push(5) push(6) pop() push(3) pop() STACK WORKING OF STACK
  • 5. #define N 5; int stack [N]; int top = -1; void push() { int x; printf(“Enter data”); scanf(“%d”, &x); if(top= =N-1) printf(“Overflow”); else { top++; stack[top]=x; } } void pop() { int item; if(top= =-1) printf(“underflow”); else { item=stack[top]; top--; printf(“pop element is %d”, item); } } void peek() { if(top= =-1) printf(“underflow”); else printf(“%d”, stack[top]); } void display() { int i; for(i=top; i >=0;i--) { printf(“%d”, stack[i]); } } void main() { push(); push(); pop(); peek(); display();} 4 3 2 1 0 stack 5 top=-1 STACK top top int x 10 -1==4, False 20 10 0==4, False 20 int item 1==-1, False 20 garbage pop element is 20 10 10 0==-1, False i=0;0>=0,true i=-1