0% found this document useful (0 votes)
17 views123 pages

IT4002 LS Unit-3 - Student

The document discusses different types of linked lists including singly linked lists, doubly linked lists, and circularly linked lists. It describes the key aspects of singly linked lists including how each node contains a data field and a link field pointing to the next node. Static and dynamic representations of singly linked lists using arrays and memory allocation are also covered.

Uploaded by

22bmiit190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views123 pages

IT4002 LS Unit-3 - Student

The document discusses different types of linked lists including singly linked lists, doubly linked lists, and circularly linked lists. It describes the key aspects of singly linked lists including how each node contains a data field and a link field pointing to the next node. Static and dynamic representations of singly linked lists using arrays and memory allocation are also covered.

Uploaded by

22bmiit190
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 123

Data Structures

Unit 3: Linked List

1
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Linked List

3.1. Singly Linked List

3.2. Doubly Linked List

3.3. Circularly Linked List

3.4. Applications

3.5. Linked Stack and Linked Queue

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.

 In the linked list, the adjacency between the elements is maintained by


means of links or pointers.

 A link or pointer actually is the address (memory location) of the


subsequent element.

 So, in a linked list, data (actual content) and link (to point to the next
data) both are required to be maintained.

 An element in a linked list is a specially termed as node.

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

Node : an element in a linked list.

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.

 In a simple way we can say that…


A linked list is a sequence of elements or nodes, where every item is
linked to the next.

Data Data Data

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.

 The classification of listed list is into three major groups:


1. Single Linked List
2. Double Linked List
3. Circular Linked List

6
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1.
Single Linked List

7
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List


 In a single linked list, each node contains only one link, which points
to the subsequent node in the list.

 For example:
 To store list of customers: Saumil, Priya, Jay, Mansi, Jugal

Saumil 2000 Priya 3000 Jay 4000 Mansi 5000 Jugal Null

1000 2000 3000 4000 5000

But, how to access the first node of the linked list?

~: Solution :~
1000
By using pointer
Header 8
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


 Implementation of a general list having elements:

1000 Header

Saumil 2000 Priya 3000 Jay 4000 Mansi 5000

1000 2000 3000 4000

Pointer storing the Jugal Null

address of first node. 5000


Next point of
second node.

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:

 Since, it is represented using array, it reserves fixed amount of


memory space before the actual processing take place i.e. the number
of elements to be stored.

 In static representation of a single linked list, two arrays are to be


maintained:
 One array for data and
 Other array for links.

 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

CE: 3.1. Single Linked List (Conti…)


Static representation using arrays of a single linked list

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:

 This technique helps allocation of memory during the program


execution itself, as and when required.

 It also helps release of memory, if memory is not required any more.

 The list of available space is called the free pool.

 It is the efficient way of representing a linked list is using the free pool
of storage.

 In this method, there is a memory bank and a memory manager.

 Memory bank – is nothing but a collection of free memory spaces.


 Memory manager – is a program 13
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Dynamic representation of a single linked list: (Conti…)

 During the creation of a linked list, whenever a node is required the


request is placed to the memory manager;

and the memory manager will then search the memory bank for the
block requested,

and if found, it grands the desired block to the caller.

 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…)

 Thus, the dynamic representation of linked list uses the dynamic


management policy.

15
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


Dynamic representation of a single linked list

Start Data Link Thus, a special list is


102 maintained, which contains
101 106
102 M 104 unused memory cells.
103 101
This list is called Avail list.
104 S 105
105 C 107
And this avail list pointer
Avail 106 111 is store in AVAIL.
108 107 109
I
108 110
109 T 112
110 103

16
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Dynamic representation of a single linked list: (Conti…)

 Thus, with every insertion, number of free nodes is AVAIL is reduced


and with every deletion this number is increased.

17
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
 The operations possible on a single linked list are:

1. Traversing the list


2. Inserting a node into the list
3. Deleting a node from the list
4. Copying the list to make a duplicate of it
5. Merging the linked list with another one to make a larger list
6. Searching for an element in the list.

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…)

