Implementation of linked list
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void end_insertion();
void beg_deletion();
void end_deletion();
void display();
struct node
{
int info;
struct node *next;
}typedef node1;
node1 *first=NULL;
void main()
{
int ch;
clrscr();
cq:
printf("\npress 1 for beg insert\n");
printf("press 2 for end insert\n");
printf("press 3 for beg delete\n");
printf("press 4 for end delete\n");
printf("\n press 6 for display\n");
printf("press 5 for exit\n");
printf("enter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:beg_insertion();
goto cq;
case 2:end_insertion();
goto cq;
case 3:beg_deletion();
goto cq;
case 4:end_deletion();
goto cq;
case 5:exit(0);
goto cq;
case 6:display();
goto cq;
default:printf("wrong input");
goto cq;
}
}
void beg_insertion()
{ int item1;
node1 *ptr=(node1*)malloc(sizeof(node1));
printf("enter link list\n");
scanf("%d",&item1);
ptr->info=item1;
if(first==NULL)
{first=ptr;
ptr->next=NULL;
}
else
{ptr->next=first;
first=ptr;
}
getch();
}
void end_insertion()
{
int item2;
node1 *temp;
node1 *ptr=(node1*)malloc(sizeof(node1));
printf("enter link list\n");
scanf("%d",&item2);
ptr->info=item2;
if(first==NULL)
{
ptr->next=NULL;
first=ptr;
}
else
{
temp=first;
while(temp->next!=NULL)
{
temp=temp->next;
}
ptr->next=NULL;
temp->next=ptr;
}
getch();
}
void beg_deletion()
{
if(first==NULL)
printf("linked list is empty");
else
{
node1 *ptr;
ptr=first;
first=first->next;
printf("deleted element is %d",ptr->info);
free(ptr);
}getch();
}
void end_deletion()
{
node1 *loc,*ptr;
if(first->next==NULL)
{
ptr=first;
free(ptr);
}
else if(first->next==NULL)
{
ptr=first;
first=NULL;
free(ptr);
}
else
{
loc=first;
ptr=first->next;
while(ptr->next!=NULL)
{
ptr=ptr->next;
loc=loc->next;
}
loc->next=NULL;
printf("deleted element is %d",ptr->info);
free(ptr);
}
}
void display()
{node1 *ptr;
ptr=first;
if(first==NULL)
{printf("empty"); return;}
else
{
while(ptr!=NULL)
{
printf("%d",ptr->info);
ptr=ptr->next;
}
}
getch();
}
0 comments:
Post a Comment