Vous êtes sur la page 1sur 3

/* Title: Program for Singly Link List

Licenes:Educational*/
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *next;
} node;
int no,i,ch,x,p;
char ans;
struct node *cnt,*temp,*newnode,*start,*null;
void create();
void display();
void del(int p);
void add(int p);
void search(int);
void main()
{
clrscr();
do
{
printf("\n ********MENU****************");
printf("\n1.create\n2.Display\n3.Delete\n4.Add\n5.search\n6.exit
");
printf("\n Enter your choice :");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
getch();break;
case 2:display();
getch();break;
case 3:printf("\n Enter the position of a node you want
to delete:");
scanf("%d",&p);
del(p);
printf("\n The given position node is deleted");
getch();break;
case 4:printf("\n Enter the position of a node you want
to add:");
scanf("%d",&p);
add(p);
printf("\n The given position node is added");
break;
case 5:printf("\n Enter the data you want to search:");
scanf("%d",&x);
search(x);
getch();break;
case 6:exit(0);
getch();break;
}
} while(ch!=7);
getch();
}
/**********Create_Function**************************/
void create()
{
start=null;
do
{
newnode=malloc(sizeof(struct node));
newnode->next=null;
printf("\n Enter the data");
scanf("%d",&newnode->data);
if(start==null)
start=newnode;
else
{
cnt=start;
while(cnt->next!=null)
{
cnt=cnt->next;
}
cnt->next=newnode;
}
printf("\n Do you want to continue");
flushall();
scanf("%c",&ans);
} while(ans!='n');
}
/**********Display_Function**************************/
void display()
{
if(start==null)
printf(" List is empty");
else
{
printf("\n");
cnt=start;
while(cnt!=null)
{
printf("%d->",cnt->data);
cnt=cnt->next;
}
//printf("%d->",start->data); //for CLL
}
}
/**************Delete_Function*******************/
void del(int p)
{
if(p==1)
{
temp=start;
start=start->next;
}
else
{
cnt=start;
for(i=1;i<p;i++)
cnt=cnt->next;
temp=cnt->next;
cnt->next=cnt->next->next;
}
free(temp);
}
/*****************Add_Function**********************/
void add(int p)
{
newnode=malloc(sizeof(struct node));
newnode->next=null;
printf("\n Enter the data");
scanf("%d",&newnode->data);
if(p==1)
{
temp=start;
start=newnode;
newnode->next=temp;
}
else
{
cnt=start;
for(i=1;i<p;i++)
cnt=cnt->next;
temp=cnt->next;
cnt->next=newnode;
}
newnode->next=temp;
}
/*********Search function***************/
void search(int x)
{
int flag=1;
cnt=start;
while(cnt->next!=null)
if(cnt->data==x)
{
printf("\n Number is found");
flag=0;
}
cnt=cnt->next;
}
if(flag)
printf("\n NUmber is not found");
return(0);
}