But, still the question arises that….

How to create a node???


Data Link
Node
And

How to implement that in C++???

20
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


How to implement in C++???

 By using structure we can create a node, because…


- structure contains data variable and
- a pointer to point to the next node.

struct node
{
int data;
node *link; // link pointer which points to a node
}

21
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


How to create a node???
struct node
{
int data; Head
node *link; 1001
NULL
}
node *head = NULL; //Global ptr
9 NULL
int main()
{ 1001
node *ptr = new node;
ptr->data = 9;
ptr->link = NULL;
head = ptr;
return 0;
}
22
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2. Inserting a node into a single linked list:

 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.

23
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


2.1. Inserting a node at the front of a single linked list:

 For example:

Head
1000
1001

ptr 9 1002 5 NULL

3 1001 1001 1002

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

CE: 3.1. Single Linked List (Conti…)


2.1. Inserting a node at the front of a single linked list: (Conti…)

How to implement in C++???

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

CE: 3.1. Single Linked List (Conti…)


2.2. Inserting a node at the end of a single linked list:

 For example:
Head
1000 temp temp temp

3 1001 9 1002 5 NULL


1000 1001 1002 ptr
temp = head; // Because it is pointing to the 7 NULL
first node of the list
1003
Now, Traverse the list using temp
temp = temp->link;
Untill temp->link != NULL;

temp->link = ptr; // Copy the ptr value in link


27
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


2.2. Inserting a node at the end of a single linked list:

 For example:
Head
1000 temp

3 1001 9 1002 5 1003


1000 1001 1002 ptr
temp = head; // Because it is pointing to the 7 NULL
first node of the list
1003
Now, Traverse the list using temp
temp = temp->link;
Untill temp->link != NULL;

temp->link = ptr; // Copy the ptr value in link


28
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
2.2. Inserting a node at the end of a single linked list: (Conti…)

 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

CE: 3.1. Single Linked List (Conti…)


2.2. Inserting a node at the end of a single linked list: (Conti…)

How to implement in C++???

struct node if(head == NULL)


{ {
int data; head = ptr;
node *link; }
} else
node *head = NULL; {
node *temp = head;
void insertend(int da) while(temp->link != NULL)
{ {
node *ptr = new node; temp = temp->link;
ptr->data = da; }
ptr->link = NULL; temp->link = ptr;
head = ptr; } }
30
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:

 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:

 Like insertions, there are also 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.

34
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.1. Deleting the node at the front of a single linked list:

 For example:

Head
1000
ptr

3 1001 9 1002 5 NULL


1000 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

35
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.1. Deleting the node at the front of a single linked list:

 For example:

Head
1001
ptr

3 1001 9 1002 5 NULL


1000 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
36
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.1. Deleting the node at the front of a single linked list:

 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

CE: 3.1. Single Linked List (Conti…)


3.1. Deleting the node at the front of a single linked list: (Conti…)

How to implement in C++???

struct node void deletebeg()


{ {
int data; if(head == NULL)
node *link; {
} cout<<"\nList is empty!";
node *head = NULL; }
else
{
node *ptr = head;
head = head->link;
delete ptr;
}
}
39
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.2. Deleting the node at the end of a single linked list:

 For example:
Head
1000 ptr1 ptr ptr1 ptr ptr1 ptr

3 1001 9 1002 5 1003 ptr


1000 1001 1002 ptr
node *ptr, *ptr1; // „ptr‟ is temporary pointer 7 NULL
ptr = head;
1003
Now, Traverse the list using ptr and ptr1
ptr1 = ptr;
ptr = ptr->link;
Untill ptr->link != NULL;
40
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.2. Deleting the node at the end of a single linked list:

 For example:
Head
1000 ptr1

3 1001 9 1002 5 NULL ptr


