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;

}

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...

Take a close look at the code I replaced.
Try to compile with extension .c
Look for "Notice : "


If you want to delete an item from the list, why dont
get input from user which student name to delete ( or student age ) ?

#define false  0
#define true   1
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));
  /* Notice : Why are you allocating memory here ??? ITs not used 
  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;
  /* Notice : Why are you allocating memory here ??? ITs not used 
  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;
  }
  /* Notice : this is not c++. Use free.
  delete Temp;
  */
  free(Temp);
  Temp2->Next=NULL;
  
  
  
  return 0;
  
 }
 
 
 
int main()
{
 int choice=0;
  /* Notice : this is not c++. No data type bool.
 bool Done=false;
  */
 int Done=false;
 
 
 student* Head;
 Head=NULL;
 
 for (; ;) 
 {
  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;
 }
 
 
printf("Press enter to end ....\n");
getchar();
return(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.