SlideShare a Scribd company logo
Linked Stack and Linked Queue
Chapter 7
Outline
● Introduction
– Linked Stacks
– Linked Queues
● Operations on Linked Stacks and Linked Queues
– Linked Stack Operations
– Linked Queue Operations
– Algorithms for Push/Pop operations on a Linked Stack
– Algorithms for Insert/Delete operations in a Linked Queue
● Dynamic memory management and Linked Stacks
● Implementation of Linked Representations
● Applications
– Balancing Symbols
– Polynomial Representation
Introduction
● A linked stack is a linear list of elements commonly
implemented as a singly linked list whose start pointer
performs the role of the top pointer of a stack
● A linked queue is also a linear list of elements
commonly implemented as a singly linked list but with two
pointers viz., FRONT and REAR. The start pointer of the
singly linked list plays the role of FRONT while the pointer
to the last node is set to play the role of REAR.
Implementing Stack and Queue Using Linked List
6
1
2
3
4
5
A
B
E
D
G
F
Top = 6
1 2 3 4 5 6
B C
Front=1 Rear = 3
Stack[1:6] Queue[1:6]
B C G H
stack
Top
B C G H
queue
front rear
Operations of Linked Stack and
Linked Queue
Operations on Linked Stack
● Push
– GETNODE(X)
– LINK(X) = Top
– Top = X
B C G H
stack
Top
C
X
Algorithm: Push item ITEM into a linked stack S with top
pointer TOP
procedure PUSH_LINKSTACK (TOP, ITEM)
/* Insert ITEM into stack */
Call GETNODE(X)
DATA(X) = ITEM /*frame node for ITEM */
LINK(X) = TOP /* insert node X into stack */
TOP = X /* reset TOP pointer */
end PUSH_LINKSTACK.
● Pop
– Temp = Top
– Item = DATA(Top)
– Top = LINK(TOP)
– RETURN(Temp)
B C G H
stack
Top
Temp
Algorithm: Pop from a linked stack S and output the
element through ITEM
procedure POP_LINKSTACK(TOP, ITEM)
/* pop element from stack and set ITEM to
the element */
if (TOP = 0) then call LINKSTACK_EMPTY
/* check if linked stack is empty */
else { TEMP = TOP
ITEM = DATA(TOP)
TOP = LINK(TOP)
}
call RETURN(TEMP) ;
end POP_LINKSTACK.
● Enqueue
– LINK(rear) = X
– Rear = X
B C G H
queue
front
rear
A
X
B C G H
queue
front rear
Algorithm: Enqueue an ITEM into a linked list queue Q
procedure INSERT_LINKQUEUE(FRONT,REAR,ITEM)
Call GETNODE(X);
DATA(X)= ITEM;
LINK(X)= NIL; /* Node with ITEM is ready to be
inserted into Q */
if (Queue = 0) then
•Queue = FRONT = REAR = X;
/* If Q is empty then ITEM is the
first element in the queue Q
else {LINK(REAR) = X;
REAR = X
}
end INSERT_LINKQUEUE.
● Dequeue
– Temp = front
– front = Link(front)
– Item = DATA(Temp)
– RETURN(Temp)
B C G H
queue
front rear
Temp
Algorithm: Dequeue an element from the linked queue Q
procedure DELETE_LINKQUEUE (FRONT,ITEM)
if (FRONT = 0) then call LINKQUEUE_EMPTY;
/* Test condition to avoid deletion in an empty
queue */
else {TEMP = FRONT;
ITEM = DATA (TEMP);
FRONT = LINK (TEMP);
}
call RETURN (TEMP); /* return the node TEMP to
the free pool */
end DELETE_LINKQUEUE.
Implementing Linked list Queue and stack
● Balancing Symbols
● Polynomial representation
Balancing symbols
Algorithm: To check for the balancing of parentheses in a string
procedure BALANCE_ EXPR(E)
/*E is the expression padded with a $ to indicateend of input*/
clear stack;
while not end_of_string(E)
read character; /* read a character from string E*/
if character == '{' //is an open symbol
then push character in to stack;
if character = '}' //is a close symbol
then
if stack is empty then ERROR ()
else {pop the stack;
if character != popped character
then ERROR();
}
endwhile
if stack not empty then ERROR();
end BALANCE_EXPR.
Polynomial representation
COEFF EXP LINK
9 6 3 2 4 0
-2 4
Node structure for polynomial
Linked list representation:
9x6
-2x4
+ 3x2
+ 4.
polynomial
Front reat

More Related Content

What's hot

Linklist
LinklistLinklist
linked list
linked listlinked list
linked list
Shaista Qadir
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
Ahsan Mansiv
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
Self-Employed
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
DrkhanchanaR
 
