Professional Documents
Culture Documents
Lab 9
Lab 9
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void ins_front();
void ins_end();
void ins_pos();
void del();
void search();
void display();
struct NODE
{
int id,sem;
char name[10];
struct NODE *link;
};
typedef struct NODE node;
node *start=NULL,*temp,*new1,*cptr,*prev;
int i,choice;
char ch;
void main()
{
clrscr();
do
{
printf("1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display\n");
printf("enter ur choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:ins_front();
break;
case 2:ins_end();
break;
case 3:ins_pos();
break;
case 4:del();
break;
case 5:search();
break;
case 6:display();
break;
}
printf("do u want to continue\n");
fflush(stdin);
scanf("%c",&ch);
}
while(ch=='y');
getch();
}
void ins_front()
{
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
if(start==NULL)
{
new1->link=NULL;
start=new1;
}
else
{
new1->link=start;
start=new1;
}
//return(0);
}
void ins_end()
{
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
if(start==NULL)
{
new1->link=NULL;
start=new1;
}
temp=start;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=new1;
new1->link=NULL;
//return(0);
}
void ins_pos()
{
int pos,count;
new1=(node*) malloc(sizeof(node));
printf("enter student id,name,sem\n");
scanf("%d%s%d",&new1->id,new1->name,&new1->sem);
printf("enter the position to be insert\n");
scanf("%d",&pos);
if(pos==1)
{
new1->link=start;
start=new1;
}
count=2;
prev=start;
temp=start->link;
while(count<pos && temp!=NULL)
{
prev=temp;
temp=temp->link;
count++;
}
new1->link=temp;
prev->link=new1;
//return(0);
}
void del()
{
int key;
printf("enter the student id to be delete\n");
scanf("%d",&key);
if(start->id==key)
{
temp=start;
start=start->link;
free(temp);
}
prev=start;
temp=start->link;
while(temp!=NULL && temp->id!=key)
{
prev=temp;
temp=temp->link;
}
if(temp==NULL)
{
printf("student with that id does not exist\n");
//return 0;
}
prev->link=temp->link;
}
void search()
{
int key;
printf("enter the student id to be search\n");
scanf("%d",&key);
temp=start;
while(temp!=NULL)
{
if(temp->id==key)
{
printf("student with that id exist & his detals are\n");
printf("%d%s%d",temp->id,temp->name,temp->sem);
printf("do u want to update(y/n)\n");
ch=getche();
if(ch=='y')
{
printf("enter new id,name,sem\n");
scanf("%d%s%d",&temp->id,temp->name,& temp->sem);
}
// return;
}
temp=temp->link;
}
}
void display()
{
printf("STUDENTS INFORMATION\n");
printf("ID NAME SEM\n");
temp=start;
while(temp!=NULL)
{
printf("%d\t%s\t%d\n",temp->id,temp->name,temp->sem);
temp=temp->link;
}
OUTPUT