SlideShare una empresa de Scribd logo
1 de 49
Ms. D. Seethalakshmi
Assistant Professor
Bon Secours College for Women
Thanjavur
1
 Stack of Books
2
 What is a Stack?
 A stack is a data structure of ordered items such
that items can be inserted and removed only at
one end.
3
 This strategy states that the element that is
inserted last will come out first.
 You can take a pile of plates kept on top of
each other as a real-life example.
 The plate which we put last is on the top and
since we remove the plate that is at the top,
we can say that the plate that was put last
comes out first.
In order to make manipulations in a stack, there
are certain operations provided to us.
 push() to insert an element into the stack
 pop() to remove an element from the stack
 top() Returns the top element of the stack.
 isEmpty() returns true is stack is empty else
false
 size() returns the size of stack
Push:
Adds an item to the stack. If the stack is full, then it is said to
be an Overflow condition.
Algorithm for push:
begin
if stack is full
return
endif
else
increment top
stack[top] assign value
end else
end procedure
Pop:
Removes an item from the stack. The items are popped in the
reversed order in which they are pushed. If the stack is empty,
then it is said to be an Underflow condition.
Algorithm for pop:
begin
if stack is empty
return
endif
else
store value of stack[top]
decrement top
return value
end else
end procedure
Top:
Returns the top element of the stack.
Algorithm for Top:
begin
return stack[top]
end procedure
isEmpty:
Returns true if the stack is empty, else false.
Algorithm for isEmpty:
begin
if top < 1
return true
else
return false
end procedure
Types of Stacks:
 Register Stack: This type of stack is also a memory element
present in the memory unit and can handle a small
amount of data only. The height of the register stack is
always limited as the size of the register stack is very small
compared to the memory.
 Memory Stack: This type of stack can handle a large
amount of memory data. The height of the memory stack
is flexible as it occupies a large amount of memory data.
 Infix to Postfix /Prefix conversion
 Redo-undo features at many places like editors,
photoshop.
 Forward and backward features in web
browsers
 Used in many algorithms like Tower of
Hanoi, tree traversals, stock span problems,
and histogram problems
There are two ways to implement a stack
 Using array
 Using linked list
Advantages of array implementation:
 Easy to implement.
 Memory is saved as pointers are not involved.
Disadvantages of array implementation:
 It is not dynamic.
 It doesn’t grow and shrink depending on
needs at runtime.
 There are two ways we can implement a
stack:
 Using an array
 Using a linked list
14
 Implementing a stack using an array is fairly
easy.
 The bottom of the stack is at data[0]
 The top of the stack is at data[numItems-1]
 push onto the stack at data[numItems]
 pop off of the stack at data[numItems-1]
15
 We can use a stack to reverse the letters in a
word.
 How?
16
 Read each letter in the word and push it onto
the stack
 When you reach the end of the word, pop
the letters off the stack and print them out.
17
18
 What is a queue?
 A data structure of ordered items such that items
can be inserted only at one end and removed at
the other end.
 Example
 A line at the supermarket
19
 What can we do with a queue?
 Enqueue - Add an item to the queue
 Dequeue - Remove an item from the queue
 These ops are also called insert and getFront
in order to simplify things.
20
 A queue is called a FIFO (First in-First out)
data structure.
 What are some applications of queues?
 Round-robin scheduling in processors
 Input/Output processing
 Queueing of packets for delivery in networks
21
 few more functions are required to make
the above-mentioned queue operation
efficient. These are −
 peek() − Gets the element at the front of
the queue without removing it.
 isfull() − Checks if the queue is full.
 isempty() − Checks if the queue is empty.
Insert Operation
 Queues maintain two data pointers, front and rear. Therefore, its
operations are comparatively difficult to implement than that of
stacks.
 The following steps should be taken to enqueue (insert) data into a
queue −
 Step 1 − Check if the queue is full.
 Step 2 − If the queue is full, produce overflow error and exit.
 Step 3 − If the queue is not full, increment rear pointer to point the
next empty space.
 Step 4 − Add data element to the queue location, where the rear is
pointing.
 Step 5 − return success.
Accessing data from the queue is
a process of two tasks − access
the data where front is pointing
and remove the data after access.
The following steps are taken to
perform dequeue operation −
•Step 1 − Check if the queue is
empty.
•Step 2 − If the queue is empty,
produce underflow error and exit.
•Step 3 − If the queue is not
empty, access the data
where front is pointing.
•Step 4 − Increment front pointer
to point to the next available data
element.
•Step 5 − Return success.
 Just like a stack, we can implementing a
