SlideShare a Scribd company logo
5
Most read
6
Most read
11
Most read
Stack & Queue
Using Linked List
INSTRUCTOR : MUHAMMAD HAMMAD WASEEM
EMAIL: m.hammad.wasim@gmail.com
Stack Using Linked List
 We can avoid the size limitation of a stack implemented with an
array by using a linked list to hold the stack elements.
 As with array, however, we need to decide where to insert elements
in the list and where to delete them so that push and pop will run
the fastest.
2
Stack Using Linked List
 For a singly-linked list, insert at start or end takes constant time using
the head and current pointers respectively.
 Removing an element at the start is constant time but removal at
the end required traversing the list to the node one before the last.
 Make sense to place stack elements at the start of the list because
insert and removal are constant time.
3
Stack Using Linked List
 No need for the current pointer; head is enough.
4
top
2
5
7
1
1 7 5 2
head
Stack Operation: List
int pop()
{
int x = head->info;
head = head->link;
return x;
}
5
top
2
5
7
1 7 5 2
head
Stack Operation: List
void push(int x)
{
ListNode *newNode;
newNode = new ListNode;
newNode->info = x;
newNode->link=head;
head = newNode;
}
6
top
2
5
7
9
7 5 2
head
push(9)
9
newNode
Stack Operation: List
int top()
{
return head->info;
}
int IsEmpty()
{
return ( head == NULL );
}
 All operations take constant time.
7
Stack: Array or List
 Since both implementations support stack
operations in constant time, any reason to choose
one over the other?
 Allocating and de-allocating memory for list nodes
does take more time than pre-allocated array.
 List uses only as much memory as required by the
nodes; array requires allocation ahead of time.
 List pointers (head, next) require extra memory.
 Array has an upper limit; List is limited by dynamic
memory allocation.
8
Queue Using Linked List
 Insert an element at the start and removal at the end but it required
extra overhead of traversing the list to the node one before the last.
9
Queue Using Linked List
 No need for the current pointer; head is enough.
10
rear
2571 1 7 5 2
rear front
front
Queue Operation: enqueue
void enqueue(int x)
{
ListNode *newNode;
newNode = new ListNode;
newNode->info = x;
newNode->link=head;
head = newNode;
}
11
Queue Operation: dequeue
Void dequeue()
{
nodePtr = head;
while (nodePtr->link != NULL)
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
previousNode->link = nodePtr->link;
delete nodePtr;
}
12

More Related Content

What's hot (20)

PDF
sparse matrix in data structure
MAHALAKSHMI P
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PPT
stack presentation
Shivalik college of engineering
 
PPTX
Linear search-and-binary-search
International Islamic University
 
PDF
Python set
Mohammed Sikander
 
PPTX
single linked list
Sathasivam Rangasamy
 
PDF
Expression trees
Salman Vadsarya
 
PPTX
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
PPTX
Tree - Data Structure
Ashim Lamichhane
 
PPTX
Evaluation of postfix expression
Akhil Ahuja
 
PPT
Linked lists
SARITHA REDDY
 
PPT
Linked List
CHANDAN KUMAR
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
Queues
Ashim Lamichhane
 
PPTX
linked list in data structure
shameen khan
 
PPT
Red black tree
Rajendran
 
PDF
Applications of stack
eShikshak
 
PDF
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPT
Binary Search
kunj desai
 
sparse matrix in data structure
MAHALAKSHMI P
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Linear search-and-binary-search
International Islamic University
 
Python set
Mohammed Sikander
 
single linked list
Sathasivam Rangasamy
 
Expression trees
Salman Vadsarya
 
linked list in Data Structure, Simple and Easy Tutorial
Afzal Badshah
 
Tree - Data Structure
Ashim Lamichhane
 
Evaluation of postfix expression
Akhil Ahuja
 
Linked lists
SARITHA REDDY
 
Linked List
CHANDAN KUMAR
 
Stack using Linked List
Sayantan Sur
 
linked list in data structure
shameen khan
 
Red black tree
Rajendran
 
Applications of stack
eShikshak
 
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
linear search and binary search
Zia Ush Shamszaman
 
Binary Search
kunj desai
 

Viewers also liked (20)

PPTX
Linked stacks and queues
Ramzi Alqrainy
 
PPTX
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
PDF
Stacks,queues,linked-list
pinakspatel
 
PPTX
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPSX
Stacks Implementation and Examples
greatqadirgee4u
 
PPTX
Data structure and its types
Navtar Sidhu Brar
 
PPTX
Linked list
akshat360
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPTX
Queue Implementation Using Array & Linked List
PTCL
 
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
PPTX
Doubly Linked List
Ninad Mankar
 
PDF
Doubly Link List
Kashif Memon
 
PPTX
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
PPT
Stack, queue and hashing
Dumindu Pahalawatta
 
PPTX
6.queue
Chandan Singh
 
PPTX
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Linked stacks and queues
Ramzi Alqrainy
 
[OOP - Lec 20,21] Inheritance
Muhammad Hammad Waseem
 
[OOP - Lec 01] Introduction to OOP
Muhammad Hammad Waseem
 
Stacks,queues,linked-list
pinakspatel
 
Data Structures - Lecture 3 [Arrays]
Muhammad Hammad Waseem
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Stacks Implementation and Examples
greatqadirgee4u
 
Data structure and its types
Navtar Sidhu Brar
 
Linked list
akshat360
 
Data structures and algorithms
Julie Iskander
 
Queue Implementation Using Array & Linked List
PTCL
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
[OOP - Lec 09,10,11] Class Members & their Accessing
Muhammad Hammad Waseem
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
Doubly Linked List
Ninad Mankar
 
Doubly Link List
Kashif Memon
 
[OOP - Lec 18] Static Data Member
Muhammad Hammad Waseem
 
Stack, queue and hashing
Dumindu Pahalawatta
 
6.queue
Chandan Singh
 
Data Structures - Lecture 7 [Linked List]
Muhammad Hammad Waseem
 
Ad

Similar to Data Structures - Lecture 9 [Stack & Queue using Linked List] (20)

PPT
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
PDF
DOC-20250201-WA0019..pdf37853675543337784326863467
alishafgh391
 
PPTX
Abscddnddmdkwkkstack implementation.pptx
zainshahid3040
 
PPTX
Data Structure
HarshGupta663
 
PPTX
Stack and Queue
Apurbo Datta
 
PPTX
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
PDF
LinkedList1LinkedList1LinkedList1111.pdf
timoemin50
 
PDF
computer notes - Stack
ecomputernotes
 
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
PPTX
module 3-.pptx
kumarkaushal17
 
PPTX
linkedlistforslideshare-210123143943.pptx
shesnasuneer
 
PPTX
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
PDF
Stacks and Queues with Linked List.pdf
GKalyani4
 
PPTX
Linked list
Arbind Mandal
 
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
PPT
Advanced s and s algorithm.ppt
LegesseSamuel
 
PPTX
Linked List Representation of a Linked List.pptx
AAUsH2
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPTX
LINKED LISTS
Dhrthi Nanda
 
PPT
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
DS Unit 2.ppt
JITTAYASHWANTHREDDY
 
DOC-20250201-WA0019..pdf37853675543337784326863467
alishafgh391
 
Abscddnddmdkwkkstack implementation.pptx
zainshahid3040
 
Data Structure
HarshGupta663
 
Stack and Queue
Apurbo Datta
 
Data Structures and Agorithm: DS 06 Stack.pptx
RashidFaridChishti
 
LinkedList1LinkedList1LinkedList1111.pdf
timoemin50
 
computer notes - Stack
ecomputernotes
 
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
shesnasuneer
 
module 3-.pptx
kumarkaushal17
 
linkedlistforslideshare-210123143943.pptx
shesnasuneer
 
Lecture ............ 3 - Linked Lists.pptx
SumeetRathi5
 
Stacks and Queues with Linked List.pdf
GKalyani4
 
Linked list
Arbind Mandal
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
Advanced s and s algorithm.ppt
LegesseSamuel
 
Linked List Representation of a Linked List.pptx
AAUsH2
 
Unit ii(dsc++)
Durga Devi
 
LINKED LISTS
Dhrthi Nanda
 
Lecture 3 List of Data Structures & Algorithms
haseebanjum2611
 
Ad

More from Muhammad Hammad Waseem (20)

PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 16] Structures in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 15] Arrays & its Types
Muhammad Hammad Waseem
 
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
[ITP - Lecture 13] Introduction to Pointers
Muhammad Hammad Waseem
 
