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

unit - 5 linked list

Linked List Note

Uploaded by

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

unit - 5 linked list

Linked List Note

Uploaded by

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

1

Unit- 5 linked list


Introduction In computer science, a linked list is one of the fundamental data structures, and can be
used to implement other data structures. It consists of a sequence of nodes, each containing arbitrary
data fields and one or two references ("links") pointing to the next and/or previous nodes. The principal
benefit of a linked list over a conventional array is that the order of the linked items may be different
from the order that the data items are stored in memory or on disk, allowing the list of items to be
traversed in a different order. A linked list is a self-referential data type because it contains a pointer or
link to another datum of the same type. Linked lists permit insertion and removal of nodes at any point
in the list in constant time, but do not allow random access. Several different types of linked lists exist:
singly linked list, doubly linked list, and circularly linked list.

Operations on a list:
insert : insert a new item into a list
delete : delete an item from list
List: list the items from the list
Retrieval : search for an element in the list

Singly Linked List The simplest kind of linked list is a singly linked list (or slist for short), which has one
link per node. This link points to the next node in the list, or to a null value or empty list if it is the final
node.

Single link list


The singly linked list is the most basic of all the linked data structures. A singly linked list is simply a
sequence of dynamically allocated objects, each of which refers to its successor in the list. Despite this
obvious simplicity there are many implementation variations.

The following figures show the most common singly linked lists.
2

a)

Single link list several operations

Operations on linked list:


The basic operations to be performed on the linked list are as follows:
Creation: This operation is used to create a linked list
Insertion: This operation is used to insert a new nose in a kinked list in a specified position. A new
node may be inserted
✔At the beginning of the linked list
✔At the end of the linked list
✔At he specified position in a linked list
Deletion: The deletion operation is used to delete a node from the linked list. A node may be deleted
from
✔The beginning of the linked list
✔the end of the linked list
✔ the specified position in the linked list.
Traversing: The list traversing is a process of going through all the nodes of the linked list
from on end to the other end. The traversing may be either forward or backward.
3

Searching or find: This operation is used to find an element in a linked list. In the desired
element is found then we say operation is successful otherwise unsuccessful.
Concatenation: It is the process of appending second list to the end of the first list.

Types of Linked List:


basically we can put linked list into the following four types:
Singly linked list
doubly linked list
circular linked list
Singly linked list:
A singly linked list is a dynamic data structure which may grow or shrink, and growing and
shrinking depends on the operation made. In this type of linked list each node contains two
fields one is info field which is used to store the data items and another is link field that is used
to point the next node in the list. The last node has a NULL pointer.

Representation of singly linked list:


We can create a structure for the singly linked list the each node has two members, one is info
that is used to store the data items and another is next field that store the address of next node
in the list. We can define a node as follows:

struct Node
{
int info;
struct Node *next;
};
typedef struct Node NodeType;
NodeType *head; //head is a pointer type structure variable This type of structure is called self-
referential structure.
Inserting Nodes:
To insert an element or a node in a linked list, the following three things to be done:
Allocating a node
Assigning a data to info field of the node
Adjusting a pointer and a new node may be inserted
At the beginning of the linked list
At the end of the linked list
At the specified position in a linked list

An algorithm to insert a node at the beginning of the singly linked list:


let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to head
NewNode->next=head;
4

4. Set the head pointer to the new node


head=NewNode;
5. End

An algorithm to insert a node at the end of the singly linked list:


let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Set next of new node to NULL
NewNode->next=NULL;
4. if (head ==NULL)then
Set head =NewNode.and exit.
5. Set temp=head;
6 while(temp->next!=NULL)
temp=temp->next; //increment temp
7. Set temp->next=NewNode;
8. End

An algorithm to insert a node at the specified position in a singly linked list:


let *head be the pointer to first node in the current list
1. Create a new node using malloc function
NewNode=(NodeType*)malloc(sizeof(NodeType));
2. Assign data to the info field of new node
NewNode->info=newItem;
3. Enter position of a node at which you want to insert a new node. Let this
position is pos.
4. Set temp=head;
5. if (head ==NULL)then
printf(“void insertion”);
exit(1).
6. for(i=1; i<pos-1; i++)
temp=temp->next;
7. Set NewNode->next=temp->next;
set temp->next =NewNode..
8. End