queue in two ways:
 Using an array
 Using a linked list
25
 Using an array to implement a queue is
significantly harder than using an array to
implement a stack. Why?
 Unlike a stack, where we add and remove at the
same end, in a queue we add to one end and
remove from the other.
26
 There are two options for implementing a
queue using an array:
 Option 1:
 Enqueue at data[0] and shift all of the rest of the
items in the array down to make room.
 Dequeue from data[numItems-1]
27
 Option 2
 Enqueue at data[rear+1]
 Dequeue at data[front]
 The rear variable always contains the index of the last
item in the queue.
 The front variable always contains the index of the
first item in the queue.
 When we reach the end of the array, wrap around to
the front again.
28
// option 2 sketch of insert
insert(Object item) {
if(manyItems == 0) front = rear = 0;
else rear = (rear + 1) mod size;
data[rear] = item;
manyItems++;
}
29
// option 2 sketch of getFront
Object getFront() {
answer = data[front];
front = (front + 1) mod size;
manyItems--;
return answer
}
30
 Linked List can be defined as collection of objects
called nodes that are randomly stored in the
memory.
 A node contains two fields i.e. data stored at that
particular address and the pointer which contains
the address of the next node in the memory.
 The last node of the list contains pointer to the
null.
Singly linked list or One way chain
Singly linked list can be defined as the collection of ordered
set of elements. The number of elements may vary according to
need of the program. A node in the singly linked list consist of two
parts: data part and link part. Data part of the node stores actual
information that is to be represented by the node while the link part
of the node stores the address of its immediate successor.
 There are various operations which can be performed on
singly linked list. A list of all such operations is given below.
 Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));
SN Operation Description
1 Insertion at beginning It involves inserting any element at the front of the list. We
just need to a few link adjustments to make the new node
as the head of the list.
2 Insertion at end of the list It involves insertion at the last of the linked list. The new
node can be inserted as the only node in the list or it can
be inserted as the last one. Different logics are
implemented in each scenario.
3 Insertion after specified node It involves insertion after the specified node of the linked
list. We need to skip the desired number of nodes in order
to reach the node after which the new node will be
inserted. .
Insertion
The insertion into a singly linked list can be performed at different positions. Based on
the position of the new node being inserted, the insertion is categorized into the
following categories.
SN Operation Description
1 Deletion at beginning It involves deletion of a node from the beginning of the list. This is the
simplest operation among all. It just need a few adjustments in the
node pointers.
2 Deletion at the end of
the list
It involves deleting the last node of the list. The list can either be empty
or full. Different logic is implemented for the different scenarios.
3 Deletion after specified
node
It involves deleting the node after the specified node in the list. we
need to skip the desired number of nodes to reach the node after
which the node will be deleted. This requires traversing through the list.
4 Traversing In traversing, we simply visit each node of the list at least once in order
to perform some specific operation on it, for example, printing data part
of each node present in the list.
5 Searching In searching, we match each element of the list with the given element.
If the element is found on any of the location then location of that
element is returned otherwise null is returned. .
The Deletion of a node from a singly linked list can be performed at different
positions. Based on the position of the node being deleted, the operation is
categorized into the following categories.
Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a
pointer to the previous as well as the next node in the sequence.
Therefore, in a doubly linked list, a node consists of three parts: node data,
pointer to the next node in sequence (next pointer) , pointer to the previous
node (previous pointer). A sample node in a doubly linked list is shown in
the figure.
A doubly linked list containing three nodes having numbers from 1
to 3 in their data part, is shown in the following image.
In C, structure of a node in
doubly linked list can be given
as :
struct node
{
struct node *prev;
int data;
struct node *next;
}
Node Creation
struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head;
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at
beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified
node
Adding the node into the linked list after the
specified node.
4 Deletion at beginning Removing the node from beginning of the list
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node
having given data
Removing the node which is present just after
the node containing the given data.
7 Searching Comparing each node data with the item to be
searched and return the location of the item in
the list if the item found else return null.
8 Traversing Visiting each node of the list at least once in
order to perform some specific operation like
searching, sorting, display, etc.
 Root − Node at the top of the tree is called root.
 Parent − Any node except root node has one edge upward to a node