1000 1001 1002 ptr
node *ptr, *ptr1; // „ptr‟ is temporary pointer 7 NULL
ptr = head;
1003
Now, Traverse the list using ptr and ptr1
ptr1 = ptr;
ptr = ptr->link;
Untill ptr->link != NULL;
ptr1->link = NULL; delete ptr;
41
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.1. Single Linked List (Conti…)


3.2. Deleting the node at the end of a single linked list:

 For example:
Head
1000 ptr1

3 1001 9 1002 5 NULL


1000 1001 1002
node *ptr, *ptr1; // „ptr‟ is temporary pointer
ptr = head;
Now, Traverse the list using ptr and ptr1
ptr1 = ptr;
ptr = ptr->link;
Untill ptr->link != NULL;
ptr1->link = NULL; delete ptr;
42
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.1. Single Linked List (Conti…)
3.2. Deleting the node at the end of a single linked list: (Conti…)

 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

CE: 3.1. Single Linked List (Conti…)


3.2. Deleting the node at the end of a single linked list: (Conti…)
How to implement in C++???
struct node if(head->link == NULL)
{ {
int data; ptr = head;
node *link; head = NULL;
} delete ptr;
node *head = NULL; }
else
void deleteend() { ptr = head;
{ while(ptr->link != NULL)
node *ptr, *ptr1; {
if(head == NULL) ptr1 = ptr;
{ ptr = ptr->link;
cout<<"\nList is Empty!"; }
} ptr1->link = NULL;
else delete ptr;
{ }
}
44
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:

 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

CE: 3.1. Single Linked List (Conti…)


5. Merging the linked list with another one to make a larger list:

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.

 In doubly linked list, each node contains one or more information


fields.

 Here, two additional information are stored with the node.


1. The pointer to the previous node. (which points to its previous node)
2. The pointer to the next node. (which points to its next node)

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

 Therefore, to set the previous pointer only, we perform Doubly Linked


List; and rest all the other operations are same as the Singly Link List.

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

Null 2 1002 1001 4 1003 1002 6 Null

1001 1002 1003


62
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
 The operations possible on a double linked list are same as the single
linked list:

1. Traversing the list


2. Inserting a node into the list
3. Deleting a node from the list
4. Copying the list to make a duplicate of it
5. Merging the linked list with another one to make a larger list
6. Searching for an element in the list.

63
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.2. Double Linked List (Conti…)


How to create a node???
struct node
{
int data; Head
node *Rlink;
1001
NULL
node *Llink;
}
node *head = NULL; //Global ptr
NULL 9 NULL
int main()
1001
{
node *ptr = new node;
ptr->data = 9;
ptr->Rlink = NULL;
ptr->Llink = NULL;
head->Rlink = ptr;
return 0; } 64
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
2. Inserting a node into a double linked list:

 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

CE: 3.2. Double Linked List (Conti…)


2.1. Inserting a node at the front of a double linked list:
Header
 Algorithm:
1000
1001
Steps: new
1. ptr = HEADER->Rlink NULL 12 1001
2. new = GetNode(NODE)
3. If (new ≠ NULL) then ptr
1000
4. new->Llink = HEADER
1000
NULL 2 1002
5. HEADER->Rlink = new
6. new->Rlink = ptr 1001
7. ptr->Llink = new
8. new->DATA = X 1001 4 1003
9. Else
10. Print “Insertion is not possible!” 1002
11. EndIf
12. Stop 1002 6 Null

1003 Tail
66
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.2. Double Linked List (Conti…)


2.2. Inserting a node at the end of a double linked list:
Header ptr
 Algorithm:
1000
Steps: Null 8 1001
1. ptr = HEADER
1000
2. While (ptr->Rlink ≠ NULL) do
ptr
3. ptr = ptr->Rlink
4. EndWhile
1000 2 1002
5. new = GetNode(NODE)
6. If (new ≠ NULL) then 1001
ptr
7. new->Llink = ptr
8. ptr->Rlink = new 1001 4 1003
9. new->Rlink = NULL
10. new->DATA = X 1002 ptr
11. Else new
12. Print “Insertion is not possible!” 1002 6 1004
Null
1003 12 NULL
13. EndIf 14. Stop 1003 Tail
Tail 1004 67
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.2. Double Linked List (Conti…)


2.3. Inserting a node at the any position of a double linked list:
 Algorithm:
Steps:
1. ptr = HEADER
2. While (ptr->Data ≠ KEY) and (ptr->Rlink ≠ NULL) do
3. ptr = ptr->Rlink
4. EndWhile
5. new = GetNode(NODE)
6. If (new = NULL) then
7. Print "Memory is not available!“
8. Exit
9. EndIf
10. If (ptr->Rlink = NULL) then
11. new->Llink = ptr
12. ptr->Rlink = new
13. new->Rlink = NULL
14. new->DATA = X 68
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.2. Double Linked List (Conti…)


2.3. Inserting a node at the any position of a double linked list: (Conti…)
Header
 Algorithm:
ptr
Steps: (Conti…) 1000
15. Else Null 8 1001
999
16. ptr1 = ptr->Rlink new 1000
17. new->Llink = ptr
1000 12 1001 ptr1
18. new->Rlink = ptr1
19. ptr->Rlink = new 999 1000
999 2 1002
20. ptr1->Llink = new 1001
21. ptr = new
22. new->DATA = X
23. EndIF 1001 4 1003
24. Stop 1002

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

CE: 3.2. Double Linked List (Conti…)


3.1. Deleting a node from the front of a double linked list:
Header
 Algorithm:
1000
1001
ptr
Steps: NULL 12 1001
1. ptr = HEADER->Rlink
2. If (ptr = NULL) then 1000
ptr1
3. Print “The list is empty!”
1000
NULL 2 1002
4. Exit
5. Else 1001
6. ptr1 = ptr->Rlink
7. HEADER->Rlink = ptr1 1001 4 1003
8. If (ptr1 ≠ NULL)
9. ptr1->Llink = HEADER 1002
10. EndIf
11. ReturnNode(ptr) 1002 6 Null

12. EndIf 13. Stop 1003 Tail


72
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.2. Double Linked List (Conti…)


3.2. Deleting a node at the end of a double linked list:
Header ptr
 Algorithm:
1000
Null 8 1001
Steps:
1000
1. ptr = HEADER
ptr
2. While (ptr->Rlink ≠ NULL ) do
3. ptr = ptr->Rlink
1000 2 1002
4. EndWhile
5. If (ptr = HEADER) then 1001
ptr
6. Print “The list is empty!”
7. Exit ptr1 1001 4 1003
NULL
8. Else
9. ptr1 = ptr->Llink 1002 Tail ptr
10. ptr1->Rlink = NULL
11. ReturnNode(ptr) 1002 6 Null

12. EndIf 13. Stop 1003 Tail


73
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.2. Double Linked List (Conti…)
3.3. Deleting a node from any position in a double linked list:

 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

CE: 3.2. Double Linked List (Conti…)


3.3. Deleting a node from any position in a double linked list: (Conti…)
Header
 Algorithm: ptr1
1000
Steps: (Conti…) Null 8 1001
999
9. If (ptr->DATA = KEY) then
ptr 1000
10. ptr1 = ptr->Llink
11. ptr2 = ptr->Rlink 1000 12 1001 ptr2
12. ptr1->Rlink = ptr2
999 999
1000 2 1002
13. If (ptr2 ≠ NULL) then
14. ptr2->Llink = ptr1 1001
15. EndIf
16. ReturnNode(ptr) 1001 4 1003
17. Else
18. Print “Node doesn‟t exist!” 1002
19. EndIf
20. Stop 1002 6 Null

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.]

