IMPLEMENTATION OF SINGLY LINKED LIST AND ITS OPERATIONS
TO LEARN C PROGRAMMING FOLLOW THIS YOUTUBE CHANNEL : Code with u - YouTube
AIM:
To implement singly linked list and performing insert, search, display and delete operations.
ALGORITHM:
1: CREATION:
a. Creating a node b. Reading details for a node from user c. Connect the node with the list
2. INSERTION:
a. Get the node using the structure node (), and read the node details using the new node().
b. Check if the list is empty or not
c. FIRST: The binding field of the new node is made to point to the data field of the first node in the list by assigning the first node.
d. The head pointer is made to point the data field of the new one by assigning the address of the new node.
e. LAST: The binding field of the last node is made to point to the data field of the first node in the list by assigning the new node.
f. The link field of the new node is set to NULL.
g. MIDDLE- the binding field of the new node is made to point to the data field of the next node in the list by assigning its address
h. The binding field of the next node is made to point to the data field of the new one by assigning the address of the new node.
3. DELETION:
a. Check whether the list is empty or not.
b. FIRST – set the head pointer of the second node in the list
c. Release the memory for the deleted node d. LAST- the link field of the previous node
e. Release the memory for the deleted node
f. MIDDLE- the link field of the previous node
g. Release the memory for the deleted node
4. DISPLAY:
a. Get the contents from the list
b. If the list is empty print it is empty.
c. If the list is not empty print the entire list.
PROGRAM :
#include<studio. h>
for(q=head ; q != NULL && q->data != y ; q=q->next) ;
if(q!=NULL)
{
p->next=q->next;
q->next=p;
}
else
print f("\n Data not found ");
return(head);
}
node *delete b(node *head)
{
node *p,*q;
if(head==NULL)
{
print f("\n Underflow....Empty Linked List");
return(head);
}
p=head;
head=head->next;
free(p);
return(head);
}
node *delete e(node *head)
{
node *p,*q;
if(head==NULL)
{
print f("\n Underflow....Empty Linked List");
return(head);
}
p=head;
if(head->next==NULL)
{
// Delete the only element
head=NULL;
free(p);
return(head);
}
//Locate the last but one node
for(q=head ;q->next->next !=NULL ;q=q->next) ;
p=q->next;
q->next=NULL;
free(p);
return(head);
}
node *delete in(node *head)
{
node *p,*q; int x, I;
if(head==NULL)
{
print f("\n Underflow....Empty Linked List");
return(head);
}
print f("\n Enter the data to be deleted : ");
scan f("%d", &x);
if(head->data==x)
{
// Delete the first element
p=head;
head=head->next;
free(p);
return(head);
}
//Locate the node previous to one to be deleted
for(q=head ;q->next->data!=x && q->next !=NULL ;q=q->next ) ;
if(q->next==NULL)
{
print f("\n Underflow.....data not found");
return(head);
}
p=q->next;
q->next=q->next->next;
free(p);
return(head);
}
void search(node *head)
{
node *p;
int data, loc=1;
print f("\n Enter the data to be searched: ");
scan f("%d", &data);
p=head;
while(p!=NULL && p->data != data)
{
loc++;
p=p->next;
}
if(p==NULL)
print f("\n Not found:");
else
print f("\n Found at location=%d", loc);
}
void print(node *head)
{
node *p;
print f("\n\n");
for(p=head ;p!=NULL ;p=p->next)
print f("%d ",p->data);
}
OUTPUT :
1)create
2)Insert
3)Delete
4)Search
5)Reverse
6)Print
7)Quit
Enter your Choice:1
Enter no of data:5
Enter the data:1 4 7 2 6
OUTPUT :
1)create
2)Insert
3)Delete
4)Search
5)Reverse
6)Print
7)Quit
Enter your Choice:6
1 4 7 2 6
OUTPUT :
1)create
2)Insert
3)Delete
4)Search
5)Reverse
6)Print
7)Quit
Enter your Choice:
5 6 2 7 4 1
OUTPUT :
1)create
2)Insert
3)Delete
4)Search
5)Reverse
6)Print
7)Quit
Enter your Choice:7
No comments:
Post a Comment