[ITP - Lecture 12] Functions in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 11] Loops in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 09] Conditional Operator in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
Muhammad Hammad Waseem
 
[ITP - Lecture 07] Comments in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
Muhammad Hammad Waseem
 
[ITP - Lecture 05] Datatypes
Muhammad Hammad Waseem
 
[ITP - Lecture 04] Variables and Constants in C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 03] Introduction to C/C++
Muhammad Hammad Waseem
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
Muhammad Hammad Waseem
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Muhammad Hammad Waseem
 
[OOP - Lec 08] Encapsulation (Information Hiding)
Muhammad Hammad Waseem
 
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 

Recently uploaded (20)

PDF
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PPTX
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PDF
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
PDF
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
PPTX
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
PPTX
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
PPTX
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
PPTX
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
PPTX
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
PDF
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Genomics Proteomics and Vaccines 1st Edition Guido Grandi (Editor)
kboqcyuw976
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
How to Add a Custom Button in Odoo 18 POS Screen
Celine George
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
How to Setup Automatic Reordering Rule in Odoo 18 Inventory
Celine George
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
IMPORTANT GUIDELINES FOR M.Sc.ZOOLOGY DISSERTATION
raviralanaresh2
 
Andreas Schleicher_Teaching Compass_Education 2040.pdf
EduSkills OECD
 
Exploring Linear and Angular Quantities and Ergonomic Design.pptx
AngeliqueTolentinoDe
 
Ward Management: Patient Care, Personnel, Equipment, and Environment.pptx
PRADEEP ABOTHU
 
Connecting Linear and Angular Quantities in Human Movement.pptx
AngeliqueTolentinoDe
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
How to Create & Manage Stages in Odoo 18 Helpdesk
Celine George
 
Aerobic and Anaerobic respiration and CPR.pptx
Olivier Rochester
 
Our Guide to the July 2025 USPS® Rate Change
Postal Advocate Inc.
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 

Data Structures - Lecture 9 [Stack & Queue using Linked List]

  • 1. Stack & Queue Using Linked List INSTRUCTOR : MUHAMMAD HAMMAD WASEEM EMAIL: [email protected]
  • 2. Stack Using Linked List  We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements.  As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. 2
  • 3. Stack Using Linked List  For a singly-linked list, insert at start or end takes constant time using the head and current pointers respectively.  Removing an element at the start is constant time but removal at the end required traversing the list to the node one before the last.  Make sense to place stack elements at the start of the list because insert and removal are constant time. 3
  • 4. Stack Using Linked List  No need for the current pointer; head is enough. 4 top 2 5 7 1 1 7 5 2 head
  • 5. Stack Operation: List int pop() { int x = head->info; head = head->link; return x; } 5 top 2 5 7 1 7 5 2 head
  • 6. Stack Operation: List void push(int x) { ListNode *newNode; newNode = new ListNode; newNode->info = x; newNode->link=head; head = newNode; } 6 top 2 5 7 9 7 5 2 head push(9) 9 newNode
  • 7. Stack Operation: List int top() { return head->info; } int IsEmpty() { return ( head == NULL ); }  All operations take constant time. 7
  • 8. Stack: Array or List  Since both implementations support stack operations in constant time, any reason to choose one over the other?  Allocating and de-allocating memory for list nodes does take more time than pre-allocated array.  List uses only as much memory as required by the nodes; array requires allocation ahead of time.  List pointers (head, next) require extra memory.  Array has an upper limit; List is limited by dynamic memory allocation. 8
  • 9. Queue Using Linked List  Insert an element at the start and removal at the end but it required extra overhead of traversing the list to the node one before the last. 9
  • 10. Queue Using Linked List  No need for the current pointer; head is enough. 10 rear 2571 1 7 5 2 rear front front
  • 11. Queue Operation: enqueue void enqueue(int x) { ListNode *newNode; newNode = new ListNode; newNode->info = x; newNode->link=head; head = newNode; } 11
  • 12. Queue Operation: dequeue Void dequeue() { nodePtr = head; while (nodePtr->link != NULL) { previousNode = nodePtr; nodePtr = nodePtr->next; } previousNode->link = nodePtr->link; delete nodePtr; } 12