ds 3
ds 3
Prepared by
Prof. Nandini K
Dr. Senthil Kumar A
UNIT III - DYNAMIC DATA STRUCTURES:
node
data next
4
Linked List …
■ Let each structure of the list (lets call it node) have two fields:
□ One containing the data
□ The other containing the address of the structure
holding the next data in the list
■ The structures in the linked list need not be contiguous
in memory
□ They are ordered by logical links that are stored as part of
the data in the structure itself
□ The link is a pointer to another structure of the same type
struct node
{
int data;
struct node *next;
}
■ The pointer variable next contains either the address of the
location in memory of the successor list element or the
special value NULL defined as 0
□ NULL is used to denote the end of the list (no successor element)
a.next = &b;
b.next = &c;
Types of Linked
List
• Singly Linked List
• Doubly Linked List
• Circular Linked List
• Circular Doubly Linked List
• Header Linked List
1. Singly Linked List
■ A singly linked list is a data structure consisting of a sequence
of nodes
■ Each node stores
□ data
□ link to the next node
char name[30];
int age;
struct stud *next;
};
■ Suppose the list has three students’ records
■ Declare three nodes n1, n2, and n3
struct stud n1, n2,
Basic operations of Linked List
14
Code for Creating a Singly Linked List
Alternative Way for Creating a Singly Linked List
■ Instead of statically declaring the structures n1, n2, n3,
□ Dynamically allocate space for the nodes
□ Use malloc individually for every node allocated
Algorithm for Creating a Singly Linked List
Algorithm: CREATE (HEAD, ITEM)
1. Create NEW node
a) Allocate memory for NEW node.
b) IF NEW = NULL then Print: “Memory not Available” and Return
c) Set NEW→DATA = ITEM
d) Set NEW→LINK = NULL
2. [Whether List is empty, head is the content of HEADER]
If HEAD = NULL then
Set HEAD = NEW
Else
a) Set Temp = HEAD
b) While Temp→LINK ≠ NULL do
Set Temp = Temp→LINK [End of while]
4. If the head node is not NULL , (Linked list already has some
elements), find the last node.
make the last node => next as the new node.
23
Insertion at Front
Steps to insert node at the beginning of singly linked list
Step 3: Make the new node as the head node, i.e. now head node will point to newNode.
Insertion at front
/*Create a new node and insert at the beginning of the linked list.*/
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = head; //Links the address part
Step 2: Traverse to the last node of the linked list and connect the last node of the list with the
new node, i.e. last node will now point to new node. (lastNode->next = newNode).
Insertion at End
/* Create a new node and insert at the end of the linked list. */
void insertNodeAtEnd(int data)
{
struct node *newNode, *temp;
newNode = (struct node*)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
Step 2: Traverse to the n-1th position of the linked list and connect the new node with the
n+1th node. (newNode->next = temp->next) where temp is the n-1th node.
Single Linked List: Insertion at any position
Step 3: Now at last connect the n-1th node with the new node i.e. the n-1th node will now
point to new node. (temp->next = newNode) where temp is the n-1th node.
Insertion at any Position
/* Create a new node and insert at middle of the linked list.*/
if(newNode == NULL)
{
printf("Unable to allocate memory.");
}
else
{
newNode->data = data; //Links the data part
newNode->next = NULL;
temp = head;
Insertion at any Position
for(i=2; i<=position-1; i++) /* Traverse to the n-1 position */
{
temp = temp->next;
if(temp == NULL)
break;
}
if(temp != NULL)
{
/* Links the address part of new node */
newNode->next = temp->next;
35
Deleting a Node from a Singly Linked List
• There are four cases, which can occur while removing the node. These cases
are similar to the cases in add operation.
- Update next link of the previous node, to point to the next node, relative to
the removed node.
#include<stdio.h> Code for Deleting Front and Last Node
struct Node
{
int data;
struct Node *next; void deleteEnd (struct Node **head)
}; {
struct Node *temp = *head;
void deleteStart (struct Node **head) struct Node *temp2;
{
struct Node *temp = *head; // if there are no nodes in Linked List can't delete
if (*head == NULL)
// if there are no nodes in Linked List can't delete {
if (*head == NULL) printf ("Linked List Empty, nothing to delete");
{ return;
printf ("Linked List Empty, nothing to delete"); }
return;
} // if Linked List has only 1 node
if (temp->next == NULL)
// move head to next node {
*head = (*head)->next; *head = NULL;
free (temp); return;
} }
// else traverse to the last node
while (temp->next != NULL)
{
// store previous link node as we need to change its next val
temp2 = temp;
temp = temp->next;
}
temp2->next = NULL;
free (temp);
}
Circular linked list
Circular list is a list in which the link field of the last node is
made to point to the start/first node of the list.
41
Circular Linked List
In a circular linked list, each node has a data element and a
pointer/reference to the next node in the sequence.
The last node in the list points back to the first node, and the
traversal of the list can continue indefinitely in a loop.
42
Insertion at the Beginning
• store the address of the current first node in the newNode (i.e. pointing
the newNode to the current first node)
43
Insertion at beginning in linked list the steps are followed
• store the address of the head node to next of newNode (making newNode
the last node)
• point the current last node to newNode make newNode as the last node
47
Insertion at end in the Circular Linked List
1.Make a new node.
2.Assign the new node next to circular list.
3.If the list is empty then return new node.
4.Assign the new node next to the front of the list.
5.Assign tail next to the new node.
6.Return the end node of the circular linked list.
48
void insertLast (struct Node **head, int data)
{
struct Node *newNode = (struct Node *) malloc (sizeof (struct
Node));
newNode->data = data;
// if LL already as >=1 node
// if its the first node being struct Node *curr = *head;
entered
// traverse till last node in LL
if (*head == NULL)
while (curr->next != *head)
{
*head = newNode;
{
(*head)->next = curr = curr->next;
*head; return; }
} // assign LL's current last node's next as
this new node
curr->next = newNode;
// assign this new node's next as current
head of LL
newNode->next = *head;
}
49
Insertion in between two nodes
• Let's insert newNode after the first node. travel to the node given
(let this node be p)
50
Insertion in between the nodes in linked list the steps are followed
:-
1.Make a new node and set the data.
2.Move to pos-1 position in the circular linked list.
3. Now link the next pointer of new node with the node
pointed by the next pointer of current(pos-1) node.
4. After that join the next pointer of current node with the
newly created node which means that the next pointer of
current node will point to new node.
5.Now print the linked list.
6.Learn algorithm given below to understand better.
51
void insertPosition (int data, int pos, struct Node
**head)
//function to insert element at specific position
{
struct Node *newnode, *curNode;
int i;
if (*head == NULL) else
{ {
printf ("List is empty"); newnode = (struct Node *) malloc (sizeof (struct
} Node)); newnode->data = data;
if (pos == 1) curNode = *head;
{ while (--pos > 1)
insertStart (head, {
data); return; curNode = curNode->next;
} }
newnode->next = curNode->next;
curNode->next = newnode;
}
}
52
Doubly Linked List
• Doubly linked list is a collection of nodes linked together in a sequential way.
• Doubly linked list is almost similar to singly linked list except it contains two
address or reference fields, where one of the address field contains reference
of the next node and other contains reference of the previous node.
• First and last node of a linked list contains a terminator generally a NULL
value, that determines the start and end of the list.
• Doubly linked list is sometimes also referred as bi-directional linked list since
it allows traversal of nodes in both direction.
• Since doubly linked list allows the traversal of nodes in both direction, we can
keep track of both first and last nodes.
Operations on Doubly Linked List
• Create a new node, say new_node with the given data and set its previous
pointer to null, new_node->prev = NULL.
• Set the next pointer of new_node to current head, new_node->next = head.
• If the linked list is not empty, update the previous pointer of the current head to
new_node, head->prev = new_node.
• Return new_node as the head of the updated linked list.
Insertion at the End of DLL
• Allocate memory for a new node and assign the provided value to its data field.
• Initialize the next pointer of the new node to nullptr.
• If the list is empty:
- Set the previous pointer of the new node to nullptr.
- Update the head pointer to point to the new node.
• If the list is not empty:
- Traverse the list starting from the head to reach the last node.
- Set the next pointer of the last node to point to the new node.
- Set the previous pointer of the new node to point to the last node.
Double Linked List: Insertion at any Position
Steps to insert a new node at nth position in a Doubly linked list.
Step 1: Traverse to N-1 node in the list, where N is the position to insert. Say temp now
points to N-1th node.
Step 2: Create a newNode that is to be inserted and assign some data to its data field.
Doubly Linked List: Insertion at any Position
Step 3: Connect the next address field of newNode with the node pointed by next
address field of temp node.
Step 4: Connect the previous address field of newNode with the temp node.
Doubly Linked List: Insertion at any Position
Step 5: Check if temp.next is not NULL then, connect the previous address field of node
pointed by temp.next to newNode.
int main()
{
int n, data;
head = NULL;
last = NULL;
last = head;
for(i=2; i<=n; i++){ /* Creates and links rest of the n-1 nodes */
newNode = (struct node *)malloc(sizeof(struct node));
printf("Enter data of %d node: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->prev = last; //Links new node with the previous node
newNode->next = NULL;
newNode->data = data;
newNode->next = temp->next; //Connects new node with n+1th node
newNode->prev = temp; //Connects new node with n-1th node
if(temp->next != NULL)
{
temp->next->prev = newNode; /* Connects n+1th node with new node */
}
temp->next = newNode; /* Connects n-1th node with new node */
printf("NODE INSERTED SUCCESSFULLY AT %d POSITION\n", position);
}
else{
printf("Error, Invalid position\n");
}
}
}
Doubly Linked List: Insertion at any Position
void displayList()
{
struct node * temp;
int n = 1;
if(head == NULL)
{
printf("List is empty.\n");
}
else
{
temp = head;
printf("DATA IN THE LIST:\n");
while(temp != NULL)
{
printf("DATA of %d node = %d\n", n, temp->data);
n++;