IT4002 LS Unit-3 - Student
IT4002 LS Unit-3 - Student
1
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Linked List
3.4. Applications
2
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.0. Introduction to Linked List
The linked list is called a dynamic data structure, where the amount
of memory required can be diverse during its use.
So, in a linked list, data (actual content) and link (to point to the next
data) both are required to be maintained.
3
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.0. Introduction to Linked List (Conti…)
A node consists of two fields:
1. Data - To store the actual information.
2. Link - To point to next node.
Link
Data Link to the next node
4
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.0. Introduction to Linked List (Conti…)
What is Linked List?
A linked list is an ordered collection of finite, homogeneous data
elements called nodes, where the linear order is maintained by means
of links or pointers.
5
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.0. Introduction to Linked List (Conti…)
Depending on the requirements the pointers are maintained, and
accordingly the linked list can be classified.
6
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1.
Single Linked List
7
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
To store list of customers: Saumil, Priya, Jay, Mansi, Jugal
Saumil 2000 Priya 3000 Jay 4000 Mansi 5000 Jugal Null
~: Solution :~
1000
By using pointer
Header 8
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1000 Header
Data part of
second node.
9
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
There are two ways to represent a linked list in memory:
1. Static - is implemented using array.
2. Dynamic - is implemented using free pool of storage.
10
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
1. Static representation of a single linked list:
So, two parallel arrays of equal size are allocated, which is sufficient
to store the entire linked list.
11
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Data Link
102
101
102 M 104
103
104 S 105
105 C 107
106
107 I 109
108
109 T 101
110
12
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Dynamic representation of a single linked list:
It is the efficient way of representing a linked list is using the free pool
of storage.
and the memory manager will then search the memory bank for the
block requested,
There is also another program called the garbage collector; it plays the
role whenever a node is no more in use;
it returns the unused node to the memory bank (memory space which
is available to a programmer).
14
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Dynamic representation of a single linked list: (Conti…)
15
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
16
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Dynamic representation of a single linked list: (Conti…)
17
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
The operations possible on a single linked list are:
18
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
1. Traversing a single linked list:
In traversing a single linked list, we visit every node in the list starting from
the first node to the last node.
Algorithm:
Steps:
1. ptr = HEADER-> LINK // ptr is to store the pointer to a current node
2. While (ptr ≠ NULL) do // Continue till the last node
3. Process(ptr) // Perform Process() on the current node
4. ptr = ptr -> LINK // Move to the next node
5. EndWhile
6. Stop
19
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
20
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
struct node
{
int data;
node *link; // link pointer which points to a node
}
21
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
23
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1000
1001
1000
ptr->link = head; // Copy the head value to the link part of the
newly created node
head = ptr; // Copy the ptr value in head
24
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2.1. Inserting a node at the front of a single linked list: (Conti…)
Algorithm:
Steps:
1. new = GetNode(NODE) // Get a memory block of type NODE and store its
pointer in new
2. If (new = NULL) then // Memory manager returns NULL on searching the
memory bank
3. Print “Memory Overflow! No insertion can take place.”
4. Exit // Quit the program
5. Else // Memory is available and get a node from memory bank
6. new -> LINK = HEADER -> LINK // Change of pointer
7. new -> DATA = X // Copy the data X to newly availed node
8. HEADER -> LINK = new // Change of pointer
9. EndIf
10. Stop
25
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
struct node
{
int data;
node *link; void insertbeg(int da)
} {
node *head = NULL; node *ptr = new node;
ptr->data = da;
ptr->link = head;
head = ptr;
}
26
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1000 temp temp temp
For example:
Head
1000 temp
Algorithm:
Steps:
1. new = GetNode(NODE) // Get a memory block of type NODE and return its
pointer in new
2. If (new = NULL) then // Unable to allocate memory for a node
3. Print “Memory is insufficient! Insertion is not possible.”
4. Exit // Quit the program
5. Else // Move to the end of the given list and then insert
6. ptr = HEADER // Start from the HEADER node
7. While (ptr -> LINK ≠ NULL) do // Move to the end
8. ptr = ptr -> LINK // Change pointer to the next node
9. EndWhile
10. ptr -> LINK = new // Change the link field of last node: Pointer
11. new -> DATA = x // Copy the content X into the new node
12. EndIf 13. Stop
29
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Algorithm:
Steps:
1. new = GetNode(NODE) // Get a memory block of type NODE and returns its
pointer as new.
2. If (new = NULL) then // Unable to allocate memory for a node.
3. Print “Memory is insufficient! Insertion is not possible.”
4. Exit // Quit the program
5. Else
6. ptr = HEADER // Start from the HEADER node
7. While(ptr -> DATA ≠ KEY) and ptr -> LINK ≠ NULL) do // Move to the
node, having data as KEY or at the end if KEY is not in the list.
8. Ptr = ptr-> LINK
9. EndWhile
31
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2.3. Inserting a node into a single linked list at any position in the list:
(Conti…)
Algorithm:
Steps: (Conti…)
10. If(ptr -> LINK = NULL) then // Search fails to find the KEY
11. Print “KEY is not available in the list.”
12. Exit
13. Else
14. new -> LINK = ptr -> LINK // Change of pointer
15. new -> DATA = X // Copy the content into the new node
16. ptr -> LINK = new // Change of pointer
17. EndIf
18. EndIf
19. Stop
32
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Create a menu driven program in C++ to implement the following operations on
the Single Linked List:
1. Insert data
a. By inserting a node at front
b. By inserting a node at end
c. By inserting a node at any other position
33
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
3. Deleting a node from the list:
34
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1000
ptr
node *ptr = head; // „ptr‟ equate to „head‟, to point to the first node
head = head->link; // „head‟ pointer equal to „link‟ part of the first node
35
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1001
ptr
node *ptr = head; // „ptr‟ equate to „head‟, to point to the first node
head = head->link; // „head‟ pointer equal to „link‟ part of the first node
delete ptr; // Delete the first node using ptr pointer
36
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1001
9 1002 5 NULL
1001 1002
node *ptr = head; // „ptr‟ equate to „head‟, to point to the first node
head = head->link; // „head‟ pointer equal to „link‟ part of the first node
delete ptr; // Delete the first node using ptr pointer
37
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
3.1. Deleting the node at the front of a single linked list: (Conti…)
Algorithm:
Steps:
1. ptr = HEADER -> LINK // Pointer to the first node
2. If (ptr = NULL) then // If the list is empty
3. Print “The list is empty! No deletion can take place.”
4. Exit // Quit the program
5. Else // The list is not empty
6. ptr1 = ptr -> LINK // ptr1 is the pointer to the second node, if any
7. HEADER -> LINK = ptr1 // Next node becomes the first node
8. ReturnNode(ptr) // Deleted node is freed to the memory bank for the future use
9. EndIf
10. Stop
38
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
For example:
Head
1000 ptr1 ptr ptr1 ptr ptr1 ptr
For example:
Head
1000 ptr1
For example:
Head
1000 ptr1
Algorithm:
Steps:
1. ptr = HEADER // Move from the header node
2. If (ptr -> LINK = NULL) then
3. Print “The list is empty! No deletion can take place.”
4. Exit // Quit the program
5. Else
6. While (ptr -> LINK ≠ NULL ) do // Go to the last node
7. ptr1 = ptr // To store the previous pointer
8. ptr = ptr -> LINK // Move to the next
9. EndWhile
10. ptr1 -> LINK = NULL // Last but one node becomes the last node
11. ReturnNode(ptr) // Deleted node is returned to the memory bank for future use
12. EndIf 13. Stop
43
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Algorithm:
Steps:
1. ptr1 = HEADER // Start from the header node
2. ptr = ptr1 -> LINK // This points to the first node, if any
3. While (ptr ≠ NULL) do
4. If (ptr -> DATA ≠ KEY) then // If not found the key
5. ptr1 = ptr // Keep a track of the pointer of the previous node
8. ptr = ptr -> LINK // Move to the next
9. Else
10. ptr1 -> LINK = ptr -> LINK // Link field of the predecessor is to point the
successor of node under deletion
11. ReturnNode(ptr) // Return the deleted node to the memory bank
12. Exit // Exit the program
13. EndIF
14. EndWhile
45
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
3.3. Deleting the node from any position of a single linked list: (Conti…)
Algorithm:
Steps: (Conti…)
15. If (ptr = NULL) then //When the desired node is not available in the list
16. Print “Node with KEY does not exit! No deletion can take place.”
17. EndIf
18. Stop
46
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Create a menu driven program in C++ to implement the following operations on
the Single Linked List:
1. Insert data
(Upon on your choice for inserting a node at front or end or any position)
2. Delete data
a. By deleting a node from the front
b. By deleting a node from the end
c. By deleting a node from any position
47
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
4. Copying a single linked list (to make a duplicate of it):
We can duplicate the content of each node into a newly allocated node.
48
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
4. Copying a single linked list (to make a duplicate of it): (Conti…)
Algorithm:
Steps:
1. ptr = HEADER // Current position in the master list
2. HEADER1 = GetNode(NODE) // Get a node for the header of the duplicate list
3. ptr1 = HEADER1 // ptr1 is the current position in the duplicated list
4. ptr1 -> DATA = NULL // Header node does not contain any data
5. While (ptr ≠ NULL) do // Till the traversal of master node is finished
6. new = GetNode(NODE) // Get a new node from memory bank
7. new -> DATA = ptr -> DATA // Copy the content
8. ptr1 -> LINK = new // Insert the node at the end of the duplicate list
9. ptr1 -> LINK = NULL
10. ptr1 -> new
11. ptr = ptr -> LINK // Move to the next node in the master list
12. EndWhile
13. Stop
49
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to make a copy of the given single linked list.
50
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
51
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
5. Merging the linked list with another one to make a larger list: (Conti…)
Algorithm:
Steps:
1. ptr = HEADER1 // Move to the last node in the list L1
2. While (ptr->LINK ≠ NULL) do
3. ptr = ptr-> LINK
4. EndWhile
5. ptr->LINK = HEADER2->LINK
// Last node in L1 points to the first node in L2
6. ReturnNode(HEADER2) // Return the header node to the memory bank
7. HEADER = HEADER1
// HEADER becomes the header node of the merged list
8. Stop
52
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to merge two single linked list into one list.
53
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
6. Searching for an element in a single linked list:
Algorithm:
Steps:
1. ptr = HEADER->LINK // Start from the first node
2. flag = 0, Location = NULL
3. While (ptr ≠ NULL) and (flag = 0) do
4. If (ptr->DATA = KEY) then
5. flag = 1 // Search is finished
6. LOCATION = ptr
7. Print “Search is successful.”
8. Return(LOCATION)
9. Else
10. ptr = ptr->LINK // Move to the next node
11. EndIF
12. EndWhile
54
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
6. Searching for an element in a single linked list: (Conti…)
Algorithm:
Steps:
13. If (ptr = NULL) then
14. Print “Search is unsuccessful.”
15. EndIf
16. Stop
55
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to search for an element in a single linked list.
56
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Advantages of Link List
A linked list is a dynamic data structure that can be grow or shrink in
the size during the execution of program.
A linked list does not waste memory space. It uses the memory that is
needed for the list item at any point and at any time.
The most important advantage is that the linked list provide the
flexibility in allowing the items to be re-arrange efficiently. It is easier
to insert or delete the item by rearranging the links.
57
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Disadvantages of Link List
It consume time to access any random item.
It will use more storage then array with the same number of items
because each item has as additional link field.
58
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2.
Double Linked List
59
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List
A double linked list is a two-way list, because one can move in both
direction, that is from left to right or from right to left.
Llink Rlink
Link to the previous Link to the next
Data
node node
Node
60
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
In Doubly linked list, both the value of the first and last nodes pointer
is always set as NULL.
Because there can be any previous node of the first node and next node
of the last node. Therefore it is always set as NULL.
Null Null
61
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
The main difference between Singly Linked List and Doubly Linked
List is that,
the singly linked list has only one pointer, which points to the next
node,
while doubly linked list has two pointers, which points to previous
and next node.
For example:
Header
1001
63
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Like single linked list, in double linked list also there are various
positions, where a node can be inserted:
1. Inserting at the front (as a first element).
2. Inserting at the end (as a last element).
3. Inserting at any other position.
65
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1003 Tail
66
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1002 6 Null
1003 Tail
69
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Create a menu driven program in C++ to implement the following operations on
the Double Linked List:
1. Insert data
a. By inserting a node at front
b. By inserting a node at end
c. By inserting a node at any other position
70
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
3. Deleting a node from the list:
Like single linked list, in double linked list also there are three cases
of deletions:
1. Deleting from the front of the list.
2. Deleting from the end of the list.
3. Deleting from any position in the list.
71
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Algorithm:
Steps:
1. ptr = HEADER->Rlink
2. If (ptr = NULL) then
3. Print “Node with KEY does not exit! No deletion can take place.”
4. Exit
5. EndIf
6. While (ptr->DATA ≠ KEY) and (ptr->Rlink ≠ NULL) do
7. ptr = ptr->Rlink
8. EndWhile
Conti…
74
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1003 Tail
75
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Create a menu driven program in C++ to implement the following operations on
the Double Linked List:
1. Insert data
(Upon on your choice for inserting a node at front or end or any position)
2. Delete data
a. By deleting a node from the front
b. By deleting a node from the end
c. By deleting a node from any position
76
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Advantages of Doubly Linked List
1. Traversal can be done in both directions.
[But this is not possible in a Singly Linked List and it can only be
traversed in one direction.]
77
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Advantages of Doubly Linked List (Conti…)
4. It is easy to reverse the list.
78
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Disadvantages of Doubly Linked List
1. Memory has to be allocated for both the next and previous pointers in
a node. So, it occupies higher memory.
79
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3.
Circular Linked List
80
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List
A linked list where the last node points the header node is called the
circular linked list.
For example:
Head
In singly circular linked list, the next pointer of the last node points to
the first node.
81
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List (Conti…)
The circular linked lists have certain advantages over ordinary linked
lists.
Some are:
1. Accessibility of a member node in the list
2. Null link problem
3. Some easy-to-implement operations
82
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List (Conti…)
Searching for an element in a single linked list:
Algorithm:
Steps:
1. ptr = HEADER->LINK // Start from the first node
2. While (ptr ≠ NULL) do
3. If (ptr->DATA ≠ KEY) then
4. ptr = ptr->LINK
5. Else
6. Print “Search is successful.”
7. Return(ptr)
8. EndIF
9. EndWhile
10. If (ptr = NULL) then
11. Print “The entire list has traversed, but KEY is not found!”
12. EndIf
13. Stop
83
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List (Conti…)
Searching for an element in a circular linked list:
Algorithm:
Steps:
1. ptr = HEADER->LINK // Start from the first node
2. While (ptr->DATA ≠ KEY) and (ptr ≠ HEADER) do
3. ptr = ptr->LINK
4. EndWhile
5. If (ptr->DATA = KEY) then
6. Return(ptr)
7. Else
8. Print “The entire list has traversed, but KEY is not found!”
9. EndIf
10. Stop
84
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to search for an element in a circular single linked list.
85
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List (Conti…)
Merging the two circular single linked list into one list:
Algorithm:
Steps:
1. ptr1 = HEADER1->LINK
2. ptr2 = HEADER2->LINK
3. HEADER1->LINK = ptr2
4. While (ptr2->LINK ≠ HEADER2) do
HEADER2
5. ptr2 = ptr2->LINK
6. EndWhile
7. ptr2->LINK = ptr1
8. ReturnNode(HEADER2)
9. Stop
86
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Header1 ptr1
1008
1001 9 1002 5 1003 7 1001
87
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to merge two circular single linked list into one list.
88
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.3. Circular Linked List (Conti…)
The advantages of both double linked list and circular linked list are
combined into another type of list structure called circular double
linked list. And it is known to be the best of its kind.
For example:
In doubly circular linked list, the next pointer of the last node points
to the first node and the previous pointer of the first node points to
the last node making the circular in both directions.
89
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.4.
Applications
90
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.4. Applications
Linked list in real world:
1. Image viewer
– Next and Previous images are linked.
3. Music Player
- Songs in music player are linked to previous and next song.
91
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.4. Applications (Conti…)
A telephone list in alphabetical order:
Ashok Jadav 7006007006
Jay Gajjar 9785600056
Kashyap Vakharia 6987453210
Om Patel 6789101112
Sujit Samanta 9632587410
92
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.
Linked Stack and Linked Queue
93
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.1. Linked Stack
The basic operations required to manipulate a dynamic stack are:
1. PUSH
2. POP
3. STATUS
(PEEP)
94
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1000 TOP
newptr
Saumil NULL
1000
95
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
newptr
Saumil NULL
1000
If (TOP == NULL)
TOP = newptr
Else
newptr->DATA = ITEM
newptr->LINK = TOP
TOP = newptr
96
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
newptr
Priya 1000 Saumil NULL
2000 1000
If (TOP == NULL)
TOP = newptr
Else
newptr->DATA = ITEM
newptr->LINK = TOP
TOP = newptr
97
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
newptr
Jay 2000 Priya 1000 Saumil NULL
3000 2000 1000
If (TOP == NULL)
TOP = newptr
Else
newptr->DATA = ITEM
newptr->LINK = TOP
TOP = newptr
98
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000
If (TOP == NULL)
TOP = newptr
Else
newptr->DATA = ITEM
newptr->LINK = TOP
TOP = newptr
99
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.1. Linked Stack (Conti…)
1. Algorithms for PUSH operation in dynamic stack:
Steps:
1. new = GetNode(NODE) // Insert at front
2. new->DATA = ITEM
3. new->LINK = TOP
4. TOP = new
5. STACK_HEADER->LINK = TOP
6. Stop
100
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Steps:
1. If (TOP = NULL)
2. Print “Stack is Empty!”
3. Exit
4. Else
5. ptr = TOP->LINK
6. ITEM = TOP->DATA
7. STACK_HEADER->LINK = ptr
8. TOP = ptr
9. EndIF
10. Exit
103
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000
temp = TOP
Else
while(temp != NULL)
If (TOP == NULL)
{
{
temp->data;
cout<<“Stack is Empty!”;
temp = temp->link;
}
}
104
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
Steps:
1. ptr = STACK_HEADER->LINK
2. If (ptr = NULL) then
3. Print “Stack is Empty!”
4. Else
5. nodeCount = 0
6. While (ptr ≠ NULL) do
7. nodeCount = nodeCount + 1
8. ptr = ptr->LINK
9. EndWhile
10. Print “The item at the front is: ”, TOP->DATA, “Stack
contains ”, nodeCount, “Number of items.”
11. EndIf
12. Exit 108
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to create a singly linked list in LIFO fashion.
109
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.2. Linked Queue
The basic operations required to manipulate a dynamic queue are:
1. Enqueue
2. Dequeue
110
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1000 Head
newptr
Saumil NULL
1000
111
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
1000
1000 2000
node *temp;
If (front == NULL && rear == NULL)
temp = front;
cout<<“Queue is Empty”;
Else
front = front->link;
delete temp;
116
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
node *temp;
If (front == NULL && rear == NULL)
temp = front;
cout<<“Queue is Empty”;
Else
front = front->link;
delete temp;
117
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration
122
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
123
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY