Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 5

9.

Write a C Program using dynamic variables and pointers, to


construct a singly linked list consisting of the following
information in each node: student id (integer), student name
(character string) and semester (integer). The operations to
be supported are:
a. The insertion operation
1. At the front of a list
2. At the back of the list
3. At any position in the list
b. Deleting a node based on student id. If the specified node
is not present in the list an error message should be
displayed. Both the options should be demonstrated.
c. Searching a node based on student id and update the info.
content. If the specified node is not present in the list
an error message should be displayed. Both situations should
be displayed.
d. Displaying all the nodes in the list.

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

1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display


enter ur choice
1
enter student id,name,sem
11 DUVAN 3
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
1
enter student id,name,sem
22 PRAJWAL 4
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
6
STUDENTS INFORMATION
--------------------
ID NAME SEM
---------------------
22 PRAJWAL 4
11 DUVAN 3
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
3
enter student id,name,sem
33 SACHIN 3
enter the position to be insert
2
do u want to continue
y
1:ins_front 2:ins_end 3:ins_pos 4:delete 5:search 6:display
enter ur choice
6
STUDENTS INFORMATION
ID NAME SEM
22 PRAJWAL 4
33 SACHIN 3
11 DUVAN 3
do u want to continue
n

You might also like