Single
linked list and its Operations
Single connected list and its Tasks
Fundamental procedure on a separately connected list
include:
1. Inclusion toward the start: Make another hub with the
given worth and make it the new top of the rundown by guiding its next pointer
toward the ongoing head.
2. Inclusion toward the end: Navigate the rundown until you
arrive at the last hub, make another hub with the given worth, and make it the
following hub of the last hub by setting its next pointer to invalid.
3. Inclusion at a particular position:Traverse the rundown
until you arrive at the ideal position (counting from 0), make another hub with
the given worth, and change the pointers to incorporate the new hub at the
predetermined position.
4. Cancellation of a hub: Navigate the rundown until you
view the hub as erased, change the pointers of the past and next hubs to
sidestep the hub to be erased, and erase the hub.
5. Looking for a worth: Cross the rundown and contrast every
hub's worth and the objective worth until a match is found or the finish of the
rundown is reached.
6. Crossing: Repeat through the rundown, beginning from the
head, and access or interaction every hub's worth.
7. Inversion: Turn around the request for the hubs in the
rundown by changing the following pointers as needs be.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node*next;
}*head;
int main()
{
int n,ch;
printf("enter no of nodes u want to enter");
scanf("%d",&n);
create(n);
display();
printf("\n 1.addfirst 2.addend 3.addmiddle 4.delfirst 5.delend 6.delmiddle\n");
printf("\nenter ur option");
scanf("%d",&ch);
switch(ch)
{
case 1:
addfirst();
break;
case 2:
addlast();
break;
case 3:
middle();
break;
case 4:
delfirst();
break;
case 5:
delend();
break;
case 6:
delmiddle();
break;
}
display();
return 0;
}
int create(int n)
{
struct node*temp,*c;
int data,i;
head=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
head->data=data;
head->next=NULL;
temp=head;
for(i=1;i<=n;i++)
{
c=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
c->data=data;
c->next=NULL;
temp->next=c;
temp=temp->next;
}
return 0;
}
int display()
{
struct node*temp;
temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
return 0;
}
int addfirst()
{
struct node*c;
int data;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
c->data=data;
c->next=head;
head=c;
}
int addlast()
{
struct node*temp,*c;
int data;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
c->data=data;
c->next=NULL;
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=c;
}
int middle()
{
struct node*temp,*c;
int data,i,x;
c=(struct node*)malloc(sizeof(struct node));
printf("enter data");
scanf("%d",&data);
printf("enter position");
scanf("%d",&x);
c->data=data;
c->next=NULL;
temp=head;
for(i=0;i<x;i++)
{
temp=temp->next;
}
c->next=temp->next;
temp->next=c;
}
int delfirst()
{
struct node*temp;
temp=head;
head=head->next;
free(temp);
return 0;
}
int delend()
{
struct node*temp,*temp1;
temp=head;
temp1=head;
while(temp!=NULL&&temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
free(temp);
return 0;
}
int delmiddle()
{
struct node*temp,*temp1;
int x,i;
printf("enter position");
scanf("%d",&x);
temp=head;
temp1=head;
for(i=1;i<x;i++)
{
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next=NULL;
free(temp);
return 0;
}
//output:
//Add first
// Add last
// Add middle
0 Comments