You are on page 1of 5

WAP TO SHOW THE FUNCTIONS OF A LINKED LIST

#include<conio.h>
#include<stdio.h>
struct node
{
struct node *next;
int info;
};
struct node *head;

void insert_end(int i) //FUNCTION TO INSERT AT END IN A LINKED LIST


{
struct node *temp,*trav;
if(head==NULL)
{
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=NULL;
head=temp;
}
else
{
trav=head;
while(trav->next!=NULL)

trav=trav->next;
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=NULL;
trav->next=temp;

}
}

void display() //FUNCTION TO DISPLAY THE LINKED LIST


{
struct node *trav;
trav=head;
if(trav==NULL)
printf(“\n LIST IS EMPTY.”);
while(trav!=NULL)
{
printf(“\n%t”,trav->info);
trav=trav->next;
}
}

void insert_beg(int i) //FUNCTION TO INSERT AT BEGINNING IN A LINKED LIST


{
struct node *temp;
if(head==NULL)
{
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=NULL;
head=temp;
}
else
{
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=head;
head=temp;
}
}

void insert_aft(int i,int pos) //FUNCTION TO INSERT AFTER A POSITION IN A LINKED LIST
{
struct node *temp,*trav;
trav=head;
for(int j=0;j<pos;j++)
{ if(trav==NULL)
{
printf(“\n POSITION DOESN’T EXIST.”);
return;
}
trav=trav->next;
}
temp=malloc(sizeof(struct dnode));
temp->info=i;
temp->next=trav->next;
trav->next=temp;
}

void delete_beg() //FUNCTION TO DELETE AT BEGINNING IN A LINKED LIST


{
struct node *ptr;
ptr=head;
if(ptr==NULL)
printf(“\n LIST IS EMPTY.”);
else
{
head=head->next;
printf(“\n ELEMENT DELETED IS-:”,ptr->info);
}
free(ptr);
}

void delete_pos(int pos) //FUNCTION TO DELETE AT A POSITION IN A LINKED LIST


{
struct node *trav,ptr;
trav=head;
for(int j=1;j<pos;j++)
{ if(trav==NULL)
{
printf(“\n POSITION DOESN’T EXIST.”);
return;
}
trav=trav->next;
}
ptr=trav->next;
trav->next=ptr->next;
printf(“\n ELEMENT DELETED IS-:”,ptr->info);
free(ptr);

void delete_end() //FUNCTION TO DELETE AT END IN A LINKED LIST

{
struct node *trav,ptr;
trav=head;
if(trav==NULL)
printf(“\n LIST IS EMPTY.”);
else
{
while(trav->next->next!=NULL)
{
trav=trav->next;
}
ptr=trav->next;
trav->next=NULL;
printf(“\n ELEMENT DELETED IS-:”,ptr->info);
free(ptr);
}
}

void main()
{
head=NULL;
int q,a,b,c,d;
start:
clrscr();
printf(“\n TO ADD AT THE END OF LIST PRESS 1.”);
printf(“\n TO ADD AT THE BEGINNING PRESS 2.”);
printf(“\n TO ADD IN BETWEEN PRESS 3.”);
printf(“\n TO DELETE AT THE BEGINNING PRESS 4.”);
printf(“\n TO DELETE AT THE END PRESS 5 .”);
printf(“\n TO DELETE AT A POSITION PRESS 6.”);
printf(“\n TO DISPLAY PRESS 7.”);
printf(“\n ENTER YOUR CHOICE.-:”);
scanf(“%d,&q);
switch(q)
{
case 1:

printf(“\n ENTER THE NUMBER-:”);


scanf(“%d”,&a);
insert_end(a);
break;
case 2:
printf(“\n ENTER THE NUMBER-:”);
scanf(“%d”,&a);
insert_beg(a);
break;
case 3:
printf(“\n ENTER THE NUMBER-:”);
scanf(“%d”,&a);
printf(“\n ENTER THE POSITION-:”);
scanf(“%d”,&b);
insert_aft(a,b);
break;
case 4:
delete_beg();
break;
case 5:
delete_end();
break;
case 6:
printf(“\n ENTER THE POSITION-:”);
scanf(“%d”,&b);
delete_pos(b);
break;

case 7:
display();
break;
default:
printf(“\n WRONG CHOICE ENTERED.”);
};
printf(“\n WISH TO CONTINUE(1/0).”);
scanf(“%d”,&d);
if(d==1)
goto start;
getch();
}

OUTPUT
TO ADD AT THE END OF LIST PRESS 1.
TO ADD AT THE BEGINNING PRESS 2.
TO ADD IN BETWEEN PRESS 3.
TO DELETE AT THE BEGINNING PRESS 4.
TO DELETE AT THE END PRESS 5 .
TO DELETE AT A POSITION PRESS 6.
TO DISPLAY PRESS 7.
ENTER YOUR CHOICE.-:1
ENTER THE NUMBER-:23
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:5
ELEMENT DELETED IS-:23
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:3
ENTER THE NUMBER-:32
ENTER THE POSITION-:1
WISH TO CONTINUE(1/0).1

ENTER YOUR CHOICE.-:6
ENTER THE POSITION-:1
ELEMENT DELETED IS-:32
WISH TO CONTINUE(1/0)….

You might also like