called parent.
 Child − Node below a given node connected by its edge downward is
called its child node.
 Sibling – Child of same node are called siblings
 Leaf − Node which does not have any child node is called leaf node.
 Sub tree − Sub tree represents descendants of a node.
 Levels − Level of a node represents the generation of a node. If root
node is at level 0, then its next child node is at level 1, its grandchild is
at level 2 and so on.
 keys − Key represents a value of a node based on which a search
operation is to be carried out for a node.
 Degreeof anode:
• Thedegreeof anode isthe number of children of that node
 Degreeof aTree:
• Thedegree of atree isthe maximumdegreeof nodesin agiventree
 Path:
• It isthe sequence of consecutive edgesfrom sourcenode to destination node.
 Heightof anode:
• Theheight of anode isthe maxpath length form that node to aleaf node.
 Heightof atree:
• Theheight of atree isthe height ofthe root
 Depthof atree:
• Depth of atree isthe maxlevelof anyleaf in the tree
Non-linear data structure
Combinesadvantagesof anordered array
Searchingasfast asin ordered array
Insertion and deletion asfast asin linked list
Simpleand fast
Directory structure of a file store
Structure of an arithmetic expressions
Used in almost every 3D video game to
determine what objects need to be
rendered.
Used in almost every high-bandwidth
router for storing router-tables.
used in compression algorithms, such as
those used by the .jpeg and .mp3 file-
formats.
A binary tree, is a tree in which no node can
have more than two children.
Consider a binary tree T, here ‘A’ is the root
node of the binary tree T.
‘B’ is the left child of ‘A’ and ‘C’ is the right
child of ‘A’
• i.e A is a father of B and C.
• The node B and C are called siblings.
Nodes D,H,I,F,J are leaf node

Más contenido relacionado

Similar a unit 5 stack & queue.ppt

Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueRai University
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddumaneesh boddu
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queueSunipa Bera
 
data structures and algorithms Unit 1
data structures and algorithms Unit 1data structures and algorithms Unit 1
data structures and algorithms Unit 1infanciaj
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)SURBHI SAROHA
 
chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdfstudy material
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptxSonaPathak4
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3infanciaj
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)Durga Devi
 

Similar a unit 5 stack & queue.ppt (20)

Mca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queueMca ii dfs u-3 linklist,stack,queue
Mca ii dfs u-3 linklist,stack,queue
 
Data Structures by Maneesh Boddu
Data Structures by Maneesh BodduData Structures by Maneesh Boddu
Data Structures by Maneesh Boddu
 
Stack and queue
Stack and queueStack and queue
Stack and queue
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
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
 
Ds stack & queue
Ds   stack & queueDs   stack & queue
Ds stack & queue
 
data structures and algorithms Unit 1
data structures and algorithms Unit 1data structures and algorithms Unit 1
data structures and algorithms Unit 1
 
Data structures
Data structuresData structures
Data structures
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
Data Structures(Part 1)
Data Structures(Part 1)Data Structures(Part 1)
Data Structures(Part 1)
 
Data Structure
Data StructureData Structure
Data Structure
 
DSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdfDSA Lab Manual C Scheme.pdf
DSA Lab Manual C Scheme.pdf
 
chapter-4-data-structure.pdf
chapter-4-data-structure.pdfchapter-4-data-structure.pdf
chapter-4-data-structure.pdf
 
1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf1.1 ADS data-structure.pdf
1.1 ADS data-structure.pdf
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Linked List
Linked ListLinked List
Linked List
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
Unit ii(dsc++)
Unit ii(dsc++)Unit ii(dsc++)
Unit ii(dsc++)
 

Más de SeethaDinesh

Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptxSeethaDinesh
 
Generations of Computers.ppt
Generations of Computers.pptGenerations of Computers.ppt
Generations of Computers.pptSeethaDinesh
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.pptSeethaDinesh
 
Arrays in JAVA.ppt
Arrays in JAVA.pptArrays in JAVA.ppt
Arrays in JAVA.pptSeethaDinesh
 
PROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxPROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxSeethaDinesh
 
Cloud Computing Basics.pptx
Cloud Computing Basics.pptxCloud Computing Basics.pptx
Cloud Computing Basics.pptxSeethaDinesh
 
Greedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptGreedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptSeethaDinesh
 
