SlideShare a Scribd company logo
1 of 16
Programming and Data Structures
Courtesy: https://cse.iitkgp.ac.in/~pds/
Lecture #1
Stacks
2
*Stack
*Basic principles
*Operation of stack
*Stack using Array
*Stack using Linked List
*Applications of stack
Basic Idea
• A stack is an Abstract Data Type (ADT), commonly used in most
programming languages. It is named stack as it behaves like a real-
world stack, for example – a deck of cards or a pile of plates, etc.
• An ADT specifies:
• Data stored
• Operations on the data
• Error conditions associated with operations
3
Stack Representation
• Can be implemented by means of Array, Structure, Pointers and
Linked List.
• Stack can either be a fixed size or dynamic.
4
5
STACK
push
create
pop
isfull
isempty
STACK: Last-In-First-Out (LIFO)
• void push (stack *s, int element);
/* Insert an element in the stack */
• int pop (stack *s);
/* Remove and return the top element */
• void create (stack *s);
/* Create a new stack */
• int isempty (stack *s);
/* Check if stack is empty */
• int isfull (stack *s);
/* Check if stack is full */
6
Assumption: stack contains integer elements!
7
top
top
PUSH
Push using Stack
Stack using Array
8
top
top
POP
Pop using Stack
• In the array implementation, we would:
• Declare an array of fixed size (which determines the maximum
size of the stack).
• Keep a variable which always points to the “top” of the stack.
• Contains the array index of the “top” element.
• In the linked list implementation, we would:
• Maintain the stack as a linked list.
• A pointer variable top points to the start of the list.
• The first element of the linked list is considered as the stack top.
9
Basic Idea
Declaration
10
#define MAXSIZE 100
struct lifo
{
int st[MAXSIZE];
int top;
};
typedef struct lifo
stack;
stack s;
struct lifo
{
int value;
struct lifo *next;
};
typedef struct lifo
stack;
stack *top;
ARRAY LINKED LIST
Stack Creation
11
void create (stack *s)
{
s->top = -1;
/* s->top points to
last element
pushed in;
initially -1 */
}
void create (stack **top)
{
*top = NULL;
/* top points to NULL,
indicating empty
stack */
}
ARRAY LINKED LIST
Pushing an element into stack
12
void push (stack *s, int element)
{
if (s->top == (MAXSIZE-1))
{
printf (“n Stack overflow”);
exit(-1);
}
else
{
s->top++;
s->st[s->top] = element;
}
}
ARRAY
void push (stack **top, int element)
{
stack *new;
new = (stack *)malloc (sizeof(stack));
if (new == NULL)
{
printf (“n Stack is full”);
exit(-1);
}
new->value = element;
new->next = *top;
*top = new;
}
LINKED LIST
Popping an element from stack
13
int pop (stack *s)
{
if (s->top == -1)
{
printf (“n Stack underflow”);
exit(-1);
}
else
{
return (s->st[s->top--]);
}
}
ARRAY
int pop (stack **top)
{
int t;
stack *p;
if (*top == NULL)
{
printf (“n Stack is empty”);
exit(-1);
}
else
{
t = (*top)->value;
p = *top;
*top = (*top)->next;
free (p);
return t;
}
}
LINKED LIST
Checking for stack empty
14
int isempty (stack *s)
{
if (s->top == -1)
return 1;
else
return (0);
}
ARRAY LINKED LIST
int isempty (stack *top)
{
if (top == NULL)
return (1);
else
return (0);
}
Checking for Stack Full
15
int isempty (stack *s)
{
if (s->top == -1)
return 1;
else
return (0);
}
ARRAY LINKED LIST
int isempty (stack *top)
{
if (top == NULL)
return (1);
else
return (0);
}
Example: A Stack using an Array
16
#include <stdio.h>
#define MAXSIZE 100
struct lifo
{
int st[MAXSIZE];
int top;
};
typedef struct lifo stack;
main() {
stack A, B;
create(&A);
create(&B);
push(&A,10);
push(&A,20);
push(&A,30);
push(&B,100);
push(&B,5);
printf (“%d %d”, pop(&A), pop(&B));
push (&A, pop(&B));
if (isempty(&B))
printf (“n B is empty”);
return;
}

More Related Content

Similar to MO 2020 DS Stacks 1 AB.ppt

Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
pallavi953613
 

Similar to MO 2020 DS Stacks 1 AB.ppt (20)