2. We can quickly insert a new node before a given node.

3. The delete operation is more efficient.


[Because we just need to know the pointer of the node to be deleted.
But in a singly linked list, to delete a node, the pointer to the previous
node is needed for which the list is to be traversed.]

So, both insertion and deletion operations are easy to implement in a


Doubly LL than a Singly LL.

77
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Advantages of Doubly Linked List (Conti…)
4. It is easy to reverse the list.

5. If we are at any node, then we can go to any node.


[But in singly linked list, it is not possible to reach at any previous
node.]

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.

2. Both the pointers will have to be modified, if any kind of operation is


performed like insertion, deletion, etc.

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

10 1001 9 1002 5 1003 7 NULL


1000
1000 1001 1002 1003

 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

CE: 3.3. Circular Linked List (Conti…)


 Merging the linked list with another one to make a larger list: (Conti…)

Header1 ptr1
1008
1001 9 1002 5 1003 7 1001

1001 1002 1003

Header2 ptr2 ptr2 ptr2


1008 10 1005 5 1009 7 1008
1001

1008 1005 1009

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:

1003 2 1002 1001 4 1003 1002 6 1001

1001 1002 1003

 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.

2. Previous and next page in web browser


- We can access previous and next url searched in web browser by
pressing back and next button.

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

CE: 3.5.1. Linked Stack (Conti…)


 For example:
PUSH(Saumil, Priya, Jay, Mansi)

1000 TOP

newptr
Saumil NULL

1000

node *newptr = new node;


newptr->DATA = ITEM
newptr->LINK = NULL

95
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.1. Linked Stack (Conti…)


 For example:
PUSH(Saumil, Priya, Jay, Mansi)

1000 TOP TOP

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

CE: 3.5.1. Linked Stack (Conti…)


 For example:
PUSH(Saumil, Priya, Jay, Mansi)

2000 TOP TOP

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

CE: 3.5.1. Linked Stack (Conti…)


 For example:
PUSH(Saumil, Priya, Jay, Mansi)

3000 TOP TOP

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

CE: 3.5.1. Linked Stack (Conti…)


 For example:
PUSH(Saumil, Priya, Jay, Mansi)

4000 TOP TOP

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

CE: 3.5.1. Linked Stack (Conti…)


 For example:
POP()
4000 TOP TOP
newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000

if (TOP == NULL) else


{ {
cout<<“Stack Empty!”; node *newptr=TOP;
} TOP = TOP->LINK;
newptr->DATA;
delete newptr;
} 101
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.1. Linked Stack (Conti…)


 For example:
POP()
3000 TOP TOP
newptr
Jay 2000 Priya 1000 Saumil NULL
3000 2000 1000

if (TOP == NULL) else


{ {
cout<<“Stack Empty!”; node *newptr = TOP;
} TOP = TOP->LINK;
newptr->DATA;
delete newptr;
} 102
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.1. Linked Stack (Conti…)
2. Algorithms for POP operation in dynamic stack:

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

CE: 3.5.1. Linked Stack (Conti…)


But… how to display the stack data using linked list???

4000 TOP TOP temp

newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000

node *temp; Mansi

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

CE: 3.5.1. Linked Stack (Conti…)


But… how to display the stack data using linked list???

4000 TOP TOP temp


newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000

node *temp; Mansi

temp = TOP Jay


Else
while(temp != NULL)
If (TOP == NULL)
{
{
temp->data;
cout<<“Stack is Empty!”;
temp = temp->link;
}
}
105
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.1. Linked Stack (Conti…)


But… how to display the stack data using linked list???

4000 TOP TOP temp


newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000

node *temp; Mansi

temp = TOP Jay


Else Priya
while(temp != NULL)
If (TOP == NULL)
{
{
temp->data;
cout<<“Stack is Empty!”;
temp = temp->link;
}
}
106
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.1. Linked Stack (Conti…)


But… how to display the stack data using linked list???

4000 TOP TOP temp


newptr
Mansi 3000 Jay 2000 Priya 1000 Saumil NULL
4000 3000 2000 1000

node *temp; Mansi

temp = TOP Jay