Structure and Union.ppt
Structure and Union.pptStructure and Union.ppt
Structure and Union.pptSeethaDinesh
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docxSeethaDinesh
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptSeethaDinesh
 
NME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxNME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxSeethaDinesh
 
NME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfNME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfSeethaDinesh
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdfSeethaDinesh
 

Más de SeethaDinesh (20)

Input Devices.pptx
Input Devices.pptxInput Devices.pptx
Input Devices.pptx
 
Generations of Computers.ppt
Generations of Computers.pptGenerations of Computers.ppt
Generations of Computers.ppt
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Arrays in JAVA.ppt
Arrays in JAVA.pptArrays in JAVA.ppt
Arrays in JAVA.ppt
 
PROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptxPROGRAMMING IN JAVA unit 1.pptx
PROGRAMMING IN JAVA unit 1.pptx
 
Cloud Computing Basics.pptx
Cloud Computing Basics.pptxCloud Computing Basics.pptx
Cloud Computing Basics.pptx
 
Greedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.pptGreedy_Backtracking graph coloring.ppt
Greedy_Backtracking graph coloring.ppt
 
Structure and Union.ppt
Structure and Union.pptStructure and Union.ppt
Structure and Union.ppt
 
Shortest Path Problem.docx
Shortest Path Problem.docxShortest Path Problem.docx
Shortest Path Problem.docx
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
NME UNIT II.ppt
NME UNIT II.pptNME UNIT II.ppt
NME UNIT II.ppt
 
Basics of C.ppt
Basics of C.pptBasics of C.ppt
Basics of C.ppt
 
chapter 1.pptx
chapter 1.pptxchapter 1.pptx
chapter 1.pptx
 
DW unit 3.pptx
DW unit 3.pptxDW unit 3.pptx
DW unit 3.pptx
 
DW unit 2.pdf
DW unit 2.pdfDW unit 2.pdf
DW unit 2.pdf
 
DW Unit 1.pdf
DW Unit 1.pdfDW Unit 1.pdf
DW Unit 1.pdf
 
NME WPI UNIt 3.pptx
NME WPI UNIt 3.pptxNME WPI UNIt 3.pptx
NME WPI UNIt 3.pptx
 
NME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdfNME UNIT I & II MATERIAL.pdf
NME UNIT I & II MATERIAL.pdf
 
graphtraversals.pdf
graphtraversals.pdfgraphtraversals.pdf
graphtraversals.pdf
 
DSA-Unit-2.pptx
DSA-Unit-2.pptxDSA-Unit-2.pptx
DSA-Unit-2.pptx
 

Último

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Último (20)

social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
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
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
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
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