Linked list
Linked listLinked list
Linked list
Trupti Agrawal
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
Abdullah Al-hazmy
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Binary tree
Binary treeBinary tree
Binary tree
Rajendran
 
Stack application
Stack applicationStack application
Stack application
Student
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
Hossain Md Shakhawat
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
ManishPrajapati78
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Linked List
Linked ListLinked List
Linked List
Ashim Lamichhane
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
Reazul Islam
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
eShikshak
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 

What's hot (20)

Linklist
LinklistLinklist
Linklist
 
linked list
linked listlinked list
linked list
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Stack and its applications
Stack and its applicationsStack and its applications
Stack and its applications
 
Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)Queue AS an ADT (Abstract Data Type)
Queue AS an ADT (Abstract Data Type)
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Unit 5 internal sorting & files
Unit 5  internal sorting & filesUnit 5  internal sorting & files
Unit 5 internal sorting & files
 
Linked list
Linked listLinked list
Linked list
 
Data Structures- Part7 linked lists
Data Structures- Part7 linked listsData Structures- Part7 linked lists
Data Structures- Part7 linked lists
 
Red Black Tree Insertion & Deletion
Red Black Tree Insertion & DeletionRed Black Tree Insertion & Deletion
Red Black Tree Insertion & Deletion
 
Binary tree
Binary treeBinary tree
Binary tree
 
Stack application
Stack applicationStack application
Stack application
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Doubly Linked List
Doubly Linked ListDoubly Linked List
Doubly Linked List
 
Linked List
Linked ListLinked List
Linked List
 
Data Structures with C Linked List
Data Structures with C Linked ListData Structures with C Linked List
Data Structures with C Linked List
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
 

Similar to Linked stack-and-linked-queue

Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
Rai University
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
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
Rai University
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
GirT2
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
SonaPathak4
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
Toseef Hasan
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
RAtna29
 
Data structures
Data structuresData structures
Data structures
Sneha Chopra
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
Pulkitmodi1998
 
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
Balwant Gorad
 
Stacks in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
Mandeep Singh
 
Queue
QueueQueue
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
tech4us
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
kulachihansraj
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
VeerannaKotagi1
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
Rajkiran Nadar
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
ssuserff72e4
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
MuhammadUmerIhtisham
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
MeghaKulkarni27
 

Similar to Linked stack-and-linked-queue (20)

Bsc cs ii dfs u-2 linklist,stack,queue
Bsc cs ii  dfs u-2 linklist,stack,queueBsc cs ii  dfs u-2 linklist,stack,queue
Bsc cs ii dfs u-2 linklist,stack,queue
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Bca ii dfs u-2 linklist,stack,queue
Bca ii  dfs u-2 linklist,stack,queueBca ii  dfs u-2 linklist,stack,queue
Bca ii dfs u-2 linklist,stack,queue
 
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
 
Chapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdfChapter 5 Stack and Queue.pdf
Chapter 5 Stack and Queue.pdf
 
Module 2 ppt.pptx
Module 2 ppt.pptxModule 2 ppt.pptx
Module 2 ppt.pptx
 
Queues presentation
Queues presentationQueues presentation
Queues presentation
 
Stack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparationStack and Queue.pptx university exam preparation
Stack and Queue.pptx university exam preparation
 
Data structures
Data structuresData structures
Data structures
 
Difference between stack and queue
Difference between stack and queueDifference between stack and queue
Difference between stack and queue
 
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 in DATA STRUCTURE
Stacks in DATA STRUCTUREStacks in DATA STRUCTURE
Stacks in DATA STRUCTURE
 
Queue
QueueQueue
Queue
 
Stacks & Queues
Stacks & QueuesStacks & Queues
Stacks & Queues
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
DS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptxDS UNIT2QUEUES.pptx
DS UNIT2QUEUES.pptx
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
chapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdfchapter10-queue-161018120329.pdf
chapter10-queue-161018120329.pdf
 
LEC4-DS ALGO.pdf
LEC4-DS  ALGO.pdfLEC4-DS  ALGO.pdf
LEC4-DS ALGO.pdf
 
queue_final.pptx
queue_final.pptxqueue_final.pptx
queue_final.pptx
 

Recently uploaded

5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
ihlasbinance2003
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
Aditya Rajan Patra
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
MIGUELANGEL966976
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
KrishnaveniKrishnara1
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 

Recently uploaded (20)