Deleting first node of the linked list:


An algorithm to deleting the first node of the singly linked list:
let *head be the pointer to first node in the current list
5

1. If(head==NULL) then
print “Void deletion” and exit
2. Store the address of first node in a temporary variable temp.
temp=head;
3. Set head to next of head.
head=head->next;
4. Free the memory reserved by temp variable.
free(temp);
5. End

Deleting the last node of the linked list:


An algorithm to deleting the last node of the singly linked list:
let *head be the pointer to first node in the current list
1. If(head==NULL) then //if list is empty
print “Void deletion” and exit
2. else if(head->next==NULL) then //if list has only one node
Set temp=head;
print deleted item as,
printf(“%d” ,head->info);
head=NULL;
free(temp);
3. else
set temp=head;
while(temp->next->next!=NULL)
set temp=temp->next;
End of while
free(temp->next);
Set temp->next=NULL;
4. End

An algorithm to delete a node after the given node in singly linked list:
let *head be the pointer to first node in the current list and *p be the pointer to
the node
after which we want to delete a new node.
1. if(p==NULL or p->next==NULL) then
print “deletion not possible and exit
2. set q=p->next
3. Set p->next=q->next;
4. free(q)
5. End
6

An algorithm to delete a node at the specified position in a singly linked list:


let *head be the pointer to first node in the current list
1. Enter position of a node at which you want to delete a new node. Let this
position is pos.
2. Set temp=head
declare a pointer of a structure let it be *p
3. if (head ==NULL)then
print “void ideletion” and exit
otherwise;.
4. for(i=1; i<pos-1; i++)
temp=temp->next;
5. print deleted item is temp->next->info
6. Set p=temp->next;
7. Set temp->next =temp->next->next;
8. free(p);
9. End

//program for single link list

#include<stdio.h>
#include<conio.h>
#include<malloc.h> //for malloc function
#include<process.h> //fpr exit function
struct node
{int info;
struct node *next;
};
typedef struct node NodeType;
NodeType *head=NULL;
//head=NULL;
void insert_atfirst(int);
void insert_givenposition(int);
void insert_atend(int);
void delet_first();
void delet_last();
void delet_nthnode();
int main()
{
int choice;
int item;
7

//clrscr();
do
{
printf("\n manu for program:\n");
printf("1. insert first \n2.insert at given position \n3 insert at last \n 4:Delete first
node\n 5:delete last node\n6:delete nth node\n7:display nodes\n8Display
items\n10:exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter item to be inserted \n");
scanf("%d", &item);
insert_atfirst(item);
break;
case 2:
printf("Enter item to be inserted");
scanf("%d", &item);
insert_givenposition(item);
break;
case 3:
printf("Enter item to be inserted");
scanf("%d", &item);
insert_atend(item);
break;
case 4:
delet_first();
break;
case 5:
delet_last();
break;
case 6:
delet_nthnode();
break;
case 7:
info_sum();
break;
case 8:
count_nodes();
8

break;
case 9:
exit(1);
break;
default:
printf("invalid choice\n");

}
}while(choice<10);
return 0;
getch();
}
void insert_atfirst(int item)
{
NodeType *nnode;
nnode=(NodeType*)malloc(sizeof(NodeType));
nnode->info=item;
nnode->next=head;
head=nnode;
}

void insert_atend(int item)


