0% found this document useful (0 votes)
61 views

Linked List - For - Student

Here are the steps to insert a new node with value 4009 after node containing 6011 in the doubly linked list: 1. Traverse the list and find the node containing 6011. Let's call this node R. 2. Create a new node N with data 4009 and left and right pointers as NULL. 3. Set N's right pointer to R's right pointer. 4. Set R's right pointer to N. 5. Set N's left pointer to R. 6. Set the left pointer of the node pointed by R's original right pointer to N. So the updated doubly linked list would be: NULL 6 2009 4006 12

Uploaded by

shail
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Linked List - For - Student

Here are the steps to insert a new node with value 4009 after node containing 6011 in the doubly linked list: 1. Traverse the list and find the node containing 6011. Let's call this node R. 2. Create a new node N with data 4009 and left and right pointers as NULL. 3. Set N's right pointer to R's right pointer. 4. Set R's right pointer to N. 5. Set N's left pointer to R. 6. Set the left pointer of the node pointed by R's original right pointer to N. So the updated doubly linked list would be: NULL 6 2009 4006 12

Uploaded by

shail
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Linked List

Linked List
• A Linked List is a Linear Data Structure.
• A linked data structure consists of elements that are linked to other
element.
• How? each element points to another element
• A linked list is a linear data structure, in which the elements are not
stored at contiguous memory locations. The elements in a linked list
are linked using pointers.
Linked List
• A linked list is represented by a pointer to the first node of the linked
list. The first node is called head. If the linked list is empty, then value
of head is NULL.
• Each node in a list consists of at least two parts:
1) Data/Info
2) Pointer/Link/Reference to the next node
Types of Linked List
• Singly linked list: each element points to the next element
• Doubly linked list: each element points to the next element and to
the previous element
• Circular Singly linked list
• Circular Doubly linked list
Singly Linked List
head pointer "defines" the linked list
head (note that it is not a node)

these are nodes

data data data .

4-5
Traversing the linked list

First

1) Save = First
2) While (Save != NULL)
PRINT (INFO(Save))
Save = Link(Save)
3) Return
Why Linked List?
Arrays can be used to store linear data of similar types, but arrays have following
limitations.
1) The size of the arrays is fixed. (static)
2) Insertion and Deletion in an array of elements is expensive.
For example, in a system if we maintain a sorted list of IDs in an array id[].
• id[] = [1000, 1010, 1050, 2000, 2040].
• And if we want to insert a new ID 1005, then to maintain the sorted order, we
have to move all the elements after 1000 (excluding 1000).
Deletion is also expensive with arrays until unless some special techniques are
used. For example, to delete 1010 in id[], everything after 1010 has to be moved.
Why Linked List?
• Advantages over arrays
1) Dynamic size
2) Ease of insertion/deletion
• Drawbacks:
1) Random access is not allowed. We have to access elements
sequentially starting from the first node. So we cannot do binary
search with linked lists.
2) Extra memory space for a pointer is required with each element of
the list.
Insert Node at Front
INSERT_FRONT(X, First)
1. Create New Node
INFO(New) = X
LINK(New) = NULL
2. LINK(New) = First
3. First = New
4. Return First
Insert Node at End
INSERT_END(X, First)
1. Create New Node
INFO(New) = X
LINK(New) = NULL
2. IF First = = NULL
First = New
3. Save = First
While (LINK(Save) != NULL)
Save = LINK(Save)
4. LINK(Save) = New
5. Return First
Linked Lists
• Search element X
• Count number of nodes
• Insert after X
• Insert before X
• Print nth node of the linked list
• Print nth node from end of the linked list
• Delete X
• Delete after X
• Delete before X
• Implement Dynamic Stack and Dynamic Queue
Search(X, First)
1) If (First = = NULL) 1) If (First = = NULL)
PRINT (“Empty Linked List”) PRINT (“Empty Linked List”)
Return Return
2) Temp = First 2) Temp = First
While (INFO(Temp) != X && LINK(Temp) != NULL) While (Temp != NULL)
Temp = LINK(Temp) if(INFO(Temp) == X)
3) If (INFO(Temp) = = X) PRINT (“X Found”)
PRINT (“X Found”) Return
else Temp = LINK(Temp)
PRINT (“X Not Found”) 3) PRINT (“X Not Found”)
4) Return 4) Return
Insert_After_X(X, Y, First)
1. Create New Node
INFO(New) = Y
LINK(New) = NULL
2. IF (First = = NULL)
PRINT (“Empty Linked List”)
Return
3. Temp = First
While (Temp != NULL)
IF(INFO(Temp) == X)
LINK(New) = LINK(Temp)
LINK(Temp) = New
Return
Temp = LINK(Temp)
4. PRINT(“X Not Found”)
5. Return
Insert_Before_X(X, Y, First)
1. Create New Node 4. Temp = First
INFO(New) = Y Pre = NULL
LINK(New) = NULL
While (Temp != NULL)
2. IF (First = = NULL)
if(INFO(Temp) == X)
PRINT (“Empty Linked List”)
LINK(New) = Temp
Return
3. IF(INFO (First) = = X)
LINK(Pre) = New
LINK(New) = First Pre = Temp
First = New Temp = LINK(Temp)
Return 5. Return
Delete(X, First)
1. IF (First = = NULL) 4. If (INFO(Temp) != X)
PRINT (“Empty Linked List”) PRINT(“X Not Found”)
Return else
2. If (INFO(First) = = X)
LINK(Pre) = LINK(Temp)
First = LINK(First);
5. Return
Return
3. Temp = First
While (INFO(Temp) != X && LINK(Temp) != NULL)
Pre = Temp
Temp = LINK(Temp)
Delete(X, First)
1. IF (First = = NULL) 4. While (Temp != NULL)
PRINT (“Empty Linked List”) if(INFO(Temp) ==X)
Return LINK(Pre) = LINK(Temp)
2. If (INFO(First) = = X)
Return
First = LINK(First);
Pre = Temp
Return
3. Pre = First Temp = LINK(Temp)
Temp = Link(First) 5. Return
Print nth node of the linked list
1) len = 1
2) Temp =First
While(Temp != NULL)
if(len == n)
PRINT(INFO(Temp)
Return
len++
Temp = LINK(Temp)
3) Return
Print nth node from end of the linked list
1) len =0
2) Temp =First
While(Temp != NULL)
len++;
Temp = LINK(Temp)
3) If(len < n)
Return
4) Temp = First
For (i=1; I < len-n+1; i++)
Temp = LINK(Temp)
5) PRINT(INFO(TEMP))
6) Return
Reverse Singly Linked List : Reverse(First)
1. Current = First, Pre = NULL, Next = NULL
2. While (Current != NULL)
Next = LINK(Current)
LINK(current) = Pre
Pre = Current
Current= Next
3. First = Pre

https://www.geeksforgeeks.org/reverse-a-linked-list/
Example :1
Example :2
Example :3
Insert_Sorted(X, First)
1. Create New Node
4. save = First
INFO(New) = X
LINK(New) = NULL While (LINK(save)!= NULL &&
2. IF (First = = NULL) INFO(LINK(save) ) <= INFO(New))
First = New save = LINK(save)
Return 5. LINK(New) = LINK(save)
3. IF(INFO (New) < = INFO(First)) LINK(Save) = New
LINK(New) = First
First = New
6. Return First
Return
Implementation of Singly Linked List in C
Structure for the Node

struct node
{
int data; // data part or information
struct node *link; //pointer to the next node
};
struct node *first =NULL; //Initially Linked List is empty.

printf("\nEnter value which u want to insert :");


scanf("%d",&n);

insert(n); //front insertion


Insert Node at Front
void insert(int x)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=x;
temp->link=NULL;
temp->link=first;
first = temp;

}
Linked List Traversal
void display()
{
struct node *trav=first;
while(trav!=NULL)
{
printf("%d|%u-->",trav->info, trav->link);

trav=trav->link;
}
}
Insert Node at End
void insert_end(int x)
{ struct node *temp, *save;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=x;
temp->link=NULL;
save=first;
while((save->link) != NULL)
save = save->link;
save->link =temp;
}
Doubly Linked List
• A Doubly Linked List (DLL) contains an extra pointer, typically
called previous pointer, together with next pointer and data which are
there in singly linked list.

33
Advantages over singly linked list
1) A DLL can be traversed in both forward and backward direction.
2) The delete operation in DLL is more efficient if pointer to the node to
be deleted is given.
3) We can quickly insert a new node before a given node.

Disadvantages over singly linked list


1) Every node of DLL Require extra space( one to maintain address of left
node and one to maintain address of right node)

34
SINGLY LINKED LIST (SLL) DOUBLY LINKED LIST (DLL)

SLL has nodes with only a data field and next DLL has nodes with a data field, a previous link
link field. field and a next link field.

In SLL, the traversal can be done using the In DLL, the traversal can be done using the
next node link only. previous node link or the next node link.

The SLL occupies less memory than DLL as it The DLL occupies more memory than SLL as it
has only 2 fields. has 3 fields.

Less efficient access to elements. More efficient access to elements.


Doubly Linked List : Example 1
L=4006 R=6011
NULL 6 2009 4006 12 6011 2009 8 NULL
4006 2009 6011

For the given linked list, if M contains 6011, insert a new node before M
with data 11 and node address 2002. Write all steps and Draw the final
linked list with all details.
Circular Singly Linked List
Circular linked list is a linked list where all nodes are connected to
form a circle. There is no NULL at the end. A circular linked list can be
a singly circular linked list or doubly circular linked list.
Circular Singly Linked List
head
Advantages  of Circular Linked Lists:
1) Any node can be a starting point. We can traverse the whole list by
starting from any point. We just need to stop when the first visited
node is visited again.
2) Useful for implementation of queue. We don’t need to maintain two
pointers for front and rear if we use circular linked list. We can
maintain a pointer to the last inserted node and front can always be
obtained as next of last.
3) Circular lists are useful in applications to repeatedly go around the
list. For example, when multiple applications are running on a PC, it is
common for the operating system to put the running applications on a
list and then to cycle through them, giving each of them a slice of time
to execute, and then making them wait while the CPU is given to
another application. It is convenient for the operating system to use a
circular list so that when it reaches the end of the list it can cycle
around to the front of the list.
Circular Doubly Linked List
Find Output
struct Node {
    int data;
    struct Node* next; };
 void print(struct Node* head)
{
    int count = 0;
     while (head != NULL)
{
      if (count % 2 == 0) 
             printf(" %d ", head->data);
         count++;
                head = head->next;
    }
}
You are given pointers to first and last nodes of a singly linked
list, which of the following operations are dependent on the
length of the linked list?

(A) Delete the first element


(B) Insert a new element as a first element
(C) Delete the last element of the list
(D) Add a new element at the end of the list
• Answer: (C) 
A doubly linked list is declared as
struct Node
{
int Value;
struct Node *Fwd;
struct Node *Bwd;
};
Where Fwd and Bwd represent forward and backward link to the adjacent elements of the list.
Which of the following segments of code deletes the node pointed to by X from the doubly linked
list, if it is assumed that X points to neither the first nor the last node of the list? (GATE 2018)

(A) X->Bwd->Fwd = X->Fwd; X->Fwd->Bwd = X->Bwd ;


(B) X->Bwd->Fwd = X->Bwd ; X->Fwd->Bwd = X->Fwd;

Answer: (A)
Write steps for following
• Print alternate nodes of the doubly linked list.
• Count number of nodes in Circular Singly Linked List
• Delete all the even value nodes from the given Doubly Linked List
• Print sum of the nodes in a given Circular Linked List
• Exchange first and last node in a doubly linked list
Exchange first and last node in a doubly
linked list
Temp = LPTR(R)
Current = R
RPTR(Temp) = L
LPTR(L) = Temp
RPTR(Current) = RPTR(L)
LPTR(RPTR(L)) =current
RPTR(L) =NULL
LPTR(R) = NULL
L =Current
R= RPTR(Temp)
Memory Representation of Linked List

You might also like