5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
5214-1693458878915-Unit 6 2023 to 2024 academic year assignment (AutoRecovere...
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Recycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part IIRecycled Concrete Aggregate in Construction Part II
Recycled Concrete Aggregate in Construction Part II
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdfBPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
BPV-GUI-01-Guide-for-ASME-Review-Teams-(General)-10-10-2023.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.pptUnit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
Unit-III-ELECTROCHEMICAL STORAGE DEVICES.ppt
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 

Linked stack-and-linked-queue

  • 1. Linked Stack and Linked Queue Chapter 7
  • 2. Outline ● Introduction – Linked Stacks – Linked Queues ● Operations on Linked Stacks and Linked Queues – Linked Stack Operations – Linked Queue Operations – Algorithms for Push/Pop operations on a Linked Stack – Algorithms for Insert/Delete operations in a Linked Queue ● Dynamic memory management and Linked Stacks ● Implementation of Linked Representations ● Applications – Balancing Symbols – Polynomial Representation
  • 3. Introduction ● A linked stack is a linear list of elements commonly implemented as a singly linked list whose start pointer performs the role of the top pointer of a stack ● A linked queue is also a linear list of elements commonly implemented as a singly linked list but with two pointers viz., FRONT and REAR. The start pointer of the singly linked list plays the role of FRONT while the pointer to the last node is set to play the role of REAR.
  • 4. Implementing Stack and Queue Using Linked List 6 1 2 3 4 5 A B E D G F Top = 6 1 2 3 4 5 6 B C Front=1 Rear = 3 Stack[1:6] Queue[1:6] B C G H stack Top B C G H queue front rear
  • 5. Operations of Linked Stack and Linked Queue
  • 6. Operations on Linked Stack ● Push – GETNODE(X) – LINK(X) = Top – Top = X B C G H stack Top C X
  • 7. Algorithm: Push item ITEM into a linked stack S with top pointer TOP procedure PUSH_LINKSTACK (TOP, ITEM) /* Insert ITEM into stack */ Call GETNODE(X) DATA(X) = ITEM /*frame node for ITEM */ LINK(X) = TOP /* insert node X into stack */ TOP = X /* reset TOP pointer */ end PUSH_LINKSTACK.
  • 8. ● Pop – Temp = Top – Item = DATA(Top) – Top = LINK(TOP) – RETURN(Temp) B C G H stack Top Temp
  • 9. Algorithm: Pop from a linked stack S and output the element through ITEM procedure POP_LINKSTACK(TOP, ITEM) /* pop element from stack and set ITEM to the element */ if (TOP = 0) then call LINKSTACK_EMPTY /* check if linked stack is empty */ else { TEMP = TOP ITEM = DATA(TOP) TOP = LINK(TOP) } call RETURN(TEMP) ; end POP_LINKSTACK.
  • 10. ● Enqueue – LINK(rear) = X – Rear = X B C G H queue front rear A X B C G H queue front rear
  • 11. Algorithm: Enqueue an ITEM into a linked list queue Q procedure INSERT_LINKQUEUE(FRONT,REAR,ITEM) Call GETNODE(X); DATA(X)= ITEM; LINK(X)= NIL; /* Node with ITEM is ready to be inserted into Q */ if (Queue = 0) then •Queue = FRONT = REAR = X; /* If Q is empty then ITEM is the first element in the queue Q else {LINK(REAR) = X; REAR = X } end INSERT_LINKQUEUE.
  • 12. ● Dequeue – Temp = front – front = Link(front) – Item = DATA(Temp) – RETURN(Temp) B C G H queue front rear Temp
  • 13. Algorithm: Dequeue an element from the linked queue Q procedure DELETE_LINKQUEUE (FRONT,ITEM) if (FRONT = 0) then call LINKQUEUE_EMPTY; /* Test condition to avoid deletion in an empty queue */ else {TEMP = FRONT; ITEM = DATA (TEMP); FRONT = LINK (TEMP); } call RETURN (TEMP); /* return the node TEMP to the free pool */ end DELETE_LINKQUEUE.
  • 14. Implementing Linked list Queue and stack ● Balancing Symbols ● Polynomial representation
  • 15. Balancing symbols Algorithm: To check for the balancing of parentheses in a string procedure BALANCE_ EXPR(E) /*E is the expression padded with a $ to indicateend of input*/ clear stack; while not end_of_string(E) read character; /* read a character from string E*/ if character == '{' //is an open symbol then push character in to stack; if character = '}' //is a close symbol then if stack is empty then ERROR () else {pop the stack; if character != popped character then ERROR(); } endwhile if stack not empty then ERROR(); end BALANCE_EXPR.
  • 16. Polynomial representation COEFF EXP LINK 9 6 3 2 4 0 -2 4 Node structure for polynomial Linked list representation: 9x6 -2x4 + 3x2 + 4. polynomial Front reat