Project of data structure
Project of data structureProject of data structure
Project of data structure
 
Stack
StackStack
Stack
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
 
Data structures
Data structuresData structures
Data structures
 
notes.pdf
notes.pdfnotes.pdf
notes.pdf
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdfC++ Program It is only 1 rotation. Up-rotation of a stack.  Write a.pdf
C++ Program It is only 1 rotation. Up-rotation of a stack. Write a.pdf
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack queue
Stack queueStack queue
Stack queue
 
Stack
StackStack
Stack
 
Data Structure.pptx
Data Structure.pptxData Structure.pptx
Data Structure.pptx
 
Stack
StackStack
Stack
 
Stack ADT
Stack ADTStack ADT
Stack ADT
 
Stacks in data structure
Stacks  in data structureStacks  in data structure
Stacks in data structure
 

More from shashankbhadouria4

IT201 Basics of Intelligent Systems-1.pptx
IT201 Basics of Intelligent Systems-1.pptxIT201 Basics of Intelligent Systems-1.pptx
IT201 Basics of Intelligent Systems-1.pptx
shashankbhadouria4
 
A New Multi-Level Inverter Topology With Reduced Switch.pptx
A New Multi-Level Inverter Topology With Reduced Switch.pptxA New Multi-Level Inverter Topology With Reduced Switch.pptx
A New Multi-Level Inverter Topology With Reduced Switch.pptx
shashankbhadouria4
 
Birla Institute of Technology Mesra Jaipur.pptx
Birla Institute of Technology Mesra Jaipur.pptxBirla Institute of Technology Mesra Jaipur.pptx
Birla Institute of Technology Mesra Jaipur.pptx
shashankbhadouria4
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.ppt
shashankbhadouria4
 

More from shashankbhadouria4 (20)

EC203DSD - Module 5 - 3.ppt
EC203DSD - Module 5 - 3.pptEC203DSD - Module 5 - 3.ppt
EC203DSD - Module 5 - 3.ppt
 
Artificial Neural Network.pptx
Artificial Neural Network.pptxArtificial Neural Network.pptx
Artificial Neural Network.pptx
 
IT201 Basics of Intelligent Systems-1.pptx
IT201 Basics of Intelligent Systems-1.pptxIT201 Basics of Intelligent Systems-1.pptx
IT201 Basics of Intelligent Systems-1.pptx
 
MO 2020 DS Doubly Linked List 1 AB.ppt
MO 2020 DS Doubly Linked List 1 AB.pptMO 2020 DS Doubly Linked List 1 AB.ppt
MO 2020 DS Doubly Linked List 1 AB.ppt
 
MO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.pptMO 2020 DS Stacks 3 AB.ppt
MO 2020 DS Stacks 3 AB.ppt
 
A New Multi-Level Inverter Topology With Reduced Switch.pptx
A New Multi-Level Inverter Topology With Reduced Switch.pptxA New Multi-Level Inverter Topology With Reduced Switch.pptx
A New Multi-Level Inverter Topology With Reduced Switch.pptx
 
Birla Institute of Technology Mesra Jaipur.pptx
Birla Institute of Technology Mesra Jaipur.pptxBirla Institute of Technology Mesra Jaipur.pptx
Birla Institute of Technology Mesra Jaipur.pptx
 
EE306_EXP1.pptx
EE306_EXP1.pptxEE306_EXP1.pptx
EE306_EXP1.pptx
 
III_Data Structure_Module_1.ppt
III_Data Structure_Module_1.pptIII_Data Structure_Module_1.ppt
III_Data Structure_Module_1.ppt
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Chap 5 Tree.ppt
Chap 5 Tree.pptChap 5 Tree.ppt
Chap 5 Tree.ppt
 
Chap 2 Arrays and Structures.ppt
Chap 2  Arrays and Structures.pptChap 2  Arrays and Structures.ppt
Chap 2 Arrays and Structures.ppt
 
Chap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.pptChap 4 List of Data Structure.ppt
Chap 4 List of Data Structure.ppt
 
SUmmer Training PPT FINAL.pptx
SUmmer Training PPT FINAL.pptxSUmmer Training PPT FINAL.pptx
SUmmer Training PPT FINAL.pptx
 
RVPN TRAINING PPT.pptx
RVPN TRAINING PPT.pptxRVPN TRAINING PPT.pptx
RVPN TRAINING PPT.pptx
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
MODULE 1.pptx
MODULE 1.pptxMODULE 1.pptx
MODULE 1.pptx
 