{
NodeType *nnode;
NodeType *temp;
temp=head;
nnode=( NodeType *)malloc(sizeof(NodeType));
nnode->info=item;
if(head==NULL)
{
nnode->next=NULL;
head=nnode;
}
else
{
while(temp->next!=NULL)
{

temp=temp->next;
9

nnode->next=NULL;
temp->next=nnode;
}
}
void info_sum()
{
NodeType *temp;
temp=head;
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
void insert_givenposition(int item)
{
NodeType *nnode;
NodeType *temp;
temp=head;
int p,i;
nnode=( NodeType *)malloc(sizeof(NodeType));
nnode->info=item;
if (head==NULL)
{
nnode->next=NULL;
head=nnode;
}
else
{
printf("Enter Position of a node at which you want to insert an new node\n");
scanf("%d",&p);
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
nnode->next=temp->next;
temp->next=nnode;
}
10

}
void delet_first()
{
NodeType *temp;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else
{
temp=head;
head=head->next;
free(temp);
}
}
void delet_last()
{
NodeType *hold,*temp;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else if(head->next==NULL)
{
hold=head;
head=NULL;
free(hold);
}
else
{
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
hold=temp->next;
temp->next=NULL;
free(hold);
11

}
}
void delet_nthnode()
{
NodeType *hold,*temp;
int pos, i;
if(head==NULL)
{
printf("Void deletion|n");
return;
}
else
{
temp=head;
printf("Enter position of node which node is to be deleted\n");
scanf("%d",&pos);
for(i=1;i<pos-1;i++)
{
temp=temp->next;
}
hold=temp->next;
temp->next=hold->next;
free(hold);
}
}
void count_nodes()
{
int cnt=0;
NodeType *temp;
temp=head;
while(temp!=NULL)
{
cnt++;
temp=temp->next;
}
printf("total nodes=%d",cnt);
}

//stack using link list


#include<stdio.h>
12

#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NodeType;
NodeType *top=0;
//top=0;
void push(int);
void pop();
void display();
int main()
{
printf("\n top is %d",top);
int choice, item;
do
{
printf("\n1.Push \n2.Pop \n3.Display\n4:Exit\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the data:\n");
scanf("%d",&item);
push(item);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
13

printf("invalid choice\n");
break;
}
}while(choice<5);

getch();
}
/**************push function*******************/
void push(int item)
{
NodeType *nnode;
int data;
nnode=( NodeType *)malloc(sizeof(NodeType));
if(top==0)
{
nnode->info=item;
nnode->next=NULL;
top=nnode;

}
else
{
nnode->info=item;
nnode->next=top;
top=nnode;
}
}
// pop function*
void pop()
{
NodeType *temp;
if(top==0)
{
printf("Stack contain no elements:\n");
return;
}
else
{
temp=top;
top=top->next;
14

printf("\ndeleted item is %d\t",temp->info);


free(temp);
}
}
//display function
void display()
{
NodeType *temp;
if(top==0)
{
printf("Stackis empty\n");
return;
}
else
{
temp=top;
printf("Stack items are:\n");
while(temp!=0)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
}

Insert function:
let *rear and *front are pointers to the first node of the list initially and insertion of node
in linked.
15

/**************Linked list implementation of queue*****************/


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<process.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NodeType;
NodeType *rear=0,*front=0;
//rear=front=0;
void insert(int);
void delet();
void display();
main()
{
int choice, item;
//clrscr();
do
{
printf("\n1.Insert \n2.Delet \n3.Display\n4:Exit\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the data:\n");
scanf("%d",&item);
insert(item);
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
exit(1);
break;
default:
printf("invalid choice\n");
break;
}
}while(choice<5);
getch();
return 0;
}
/**************insert function*******************/
16

void insert(int item)


{
NodeType *nnode;
nnode=( NodeType *)malloc(sizeof(NodeType));
if(rear==0)
{
nnode->info=item;
nnode->next=NULL;
rear=front=nnode;
}
else
{
nnode->info=item;
nnode->next=NULL;
rear->next=nnode;
rear=nnode;
}
}
/******************delet function********************/
void delet()
{
NodeType *temp;
if(front==0)
{
printf("Queue contain no elements:\n");
return;
}
else if(front->next==NULL)
{
temp=front;
rear=front=NULL;
printf("\nDeleted item is %d\n",temp->info);
free(temp);
}
else
{
temp=front;
front=front->next;
printf("\nDeleted item is %d\n",temp->info);
free(temp);
}
}
/**************display function***********************/
void display()
{
NodeType *temp;
temp=front;
printf("\nqueue items are:\t");
while(temp!=NULL)
{
17

printf("%d\t",temp->info);
temp=temp->next;
}
}

Doubly Linked List:


A linked list in which all nodes are linked together by multiple number of links ie
each node contains three fields (two pointer fields and one data field) rather than two
fields is called doubly linked list.
It provides bidirectional traversal.

C representation of doubly linked list:


struct node
{
int info;
struct node *prev;
struct node *next;
};
typedef struct node NodeType;
NodeType *head=NULL:

Algorithms to insert a node in a doubly linked list:


Algorithm to insert a node at the beginning of a doubly linked list :
1.Allocate memory for the new node as,
newnode=(NodeType*)malloc(sizeof(NodeType))
2. Assign value to info field of a new node
set newnode->info=item
3. set newnode->prev=newnode->next=NULL
18

4. set newnode->next=head
5. set head->prev=newnode
6. set head=newnode
7. End

Algorithm to insert a node at the end of a doubly linked list :


1. Allocate memory for the new node as,
newnode=(NodeType*)malloc(sizeof(NodeType))
2. Assign value to info field of a new node
set newnode->info=item
3. set newnode->next=NULL
4. if head==NULL
set newnode->prev=NULL;
set head=newnode;
5. if head!=NULL
set temp=head
while(temp->next!=NULL)
temp=temp->next;
end while
set temp->next=newnode;
set newnode->prev=temp
6. End
Algorithm to delete a node from beginning of a doubly linked list:
1. if head==NULL then
print “empty list” and exit
2. else
set hold=head
set head=head->next
set head->prev=NULL;
free(hold)
3. End
Algorithm to delete a node from end of a doubly linked list:
1. if head==NULL then
print “empty list” and exit
2. else if(head->next==NULL) then
set hold=head
set head=NULL
free(hold)
3. else
set temp=head;
while(temp->next->next !=NULL)
temp=temp->next
end while
set hold=temp->next
set temp->next=NULL
free(hold)
4. End

Program for double link list


19

/* Program of double linked list*/


# include <stdio.h>
# include <malloc.h>
#include<stdlib.h>

struct node
{
struct node *prev;
int info;
struct node *next;
}*head;
typedef struct node nodetype;
void create_list(int m);
void create_list(int m);
void addatbeg(int m);
void del(int m);
void display();
void addafter(int m,int po);
main()
{
int choice,n,m,po,i;
head=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after\n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.exit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
20

}
break;
case 2:
printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3:
printf("Enter the element : ");
scanf("%d",&m);
printf("Enter the position after which this element is
inserted : ");
scanf("%d",&po);
addafter(m,po);
break;
case 4:
printf("Enter the element for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5:
display();
break;
case 6:
// exit();
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

void create_list(int num)


{
struct node *q,*tmp;
tmp= (nodetype *)malloc(sizeof(struct node));
tmp->info=num;
tmp->next=NULL;
if(head==NULL)
{
tmp->prev=NULL;
head->prev=tmp;
head=tmp;
}
21

else
{
q=head;
while(q->next!=NULL)
{
q=q->next;

}
q->next=tmp;
tmp->prev=q;
}
}/*End of create_list()*/

void addatbeg(int num)


{
struct node *tmp;
tmp=(nodetype *)malloc(sizeof(struct node));
tmp->prev=NULL;
tmp->info=num;
tmp->next=head;
head->prev=tmp;
head=tmp;
}/*End of addatbeg()*/

void addafter(int num,int c)


{
struct node *tmp,*q;
int i;
q=head;
for(i=0;i<c-1;i++)
{
q=q->next;
if(q==NULL)
{
printf("There are less than %d elements\n",c);
return;
}
}
tmp=(nodetype *)malloc(sizeof(struct node) );
tmp->info=num;
q->next->prev=tmp;
tmp->next=q->next;
tmp->prev=q;
22

q->next=tmp;
}/*End of addafter() */

void del(int num)


{
struct node *tmp,*q;
if(head->info==num)
{
tmp=head;
head=head->next; /*first element deleted*/
head->prev = NULL;
free(tmp);
return;
}
q=head;
while(q->next->next!=NULL)
{

if(q->next->info==num) /*Element deleted in between*/


{
tmp=q->next;
q->next=tmp->next;
tmp->next->prev=q;
free(tmp);
return;
}
q=q->next;
}
if(q->next->info==num) /*last element deleted*/
{ tmp=q->next;
free(tmp);
q->next=NULL;
return;
}
printf("Element %d not found\n",num);
}/*End of del()*/

void display()
{
struct node *q;
if(head==NULL)
{
printf("List is empty\n");
23

return;
}
q=head;
printf("List is :\n");
while(q!=NULL)
{
printf("%d ", q->info);
q=q->next;
}
printf("\n");
}/*End of display() */

You might also like