Sei sulla pagina 1di 3

#include<conio.

h>
#include<stdio.h>
#include<stdlib.h>
struct link
{
int data;
struct link *next;
};
struct link *start;
void createlist(struct link *);
void insertbeg(struct link *);
void insertend(struct link *);
void insertloc(struct link *);
void display(struct link *);
void main()
{
char ch='1';
struct link *node;
clrscr();
node=(struct link *)malloc(sizeof(struct link));
if(node==NULL)
{
printf("Out of memory.");
exit(0);
}
start=node;
createlist(node);
while(ch<='4')
{
printf("\nLINKED LIST MENU");
printf("\n1.INSERT AT BEG");
printf("\n2.INSERT AT END");
printf("\n3.INSERT AT SPECIFIC POSITION");
printf("\n4.DISPLAY");
printf("\nEnter ur choice :");
fflush(stdin);
ch=getchar();
switch(ch)
{
case '1': insertbeg(node);
break;
case '2': insertend(node);
break;
case '3': insertloc(node);
break;
case '4': display(node);
break;
default: printf("\nWrong choice. ");
}}}
void createlist(struct link *node)
{
char ch;
int i=1;
printf("\nEnter the value for %d node: ",i);
scanf("%d",&node->data);
node->next=NULL;
i++;
printf("\nPress 'n' to quit and other key to continue :");
fflush(stdin);
ch=getchar();
while(ch!='n')
{
node->next=(struct link*)malloc(sizeof(struct link));
if(node->next==NULL)
{
printf("Out of memory :");
exit(0);
}
node=node->next;
printf("\nEnter the value of %d node :",i);
scanf("%d",&node->data);
node->next=NULL;
i++;
printf("\nPress 'n' to quit and other key to continue :");
fflush(stdin);
ch=getchar();
}}
void insertbeg(struct link *node)
{
struct link *curr;
curr=(struct link*)malloc(sizeof(struct link));
if(curr==NULL)
{
printf("Out of memory ;");
exit(0);
}
curr->next=node;
printf("\nInput 1st node value :");
scanf("%d",&curr->data) ;
curr->next=node;
start=curr;
}
void insertend(struct link *node)
{
struct link *curr;
while(node->next!=NULL)
node=node->next;
curr=(struct link*)malloc(sizeof(struct link));
if(curr==NULL)
{
printf("Out of memory ;");
exit(0);
}
curr->next=NULL;
node->next=curr;
printf("\nInput the last node value :");
scanf("%d",&curr->data);
}
void display(struct link *node)
{
printf("\nValues of node in the list are \n");
while(node!=NULL)
{
printf("%d\n",node->data);
node=node->next ;
}
printf("\n");
}
int count(struct link *node)
{
int i=0;
while(node!=NULL)
{
i++;
node=node->next;
}
return i ;
}
void insertloc(struct link *node)
{
int loc,c=0,i=1;
struct link *curr,*prev;
printf("\nEnter the location at which node will be inserted :") ;
scanf("%d",&loc);
c=count(node);
if(loc>c)
{
printf("\nEntered location exceed no of nodes .");
return;
}
curr=(struct link*)malloc(sizeof(struct link));
if(curr==NULL)
{
printf("Out of memory ;");
exit(0);
}
while(i<loc)
{
prev=node;
node=node->next;
i++;
}
printf("\nInput node value at %d location :",loc);
scanf("%d",&curr->data);
if(loc==1)
start=curr;
else
prev->next=curr;
curr->next=node;
}t link *curr,*prev;
printf("\nEnter the location at which node will be inserted :"

Potrebbero piacerti anche