SlideShare a Scribd company logo
Unit V
Stack
In the field of computing we have formulated many ways to handle data
efficiently.
So far we have seen how to use variables to store data. However
variables are not feasible when handling a huge amount of data.
Also we need an organized medium of storage to handle any correlated
information. Thus the concept of data structure is introduced.
Data structure is a collection of organized data that are related to each
other.
Data structures are primarily of two types they are
1. Primitive data structures
- Integers
- Real
- Char
2. Non-Primitive Data Structures
- Linear Data structures
- stack
-Queue
- List
- Array
- Non-Linear Data Structures
- Trees
- Graphs.
Operations performed on data structure:
The most common operations on the Data Structure
Insertion: Adding a new element from the Data Structure
Deletion: Deleting an element from the Data Structure.
Traversing: Each element of data is accessed on a process is known as
traversing.
Searching: To find the position of the element in the given Data
Structure.
Merging : Combining the elements of two similar data structures into
one.
Stack:
A Stack is a linear data structure in which a data item is inserted and deleted at one
end.
- A stack is called a “Last in First Out(LIFO)” Structure because the data item that
is inserted last into the stack is the first data item to be deleted from the stack.
- Stacks are extensively used in computer applications.
- Their most notable use is in system software.
- When one function calls another function and passes some
parameters.These parameters are passed using stack. In
fact even many compilers store the local variables inside a
function on the stack.
- Inserting a value to the stack is called as “push” operation,
Where as reading a value from it is called as a “pop”
operation.
Deleting: - The pop operation in the case of a stack is destructive i.e
once an item is popped from the stack, it is no longer available
Operation Content of stack after operation
Push(A) A
Push(B) A B
Pop A
Push( c) A C
Push(D) A C D
Pop A C
POP A
Pop Empty
Stack Implementation:
- Stack implementation can be achieved using arrays. It is a
very simple method but has few limitations.
- Once the size of an array is declared it can not be altered
during program execution.
- The program itself decides the amount of memory needed
and it informs accordingly to the compiler.
- In this method the memory requirement is determined
before compilation and compiler provides the required
memory.
- It is an in efficient memory allocation technique.
- This is because in case we intend to store less arguments
than declared memory is wasted and if we desire to store
more elements than declared array could not expand.
- It is suitable only when we exactly know the number of
elements to be stored.
Dynamic implementation:
- Pointers can also be used for implementation of a stack.
- The dynamic implementation is achieved using pointers.
- The limitations noticed in static implementation can be
removed using dynamic implementation.
- Using pointer implementation at run time. There is no
restriction for number of elements the stack may be
expandable.
- It is efficient in memory allocation because the program
informs the compiler its memory requirement at run time.
- Memory is allocated only after an element is pushed
- Pointer implementation of a stack is carried similar to
array implementation.
- Like an array elements can be stored in successive memory
locations using a pointer.
WAp to perform the stack operations such as push() and pop()
functions using arrays.
#include<stdio.h>
#include<conio.h>
#define max 100;
int stack[max]=n;
int top=-1;
void main()
{
int option,x;
char ch=’y’;
clrscr();
while(ch==’y’)
{
Printf(“1.pushn2.popn3.displayn”);
Printf(“enter choice”);
Scanf(“ %d”,&option);
Switch(option)
{
Case 1:
{
Printf(“enter elements to be pushedn”);
Scanf(“%d”,&n);
Push(stack,n);
Break;
}
Case 2:
{
Pop(stack)
Break;
}
Case 3:
{
Printf(“the elements are n”);
Display(stack);
Break;
Case 3:
{
Printf(“the elements are”);
Display(stack);
Break;
}
}
Printf(“do u want to continue(y/n) n”);
Ch=getche();
}
Getch();
Push(int a[],int item)
{
If(top>=max-1)
{
Printf(“stack if full n”);
}
Else
{
Top++;
A[top]=item;
Printf(“pushed”);
}
Return;
}
Pop(int a[])
{
Int p;
If(top==-1)
Printf(“stack is empty”);
Else
{
P=a[top];
Top--;
Printf(“poped”);
}
Return;
}
Display(int c[])
{
Int i;
If(top>=0)
For(i=top;i>=0;i--)
{
Printf(“%d”,c[i]);
}
Else
Printf(“no element”);
Return;
}
o/p:
1.push
2.pop
3.display
Enter choice
1
Enter elements to be pushed
80
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
1
Enter element to be pushed
90
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice
3
The elements are
90
80
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice 2
Poped
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
2
Poped
Do u want to continue(y/n) y
1.push
2.pop
3.display
Enter choice 2
Stack is empty
Do u want to continue(y/n) n
A stack is a ordered collection of data items into which new items
may be inserted and from which data items may be deleted at one end.
- A stack is also called push down lists.
Eg: A coin stacker etc.
- A stack is most commonly used as a place to store local
variables,parameters and return addresses when a function
is called.
Applications of stack:
1.Situations where useful information must be held temporarily during
the course of a program.
2. compiler in evaluation of an expression by recursion.
3. Memory management in operating system.
4. Evaluation of an arithmetic expression.
5. Nesting of functions.
6. Storing of register contents in process swapping.
Basic terminology associated with stacks.
1.Stack pointer(top) : Keeps track of the current position on the stack.
2.Overflow: occurs when we try to insert more information on a stack
than it can hold.
Underflow:occurs when we try to delete an item of stack which is
empty.
Algorithm for inserting an item into stack:
Procedure push(s,max,top,item)
s-stack
max- stack size
top- stack pointer
item- value in a cell
step1: {check for stack overflow}
if top>=max then
print stack overflow
return
step 2: {Increment pointer top}
toptop+1
step 3:{Insert item at top of the stack}
s[top]item
return
Algorithm for deleting an item from the stack:
Function pop(s top)
S array
Top stack pointer
Step 1: {check for stack underflow}
If top=0 then
Print “stack underflow”
Return
Step 2: {return former top element of stack}
Items[top]
Step 3:{decrement pointer top}
Toptop-1
Return
Applications of stack:
A stack has various real life applications
1. Stack is very useful to maintain the sequence of processing
2. Stacks are used to convert bases of numbers.
3. In evaluation of expressions stacks are used.
Queues
-A queue is a linear sequential list of
items that are accessed in the order
First In a Out(FIFO)
- A queue is special type of data
structure in which insertions take
place from one end called “rear” end
and deletions take place from another
end called “front” end i.e insertions
and deletions take place from different
ends.
- Queue works on the basis of first in
first out[FIFO]. Since the first element
entered in a queue will be the first
element to be deleted.
A queue can be represented using
sequential allocation.
Queue type of data structures is used
in time sharing system where many
user jobs will be waiting in the system
queue for processing. These jobs may
request the service of CPU,Main
memory or external devices such as
printer.
Eg: people waiting in a line at a bank
from a queue, where the first person in
line is the first person to be waiting
and so on.

More Related Content

DOCX
Stacks
PDF
R Cheat Sheet
PPTX
Ppt presentation of queues
PPSX
Data structure stack&queue basics
PPTX
Basic of python for data analysis
PPT
Stack & queue
PPTX
My lecture stack_queue_operation
PPTX
Data Analysis in Python-NumPy
Stacks
R Cheat Sheet
Ppt presentation of queues
Data structure stack&queue basics
Basic of python for data analysis
Stack & queue
My lecture stack_queue_operation
Data Analysis in Python-NumPy

What's hot (20)

PPTX
Stack Data structure
PDF
4 heapsort pq
PPT
Queue in Data Structure
PPTX
R: Apply Functions
PDF
Next Generation Programming in R
PDF
4 R Tutorial DPLYR Apply Function
PPTX
Python - Numpy/Pandas/Matplot Machine Learning Libraries
PDF
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
ODP
Java - Collections
PPT
Queue Data Structure
PPTX
Queue Implementation Using Array & Linked List
PPTX
PPT
Queue Data Structure
PPTX
Introduction to matplotlib
PPTX
queue & its applications
PPTX
Array vs array list
PPTX
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PPT
Stacks and queues
PDF
Data manipulation with dplyr
PPT
Stack, queue and hashing
Stack Data structure
4 heapsort pq
Queue in Data Structure
R: Apply Functions
Next Generation Programming in R
4 R Tutorial DPLYR Apply Function
Python - Numpy/Pandas/Matplot Machine Learning Libraries
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
Java - Collections
Queue Data Structure
Queue Implementation Using Array & Linked List
Queue Data Structure
Introduction to matplotlib
queue & its applications
Array vs array list
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
Stacks and queues
Data manipulation with dplyr
Stack, queue and hashing
Ad

Similar to Ds (20)

PDF
DS UNIT 1.pdf
PDF
DS UNIT 1.pdf
PPTX
هياكلبيانات
PPTX
01-Introduction of DSA-1.pptx
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
PPTX
STACK.pptx
PPTX
Data structure , stack , queue
PPTX
TSAT Presentation1.pptx
PDF
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
PPTX
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
PDF
Stacks-and-Queues.pdf
PDF
PPTX
The presentation on stack data structure
PDF
04 stacks
PPTX
data structures with algorithms vtu 2023 notes.pptx
PPT
The Stack in data structures .ppt
PPT
Unit i(dsc++)
PPTX
STACK 20 INTERVIEW QUESTIONS and answers.pptx
PPTX
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
PDF
LectureNotes-06-DSA
DS UNIT 1.pdf
DS UNIT 1.pdf
هياكلبيانات
01-Introduction of DSA-1.pptx
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
STACK.pptx
Data structure , stack , queue
TSAT Presentation1.pptx
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
DSA_Ques ewoifhjerofhefhehfreofheek.pptx
Stacks-and-Queues.pdf
The presentation on stack data structure
04 stacks
data structures with algorithms vtu 2023 notes.pptx
The Stack in data structures .ppt
Unit i(dsc++)
STACK 20 INTERVIEW QUESTIONS and answers.pptx
STACK 20 INTERVIEW QUESTIONS and answers for interview.pptx
LectureNotes-06-DSA
Ad

More from Acad (20)

PPTX
routing alg.pptx
PPTX
Network Layer design Issues.pptx
PPTX
Computer Science basics
DOCX
Union
DOCX
Str
DOC
Functions
DOCX
File
DOCX
Dma
PDF
Botnet detection by Imitation method
PDF
Bot net detection by using ssl encryption
PDF
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
PDF
Literature survey on peer to peer botnets
PPT
Tiny os
PPT
Data retrieval in sensor networks
DOCX
Structure and Typedef
DOCX
Union from C and Data Strutures
PPT
Cluster analysis
PPT
Classification and prediction
PPT
Association rule mining
PPT
Memory Organization
routing alg.pptx
Network Layer design Issues.pptx
Computer Science basics
Union
Str
Functions
File
Dma
Botnet detection by Imitation method
Bot net detection by using ssl encryption
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
Literature survey on peer to peer botnets
Tiny os
Data retrieval in sensor networks
Structure and Typedef
Union from C and Data Strutures
Cluster analysis
Classification and prediction
Association rule mining
Memory Organization

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Complications of Minimal Access Surgery at WLH
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
human mycosis Human fungal infections are called human mycosis..pptx
01-Introduction-to-Information-Management.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Business Ethics Teaching Materials for college
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Supply Chain Operations Speaking Notes -ICLT Program

Ds

  • 1. Unit V Stack In the field of computing we have formulated many ways to handle data efficiently. So far we have seen how to use variables to store data. However variables are not feasible when handling a huge amount of data. Also we need an organized medium of storage to handle any correlated information. Thus the concept of data structure is introduced. Data structure is a collection of organized data that are related to each other. Data structures are primarily of two types they are 1. Primitive data structures - Integers - Real - Char 2. Non-Primitive Data Structures - Linear Data structures - stack -Queue - List - Array - Non-Linear Data Structures - Trees - Graphs.
  • 2. Operations performed on data structure: The most common operations on the Data Structure Insertion: Adding a new element from the Data Structure Deletion: Deleting an element from the Data Structure. Traversing: Each element of data is accessed on a process is known as traversing. Searching: To find the position of the element in the given Data Structure. Merging : Combining the elements of two similar data structures into one. Stack: A Stack is a linear data structure in which a data item is inserted and deleted at one end. - A stack is called a “Last in First Out(LIFO)” Structure because the data item that is inserted last into the stack is the first data item to be deleted from the stack. - Stacks are extensively used in computer applications. - Their most notable use is in system software. - When one function calls another function and passes some parameters.These parameters are passed using stack. In fact even many compilers store the local variables inside a function on the stack. - Inserting a value to the stack is called as “push” operation, Where as reading a value from it is called as a “pop” operation.
  • 3. Deleting: - The pop operation in the case of a stack is destructive i.e once an item is popped from the stack, it is no longer available Operation Content of stack after operation Push(A) A Push(B) A B Pop A Push( c) A C Push(D) A C D Pop A C POP A Pop Empty Stack Implementation: - Stack implementation can be achieved using arrays. It is a very simple method but has few limitations. - Once the size of an array is declared it can not be altered during program execution. - The program itself decides the amount of memory needed and it informs accordingly to the compiler. - In this method the memory requirement is determined before compilation and compiler provides the required memory. - It is an in efficient memory allocation technique.
  • 4. - This is because in case we intend to store less arguments than declared memory is wasted and if we desire to store more elements than declared array could not expand. - It is suitable only when we exactly know the number of elements to be stored. Dynamic implementation: - Pointers can also be used for implementation of a stack. - The dynamic implementation is achieved using pointers. - The limitations noticed in static implementation can be removed using dynamic implementation. - Using pointer implementation at run time. There is no restriction for number of elements the stack may be expandable. - It is efficient in memory allocation because the program informs the compiler its memory requirement at run time. - Memory is allocated only after an element is pushed - Pointer implementation of a stack is carried similar to array implementation. - Like an array elements can be stored in successive memory locations using a pointer. WAp to perform the stack operations such as push() and pop() functions using arrays. #include<stdio.h> #include<conio.h> #define max 100;
  • 5. int stack[max]=n; int top=-1; void main() { int option,x; char ch=’y’; clrscr(); while(ch==’y’) { Printf(“1.pushn2.popn3.displayn”); Printf(“enter choice”); Scanf(“ %d”,&option); Switch(option) { Case 1: { Printf(“enter elements to be pushedn”); Scanf(“%d”,&n); Push(stack,n); Break; }
  • 6. Case 2: { Pop(stack) Break; } Case 3: { Printf(“the elements are n”); Display(stack); Break; Case 3: { Printf(“the elements are”); Display(stack); Break; } } Printf(“do u want to continue(y/n) n”); Ch=getche(); }
  • 7. Getch(); Push(int a[],int item) { If(top>=max-1) { Printf(“stack if full n”); } Else { Top++; A[top]=item; Printf(“pushed”); } Return; } Pop(int a[]) { Int p; If(top==-1) Printf(“stack is empty”); Else
  • 9. 1.push 2.pop 3.display Enter choice 1 Enter elements to be pushed 80 Pushed Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter choice 1 Enter element to be pushed 90 Pushed Do u want to continue(y/n) Y 1.push
  • 10. 2.pop 3.display Enter ur choice 3 The elements are 90 80 Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter ur choice 2 Poped Do u want to continue(y/n) Y 1.push 2.pop 3.display Enter choice 2
  • 11. Poped Do u want to continue(y/n) y 1.push 2.pop 3.display Enter choice 2 Stack is empty Do u want to continue(y/n) n A stack is a ordered collection of data items into which new items may be inserted and from which data items may be deleted at one end. - A stack is also called push down lists. Eg: A coin stacker etc. - A stack is most commonly used as a place to store local variables,parameters and return addresses when a function is called. Applications of stack: 1.Situations where useful information must be held temporarily during the course of a program. 2. compiler in evaluation of an expression by recursion. 3. Memory management in operating system. 4. Evaluation of an arithmetic expression.
  • 12. 5. Nesting of functions. 6. Storing of register contents in process swapping. Basic terminology associated with stacks. 1.Stack pointer(top) : Keeps track of the current position on the stack. 2.Overflow: occurs when we try to insert more information on a stack than it can hold. Underflow:occurs when we try to delete an item of stack which is empty. Algorithm for inserting an item into stack: Procedure push(s,max,top,item) s-stack max- stack size top- stack pointer item- value in a cell step1: {check for stack overflow} if top>=max then print stack overflow return step 2: {Increment pointer top}
  • 13. toptop+1 step 3:{Insert item at top of the stack} s[top]item return Algorithm for deleting an item from the stack: Function pop(s top) S array Top stack pointer Step 1: {check for stack underflow} If top=0 then Print “stack underflow” Return Step 2: {return former top element of stack} Items[top] Step 3:{decrement pointer top} Toptop-1 Return Applications of stack: A stack has various real life applications
  • 14. 1. Stack is very useful to maintain the sequence of processing 2. Stacks are used to convert bases of numbers. 3. In evaluation of expressions stacks are used. Queues -A queue is a linear sequential list of items that are accessed in the order First In a Out(FIFO) - A queue is special type of data structure in which insertions take place from one end called “rear” end and deletions take place from another end called “front” end i.e insertions and deletions take place from different ends. - Queue works on the basis of first in first out[FIFO]. Since the first element
  • 15. entered in a queue will be the first element to be deleted. A queue can be represented using sequential allocation. Queue type of data structures is used in time sharing system where many user jobs will be waiting in the system queue for processing. These jobs may request the service of CPU,Main memory or external devices such as printer. Eg: people waiting in a line at a bank from a queue, where the first person in line is the first person to be waiting and so on.