Else Priya
while(temp != NULL) Saumil
If (TOP == NULL)
{
{
temp->data;
cout<<“Stack is Empty!”;
temp = temp->link;
}
}
107
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
CE: 3.5.1. Linked Stack (Conti…)
3. Algorithms for STATUS operation in dynamic stack:

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

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Enqueue(Saumil, Priya, Jay, Mansi)

1000 Head

newptr
Saumil NULL

1000

node *newptr = new node;


newptr->data = item;
newptr->link = NULL;

111
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Enqueue(Saumil, Priya, Jay, Mansi)
FRONT
1000 Front Rear
newptr
Saumil NULL

1000

node *newptr = new node;


If (front == NULL && rear == NULL)
newptr->data = item;
front=rear=newptr;
newptr->link = NULL;
Else
node *front = NULL rear->link = newptr;
node *rear = NULL rear = newptr;
112
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Enqueue(Saumil, Priya, Jay, Mansi)
FRONT
1000 Front Rear
newptr
Saumil 2000 Priya NULL

1000 2000

node *newptr = new node;


If (front == NULL && rear == NULL)
newptr->data = item;
front=rear=newptr;
newptr->link = NULL;
Else
node *front = NULL rear->link = newptr;
node *rear = NULL rear = newptr;
113
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Enqueue(Saumil, Priya, Jay, Mansi)
FRONT
1000 Front Rear
newptr
Saumil 2000 Priya 3000 Jay NULL

1000 2000 3000

node *newptr = new node;


If (front == NULL && rear == NULL)
newptr->data = item;
front=rear=newptr;
newptr->link = NULL;
Else
node *front = NULL rear->link = newptr;
node *rear = NULL rear = newptr;
114
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Enqueue(Saumil, Priya, Jay, Mansi)
FRONT
1000 Front Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000

node *newptr = new node;


If (front == NULL && rear == NULL)
newptr->data = item;
front=rear=newptr;
newptr->link = NULL;
Else
node *front = NULL rear->link = newptr;
node *rear = NULL rear = newptr;
115
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Dequeue()
FRONT
1000 Front temp Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000

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

CE: 3.5.2. Linked Queue (Conti…)


 For example:
Dequeue()
FRONT
2000 Front Rear
newptr
Priya 3000 Jay 4000 Mansi NULL

2000 3000 4000

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

CE: 3.5.2. Linked Queue (Conti…)


But… how to display the queue data using linked list???
FRONT
1000 Front temp Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000


node *temp; Saumil
temp = front
Else
while(temp != NULL)
If (front == NULL && rear == NULL)
{
{
temp->data;
cout<<“Queue is Empty!”;
temp = temp->link;
}
}
118
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


But… how to display the queue data using linked list???
FRONT
1000 Front temp Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000


node *temp; Saumil Priya
temp = front
Else
while(temp != NULL)
If (front == NULL && rear == NULL)
{
{
temp->data;
cout<<“Queue is Empty!”;
temp = temp->link;
}
}
119
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


But… how to display the queue data using linked list???
FRONT
1000 Front temp Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000


node *temp; Saumil Priya Jay
temp = front
Else
while(temp != NULL)
If (front == NULL && rear == NULL)
{
{
temp->data;
cout<<“Queue is Empty!”;
temp = temp->link;
}
}
120
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Demonstration

CE: 3.5.2. Linked Queue (Conti…)


But… how to display the queue data using linked list???
FRONT
temp
1000 Front Rear
newptr
Saumil 2000 Priya 3000 Jay 4000 Mansi NULL

1000 2000 3000 4000


node *temp; Saumil Priya Jay Mansi
temp = front
Else
while(temp != NULL)
If (front == NULL && rear == NULL)
{
{
temp->data;
cout<<“Queue is Empty!”;
temp = temp->link;
}
}
121
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
Practical Work
Write a C++ program to create a singly linked list in FIFO fashion.

122
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY
123
BHAVIK SARANG | MSC IT | UKA TARSADIA UNIVERSITY

You might also like