unit 5 stack & queue.ppt

  • 1. Ms. D. Seethalakshmi Assistant Professor Bon Secours College for Women Thanjavur 1
  • 2.  Stack of Books 2
  • 3.  What is a Stack?  A stack is a data structure of ordered items such that items can be inserted and removed only at one end. 3
  • 4.  This strategy states that the element that is inserted last will come out first.  You can take a pile of plates kept on top of each other as a real-life example.  The plate which we put last is on the top and since we remove the plate that is at the top, we can say that the plate that was put last comes out first.
  • 5. In order to make manipulations in a stack, there are certain operations provided to us.  push() to insert an element into the stack  pop() to remove an element from the stack  top() Returns the top element of the stack.  isEmpty() returns true is stack is empty else false  size() returns the size of stack
  • 6.
  • 7. Push: Adds an item to the stack. If the stack is full, then it is said to be an Overflow condition. Algorithm for push: begin if stack is full return endif else increment top stack[top] assign value end else end procedure
  • 8. Pop: Removes an item from the stack. The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition. Algorithm for pop: begin if stack is empty return endif else store value of stack[top] decrement top return value end else end procedure
  • 9. Top: Returns the top element of the stack. Algorithm for Top: begin return stack[top] end procedure isEmpty: Returns true if the stack is empty, else false. Algorithm for isEmpty: begin if top < 1 return true else return false end procedure
  • 10. Types of Stacks:  Register Stack: This type of stack is also a memory element present in the memory unit and can handle a small amount of data only. The height of the register stack is always limited as the size of the register stack is very small compared to the memory.  Memory Stack: This type of stack can handle a large amount of memory data. The height of the memory stack is flexible as it occupies a large amount of memory data.
  • 11.  Infix to Postfix /Prefix conversion  Redo-undo features at many places like editors, photoshop.  Forward and backward features in web browsers  Used in many algorithms like Tower of Hanoi, tree traversals, stock span problems, and histogram problems
  • 12. There are two ways to implement a stack  Using array  Using linked list
  • 13. Advantages of array implementation:  Easy to implement.  Memory is saved as pointers are not involved. Disadvantages of array implementation:  It is not dynamic.  It doesn’t grow and shrink depending on needs at runtime.
  • 14.  There are two ways we can implement a stack:  Using an array  Using a linked list 14
  • 15.  Implementing a stack using an array is fairly easy.  The bottom of the stack is at data[0]  The top of the stack is at data[numItems-1]  push onto the stack at data[numItems]  pop off of the stack at data[numItems-1] 15
  • 16.  We can use a stack to reverse the letters in a word.  How? 16
  • 17.  Read each letter in the word and push it onto the stack  When you reach the end of the word, pop the letters off the stack and print them out. 17
  • 18. 18
  • 19.  What is a queue?  A data structure of ordered items such that items can be inserted only at one end and removed at the other end.  Example  A line at the supermarket 19
  • 20.  What can we do with a queue?  Enqueue - Add an item to the queue  Dequeue - Remove an item from the queue  These ops are also called insert and getFront in order to simplify things. 20
  • 21.  A queue is called a FIFO (First in-First out) data structure.  What are some applications of queues?  Round-robin scheduling in processors  Input/Output processing  Queueing of packets for delivery in networks 21
  • 22.  few more functions are required to make the above-mentioned queue operation efficient. These are −  peek() − Gets the element at the front of the queue without removing it.  isfull() − Checks if the queue is full.  isempty() − Checks if the queue is empty.
  • 23. Insert Operation  Queues maintain two data pointers, front and rear. Therefore, its operations are comparatively difficult to implement than that of stacks.  The following steps should be taken to enqueue (insert) data into a queue −  Step 1 − Check if the queue is full.  Step 2 − If the queue is full, produce overflow error and exit.  Step 3 − If the queue is not full, increment rear pointer to point the next empty space.  Step 4 − Add data element to the queue location, where the rear is pointing.  Step 5 − return success.
  • 24. Accessing data from the queue is a process of two tasks − access the data where front is pointing and remove the data after access. The following steps are taken to perform dequeue operation − •Step 1 − Check if the queue is empty. •Step 2 − If the queue is empty, produce underflow error and exit. •Step 3 − If the queue is not empty, access the data where front is pointing. •Step 4 − Increment front pointer to point to the next available data element. •Step 5 − Return success.
  • 25.  Just like a stack, we can implementing a queue in two ways:  Using an array  Using a linked list 25
  • 26.  Using an array to implement a queue is significantly harder than using an array to implement a stack. Why?  Unlike a stack, where we add and remove at the same end, in a queue we add to one end and remove from the other. 26
  • 27.  There are two options for implementing a queue using an array:  Option 1:  Enqueue at data[0] and shift all of the rest of the items in the array down to make room.  Dequeue from data[numItems-1] 27
  • 28.  Option 2  Enqueue at data[rear+1]  Dequeue at data[front]  The rear variable always contains the index of the last item in the queue.  The front variable always contains the index of the first item in the queue.  When we reach the end of the array, wrap around to the front again. 28
  • 29. // option 2 sketch of insert insert(Object item) { if(manyItems == 0) front = rear = 0; else rear = (rear + 1) mod size; data[rear] = item; manyItems++; } 29
  • 30. // option 2 sketch of getFront Object getFront() { answer = data[front]; front = (front + 1) mod size; manyItems--; return answer } 30
  • 31.  Linked List can be defined as collection of objects called nodes that are randomly stored in the memory.  A node contains two fields i.e. data stored at that particular address and the pointer which contains the address of the next node in the memory.  The last node of the list contains pointer to the null.
  • 32. Singly linked list or One way chain Singly linked list can be defined as the collection of ordered set of elements. The number of elements may vary according to need of the program. A node in the singly linked list consist of two parts: data part and link part. Data part of the node stores actual information that is to be represented by the node while the link part of the node stores the address of its immediate successor.
  • 33.  There are various operations which can be performed on singly linked list. A list of all such operations is given below.  Node Creation 1. struct node 2. { 3. int data; 4. struct node *next; 5. }; 6. struct node *head, *ptr; 7. ptr = (struct node *)malloc(sizeof(struct node *));
  • 34. SN Operation Description 1 Insertion at beginning It involves inserting any element at the front of the list. We just need to a few link adjustments to make the new node as the head of the list. 2 Insertion at end of the list It involves insertion at the last of the linked list. The new node can be inserted as the only node in the list or it can be inserted as the last one. Different logics are implemented in each scenario. 3 Insertion after specified node It involves insertion after the specified node of the linked list. We need to skip the desired number of nodes in order to reach the node after which the new node will be inserted. . Insertion The insertion into a singly linked list can be performed at different positions. Based on the position of the new node being inserted, the insertion is categorized into the following categories.
  • 35. SN Operation Description 1 Deletion at beginning It involves deletion of a node from the beginning of the list. This is the simplest operation among all. It just need a few adjustments in the node pointers. 2 Deletion at the end of the list It involves deleting the last node of the list. The list can either be empty or full. Different logic is implemented for the different scenarios. 3 Deletion after specified node It involves deleting the node after the specified node in the list. we need to skip the desired number of nodes to reach the node after which the node will be deleted. This requires traversing through the list. 4 Traversing In traversing, we simply visit each node of the list at least once in order to perform some specific operation on it, for example, printing data part of each node present in the list. 5 Searching In searching, we match each element of the list with the given element. If the element is found on any of the location then location of that element is returned otherwise null is returned. . The Deletion of a node from a singly linked list can be performed at different positions. Based on the position of the node being deleted, the operation is categorized into the following categories.
  • 36. Doubly linked list Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts: node data, pointer to the next node in sequence (next pointer) , pointer to the previous node (previous pointer). A sample node in a doubly linked list is shown in the figure.
  • 37. A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the following image. In C, structure of a node in doubly linked list can be given as : struct node { struct node *prev; int data; struct node *next; }
  • 38.
  • 39. Node Creation struct node { struct node *prev; int data; struct node *next; }; struct node *head;
  • 40. SN Operation Description 1 Insertion at beginning Adding the node into the linked list at beginning. 2 Insertion at end Adding the node into the linked list to the end. 3 Insertion after specified node Adding the node into the linked list after the specified node. 4 Deletion at beginning Removing the node from beginning of the list 5 Deletion at the end Removing the node from end of the list. 6 Deletion of the node having given data Removing the node which is present just after the node containing the given data. 7 Searching Comparing each node data with the item to be searched and return the location of the item in the list if the item found else return null. 8 Traversing Visiting each node of the list at least once in order to perform some specific operation like searching, sorting, display, etc.
  • 41.
  • 42.
  • 43.
  • 44.  Root − Node at the top of the tree is called root.  Parent − Any node except root node has one edge upward to a node called parent.  Child − Node below a given node connected by its edge downward is called its child node.  Sibling – Child of same node are called siblings  Leaf − Node which does not have any child node is called leaf node.  Sub tree − Sub tree represents descendants of a node.  Levels − Level of a node represents the generation of a node. If root node is at level 0, then its next child node is at level 1, its grandchild is at level 2 and so on.  keys − Key represents a value of a node based on which a search operation is to be carried out for a node.
  • 45.  Degreeof anode: • Thedegreeof anode isthe number of children of that node  Degreeof aTree: • Thedegree of atree isthe maximumdegreeof nodesin agiventree  Path: • It isthe sequence of consecutive edgesfrom sourcenode to destination node.  Heightof anode: • Theheight of anode isthe maxpath length form that node to aleaf node.  Heightof atree: • Theheight of atree isthe height ofthe root  Depthof atree: • Depth of atree isthe maxlevelof anyleaf in the tree
  • 46.
  • 47. Non-linear data structure Combinesadvantagesof anordered array Searchingasfast asin ordered array Insertion and deletion asfast asin linked list Simpleand fast
  • 48. Directory structure of a file store Structure of an arithmetic expressions Used in almost every 3D video game to determine what objects need to be rendered. Used in almost every high-bandwidth router for storing router-tables. used in compression algorithms, such as those used by the .jpeg and .mp3 file- formats.
  • 49. A binary tree, is a tree in which no node can have more than two children. Consider a binary tree T, here ‘A’ is the root node of the binary tree T. ‘B’ is the left child of ‘A’ and ‘C’ is the right child of ‘A’ • i.e A is a father of B and C. • The node B and C are called siblings. Nodes D,H,I,F,J are leaf node