my delete operation doesn't work.the element which is added last is being deleted.ı want to delete the one ı want; not the last entered.how can ı do that?can you help me?thank you very much for your consideration...
#include <stdio.h>
#include <stdlib.h>
typedef struct student
{
char name [25];
int age;
struct student* Next;
}student;
student* Addstudent(student* Head)
{
student* Fulanito;
student* Temp;
Fulanito=(student*)malloc(sizeof (student));
Temp=(student*)malloc(sizeof (student));
printf("Name of Student\n");
scanf("%s",&Fulanito->name);
printf("Age of Student\n");
scanf("%d",&Fulanito->age);
Fulanito->Next=NULL;
if (Head==NULL)
Head=Fulanito;
else
{
Temp=Head;
while (Temp->Next!=NULL)
{
Temp=Temp->Next;
}
Temp->Next=Fulanito;
}
return Head;
}
void ViewList(student* Head)
{
student* Temp;
Temp=(student*)malloc(sizeof (student));
Temp=Head;
while(1)
{
printf("\nName of Student %s",Temp->name);
printf("\nAge of Student %d\n",Temp->age);
if (Temp->Next==NULL)
break;
Temp=Temp->Next;
}
}
int DeleteStudent(student* Head)
{
if (Head==NULL)
{
printf("\nList is empty\n");
return 1;
}
student* Temp;
student* Temp2;
Temp=(student*)malloc(sizeof (student));
Temp2=(student*)malloc(sizeof (student));
Temp=Head;
while (Temp->Next!=NULL)
{
Temp2=Temp;
Temp=Temp->Next;
}
delete Temp;
Temp2->Next=NULL;
return 0;
}
int main()
{
int choice=0;
bool Done=false;
student* Head;
Head=NULL;
for (; ;) // here for(;(space) ;) system shows here as smile...it is only a for statement..there is no condition
{
printf("\nWhat would you like to do\n");
printf("1)Insert student\n");
printf("2)View List\n");
printf("3)Delete Student\n");
printf("4)Quit\n");
scanf("%d",&choice);
fflush(stdin);
switch (choice)
{
case 1: Head=Addstudent(Head);
break;
case 2:
ViewList(Head);
break;
case 3: DeleteStudent(Head);
break;
case 4: Done=true;
break;
}
if (Done)
break;
}
return 0;
}