DS. Unit II
DS. Unit II
Linked Lists : Introduction to Lists and Linked Lists, Basic Linked List Operations, Doubly
Linked List, Circular Linked List, Linked List versus Arrays.
LINKED LISTS
A linked list or one way list is a liner collection of data elements, called nodes where the linear
order is given by means of references. That is each node is divided into to parts the first part
contains the information of the element and the second part called the link field or next
reference field, contains the address of the next node in the list.
Node
The reference of the last node contains a special value called “NULL” reference
denoted by “ X ” in the diagram signals the end of the list. The linked list also contains a
pointer variable called “START” which refer to the first or head node in the list .
A special case is the list that has no nodes, such list is called the “NULL” list or empty
list and is denoted by null reference in the start variable.
6. item = 20
7. Since, item != -1 create a new node called temp.
10. item=30
11. Since, item != -1 create a new node called temp.
14. item = -1
15. since item = -1, we will stop the process.
Traversing a Linked List
Visiting each and every node in the list is called traversing.
Algorithm Traverse( )
Step-1 : set temp = head
Step-2 : while temp!=null
Step-2.1 : display temp.data
Step-2.2 : temp=temp.link
Step-3 : Exit
Implementation:
Let us consider the following linked list with four nodes.
Implementation:
Let us consider the following linked list with four nodes.
9. Now temp=temp.link
10. Since temp=null, the process stops here.
Inserting
Adding a new node to the existing list is called as inserting. We can insert a node in the
followingthree places.
1. Insertion at head
2. Insertion at last
3. Insertion in between any two nodes.
1. Insertion at head:
If the node is inserted before the head node that node becomes new head of the list.
Algorithm: Insert-At-Head( )
Step-1: Create a new node called temp.
Step-2: Read a value into item.
Step-3: Insert temp.data=item
temp.link=null
Step-4: temp.link=head
head=temp
Step-5: Exit
Implementation:
Let us consider the following the list with four nodes.
3. temp.link=headhead=temp
2. Insertion at Last
Algorithm insertAtLast( )
Step-1: Create a new node called “nodex”
Step-2: Read a value into item.
Step-3: Insert nodex.data=item
nodex.link=null
Step-4: set temp=head
step-4.1: while(temp!=null)
temp1=temp
temp=temp.link
step-4.2: if(temp=null)
temp1.link=nodex
last=nodex
Step-5: Exit
Implementation
Let us consider the following the list with four nodes.
1. create a new node called “nodex”.
2. item=50
3. nodex.data=item , nodex.link=null
4. temp=head
Algorithm insertAtMiddle( )
Step-1: Create a new node called “nodex”
Step-2: Read a value into item.
Step-3: Insert nodex.data=item
nodex.link=null
Step-4: Read place to insert into place.
Step-5: set temp=head , i=1
step-5.1: while(i<place)
temp1=temp
temp=temp.linki=i+1
step-5.2: temp1.link=nodex
nodex.link=temp.
Step-6: Exit
Implementation
Let us consider the following the list with four nodes.
2. item=25
3. nodex.data=item and nodex.link=null
4. place=3
5. set i=1 and temp=head
Implementation
Let us consider the following list with four nodes.
1. head = head.link
Implementation
Let us consider the following list with four nodes.
1. temp = head
Algorithm deleteMiddle( )
Step-1: Read place to delete into “place”
Step-2: set i=1, temp = head
Step-3: while ( i<place)
temp1 = temp
temp = temp.linki = i + 1
Step-4: temp1.link = temp.link
Step-5: Exit
Implementation
Let us consider the following list with four nodes.
1. place = 3
2. i=1 and temp = head
Algorithm search( )
Step-1: set flag=0
Step-2: Read search value into “key”
Step-3: set temp=head
Step-4: while ( temp!=null )if( temp.data = key )
flag=1
break the looptemp = temp.link
Step-5: if ( flag = 1) then
Display “FOUND”
Else
Display “NOT FOUND”
Step-6: Exit
Implementation
Let us consider the following list with four nodes.
1. flag = 0
2. key = 30
3. temp = head
6. since temp != null and temp.data = 30 so flag = 1 and loop stops here.
7. Since flag = 1 we conclude that the element was found.
DOUBLE LINKED LIST:
In Double Linked List, each node contains two reference parts. One reference is used to
remember the previous node address and another reference is used to remember the next
node address. The node in double linked list is represented as follows.
Node
Plink Data Nlink
❖ Plink is used to refer the previous node.
❖ Data part contains value of the node.
❖ Nlink is used to refer the next node.
In Double linked list, we need to remember two nodes, one is the first node called “head
node”. The Plink of the head node points to NULL. Another one is last node. The Nlink of
the last node points to NULL. The following is doublelinked list with four nodes.
7. item=20
8. since item!= -1, so
9. create a new node called temp
11. since head!= NULL ; so, last.nlink = temp ; temp.plink = last ; last = temp
12. item=30
13. since item != -1, so
14. create a new node called temp
16. since head != NULL. So, last.nlink = temp ; temp.plink = last ; temp = last
17. item = -1
18. since item = -1, so the process stops.
TRAVERSING A DOUBLE LINKED LIST
Traversing means visiting each and every node. A double linked list can be traversed in two
ways. They are
1. forward direction – from head to last
2. backward direction – from last to head
Implementation:
1. let us consider the following list with four nodes.
1. Inserting at head
If a node is inserted before a head node that node becomes the new head of the list.
Algorithm insertAtHead( )
Step1: Create a new node called “temp”
Step2: Read a value into “item”
Step3: Insert,
temp.data = item
temp.nlink = NULL
temp.plink = NULL
Step4: temp.nlink = head
head.plink = temp
head = temp
Step5: Exit
Implementation:
1. Let us consider the list with following 4 nodes.
3. Item = 5
2. Inserting at Last:
Algorithm insertAtLast( )
Step1: Create a new node called “temp”.
Step2: Read a value into “item”
Step3: Insert, temp.data = item
temp.plink = NULL
temp.nlink = NULL
Step4: last.nlink = temp
temp.plink = last
last = temp
Step5: Exit
Implementation
1. Let us consider the list with following 4 nodes.
3. Item = 50
4. temp.data= item ; temp.nlink = NULL ; temp.plink = NULL
Implementation
1. Let us consider the following lists with four nodes.
3. item=25
4. nodex.data = item , nodex.nlink = NULL , nodex.plink = NULL
5. place = 3
6. set i=1, temp = head
Implementation
2. head = head.nlink
Implementation
1. let us consider the following list of 4 nodes.
2. last = last.plink
3. last.nlink = NULL
Implementation
1. Let us consider the following list with 4 nodes.
2. Place = 3
3. i = 1 , temp = head
4. a. since, 1<=2. So, temp1 = temp , temp = temp.nlink , i = i + 1
5. temp = temp.nlink
If it is a double linked list, the nlink of the last node refers to the head node and the plink
of the head node refers to the last node. The following is circular double linked with five
nodes.
1. Triplet Representation
In this representation, we consider only non-zero values along with their row and
column index values. In this representation, the 0th row stores total rows, total
columns and total non- zero values in the matrix. For example, consider a matrix of size
5 X 6 containing 6 number of non- zero values. This matrix can be represented as
2. Linked Representation
In linked representation, we use linked list data structure to represent a sparse matrix.
In this linked list, we use two different nodes namely header node and element node.
Header node consists of three fields and element node consists of five fields as shown
in the image...
Consider the above same sparse matrix used in the Triplet representation. This sparse
matrix can be represented using linked representation as shown in the below image...
In above representation, H0, H1,...,H5 indicates the header nodes which are used to
represent indexes. Remaining nodes are used to represent non-zero elements in the matrix,
except the very first node which is used to represent abstract information of the sparse matrix
(i.e., It is a matrix of 5 X 6 with 6 non-zero elements).
In this representation, in each row and column, the last node right field points to it's
respective header node.
LAB PROGRAMS
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e;
Position next;
};
void Insert(int x, List l, Position p)
{
Position TmpCell;
TmpCell = (struct Node*) malloc(sizeof(struct Node));
if(TmpCell == NULL)
printf("Memory out of space\n");
else
{
TmpCell->e = x;
TmpCell->next = p->next;
p->next = TmpCell;
}
}
int isLast(Position p)
{
return (p->next == NULL);
}
if(!isLast(p))
{
TmpCell = p->next;
p->next = TmpCell->next;
free(TmpCell);
}
else
printf("Element does not exist!!!\n");
}
void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}
int main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->next = NULL;
List p = l;
printf("LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. MERGE\t 4. PRINT\t 5. QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:
p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;
case 3:
l1 = (struct Node *) malloc(sizeof(struct Node));
l1->next = NULL;
Merge(l, l1);
break;
case 4:
Display(l);
break;
}
}
while(ch<5);
return 0;
}
OUTPUT
#include<stdio.h>
#include<stdlib.h>
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
struct Node
{
int e;
Position previous;
Position next;
};
int isLast(Position p)
{
return (p->next == NULL);
}
void Display(List l)
{
printf("The list element are :: ");
Position p = l->next;
while(p != NULL)
{
printf("%d -> ", p->e);
p = p->next;
}
}
void main()
{
int x, pos, ch, i;
List l, l1;
l = (struct Node *) malloc(sizeof(struct Node));
l->previous = NULL;
l->next = NULL;
List p = l;
printf("DOUBLY LINKED LIST IMPLEMENTATION OF LIST ADT\n\n");
do
{
printf("\n\n1. INSERT\t 2. DELETE\t 3. FIND\t 4. PRINT\t 5. QUIT\n\nEnter the choice :: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = l;
printf("Enter the element to be inserted :: ");
scanf("%d",&x);
printf("Enter the position of the element :: ");
scanf("%d",&pos);
for(i = 1; i < pos; i++)
{
p = p->next;
}
Insert(x,l,p);
break;
case 2:
p = l;
printf("Enter the element to be deleted :: ");
scanf("%d",&x);
Delete(x,p);
break;
case 3:
p = l;
printf("Enter the element to be searched :: ");
scanf("%d",&x);
p = Find(x,p);
if(p == NULL)
printf("Element does not exist!!!\n");
else
printf("Element exist!!!\n");
break;
case 4:
Display(l);
break;
}
}
while(ch<5);
}
OUTPUT
Unit II
IMPORTANT QUESTIONS
1.What is linked list ? Explain about arrays and linked lists ?
2.Write about the types of linked lists ?
3.Explain the operations on Single Linked Lists ?
4.Explain the operations on Doubly Linked Lists ?
5.Explain the differences between Arrays and Linked Lists ?
6.Write a program on single linked list ?
7.Write a program on Doubly Linked list ?