DSA 4 (The List)
DSA 4 (The List)
Self-referential structure:
It is sometimes desirable to include within a structure one member that is a pointer to the
parent structure type. Hence, a structure which contains a reference to itself is called self-referential
structure. In general terms, this can be expressed as:
struct node
{
member 1;
member 2;
…….
struct node *name;
};
For example, struct node
{
int info;
struct node *next;
};
This is a structure of type node. The structure contains two members: a info integer member, and a
pointer to a structure of the same type (i.e., a pointer to a structure of type node), called next.
Therefore this is a self-referential structure.
Linked List:
A linked list is a collection of nodes, where each node consists of two parts:
• info: the actual element to be stored in the list. It is also called data field.
Illustration:
info next info next info next
list 5 3 8 null
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.
• The NULL value of the next field of the linked list indicates the last node and we define macro for
NULL and set it to 0 as below:
#define NULL 0
Creating a Node:
• To create a new node, we use the malloc function to dynamically allocate memory for the new node.
• After creating the node, we can store the new item in the node using a pointer to that nose.
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
Insertion requires obtaining a new node and changing two links
The C function to insert a node at the beginning of the singly linked list:
void InsertAtBeg(int newItem)
{
NodeType *NewNode;
NewNode=getNode();
NewNode->info=newItem;
NewNode->next=head;
head=NewNode;
}
An algorithm to insert 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 insert a new node.
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 next of p
NewNode->next=p->next;
4. Set next of p to NewNode
p->next =NewNode..
5. End
The C function to insert a node after the given node in singly linked list:
void InsertAfterNode(NodeType *p int newItem)
{
NodeType *NewNode;
NewNode=getNode();
NewNode->info=newItem;
if(p==NULL)
{
printf(“Void insertion”);
exit(1);
}
else
{
NewNode->next=p->next;
Compiled by: Er. Sandesh S Poudel
p->next =NewNode..
}
}
8. 8. End
The C function to insert a node at the specified position in a singly linked list:
void InsertAtPos(int newItem)
{
NodeType *NewNode;
int pos , i ;
printf(“ Enter position of a node at which you want to insert a new node”);
scanf(“%d”,&pos);
if(head==NULL)
{
printf(“void insertion”);
exit(1).
}
else
Deleting Nodes:
A node may be deleted:
• From the beginning of the linked list
• from the end of the linked list
• from the specified position in a linked list
The C function to deleting the first node of the singly linked list:
void deleteBeg()
{
Compiled by: Er. Sandesh S Poudel
NodeType *temp; if(head==NULL)
{
printf(“Empty list”); exit(1).
}
else
{ temp=head;
printf(“Deleted item is %d” ,
head->info);
head=head->next;
free(temp);
}
}
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
The C function to delete a node after the given node in singly linked list:
let *p be the pointer to the node after which we want to delete a new node.
void deleteAfterNode(NodeType *p)
NodeType *q;
if(p==NULL || p->next==NULL )
{
printf(“Void insertion”);
exit(1);
}
else
{
q=p->next;
p->next=q->next;
free(q);
}
}
The C function to delete a node at the specified position in a singly linked list
void deleteAtSpecificPos()
{
NodeType *temp *p;
int pos, i;
if(head==NULL)
{
printf(“Empty list”);
return;
}
else
{
printf(“Enter position of a node which you want to delete”);
scanf(“%d” , &pos);
temp=head;
for(i=1; i<pos-1; i++)
{
temp=temp->next;
}
p=temp->next;
` printf(“Deleted item is %d”, p->info);
temp->next =p->next;
free(p);
}
}
Compiled by: Er. Sandesh S Poudel
Searching an item in a linked list:
To search an item from a given linked list we need to find the node that contain this data item.
If we find such a node then searching is successful otherwise searching unsuccessful.
let *head be the pointer to first node in the current list
void searchItem()
{
NodeType *temp;
int key;
if(head==NULL)
{
printf(“empty list”); exit(1);
}
else
{
printf(“Enter searched item”);
scanf('%d”,&key);
temp=head;
while(temp!=NULL)
{
if(temp->info==key)
{
printf(“Search successful”); break;
}
temp=temp->next;
}
if(temp==NULL)
printf(“Unsuccessful search”);
}
}
Complete program:
/******Various operations on singly linked list**************/
#include<stdio.h>
/************function definitions**************/
void insert_atfirst(int item)
{
NodeType *nnode;
nnode=(NodeType*)malloc(sizeof(NodeType));
nnode->info=item;
nnode->next=head;
head=nnode;
}
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);
}
}
void push(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:
let *top be the top of the stack or pointer to the first node of the list.
void pop()
{
NodeType *temp;
if(top==0)
{
printf("Stack contain no elements:\n");
return; }
else
{
temp=top;
top=top->next;
printf("\ndeleted item is %d\t",temp->info);
free(temp);
}
}
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:
printf("invalid choice\n");
break;
}
}while(choice<5);
Compiled by: Er. Sandesh S Poudel
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;
Compiled by: Er. Sandesh S Poudel
top=top->next;
printf("\ndeleted item is %d\t",temp->info);
free(temp);
}
}
/**************display function***********************/
void display()
{
NodeType *temp;
if(top==0)
{
printf("Stack is empty\n");
return;
}
else
{
temp=top;
printf("Stack items are:\n");
while(temp!=0)
{
printf("%d\t",temp->info);
temp=temp->next;
}
}
}
if(rear==0)
{
nnode->info=item;
nnode->next=NULL;
rear=front=nnode;
}
else
{
nnode->info=item;
nnode->next=NULL;
rear->next=nnode;
rear=nnode;
}
}
Delete function:
let *rear and *front are pointers to the first node of the list initially and insertion of node in
linked list done at the rear part and deletion of node from the linked list done from front part.
void delet()
{
NodeType *temp;
if(front==0)
{
Compiled by: Er. Sandesh S Poudel
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);
}
}
getch();
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)
{
In a circular linked list there are two methods to know if a node is the first node or not.
• Either a external pointer, list, points the first node or
• A header node is placed as the first node of the circular list.
The header node can be separated from the others by either heaving a sentinel value as the info
part or having a dedicated flag variable to specify if the node is a header node or not.
newnode->info=item;
last->next=newnode;
last=newnode;
last->next=start;
}
}
}
}
PUSH function:
void PUSH(int item)
{
NodeType newnode;
newnode=(NodeType*)malloc(sizeof(NodeType));
newnode->info=item;
if(pstack==NULL)
{
pstack=newnode;
pstack->next=pstack;
}
else
{
newnode->next=pstack->next;
pstack->next=newnode;
}
}
Insertion function:
void insert(int item)
{
Deletion function:
Void delet(int item)
{
NodeType *temp;
if(pq==NULL)
{
printf(“void deletion\n”);
exit(1);
}
else if(pq->next==pq) //for only one node
{
printf(“poped item=%d”, pq->info);
Compiled by: Er. Sandesh S Poudel
pq=NULL;
}
else
{
temp=pq->next;
pq->next=temp->next;
printf(“poped item=%d”, temp->info);
free(temp);
}
}
NodeType *head=NULL:
Algorithm to delete a node from the beginning of a circular doubly linked list:
1. if head->next==NULL then
print “empty list” and exit
2. else
set temp=head->next;
set head->next=temp->next
set temp->next=head
free(temp)
3. End
Algorithm to delete a node from the end of a circular doubly linked list:
1. if head->next==NULL then
print “empty list” and exit
2. else
set temp=head->prev;
set head->left=temp->left