#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
create();
display();
del_item(int);
ins_beg();
search(int);
struct node
{
int rollno;
char name[20];
struct node *link;
} *start=NULL,*p,*q,*prev;
int regno,rno;
char str[20];
void main()
{
int ch,i;
clrscr();
while(1)
{
printf("\n 1.create a linked list ");
printf("\n 2.insertion of node in front of the list ");
printf("\n 3.deletion of node based on rollno");
printf("\n 4.searching a node based on rollno");
printf("\n 5.display linked list ");
printf("\n 6.exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("\n linked list before insertion\n");
display();
ins_beg();
printf("\n linked list after insertion\n");
display();
break;
case 3:printf("\n enter the item to be deleted:");
scanf("%d",®no);
del_item(regno);
printf("\n linked list after deletion");
display();
break;
case 4:printf("\n searching for an node based on rollno");
scanf("%d",&rno);
search(rno);
break;
case 5:display();
break;
case 6:exit(0);
default:printf("\n error in choice");
}
}
}
create()
{
char choice='y';
clrscr();
start=NULL;
q=start;
while(choice=='y')
{
p=malloc(sizeof(struct node));
printf("\n enter the register number :");
scanf("%d",®no);
printf("\n enter the student name:");
scanf("%s",str);
p->rollno=regno;
strcpy(p->name,str);
p->link=NULL;
if(start==NULL)
{
start=p;
q=p;
}
else
{
q->link=p;
q=p;
}
printf("\n do you want to create another node (y/n");
choice=getch();
}
return 0;
}
display()
{
printf("\n the elements are");
p=start;
while(p!=NULL)
{
printf("\n register number :%d",p->rollno);
printf("\n NAME :%s",p->name);
p=p->link;
}
getch();
return 0;
}
ins_beg()
{
printf("\n enter register number:");
scanf("%d",®no);
printf("\n enter name of the student:");
scanf("%s",str);
p=malloc(sizeof(struct node));
p->rollno=regno;
strcpy(p->name,str);
p->link=start;
start=p;
return 0;
}
del_item(int regno)
{
p=start;
prev=NULL;
if(start==NULL)
{
printf("\n linked list is empty");
return 0;
}
if(start->rollno==regno)
{
start=start->link;
free(p);
return 0;
}
while((p->rollno!=regno)&&(p!=NULL))
{
prev=p;
p=p->link;
}
if(p==NULL)
printf("\n register number %d is not found in the linked list \n",regno);
else
prev->link=p->link;
return 0;
}
search(int rno)
{
int i=0;
p=start;
while(p!=NULL)
{
if(rno==p->rollno)
{
i++;
printf("\n node found at position %d",i);
printf("register number:%d",p->rollno);
printf("\n name:%s",p->name);
}
else
{
p=p->link;
i++;
}
}
printf("\n node with register number %d does not exist",regno);
return 0;
}
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#define NULL 0
create();
display();
del_item(int);
ins_beg();
search(int);
struct node
{
int rollno;
char name[20];
struct node *link;
} *start=NULL,*p,*q,*prev;
int regno,rno;
char str[20];
void main()
{
int ch,i;
clrscr();
while(1)
{
printf("\n 1.create a linked list ");
printf("\n 2.insertion of node in front of the list ");
printf("\n 3.deletion of node based on rollno");
printf("\n 4.searching a node based on rollno");
printf("\n 5.display linked list ");
printf("\n 6.exit");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("\n linked list before insertion\n");
display();
ins_beg();
printf("\n linked list after insertion\n");
display();
break;
case 3:printf("\n enter the item to be deleted:");
scanf("%d",®no);
del_item(regno);
printf("\n linked list after deletion");
display();
break;
case 4:printf("\n searching for an node based on rollno");
scanf("%d",&rno);
search(rno);
break;
case 5:display();
break;
case 6:exit(0);
default:printf("\n error in choice");
}
}
}
create()
{
char choice='y';
clrscr();
start=NULL;
q=start;
while(choice=='y')
{
p=malloc(sizeof(struct node));
printf("\n enter the register number :");
scanf("%d",®no);
printf("\n enter the student name:");
scanf("%s",str);
p->rollno=regno;
strcpy(p->name,str);
p->link=NULL;
if(start==NULL)
{
start=p;
q=p;
}
else
{
q->link=p;
q=p;
}
printf("\n do you want to create another node (y/n");
choice=getch();
}
return 0;
}
display()
{
printf("\n the elements are");
p=start;
while(p!=NULL)
{
printf("\n register number :%d",p->rollno);
printf("\n NAME :%s",p->name);
p=p->link;
}
getch();
return 0;
}
ins_beg()
{
printf("\n enter register number:");
scanf("%d",®no);
printf("\n enter name of the student:");
scanf("%s",str);
p=malloc(sizeof(struct node));
p->rollno=regno;
strcpy(p->name,str);
p->link=start;
start=p;
return 0;
}
del_item(int regno)
{
p=start;
prev=NULL;
if(start==NULL)
{
printf("\n linked list is empty");
return 0;
}
if(start->rollno==regno)
{
start=start->link;
free(p);
return 0;
}
while((p->rollno!=regno)&&(p!=NULL))
{
prev=p;
p=p->link;
}
if(p==NULL)
printf("\n register number %d is not found in the linked list \n",regno);
else
prev->link=p->link;
return 0;
}
search(int rno)
{
int i=0;
p=start;
while(p!=NULL)
{
if(rno==p->rollno)
{
i++;
printf("\n node found at position %d",i);
printf("register number:%d",p->rollno);
printf("\n name:%s",p->name);
}
else
{
p=p->link;
i++;
}
}
printf("\n node with register number %d does not exist",regno);
return 0;
}
No comments:
Post a Comment