UNIT - 4::: TOPIC Link List
UNIT - 4::: TOPIC Link List
Introduction:
Same as an array, we can use linked list to store similar type of elements in memory. An
array is simple to understand and elements of an array are easily accessible. But there
are some limitations of an array as follows:
Limitations of an Array:
Size is fixed: Once the size of an array is decided it cannot be increased or decreased
during execution. So, array is known as Static Data Structure.
Array Items are stored contiguously: The array elements are stored in sequential
manner. That means it stores data in computer memory in contiguous memory location.
Operations like insertion of a new element in an array or deletion of an existing form the
array are complex and tedious.
Page 1
:: UNIT – 4 ::
Linked List:
A linked list is a collection of nodes with various fields. It contains Data Field and
Address Field. It is a linear type data structure. The Linked list is defined as an ordered
collection of homogenous data elements. The each element of linked list is known as
NODE. The linked list overcomes all these disadvantages. A linked list can grow and reduce in
size during its lifetime. For this reason linked list is known as Dynamic Data Structure. That
means there is no maximum size of linked list. The advantages of linked list are:
Size is not fixed.
Data can be stored at any place.
Insertions and deletions are simple and faster.
The Linked List types are:
Singly Linked List
Circular Singly Linked List
Doubly Linked List
Circular Doubly Linked List
Singly Linked List:
The singly linked list is the most basic of all the linked data structures.
A sequence of dynamically allocated nodes (items/objects) is known as singly
linked list.
Each node refers to its successor in the list.
A singly linked list is a collection of nodes and each node contains a pointer to
the next element.
In singly linked list, we can move forward only. That means we can move to the
next node but return back is not possible.
The node can be of integer, character, float, structure etc.
Each node in list contains two parts: Information Part and Pointer to Next Node.
Page 2
:: UNIT – 4 ::
The linked list can be defined as:
struct node
{
int data; // The Information Part
struct node *next; // Pointer to next node.
} *start;
We can describe the above definition of structure (node) in graphical way as follow:
data *next
Here, the data is used to store the information or actual value and *next stores the
address of the successor (next) node in the list. The *next is used to maintain the link of the
node with another node. The following figure shows the linked list which stores the marks
obtained by the students:
A linked list also contains a list pointer variable called “start” which contains the address of the
first node in the list. The is used to link between the two nodes. If there is no node in the
list, the list is called NULL list or empty list.
Advantages:
o Memory efficiency is very high.
o Insertion and deletion is very easy and fast.
Disadvantages:
o Traversal is very slow.
o The singly linked list is one directional only. That means we can move forward only.
o If one link is corrupted remaining data will be lost.
Page 3
:: UNIT – 4 ::
Menu Driven Singly Linked List Program which performs following operations:
1) Create 2) Display / Traverse 3) Count 4) Quit / Exit
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*start;
int count = 0;
void create(struct node *);
void display(struct node *);
void main()
{
int ch;
start = NULL;
clrscr();
while(1)
{
clrscr();
printf("\n\t1. => Create ");
printf("\n\t2. => Display ");
printf("\n\t3. => Count ");
printf("\n\t0. => Exit ");
printf("\n\t====> Enter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create(start);
break;
case 2:
display(start);
break;
case 3:
printf("\n\t Total No. of Nodes are : %d",count);
break;
case 0:
Page 4
:: UNIT – 4 ::
exit(0);
default:
printf("\n\t Invalid Choice : ");
break;
}
getch();
}
}
void create (struct node *temp)
{
if(temp == NULL)
{
temp = (struct node*) malloc(sizeof(struct node));
start = temp;
}
else
{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next =(struct node*) malloc(sizeof(struct node));
temp = temp->next;
}
printf("\n\tEnter Value : ");
scanf("%d",&temp->data);
temp->next = NULL;
count++;
}
Page 5
:: UNIT – 4 ::
FUNCTION TO SEARCH THE NODE FROM THE LIST
void search(struct node *temp)
{
int key,pos=1,flag=0;
printf("\n\tEnter number to search from the list : ");
scanf("%d",&key);
if(temp == NULL)
printf("\n\tList is empty...");
else
{
while(temp != NULL)
{
//printf("%d\n",temp->data);
if(key == temp->data)
{
printf("\n\tElement %d is found at %d position",key,pos);
flag = 1;
}
temp = temp->next;
pos++;
}
if(!flag)
{
printf("\n\tElement is not available in the list");
}
}
}
void insert_at_first()
{ struct node *temp;
temp = (struct node *) malloc (sizeof(struct node));
printf("\n\tEnter Value : ");
scanf("%d",&temp->data);
temp->next =start;
start = temp;
}
Page 6
:: UNIT – 4 ::
void insert_at_last()
{
struct node *temp;
temp = start;
while(temp->next != NULL)
{ temp = temp->next; }
temp->next =(struct node*) malloc(sizeof(struct node));
temp = temp->next;
Page 7
:: UNIT – 4 ::
printf("\n\tEnter Position: ");
scanf("%d",&pos);
i=1;
while(temp != NULL)
{
if(pos == i)
{
new1 = (struct node*)malloc(sizeof(struct node));
printf("\n\tEnter Data : ");
scanf("%d",&new1->data);
new1->next = temp->next;
temp->next = new1;
}
i++;
temp = temp->next;
}
}
Page 8
:: UNIT – 4 ::
FUNCTIONS TO DISPLAYING THE NODES WITH DIFFERENT WAYS:
1) Display from Last Position
2) Display from Specific Position
3) Display after Specific Position
4) Display odd number of nodes Value
5) Display odd Values of nodes
6) Display from Specific Value
7) Display from Specific Range
for(j=i-1;j>=0;j--)
printf("\n\t%d",node[j]);
}
Page 9
:: UNIT – 4 ::
while(temp != NULL)
{
if(cnt >= pos)
printf("\n\t%d",temp->data);
temp = temp->next;
cnt++;
}
}
void odd_node(struct node *temp)
{
int cnt=1;
while(temp!=NULL)
{
if(cnt%2 != 0)
printf("\n\t%d",temp->data);
temp=temp->next;
cnt++;
}
}
void odd_value(struct node *temp)
{
while(temp!=NULL)
{
if(temp->data%2 != 0)
printf("\n\t%d",temp->data);
temp=temp->next;
}
}
void disp_frm_spec_value(struct node *temp)
{
int val;
struct node *t1;
printf("\n\tEnter Value : ");
scanf("%d",&val);
while(temp != NULL)
{
if(val == temp->data)
{
t1 = temp;
break;
}
Page 10
:: UNIT – 4 ::
temp = temp -> next;
}
while(t1 != NULL)
{
printf("\n\t%d",t1->data);
t1 = t1->next;
}
}
Page 11
:: UNIT – 4 ::
void del_last(struct node*temp)
{
int i;
while(temp->next!=NULL)
temp = temp->next;
printf("\n\tDeleted Node Value is %d",temp->data);
count--;
temp = start;
for(i=1;i<count;i++)
temp=temp->next;
temp->next = NULL;
}
Page 12
:: UNIT – 4 ::
void del_by_val(struct node *temp)
{
int no,i;
struct node *prev;
prev=temp;
printf("\n\n\t\tEnter Number you want to delete:");
scanf("%d",&no);
if(temp->data == no && temp->next !=NULL)
{
printf("\n\n\t\tDeleted element is %d",temp->data);
start = start->next;
}
else if(temp->data == no)
{
printf("\n\n\t\tDeleted element is %d",temp->data);
start = NULL;
}
else
{
while(temp!=NULL)
{
if(temp->data==no)
{
printf("\n\n\t\tDeleted element is %d",temp->data);
prev->next = temp->next;
break;
}
prev = temp;
temp=temp->next;
}
temp=start;
display(start);
}
}
Page 13
:: UNIT – 4 ::
while(temp != NULL)
{
if(temp->next->data >=sr && temp->next->data <= er)
{
printf("\n\tDeleted Node is %d",temp->next->data);
temp->next = temp->next->next;
}
else
temp = temp->next;
}
}
Page 14
:: UNIT – 4 ::
for(i=1;i<=count;i++)
{
a=temp->next;
for(j=1;j<cnt;j++)
{
if(temp->data < a->data)
{
b=temp->data;
temp->data=a->data;
a->data=b;
}
a=a->next;
}
printf("\n\t%d",temp->data);
temp=temp->next;
cnt--;
}
}
FUNCTIONS TO UPDATE THE NODES
1) Update First Node
2) Update Last Node
3) Update Node By Value
4) Update Node By Position
void update_first()
{
if(start == NULL)
printf("\n\tLIST IS EMPTY....");
else
{
printf("\n\tEnter New Value for First Node : ");
scanf("%d",&start->data);
}
}
void update_last(struct node *temp)
{
if(temp == NULL)
printf("\n\tLIST IS EMPTY....");
else
{
while(temp->next!=NULL)
temp=temp->next;
Page 15
:: UNIT – 4 ::
printf("\n\tEnter New Value for Last Node : ");
scanf("%d",&temp->data);
}
}
void update_by_value(struct node *temp)
{
int val;
if(temp ==NULL)
printf("\n\tLIST IS EMPTY...");
else
{
printf("\n\tEnter Old value that you want to update : ");
scanf("%d",&val);
while(temp!= NULL)
{
if(temp->data == val)
{
printf("\n\tThe Old Value of this node is %d", temp->data);
printf("\n\tEnter New Value : ");
scanf("%d",&temp->data);
}
else
temp = temp->next;
}
}
}
Page 16
:: UNIT – 4 ::
scanf("%d",&temp->data);
break;
}
else
{
temp = temp->next;
i++;
}
}
}
}
Page 17
:: UNIT – 4 ::
Pointer to previous node Information Part Pointer to next node
We can describe the above definition of structure (node) in graphical way as follow:
Here, the data is used to store the information or actual value and *next stores the
address of the successor (next) node in the list. The *next is used to maintain the link of the
node with another node. The *prev stores the address of the predecessor (previous) node in the
list. If the Node is first node then *prev has NULL. The following figure shows the linked list
which stores the marks obtained by the students:
Page 18
:: UNIT – 4 ::
It works same as singly linked list except it has no end – that means the there
isn’t any node which contains NULL.
Each node in list contains two parts: Information Part and Pointer to Next Node.
We can describe the above definition of structure (node) in graphical way as follow:
data *next
The *next can be used to point the last / next node in the list.
In this type of linked list, *next of last node has START.
Page 19
:: UNIT – 4 ::
Advantages of Circular Linked List over Singly Linked List:
o In circular list every node is accessible from given node.
Disadvantages:
o It is possible to get into an infinite loop if care is not taken.
Page 20
:: UNIT – 4 ::
*prev data *next
This type of linked list is similar to the singly or doubly linked list except that it
contains one additional node that is header node.
All the possible operations that we can perform in singly or doubly linked list, is
possible in header linked list.
The Header node is created when the first node of the list is created.
Header Linked List are of two types: 1) Grounded Header Linked List and 2)
Circular Header Linked List.
DIFFERENCES:
Also check the differences of:
1. Doubly Linked List Vs. Singly Linked List
2. Circular Linked List Vs. Singly Linked List
3. Linked List Vs. Header Linked List
Page 21