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

What's hot

Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in RFlorian Uhlitz
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply FunctionSakthi Dasans
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES Kathirvel Ayyaswamy
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListPTCL
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureSriram Raj
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlibPiyush rai
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Array vs array list
Array vs array listArray vs array list
Array vs array listRavi Shetye
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYAMaulik Borsaniya
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyrRomain Francois
 

What's hot (20)

Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
 
4 heapsort pq
4 heapsort pq4 heapsort pq
4 heapsort pq
 
Queue in Data Structure
Queue in Data StructureQueue in Data Structure
Queue in Data Structure
 
R: Apply Functions
R: Apply FunctionsR: Apply Functions
R: Apply Functions
 
Next Generation Programming in R
Next Generation Programming in RNext Generation Programming in R
Next Generation Programming in R
 
4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function4 R Tutorial DPLYR Apply Function
4 R Tutorial DPLYR Apply Function
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 
Java - Collections
Java - CollectionsJava - Collections
Java - Collections
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Queues
QueuesQueues
Queues
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Introduction to matplotlib
Introduction to matplotlibIntroduction to matplotlib
Introduction to matplotlib
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Array vs array list
Array vs array listArray vs array list
Array vs array list
 
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYAPYTHON-Chapter 4-Plotting and Data Science  PyLab - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Data manipulation with dplyr
Data manipulation with dplyrData manipulation with dplyr
Data manipulation with dplyr
 
Stack, queue and hashing
Stack, queue and hashingStack, queue and hashing
Stack, queue and hashing
 

Similar to Ds

هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptxDwijBaxi
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueBalwant Gorad
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdfTobyWtf
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for publiciqbalphy1
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using Ccpjcollege
 

Similar to Ds (20)

DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
DS UNIT 1.pdf
DS UNIT 1.pdfDS UNIT 1.pdf
DS UNIT 1.pdf
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
STACK.pptx
STACK.pptxSTACK.pptx
STACK.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority QueueWhat is Stack, Its Operations, Queue, Circular Queue, Priority Queue
What is Stack, Its Operations, Queue, Circular Queue, Priority Queue
 
Stacks-and-Queues.pdf
Stacks-and-Queues.pdfStacks-and-Queues.pdf
Stacks-and-Queues.pdf
 
Stack
StackStack
Stack
 
04 stacks
04 stacks04 stacks
04 stacks
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack and its operations
Stack and its operationsStack and its operations
Stack and its operations
 
Stack data structure
Stack data structureStack data structure
Stack data structure
 
stacks and queues for public
stacks and queues for publicstacks and queues for public
stacks and queues for public
 
Data structures
Data structuresData structures
Data structures
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure Using C
Data Structure Using CData Structure Using C
Data Structure Using C
 

More from Acad

routing alg.pptx
routing alg.pptxrouting alg.pptx
routing alg.pptxAcad
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptxAcad
 
Computer Science basics
Computer Science basics Computer Science basics
Computer Science basics Acad
 
Union
UnionUnion
UnionAcad
 
Str
StrStr
StrAcad
 
Functions
FunctionsFunctions
FunctionsAcad
 
File
FileFile
FileAcad
 
Dma
DmaDma
DmaAcad
 
Botnet detection by Imitation method
Botnet detection  by Imitation methodBotnet detection  by Imitation method
Botnet detection by Imitation methodAcad
 
Bot net detection by using ssl encryption
Bot net detection by using ssl encryptionBot net detection by using ssl encryption
Bot net detection by using ssl encryptionAcad
 
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...Acad
 
Literature survey on peer to peer botnets
Literature survey on peer to peer botnetsLiterature survey on peer to peer botnets
Literature survey on peer to peer botnetsAcad
 
Tiny os
Tiny osTiny os
Tiny osAcad
 
Data retrieval in sensor networks
Data retrieval in sensor networksData retrieval in sensor networks
Data retrieval in sensor networksAcad
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and TypedefAcad
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data StruturesAcad
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysisAcad
 
Classification and prediction
Classification and predictionClassification and prediction
Classification and predictionAcad
 
Association rule mining
Association rule miningAssociation rule mining
Association rule miningAcad
 
Memory Organization
Memory OrganizationMemory Organization
Memory OrganizationAcad
 

More from Acad (20)

routing alg.pptx
routing alg.pptxrouting alg.pptx
routing alg.pptx
 
Network Layer design Issues.pptx
Network Layer design Issues.pptxNetwork Layer design Issues.pptx
Network Layer design Issues.pptx
 
Computer Science basics
Computer Science basics Computer Science basics
Computer Science basics
 
Union
UnionUnion
Union
 
Str
StrStr
Str
 
Functions
FunctionsFunctions
Functions
 
File
FileFile
File
 
Dma
DmaDma
Dma
 
Botnet detection by Imitation method
Botnet detection  by Imitation methodBotnet detection  by Imitation method
Botnet detection by Imitation method
 
Bot net detection by using ssl encryption
Bot net detection by using ssl encryptionBot net detection by using ssl encryption
Bot net detection by using ssl encryption
 
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
An Aggregate Location Monitoring System Of Privacy Preserving In Authenticati...
 
Literature survey on peer to peer botnets
Literature survey on peer to peer botnetsLiterature survey on peer to peer botnets
Literature survey on peer to peer botnets
 
Tiny os
Tiny osTiny os
Tiny os
 
Data retrieval in sensor networks
Data retrieval in sensor networksData retrieval in sensor networks
Data retrieval in sensor networks
 
Structure and Typedef
Structure and TypedefStructure and Typedef
Structure and Typedef
 
Union from C and Data Strutures
Union from C and Data StruturesUnion from C and Data Strutures
Union from C and Data Strutures
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysis
 
Classification and prediction
Classification and predictionClassification and prediction
Classification and prediction
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Memory Organization
Memory OrganizationMemory Organization
Memory Organization
 

Recently uploaded

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 

Recently uploaded (20)

2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

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.