0% found this document useful (0 votes)
12 views4 pages

LList

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views4 pages

LList

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//---------------------------------------------------------------

// To create a singly linked list to perform all the operations.


//-----------------------------------------------------------------

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *first,*last;
void delete(int pos);
void delete_end();
void delete_beg();
void display(struct node *x);
void insert(int pos,int x);
void insert_begin(int x);
void insert_end(int x);
int search(int x);

int main()
{
int ch,x,pos;
first=last=NULL;
while(1)
{
printf("\n -----------------------------------------------");
printf("\n 1.Insertion at beginning of linked list ");
printf("\n 2.Insertion at a given position of list ");
printf("\n 3.Insertion at the end of the linked list ");
printf("\n 4.Delete from the beginning of the list ");
printf("\n 5.Delete from a given position of list ");
printf("\n 6.Delete from end of the list ");
printf("\n 7.Search for a node in the list ");
printf("\n 8.Display all the elements from the list ");
printf("\n 9.Exit ");
printf("\n -----------------------------------------------");
printf("\n Your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Number to insert at beginning :");
scanf("%d",&x);
insert_begin(x);
break;
case 2:
printf("\n Location number to insert the node :");
scanf("%d",&pos);
printf("\n Number to insert :");
scanf("%d",&x);
insert(pos,x);
break;
case 3:
printf("\n Number to insert at the end :");
scanf("%d",&x);
insert_end(x);
break;
case 4:
if(first!=NULL)
delete_beg();
else
printf("\n Empty List \n");

break;
case 5:
printf("\n Location number of the node to be deleted :");
scanf("%d",&pos);
delete(pos);
break;
case 6:
if(first!=NULL)
delete_end();
break;
case 7:
printf("\n Enter node data to be searched :");
scanf("%d",&x);
// search(x);
if(search(x)==-1)
printf("\n %d is not Found ",x);
else
printf("\n %d is found ",x);

break;
case 8:
display(first);
break;
case 9:
exit(1);
} /* End of switch */
} /* End of while */
return 0;
} /* End of main */

void delete(int pos)


{
int i;
struct node *p;
p=first;
if(pos==1)
first=first->link;
else
{
i=2;
while(i<pos && p!=NULL)
{
p=p->link;
i++;
}
p->link=p->link->link;
}
}
void display(struct node *p)
{
printf("\n Nodes in the list \n");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->link;
}
printf("NULL");
}

void insert_begin(int x)
{
struct node *new;
int i;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->link=first;
first=new;

void insert(int pos,int x)


{
struct node *new,*p;
int i;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
i=2;
if(pos==1)
{
new->link=first;
first=new;
}
else
{
p=first;
while(i<pos && p!=NULL)
{
p=p->link;
i++;
}
new->link=p->link;
p->link=new;
}
}

void insert_end(int x)
{
struct node *p,*new;
new=(struct node*)malloc(sizeof(struct node));
new->data=x;
new->link=NULL;

if(first==NULL)
first=new;
else
{
p=first;
while(p->link!=NULL)
{
p=p->link;
}
p->link=new;
}
}

void delete_end()
{
struct node *p,*q;
p=first;
q=first->link;
if(first==NULL)
printf("\n Empty List ");
else if(first->link==NULL)
{
printf("\n Deleted data %d",first->data);
first=first->link;
}
else
{
while(q->link!=NULL)
{
p=q;
q=q->link;
}
printf("\n %d ",q->data);
p->link=q->link;
}
}
void delete_beg()
{
int x;
x=first->data;
printf("\n I location deleted value %d",first->data);
first=first->link;
}

int search(int x)
{
int pos;
struct node *p;
p=first;

while(p!=NULL)
{
if(x==p->data)
return pos;
else
{
pos++;
p=p->link;
}
}
return -1;
}

You might also like