This document discusses implementing stacks and queues using linked lists. For a stack, elements are inserted and removed from the head (start) of the linked list for constant time operations. For a queue, elements are inserted at the head and removed from the tail (end) of the linked list, requiring traversing to the second last node for removal. Implementing stacks and queues with linked lists avoids size limitations of arrays and uses dynamic memory allocation.
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree.
a. Concept and Definition✓
b. Inserting and Deleting nodes ✓
c. Linked implementation of a stack (PUSH/POP) ✓
d. Linked implementation of a queue (Insert/Remove) ✓
e. Circular List
• Stack as a circular list (PUSH/POP) ✓
• Queue as a circular list (Insert/Remove) ✓
f. Doubly Linked List (Insert/Remove) ✓
For more course related material:
https://github.com/ashim888/dataStructureAndAlgorithm/
Personal blog
www.ashimlamichhane.com.np
Traversal is a process to visit all the nodes of a tree and may print their values too. Because, all nodes are connected via edges (links) we always start from the root (head) node. That is, we cannot randomly access a node in a tree.
a. Concept and Definition
b. Binary Tree
c. Introduction and application
d. Operation
e. Types of Binary Tree
• Complete
• Strictly
• Almost Complete
f. Huffman algorithm
g. Binary Search Tree
• Insertion
• Deletion
• Searching
h. Tree Traversal
• Pre-order traversal
• In-order traversal
• Post-order traversal
Slides at myblog
http://www.ashimlamichhane.com.np/2016/07/tree-slide-for-data-structure-and-algorithm/
Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/Assignments/assignment_7
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
a. Concept and Definition
b. Binary Tree
c. Introduction and application
d. Operation
e. Types of Binary Tree
• Complete
• Strictly
• Almost Complete
f. Huffman algorithm
g. Binary Search Tree
• Insertion
• Deletion
• Searching
h. Tree Traversal
• Pre-order traversal
• In-order traversal
• Post-order traversal
Slides at myblog
http://www.ashimlamichhane.com.np/2016/07/tree-slide-for-data-structure-and-algorithm/
Assignments at github
https://github.com/ashim888/dataStructureAndAlgorithm/tree/dev/Assignments/assignment_7
Queues
a. Concept and Definition
b. Queue as an ADT
c. Implementation of Insert and Delete operation of:
• Linear Queue
• Circular Queue
For More:
https://github.com/ashim888/dataStructureAndAlgorithm
http://www.ashimlamichhane.com.np/
in Java (ignore the last line thats hidden) Create a doubly linked l.pdfsauravmanwanicp
in Java (ignore the last line thats hidden) Create a doubly linked list whose nodes contain Strings.
Your list should include the following methods: Insert a node in the list in alphabetical order
Find a node that matches a String Traverse the list forwards and print Traverse the list backwards
and print Delete a node from the list Delete/destroy the list In addition to creating the class(es)
for your linked list, you\'ll need to create a main method that thoroughly tests each function you
build to show that it works. You should do these things in
Solution
/ Java program to create , insert , traverse and delete a node from doubly linked list
class LinkedList {
static Node head = null;
class Node {
int data;
Node next, prev;
Node(int d) {
data = d;
next = prev = null;
}
}
/* Let us create the doubly linked list 10<->8<->4<->2 */
list.push(head, 2);
list.push(head, 4);
list.push(head, 8);
list.push(head, 10);
System.out.println(\"Original Linked list \");
list.printList(head);
/*Function to delete a node in a Doubly Linked List.
head_ref --> pointer to head node pointer.
del --> pointer to node to be deleted. */
void deleteNode(Node head_ref, Node del) {
/* base case */
if (head == null || del == null) {
return;
}
/* If node to be deleted is head node */
if (head == del) {
head = del.next;
}
/* Change next only if node to be deleted is NOT the last node */
if (del.next != null) {
del.next.prev = del.prev;
}
/* Change prev only if node to be deleted is NOT the first node */
if (del.prev != null) {
del.prev.next = del.next;
}
/* Finally, free the memory occupied by del*/
return;
}
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginning of the Doubly Linked List */
void push(Node head_ref, int new_data) {
/* allocate node */
Node new_node = new Node(new_data);
/* since we are adding at the begining,
prev is always NULL */
new_node.prev = null;
/* link the old list off the new node */
new_node.next = (head);
/* change prev of head node to new node */
if ((head) != null) {
(head).prev = new_node;
}
/* move the head to point to the new node */
(head) = new_node;
}
/*Function to print nodes in a given doubly linked list
This function is same as printList() of singly linked lsit */
void printList(Node node) {
while (node != null) {
System.out.print(node.data + \" \");
node = node.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
/* delete nodes from the doubly linked list */
list.deleteNode(head, head); /*delete first node*/
list.deleteNode(head, head.next); /*delete middle node*/
list.deleteNode(head, head.next); /*delete last node*/
System.out.println(\"\");
/* Modified linked list will be NULL<-8->NULL */
System.out.println(\"Modified Linked List\");
list.printList(head);
}
}.
Similar to Data Structures - Lecture 9 [Stack & Queue using Linked List] (20)
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdfTechSoup
In this webinar you will learn how your organization can access TechSoup's wide variety of product discount and donation programs. From hardware to software, we'll give you a tour of the tools available to help your nonprofit with productivity, collaboration, financial management, donor tracking, security, and more.
The French Revolution, which began in 1789, was a period of radical social and political upheaval in France. It marked the decline of absolute monarchies, the rise of secular and democratic republics, and the eventual rise of Napoleon Bonaparte. This revolutionary period is crucial in understanding the transition from feudalism to modernity in Europe.
For more information, visit-www.vavaclasses.com
This is a presentation by Dada Robert in a Your Skill Boost masterclass organised by the Excellence Foundation for South Sudan (EFSS) on Saturday, the 25th and Sunday, the 26th of May 2024.
He discussed the concept of quality improvement, emphasizing its applicability to various aspects of life, including personal, project, and program improvements. He defined quality as doing the right thing at the right time in the right way to achieve the best possible results and discussed the concept of the "gap" between what we know and what we do, and how this gap represents the areas we need to improve. He explained the scientific approach to quality improvement, which involves systematic performance analysis, testing and learning, and implementing change ideas. He also highlighted the importance of client focus and a team approach to quality improvement.
How to Create Map Views in the Odoo 17 ERPCeline George
The map views are useful for providing a geographical representation of data. They allow users to visualize and analyze the data in a more intuitive manner.
The Art Pastor's Guide to Sabbath | Steve ThomasonSteve Thomason
What is the purpose of the Sabbath Law in the Torah. It is interesting to compare how the context of the law shifts from Exodus to Deuteronomy. Who gets to rest, and why?
Model Attribute Check Company Auto PropertyCeline George
In Odoo, the multi-company feature allows you to manage multiple companies within a single Odoo database instance. Each company can have its own configurations while still sharing common resources such as products, customers, and suppliers.
How to Split Bills in the Odoo 17 POS ModuleCeline George
Bills have a main role in point of sale procedure. It will help to track sales, handling payments and giving receipts to customers. Bill splitting also has an important role in POS. For example, If some friends come together for dinner and if they want to divide the bill then it is possible by POS bill splitting. This slide will show how to split bills in odoo 17 POS.
Data Structures - Lecture 9 [Stack & Queue using Linked List]
1. Stack & Queue
Using Linked List
INSTRUCTOR : MUHAMMAD HAMMAD WASEEM
EMAIL: m.hammad.wasim@gmail.com
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