Write a C program using dynamic variables and pointers, to construct a singly linked list containing of the following information in each node: student ID(int), name(string) and semester(int). The operations to be supported are:
a] The insertion operation:
i] At the front of the list
ii] At the back of the list
iii] At any position of the list
b] Deleting a node based on student ID. If the specified node is not present in the list, an error message to be displayed.
c] Searching a node based on student ID and update the information content. If the specified node is not present in the list, an error message to be displayed.
d] Displaying all nodes of a file.
#include"stdio.h" /* Including header files*/
#include"conio.h"
#include"stdlib.h"
#include"alloc.h"
#include"string.h"
struct node /* Structure template declaration*/
{
char name[20];
int id,sem;
struct node *next;
};
typedef struct node *Node;
Node getnode(); /*function prototype*/
void freenode(Node);
Node insertf(struct node,Node);
Node insertr(struct node,Node);
Node insert_at(struct node,int,Node);
Node delete_info (int,Node);
void search_up(int,Node);
void display (Node);
Node create(struct node *);
void main()
{
Node first=NULL;
struct node s;
int ch,tid,pos;
clrscr();
do /*do while statement*/
{
clrscr();
printf("\n1.Insert at front of list");
printf("\n2.Insert at back of list");
printf("\n3.Insert at any position of list");
printf("\n4.delete a student id");
printf("\n5.Search a Node");
printf("\n6.Display the contents\n7. exit");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch) /* switch statement begin*/
{
case 1:
create(&s); /*create function call*/
first=insertf(s,first); /*insertf function call*/
break;
case 2:
create(&s);
first=insertr(s,first); /*insertr function call*/
break;
case 3:
create(&s);
printf("\n Enter the position");
scanf("%d",&pos);
first=insert_at(s,pos,first); /*insert_at function call*/
break;
case 4:
printf("\n Enter the student id");
scanf("%d",&tid);
first=delete_info(tid,first); /*delete function call*/
getch();
break;
case 5:
printf("\n Enter the student id to be searched");
scanf("%d",&tid);
search_up(tid,first); /*search_up function call*/
getch();
break;
case 6:
printf("\n Student Information\n");
display(first); /*display function call*/
getch();
break;
case 7:
exit(0);
} /*end of switch statement*/
} /*end of do while loop*/
while(1);
} /*end of function main*/
Node insertf(struct node s,Node first) /*insertf function defination*/
{
Node temp; /* pointer temp to type Node*/
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
temp->next=first;
return(temp);
}
}
Node getnode() /*getnode function defination*/
{
Node temp;
temp=(Node)malloc(sizeof(struct node));/*creating node using malloc function*/
return temp;
}
Node insert_at(struct node s,int pos,Node first)
{
Node temp,prev,cur;
int count;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL && pos==1)
{
return temp;
}
if(first==NULL)
{
printf("\n invalid position....");
getch();
return first;
}
if(pos==1)
{
temp->next=first;
return temp;
}
prev=NULL;
cur=first;
count=1;
while(cur!=NULL && pos!=count)
{
prev=cur;
cur=cur->next;
count++;
}
if(pos==count)
{
prev->next=temp;
temp->next=cur;
return first;
}
printf("\n invalid position");
getch();
return first;
}
void freenode(Node p)
{
free(p); /*removing a node using free function*/
}
Node insertr(struct node s,Node first) /*insertr function defination*/
{
Node temp,cur;
temp=getnode();
strcpy(temp->name,s.name);
temp->id=s.id;
temp->sem=s.sem;
temp->next=NULL;
if(first==NULL)
return(temp);
else
{
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=temp;
return first;
}
}
Node create(struct node *p) /*create node function defination*/
{
printf("\n Enter student name");
scanf("%s",p->name);
printf("\n Enter student id");
scanf("%d",&p->id);
printf("\n Enter the semester");
scanf("%d",&p->sem);
return;
}
Node delete_info(int id,Node first)
{
Node prev,cur;
if(first==NULL)
{
printf("\n list is empty");
getch();
return first;
}
if(id==first->id)
{
cur=first;
first=first->next;
freenode(cur);
printf("\n deletipn successful");
return first;
}
prev=NULL;
cur=first;
while(cur!=NULL&&id!=cur->id)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
return first;
}
prev->next=cur->next;
freenode(cur);
printf("\n deletion successful");
return first;
}
void search_up(int id,Node first)
{
Node prev,cur;
int choice,ch;
if(first==NULL)
{
printf("\n list is empty");
getch();
return;
}
cur=first;
while(cur!=NULL&&id!=cur->id)
{
cur=cur->next;
}
if(cur==NULL)
{
printf("\n student with given id was not found");
getch();
}
else
{
printf("\nstudent found");
printf("\n wnt to update...(y/n)");
ch=getche();
if(ch=='y')
{
printf("\n update field...");
printf("\n1:Name");
printf("\n2:Id");
printf("\n3:Sem");
printf("\nEnter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\n enter the new name:");
scanf("%s",&cur->name);
break;
case 2:printf("\n enter the new ud:");
scanf("%d",&cur->id);
break;
case 3:printf("\n enter the new sem:");
scanf("%s",&cur->sem);
break;
defualt:printf("\n invalid choice");
}
}
}
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
printf("\n list is empty");
return;
}
temp=first;
printf("%20s%10s%5s\n","student name","id","sem");
printf("________________________________________\n");
while(temp!=NULL)
{
printf("%20s",temp->name);
printf("%10d",temp->id);
printf("%5d",temp->sem);
temp=temp->next;
printf("\n");
}
}
No comments:
Post a Comment