SlideShare a Scribd company logo
STACK ADT:
• Stack is a linear data structure in which the
insertion and deletion operations are
performed at only one end.
• In a stack, adding and removing of elements
are performed at a single position which is
known as "top".
• In stack, the insertion and deletion operations
are performed based on LIFO (Last In First
Out) & FILO (First In Last Out) principle.
• In a stack, the insertion operation is performed
using a function called "push" and deletion
operation is performed using a function
called "pop".
Operations on a Stack
• The following operations are performed on the
stack.
1. Push (To insert an element on to the stack)
2. Pop (To delete an element from the stack)
3. Display (To display elements of the stack)
• Stack data structure can be implemented in
two ways. They are as follows.
1. Using Array
2. Using Linked List
• When a stack is implemented using an array,
that stack can organize an only limited number
of elements.
• When a stack is implemented using a linked
list, that stack can organize an unlimited
number of elements.
Implementation of Stack using Array:
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
int stack[SIZE], top = -1;
void main()
{
int value, choice;
clrscr();
while(1)
{
printf("nn***** MENU *****n");
printf("1.Push 2.Pop 3.Display 4.Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the value to insert: ");
scanf("%d",&value);
push(value); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("nWrong selection!");
}
} }
void push(int value)
{
if(top == SIZE-1)
printf("nStack is Full!");
else
{
top++;
stack[top] = value;
printf("nInsertion success!!!");
}
}
void pop()
{
if(top == -1)
printf("nStack is Empty!");
else
{
printf("nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
if(top == -1)
printf("nStack is Empty!!!");
else
{
int i;
printf("nStack elements are:n");
for(i=top; i>=0; i--)
printf("%dn",stack[i]);
} }
Implementation of Stack using Linked List:
#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*top;
void push();
void pop();
void display();
void main()
{
int choice;
clrscr();
while(1)
{
printf("nn***** MENU *****n");
printf("1.Push 2.Pop 3.Display 4.Exit");
printf("nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("nWrong selection!");
}
}
}
void push ()
{
int val;
struct node *new =(struct node*)malloc(sizeof
(struct node));
printf("Enter the value");
scanf("%d",&val);
if(top == NULL)
{
new->data = val;
new->next = NULL;
top=new;
}
else
{
new->data = val;
new->next = top;
top=new;
}
}
void pop()
{
struct node *temp=top;
if (temp== NULL)
printf("Underflow");
else
{
printf(“Deleted element %d”,temp->data);
top = temp->next;
free(temp);
} }
void display()
{
struct node * temp =top;
if(temp == NULL)
printf("Stack is emptyn");
else
{
printf("Printing Stack elements n");
while(temp!=NULL)
{
printf("%dn",temp->data);
temp = temp ->next;
}
}
}
Application of Stack:
• Evaluating arithmetic expression
• Balancing the symbols
• Towers of Hanoi
• Function calls
• 8 Queen problem

More Related Content

What's hot (19)

Avl tree
Avl treeAvl tree
Avl tree
loyola ICAM college of engineering and technology
 
Stack1
Stack1Stack1
Stack1
Iqrazb
 
Array imp of list
Array imp of listArray imp of list
Array imp of list
Elavarasi K
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
Er. Ganesh Ram Suwal
 
Grokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascriptGrokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascript
Grokking VN
 
New microsoft word document
New microsoft word documentNew microsoft word document
New microsoft word document
Abhishek Arya
 
Data Structures : array operations in c program
Data Structures : array operations in c program Data Structures : array operations in c program
Data Structures : array operations in c program
Raghavendra Narayan
 
Python basic Program
Python basic ProgramPython basic Program
Python basic Program
nuripatidar
 
week-17x
week-17xweek-17x
week-17x
KITE www.kitecolleges.com
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
Rumman Ansari
 
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
SAKSHISINGH486
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
Rumman Ansari
 
1346
13461346
1346
VINAY KUMAR KATURI
 
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
9096308941
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
Rumman Ansari
 
Qprgs
QprgsQprgs
Qprgs
Ssankett Negi
 
Luhn sh
Luhn shLuhn sh
Luhn sh
Ben Pope
 
Functional Programming on Android: is it possible?
Functional Programming on Android: is it possible?Functional Programming on Android: is it possible?
Functional Programming on Android: is it possible?
Lucas Albuquerque
 
CalculateLoanPayments
CalculateLoanPaymentsCalculateLoanPayments
CalculateLoanPayments
William Rutherford
 
Stack1
Stack1Stack1
Stack1
Iqrazb
 
Array imp of list
Array imp of listArray imp of list
Array imp of list
Elavarasi K
 
Grokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascriptGrokking TechTalk #16: Maybe functor in javascript
Grokking TechTalk #16: Maybe functor in javascript
Grokking VN
 
New microsoft word document
New microsoft word documentNew microsoft word document
New microsoft word document
Abhishek Arya
 
Data Structures : array operations in c program
Data Structures : array operations in c program Data Structures : array operations in c program
Data Structures : array operations in c program
Raghavendra Narayan
 
Python basic Program
Python basic ProgramPython basic Program
Python basic Program
nuripatidar
 
C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5C Programming Language Step by Step Part 5
C Programming Language Step by Step Part 5
Rumman Ansari
 
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
SAKSHISINGH486
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
Rumman Ansari
 
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
9096308941
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
Rumman Ansari
 
Functional Programming on Android: is it possible?
Functional Programming on Android: is it possible?Functional Programming on Android: is it possible?
Functional Programming on Android: is it possible?
Lucas Albuquerque
 

Similar to DS- Stack ADT (20)

DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
Ahsan Mansiv
 
STACKS implimentarions AND stack applications .pptx
STACKS implimentarions AND stack applications .pptxSTACKS implimentarions AND stack applications .pptx
STACKS implimentarions AND stack applications .pptx
Dr. Amna Mohamed
 
04 stacks
04 stacks04 stacks
04 stacks
Rajan Gautam
 
6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Stack-data-structure.ppsxErwewwwrrterewewew
Stack-data-structure.ppsxErwewwwrrterewewewStack-data-structure.ppsxErwewwwrrterewewew
Stack-data-structure.ppsxErwewwwrrterewewew
RamaKrishnaErroju
 
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
RAtna29
 
Stack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptxStack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptx
yukti266975
 
Stack
StackStack
Stack
Amrutha Rajan
 
Stack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptxStack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptx
Shivam Kumar
 
STACK1.pptx
STACK1.pptxSTACK1.pptx
STACK1.pptx
MouDhara1
 
Chapter 4 stack
Chapter 4 stackChapter 4 stack
Chapter 4 stack
jadhav_priti
 
Stack of Data structure
Stack of Data structureStack of Data structure
Stack of Data structure
Sheikh Monirul Hasan
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
sreekanth3dce
 
Stack.pptx
Stack.pptxStack.pptx
Stack.pptx
SherinRappai
 
Data Structures and algorithms using c .ppt
Data Structures and algorithms using c .pptData Structures and algorithms using c .ppt
Data Structures and algorithms using c .ppt
RaviKumarChavali1
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
EktaVaswani2
 
Stack data structures with definition and code
Stack data structures with definition and codeStack data structures with definition and code
Stack data structures with definition and code
bansidharj11
 
DSA_Unit3_ Stacks and Queues using array (1).pptx
DSA_Unit3_ Stacks and Queues  using array  (1).pptxDSA_Unit3_ Stacks and Queues  using array  (1).pptx
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
introduction of the Stacks and Queues.pptx
introduction of the  Stacks and Queues.pptxintroduction of the  Stacks and Queues.pptx
introduction of the Stacks and Queues.pptx
kavitashingi123
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
swathirajstar
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
Ahsan Mansiv
 
STACKS implimentarions AND stack applications .pptx
STACKS implimentarions AND stack applications .pptxSTACKS implimentarions AND stack applications .pptx
STACKS implimentarions AND stack applications .pptx
Dr. Amna Mohamed
 
6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx6 - STACKS in Data Structure and Algorithm.pptx
6 - STACKS in Data Structure and Algorithm.pptx
RahulRaj493025
 
Stack-data-structure.ppsxErwewwwrrterewewew
Stack-data-structure.ppsxErwewwwrrterewewewStack-data-structure.ppsxErwewwwrrterewewew
Stack-data-structure.ppsxErwewwwrrterewewew
RamaKrishnaErroju
 
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
RAtna29
 
Stack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptxStack,queue and linked list data structure.pptx
Stack,queue and linked list data structure.pptx
yukti266975
 
Stack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptxStack and its operation implemented with array new - Copy.pptx
Stack and its operation implemented with array new - Copy.pptx
Shivam Kumar
 
Datastructures asignment
Datastructures asignmentDatastructures asignment
Datastructures asignment
sreekanth3dce
 
Data Structures and algorithms using c .ppt
Data Structures and algorithms using c .pptData Structures and algorithms using c .ppt
Data Structures and algorithms using c .ppt
RaviKumarChavali1
 
Stack data structures with definition and code
Stack data structures with definition and codeStack data structures with definition and code
Stack data structures with definition and code
bansidharj11
 
DSA_Unit3_ Stacks and Queues using array (1).pptx
DSA_Unit3_ Stacks and Queues  using array  (1).pptxDSA_Unit3_ Stacks and Queues  using array  (1).pptx
DSA_Unit3_ Stacks and Queues using array (1).pptx
nandinigujarathi9
 
introduction of the Stacks and Queues.pptx
introduction of the  Stacks and Queues.pptxintroduction of the  Stacks and Queues.pptx
introduction of the Stacks and Queues.pptx
kavitashingi123
 

More from MythiliMurugan3 (9)

DS - Quick Sort
DS - Quick SortDS - Quick Sort
DS - Quick Sort
MythiliMurugan3
 
DS - Graph Traversal
DS - Graph TraversalDS - Graph Traversal
DS - Graph Traversal
MythiliMurugan3
 
DS - BST
DS - BSTDS - BST
DS - BST
MythiliMurugan3
 
DS - Application of List
DS - Application of ListDS - Application of List
DS - Application of List
MythiliMurugan3
 
DBMS - Relational Algebra
DBMS - Relational AlgebraDBMS - Relational Algebra
DBMS - Relational Algebra
MythiliMurugan3
 
DBMS - Distributed Databases
DBMS - Distributed DatabasesDBMS - Distributed Databases
DBMS - Distributed Databases
MythiliMurugan3
 
DBMS - RAID
DBMS - RAIDDBMS - RAID
DBMS - RAID
MythiliMurugan3
 
DBMS - Transactions
DBMS - TransactionsDBMS - Transactions
DBMS - Transactions
MythiliMurugan3
 
DBMS - ER Model
DBMS - ER ModelDBMS - ER Model
DBMS - ER Model
MythiliMurugan3
 

Recently uploaded (20)

David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
22PCOAM16 ML Unit 3 Full notes PDF & QB.pdf
Guru Nanak Technical Institutions
 
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning ModelsMode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Mode-Wise Corridor Level Travel-Time Estimation Using Machine Learning Models
Journal of Soft Computing in Civil Engineering
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation RateModeling the Influence of Environmental Factors on Concrete Evaporation Rate
Modeling the Influence of Environmental Factors on Concrete Evaporation Rate
Journal of Soft Computing in Civil Engineering
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdfDavid Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry - Specializes In AWS, Microservices And Python.pdf
David Boutry
 
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdfML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
ML_Unit_V_RDC_ASSOCIATION AND DIMENSIONALITY REDUCTION.pdf
rameshwarchintamani
 
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdfLittle Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
Little Known Ways To 3 Best sites to Buy Linkedin Accounts.pdf
gori42199
 
Generative AI & Large Language Models Agents
Generative AI & Large Language Models AgentsGenerative AI & Large Language Models Agents
Generative AI & Large Language Models Agents
aasgharbee22seecs
 
Construction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.pptConstruction-Chemicals-For-Waterproofing.ppt
Construction-Chemicals-For-Waterproofing.ppt
ssuser2ffcbc
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdfML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
ML_Unit_VI_DEEP LEARNING_Introduction to ANN.pdf
rameshwarchintamani
 
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdfSmart City is the Future EN - 2024 Thailand Modify V1.0.pdf
Smart City is the Future EN - 2024 Thailand Modify V1.0.pdf
PawachMetharattanara
 
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink DisplayHow to Build a Desktop Weather Station Using ESP32 and E-ink Display
How to Build a Desktop Weather Station Using ESP32 and E-ink Display
CircuitDigest
 
Automatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and BeyondAutomatic Quality Assessment for Speech and Beyond
Automatic Quality Assessment for Speech and Beyond
NU_I_TODALAB
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
introduction technology technology tec.pptx
introduction technology technology tec.pptxintroduction technology technology tec.pptx
introduction technology technology tec.pptx
Iftikhar70
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Lecture - 7 Canals of the topic of the civil engineering
Lecture - 7  Canals of the topic of the civil engineeringLecture - 7  Canals of the topic of the civil engineering
Lecture - 7 Canals of the topic of the civil engineering
MJawadkhan1
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 

DS- Stack ADT

  • 1. STACK ADT: • Stack is a linear data structure in which the insertion and deletion operations are performed at only one end. • In a stack, adding and removing of elements are performed at a single position which is known as "top". • In stack, the insertion and deletion operations are performed based on LIFO (Last In First Out) & FILO (First In Last Out) principle.
  • 2. • In a stack, the insertion operation is performed using a function called "push" and deletion operation is performed using a function called "pop". Operations on a Stack • The following operations are performed on the stack. 1. Push (To insert an element on to the stack) 2. Pop (To delete an element from the stack) 3. Display (To display elements of the stack)
  • 3. • Stack data structure can be implemented in two ways. They are as follows. 1. Using Array 2. Using Linked List • When a stack is implemented using an array, that stack can organize an only limited number of elements. • When a stack is implemented using a linked list, that stack can organize an unlimited number of elements.
  • 4. Implementation of Stack using Array: #include<stdio.h> #include<conio.h> #define SIZE 10 void push(int); void pop(); void display(); int stack[SIZE], top = -1;
  • 5. void main() { int value, choice; clrscr(); while(1) { printf("nn***** MENU *****n"); printf("1.Push 2.Pop 3.Display 4.Exit"); printf("nEnter your choice: "); scanf("%d",&choice);
  • 6. switch(choice) { case 1: printf("Enter the value to insert: "); scanf("%d",&value); push(value); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("nWrong selection!"); } } }
  • 7. void push(int value) { if(top == SIZE-1) printf("nStack is Full!"); else { top++; stack[top] = value; printf("nInsertion success!!!"); } }
  • 8. void pop() { if(top == -1) printf("nStack is Empty!"); else { printf("nDeleted : %d", stack[top]); top--; } }
  • 9. void display() { if(top == -1) printf("nStack is Empty!!!"); else { int i; printf("nStack elements are:n"); for(i=top; i>=0; i--) printf("%dn",stack[i]); } }
  • 10. Implementation of Stack using Linked List: #include<stdio.h> #include<conio.h> struct node { int data; struct node *next; }*top; void push(); void pop(); void display();
  • 11. void main() { int choice; clrscr(); while(1) { printf("nn***** MENU *****n"); printf("1.Push 2.Pop 3.Display 4.Exit"); printf("nEnter your choice: "); scanf("%d",&choice);
  • 12. switch(choice) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("nWrong selection!"); } } }
  • 13. void push () { int val; struct node *new =(struct node*)malloc(sizeof (struct node)); printf("Enter the value"); scanf("%d",&val); if(top == NULL) { new->data = val; new->next = NULL;
  • 15. void pop() { struct node *temp=top; if (temp== NULL) printf("Underflow"); else { printf(“Deleted element %d”,temp->data); top = temp->next; free(temp); } }
  • 16. void display() { struct node * temp =top; if(temp == NULL) printf("Stack is emptyn"); else { printf("Printing Stack elements n");
  • 18. Application of Stack: • Evaluating arithmetic expression • Balancing the symbols • Towers of Hanoi • Function calls • 8 Queen problem
  翻译: