R23 - DS - Unit II-1
R23 - DS - Unit II-1
Linked Lists: Singly linked lists: representation and operations, doubly linked lists and
circular linked lists, comparing arrays and linked lists, Applications of linked lists.
1. Array representation: In this representation a singly linked list is represented by using two
dimensional arrays. When a singly linked list can be represented using an array then each
element of the array represents a node. Each element would contain the data and an index would
represent the link to the next node. However, it may require resizing the array when adding or
removing elements, but that can be inefficient. Consider the following example, where the word
“HELLO” is stored as a singly linked list as follows
2. Linked Representation: This is the most common representation of a singly linked list. In
this representation, each element of the list, called a node, consists of two parts, i.e the data and a
pointer. The data field is used to hold the value associated with the node, and a pointer (or
reference) is used to link the next node in the sequence. Each node is an instance of a node class
or structure. This representation becomes an efficient representation because we can perform
insertion and deletion of nodes without resizing the list. In this representation a SLL is shown as
Start pointer stores the address of the first node in the list.
The last node is not having the next node, so the address field of last node is NULL,
c) Insertion at the specific location of the list: we can use the following steps to insert a node
at specific location of the list.
Step 1: Create a new node with given value
Step 2: Check whether list is empty or not
Step 3: If it is empty then node -> next = NULL and set Start = node
Step 4: If it is not empty then set temp = Start
Step 5: Keep moving temp to its next node until it reaches node after which we want to
insert new node. Every time check whether temp reaches last node or not. If it is
reached last node then display “given node is not found to insert”.
Step 6: Finally, set node -> next = temp -> next and temp -> next = node.
This operation can be implemented by using the following function in C language
void addatp()
{
NODE *p=h,*n;
int x;
n=(NODE *)malloc(sizeof(NODE));
printf("\nEnter data to insert ");
scanf("%d",&n->data);
printf("\nEnter data after which to insert ");
scanf("%d",&x);
while(p->data!=x)
p=p->next;
n->next=p->next;
p->next=n;
}
2. Deletion: We can perform this operation in 3 ways
a) Deletion of beginning node of list
b) Deletion of the end node of the list
M. Purnachandra Rao, Assoc. Prof., Dept. of IT, KITS Page 3
c) Deletion of specific node of the list
a) Deletion of beginning node: we can use the following steps to delete a node at beginning of
the linked list
Step 1: Check whether list is empty or not.
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 3: If it is not empty then set temp = Start and Start = Start -> next.
Step 4: free(temp)
This operation can be implemented by using the following function in C language
void delatb()
{
NODE *t;
if(h==NULL)
printf("List is empty");
else
{
t=h;
h=t->next;
free(t);
}
}
b) Deletion of the end node: we can use the following steps to delete a node at end of list
Step 1: Check whether the list is empty or not
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 3: If it is not empty then set temp = Start
Step 4: Keep moving temp to its next node until reaches last but one node,
i.e temp -> next -> next ==NULL
Step 5: Check whether the list contains nodes, if not then set Start = NULL
Step 6: Set t = temp -> next and Set temp -> next = NULL.
Step 7: free(t)
This operation can be implemented by using the following function in C language
void delate()
{
NODE *p=h,*t;
if(p==NULL)
printf("\nList is empty");
else if(p->next==NULL)
h=NULL;
else
while(p->next->next!=NULL)
p=p->next;
t=p->next;
p->next=NULL;
free(t);
}
c) Deletion of specific node: we can use the following steps to delete a specific node of the
linked list
Step 1: Check whether list is empty or not
Step 2: If it is empty then display that “List is empty and deletion is not possible”
Step 4: If it is not empty then set temp1 = Start
Step 5: Keep moving temp1 to its next node until it reaches node which we want to
delete. Every time set temp2 = temp1 before moving temp1 to its next node.
Step 6: Every time check whether temp reaches last node or not. If it is reached last node
Representation of Doubly Linked List in Memory: similar to SLL, a DLL is also represented
by using 2 ways like
1. Array Representation
2. Linked List Representation
Operations on Doubly linked list: we can perform the following operations on this list.
1. Insertion
2. Deletion
3. Traversing
1. Insertion: It can be performed in 3 ways
a) Insertion at beginning of list
b) Insertion at end of the list
c) Insertion at specific location of the list
a) Insertion at beginning: we can use following steps to insert node at beginning of list
This sparse matrix can be represented using linked list for every row and column. Because a
value is in exactly one row and one column, it will appear in both lists exactly once. The
following is the multi linked list representation of above sparse matrix.