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

Link List

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

Link List

Copyright
© © All Rights Reserved
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;
struct node mknode(int item);
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 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:
// printf("\n Delete from the beginning of the linked list :");
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);
break; */
case 8:
display(first);
break;
case 9:
exit(1);
} /* End of switch */
} /* End of while */
return 0;
} /* End of main */

struct node mknode(int item)


{
struct node *curr;
curr=(struct node*)malloc(sizeof(struct node));
curr->data=item;
curr->link=NULL;
if(first==NULL)
first=curr;
else
last->link=curr;

last=curr;
}
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;
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;
}

You might also like