MO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.pptMO 2020 DS Applications of Linked List 1 AB.ppt
MO 2020 DS Applications of Linked List 1 AB.ppt
 
III_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptxIII_Data Structure_Module_1.pptx
III_Data Structure_Module_1.pptx
 
Chap 2 Arrays and Structures.pptx
Chap 2  Arrays and Structures.pptxChap 2  Arrays and Structures.pptx
Chap 2 Arrays and Structures.pptx
 

Recently uploaded

Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
Kamal Acharya
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 

Recently uploaded (20)

Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLEGEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
GEAR TRAIN- BASIC CONCEPTS AND WORKING PRINCIPLE
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
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
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 

MO 2020 DS Stacks 1 AB.ppt

  • 1. Programming and Data Structures Courtesy: https://cse.iitkgp.ac.in/~pds/ Lecture #1 Stacks
  • 2. 2 *Stack *Basic principles *Operation of stack *Stack using Array *Stack using Linked List *Applications of stack
  • 3. Basic Idea • A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real- world stack, for example – a deck of cards or a pile of plates, etc. • An ADT specifies: • Data stored • Operations on the data • Error conditions associated with operations 3
  • 4. Stack Representation • Can be implemented by means of Array, Structure, Pointers and Linked List. • Stack can either be a fixed size or dynamic. 4
  • 6. STACK: Last-In-First-Out (LIFO) • void push (stack *s, int element); /* Insert an element in the stack */ • int pop (stack *s); /* Remove and return the top element */ • void create (stack *s); /* Create a new stack */ • int isempty (stack *s); /* Check if stack is empty */ • int isfull (stack *s); /* Check if stack is full */ 6 Assumption: stack contains integer elements!
  • 9. • In the array implementation, we would: • Declare an array of fixed size (which determines the maximum size of the stack). • Keep a variable which always points to the “top” of the stack. • Contains the array index of the “top” element. • In the linked list implementation, we would: • Maintain the stack as a linked list. • A pointer variable top points to the start of the list. • The first element of the linked list is considered as the stack top. 9 Basic Idea
  • 10. Declaration 10 #define MAXSIZE 100 struct lifo { int st[MAXSIZE]; int top; }; typedef struct lifo stack; stack s; struct lifo { int value; struct lifo *next; }; typedef struct lifo stack; stack *top; ARRAY LINKED LIST
  • 11. Stack Creation 11 void create (stack *s) { s->top = -1; /* s->top points to last element pushed in; initially -1 */ } void create (stack **top) { *top = NULL; /* top points to NULL, indicating empty stack */ } ARRAY LINKED LIST
  • 12. Pushing an element into stack 12 void push (stack *s, int element) { if (s->top == (MAXSIZE-1)) { printf (“n Stack overflow”); exit(-1); } else { s->top++; s->st[s->top] = element; } } ARRAY void push (stack **top, int element) { stack *new; new = (stack *)malloc (sizeof(stack)); if (new == NULL) { printf (“n Stack is full”); exit(-1); } new->value = element; new->next = *top; *top = new; } LINKED LIST
  • 13. Popping an element from stack 13 int pop (stack *s) { if (s->top == -1) { printf (“n Stack underflow”); exit(-1); } else { return (s->st[s->top--]); } } ARRAY int pop (stack **top) { int t; stack *p; if (*top == NULL) { printf (“n Stack is empty”); exit(-1); } else { t = (*top)->value; p = *top; *top = (*top)->next; free (p); return t; } } LINKED LIST
  • 14. Checking for stack empty 14 int isempty (stack *s) { if (s->top == -1) return 1; else return (0); } ARRAY LINKED LIST int isempty (stack *top) { if (top == NULL) return (1); else return (0); }
  • 15. Checking for Stack Full 15 int isempty (stack *s) { if (s->top == -1) return 1; else return (0); } ARRAY LINKED LIST int isempty (stack *top) { if (top == NULL) return (1); else return (0); }
  • 16. Example: A Stack using an Array 16 #include <stdio.h> #define MAXSIZE 100 struct lifo { int st[MAXSIZE]; int top; }; typedef struct lifo stack; main() { stack A, B; create(&A); create(&B); push(&A,10); push(&A,20); push(&A,30); push(&B,100); push(&B,5); printf (“%d %d”, pop(&A), pop(&B)); push (&A, pop(&B)); if (isempty(&B)) printf (“n B is empty”); return; }