I want a C program for : If the list of students’ information is represented by a linked list, write a program which implements deletion of the node with given student ID. The student information which will be captured is Student ID and Student Name. Input:Input consists of list of students with their IDs and their names and the ID of the student whose information should be deleted from the linked list.Output:The information of all the students after the student with given ID is removed.Sample Input:Enter the number of students.3Enter the information of the studentsInformation of student 1:Enter student ID: 101Enter Student Name: JohnInformation of student 2:Enter student ID: 102Enter Student Name: SteveInformation of student 3:Enter student ID: 103Enter Student Name: CharlieEnter student ID whose record should be deleted:102Sample Output:Student information after deletion101 John103 Charlie

Questions by souji1425

Showing Answers 1 - 3 of 3 Answers

baseersd

  • Jul 23rd, 2007
 

#include
struct node
{
int id;
char name[25];
struct node *next;??????
};

typedef struct node* NODE;

NODE CreateNode()
{
?NODE new_node;
?new_node=(NODE)malloc(sizeof(struct node));
?if(new_node == NULL)
?{
? printf("nCannot allocate memory");
? return NULL;???????????
?}
?new_node->next=NULL;
?return new_node;
}

void Getinfo(NODE new_node)
{
???? printf("nEnter the Student ID :: ");
???? scanf("%d",&(new_node->id));
???? printf("nEnter the Student Name ::");
???? fflush(stdin);
???? scanf("%s",new_node->name);
???? fflush(stdin);
}

void Printinfo(NODE Head)
{
???? NODE new_node=Head;
???? while(new_node!=NULL)
???? {
????? printf("n%d :",new_node->id);
????? printf("t%s",new_node->name);?????
????? new_node=new_node->next;
????? }
}

NODE Addtolist(NODE Head)
{
???? NODE new_node,curr,prev;
???? new_node=CreateNode();
Getinfo(new_node);
curr=Head;
prev=NULL;
if(Head->id > new_node->id)
{
??????????? new_node->next=Head;
??????????? return new_node;
}

while(curr != NULL && curr->id < new_node->id )
{
prev=curr;
curr=curr->next;
}
prev->next=new_node;
new_node-> next=curr;
return Head;?
}
NODE Delete_ID(int del_id,NODE Head)
{
?NODE curr,prev;
????
curr=Head;
prev=NULL;
if(Head->id == del_id)
{
???????? free(curr);??
??????????? return Head->next;
}

while(curr != NULL )
{
?if(curr->id == del_id )
{
???????????? prev->next=curr->next;
???????????? free(curr);
}
else
{
???????????? prev=curr;
???????????? curr=curr->next;
}
}
?
return Head;
}

int main()
{
??? NODE Head=NULL;
??? int No_of_students;
??? int del_id;
??? printf("nEnter the number of students ::");
??? scanf("%d",&No_of_students);
??? while(No_of_students--)
??? {
?????? if(Head == NULL)
?????? {
???????? Head=CreateNode();
???????? Getinfo(Head);???????
??????? }
??????? else
???????? Head=Addtolist(Head);???
??? }
??? Printinfo(Head);
??? printf("nEnter student ID whose record should be deleted :: ");
??? scanf("%d",&del_id);
??? Head=Delete_ID(del_id,Head);
??? printf("nStudent information after deletionn");
??? Printinfo(Head);
???
getch();
return 0;
}


Regards,
Syed Baseer Ahmed
baseersd @ gmail . com

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions