Unit - 3
Unit - 3
GTU # 3130702
Unit-3
Linear Data Structure
(Linked List)
Dept. of B.Sc.CSIT
[email protected]
+977 – 9844071035
Linked Storage Representation
There are many applications where sequential allocation method is unacceptable because of
following characteristics
Unpredictable storage requirement
Extensive manipulation of stored data
One method of obtaining the address of node is to store address in computer’s main memory,
we refer this addressing mode as pointer of link addressing.
A simple way to represent a linear list is to expand each node to contain a link or pointer to the
next node. This representation is called one-way chain or Singly Linked Linear List.
Linked Storage Representation
NULL
Last Node Value
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
A linked List
The linked allocation method of storage can result in both efficient use of computer storage
and computer time.
A linked list is a non-sequential collection of data items.
Each node is divided into two parts, the first part represents the information of the element and the second
part contains the address of the next mode.
The last node of the list does not have successor node, so null value is stored as the address.
It is possible for a list to have no nodes at all, such a list is called empty list.
Pros & Cons of Linked Allocation
Insertion Operation
We have an n elements in list and it is required to insert a new element between the first and second element,
what to do with sequential allocation & linked allocation?
Insertion operation is more efficient in Linked allocation.
X 1000
2100
Pros & Cons of Linked Allocation
Deletion Operation
Deletion operation is more efficient in Linked Allocation
Join Operation
Join operation is more efficient in Linked Allocation.
Linked list require more memory compared to array because along with value it stores pointer
to next node.
Linked lists are among the simplest and most common data structures. They can be used to
implement other data structures like stacks, queues, and symbolic expressions, etc…
Operations & Type of Linked List
Operations on Linked List Types of Linked List
Insert Singly Linked List
• Insert at first position Circular Linked List
• Insert at last position Doubly Linked List
• Insert into ordered list
Delete
Traverse list (Print list)
Copy linked list
Singly Linked List
Info Link
Accessing Part
Node
of Node
Typical Node Info (Node)
Data Pointer to Link (Node)
Next Node
struct node
{
C Structure to int info;
represent a node struct node *link;
};
Algorithms for singly linked list
1. Insert at first position
2. Insert at last position
3. Insert in Ordered Linked list
4. Delete Element
5. Copy Linked List
Function: INSERT(data, First)
This function inserts a new node at the first position of Singly linked list.
This function returns address of FIRST node.
data is a new element to be inserted.
head is a pointer to the first element of a Singly linked linear list.
Typical node contains data and next fields.
NEWNODE is pointer for new node
Function: INSERT(X,FIRST) Cont…
If(head==null)
{
printf(“linked list is empty”)
}
Else
{
struct node
{
int data;
struct node *head,*newnode;
}
Struct node *head, *newnode;
Newnode=(struct node*)malloc(size of(struct node));
Printf(“enter the data:”);
Scanf(“%d”, &newnode->data);
Newnode->next=head;
Head=newnode;
}
Example: INSERT(50, FIRST)
TEMP
10 50 -1 8 35 44 50
NEW
HEAD
Function: INSORD(X, FIRST)
This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their DATA field.
This function returns address of NEWNODE node.
DATA is a new element to be inserted.
HEAD is a pointer to the first element of a Singly linked linear list.
Typical node contains DATA and NEXT fields.
TEMP is a temporary pointer variable.
Predecessor
5 10 15 20 25 30
HEAD
22
NEWNODE
Function: INSORD(X, FIRST)(after given position)
If(head==null)
{
printf(“linked list is empty”);
}
Else
{
int pos; int i = 1;
struct node
{
int data;
struct node *next;
};
strcut node *head,*newnode,*temp;
printf(“enter the position:”);
scanf(“%d”,&pos);
if(pos>count)
{
printf(“invalid position”);
}
else
{
temp = head;
while(i<pos)
{
temp = temp -> next;
i++;
}
newnode = (struct node*) malloc(size of (struct node));
printf(“enter data:”);
scanf(“%d”, &newnode->data);
newnode -> next = temp->next;
temp->next=newnode;
}
}
Procedure: DELETE(data, FIRST)
This algorithm delete a node whose address is given by variable data.
HEAD is a pointer to the first element of a Singly linked linear list.
Typical node contains DATA and NEXT fields.
TEMP is a temporary pointer variable.
Procedure: del_from_beg( data, head)
If (head == null)
{
printf(“linked list is empty”);
}
Else
{
struct node *temp;
temp = head;
head = head -> next;
free(temp);
}
Function: del_from_end()
LASTNODE is pointer for the node after the deleted node.
HEAD is a pointer to the first element of a Singly linked linear list.
Typical node contains DATA and NEXT fields.
TEMPis a Temporary pointer variable.
Function: del_from_end()
If (head == null)
{
printf(“the linked list is empty”)
}
Else
{
Struct node *temp, *lastnode;
Temp = head;
While(temp->next != 0)
{
Lastnode = temp;
Temp = temp -> next;
}
If(temp==head)
{
head = 0;
}
Else
{
lastnode -> next = 0;
}
Free(temp);
}
Function: del_from_pos()
nextnode is pointer for the node to be deleted
HEAD is a pointer to the first element of a Singly linked linear list.
Typical node contains DATA and NEXT fields.
TEMPis a Temporary pointer variable.
POS is the variable to point the position to delete the node.
Function: del_from_pos()
If(head==null)
{
printf(“linked list is empty”)
}
Else
{
Struct node
{
int data;
struct node *next;
};
Struct node *head, *temp;
del_frm_pos()
{
struct node *nextnode;
int pos,i=1;
temp=head;
Printf(“enter position”);
scanf(“%d”,&pos);
while(i<pos-1)
{
temp = temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
}
Circularly Linked Linear List
If we replace NULL pointer of the last node of Singly Linked Linear List with the address of its
first node, that list becomes circularly linked linear list or Circular List.
FIRST is the address of first node of Circular List
LAST is the address of the last node of Circular List
Advantages of Circular List
In circular list, every node is accessible from given node
It saves time when we have to go to the first node from the last node. It can be done in single step because
there is no need to traverse the in between nodes. But in double linked list, we will have to go through in
between nodes LAST
5 10 15 20 25 30
FIRST
Circularly Linked Linear List Cont…
Disadvantages of Circular List
It is not easy to reverse the linked list.
If proper care is not taken, then the problem of infinite loop can occur.
If we at a node and go back to the previous node, then we can not do it in single step. Instead we have to
complete the entire circle by going through the in between nodes and then we will reach the required node.
50
TEMP 5 10 1 -5
HEAD
Procedure: CIR_INS_LAST(DATA,HEAD,LAST)
This procedure inserts a new node at the last position of Circular linked list.
DATA is a new element to be inserted.
HEAD and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains DATA and NEXT fields.
TEMP is a temporary pointer variable.
Procedure: CIR_INS_LAST( DATA,HEAD,LAST)
1. [Creates a new empty node] IF HEAD = NULL
TEMP NODE THEN NEXT (TEMP) TEMP
2. [Initialize fields of new node HEAD LAST TEMP
and its link] ELSE NEXT (TEMP) HEAD
DATA (TEMP) DATA NEXT (LAST) TEMP
LAST TEMP
Return
LAST
TEMP
HEAD LAST
50
LAST
50
TEMP
5 10 1 -5
HEAD
Procedure: CIR_INS_ORD(X,FIRST,LAST)
This function inserts a new node such that circular linked list preserves the ordering of the
terms in increasing order of their DATA field.
This function returns address of NEWNODE node.
DATA is a new element to be inserted.
HEAD is a pointer to the first element of a circular linked linear list.
Typical node contains DATA and NEXT fields.
TEMP is a temporary pointer variable.
Function: INSORD(DATA, FIRST)
If(head==null)
{
printf(“circular linked list is empty”);
}
Else
{
int pos; int i = 1;
struct node
{
int data;
struct node *next;
};
strcut node *head,*newnode,*temp;
printf(“enter the position:”);
scanf(“%d”,&pos);
if(pos>count||pos<0)
{
printf(“invalid position”);
}
else
{
temp = head -> next;
while(i<pos)
{
temp = temp -> next;
i++;
}
newnode = (struct node*) malloc(size of (struct node));
printf(“enter data:”);
scanf(“%d”, &newnode->data);
newnode -> next = temp->next;
temp->next=newnode;
}
}
Procedure: CIR_DELETE(DATA,DATA,LAST)
This algorithm delete a node whose address is given by variable DATA.
DATA & LAST are pointers to the First & Last elements of a Circular linked list, respectively.
Typical node contains DATA and NEXT fields.
TEMP is a temporary pointer variable.
Procedure: CIR_Del_begining(DATA,HEAD,LAST)
If(head==null)
{
printf(“circular linked
list is empty”);
}
Else
{
Temp=head;
Head=head->next;
Tail->next=head;
Temp->next=null;
Free(*temp);
}
Procedure: CIR_Del_last(DATA,HEAD,LAST)
If(head==null)
{
printf(“circular linked list is empty”);
}
Else
{
Temp=head;
While(temp->next!=tail)
{
temp=temp->next;
}
tail->next=null;
lastnode=tail;
temp->next=head;
tail=temp;
free(lastnode);
}
Procedure: CIR_del_specific_pos()
This procedure delete a node from the specific position of Circular linked list with Head node.
nextnode is a pointer for the node to be delete.
HEAD and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
Typical node contains Data and next fields.
HEAD is pointer pointing to Head node of Linked List.
temp and nextnode is a temporary pointer variable.
Procedure: CIR_del_specific_pos()
Struct node *temp, *nextnode;
Int pos, i=1, l;
Temp= head;
Printf(“enter the position”);
Scanf(“%d”,&pos);
l=getlenght();
If(pos<1||pos>l)
{
printf(“invalid position”);
}
Elseif(pos==1)
{
CIR_Del_begining();
}
Else
{
while(i<=pos-1)
{
temp=temp->next;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
tail=temp;
nextnode->next=null;
free(nextnode);
}
Doubly Linked Linear List
In certain Applications, it is very desirable that a list be traversed in either forward or reverse
direction.
This property implies that each node must contain two link fields instead of usual one.
The links are used to denote Predecessor and Successor of node.
The link denoting its predecessor is called Left Link.
The link denoting its successor is called Right Link.
A list containing this type of node is called doubly linked list or two way chain.
Doubly Linked Linear List
Typical node of doubly linked linear list contains INFO, LPTR RPTR Fields
LPTR is pointer variable pointing to Predecessor of a node
RPTR is pointer variable pointing to Successor of a node
Left most node of doubly linked linear list is called L, LPTR of node L is always NULL
Right most node of doubly linked linear list is called R, RPTR of node R is always NULL
Typical node of
Doubly Linked List L Doubly linked linear list R
Insert node in Doubly Linked List
Insertion in the middle of Doubly Linked Linear List
Before Insertion temp
L R
LPTR(NEW) LPTR(temp)
NEW RPTR(NEW) temp
LPTR(temp) NEW
RPTR(LPTR(NEW)) NEW
After Insertion
temp
L R
NEW
Insert node in Doubly Linked List
Left most insertion in Doubly Linked Linear List
Before Insertion
temp
L R
LPTR(NEW) NULL
RPTR(NEW) temp
NEW LPTR(temp) NEW
L NEW
L
L R
NEW
Procedure: DOU_INS (L,R,M,X)
This algorithm inserts a new node in doubly linked linear list.
The insertion is to be performed to the left of a specific node with its address given by the
pointer variable temp.
Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
LPTR is pointer variable pointing to Predecessor of a node.
RPTR is pointer variable pointing to Successor of a node.
L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
NEW is the address of New Node.
data is value to be inserted.
Procedure: DOU_INS (L,R,M,X)
1. [Create New Empty Node] 4. [Is left most insertion ?]
NEW NODE IF temp = L
2. [Copy information field] THEN LPTR(NEW) NULL
INFO(NEW) data RPTR(NEW) temp
3. [Insert into an empty list] LPTR(temp) NEW
IF R = NULL L NEW
THEN LPTR(NEW) NULL Return
RPTR(NEW) NULL 5. [Insert in middle]
L R NEW LPTR(NEW) LPTR(temp)
Return RPTR(NEW) temp
LPTR(temp) NEW
RPTR(LPTR(NEW)) NEW
Return
PROCEDURE: DOU _DEL (L, R, OLD)
This algorithm deletes the node whose address is contained in the variable OLD.
Typical node of doubly linked list contains following fields LPTR, RPTR and INFO.
LPTR is pointer variable pointing to Predecessor of a node.
RPTR is pointer variable pointing to Successor of a node.
L & R are pointer variables pointing for Leftmost and Rightmost node of Linked List.
Delete from Doubly Linked List
10 L R NULL
OLD
L R
L L R R