Thread: Linked lists.....

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Linked lists.....

    This is due on Thursday (about 8 exercises) using linked lists. This is not how I have my program structured I just trimmed down eveyrthing and got it to this just to test if it would work, but it's not. On the second run instead of going in thruh the list, the information that I enter on Temp overrides what Head has. I have been trying this the whole day and could not come up with an answer. Please help. (This time I won't delete my post right away, I promise)

    Code:
     
     
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct Student
    {
     int x;
     char nombre[30];
     struct Student* Next;
    }Student;
    
    int main()
    {
     Student *Head=NULL;
     Student *Temp;
     Student *Temp2;
     Temp=(Student*)malloc(sizeof(Student));
     Temp2=(Student*)malloc(sizeof (Student));
     int choice;
     printf("Insert Info\n");
     while (1)
     {
      printf("Press 1 to exit\n");
      scanf("%d",&choice);
      if (choice==1)
       break;
      printf("Student's Name\n");
      scanf("%s",&Temp->nombre);
      Temp->Next=NULL;
      if(Head==NULL)
       Head=Temp;
      else
      {
       Temp2=Head;
       while (Temp2->Next!=NULL)
       {
    	Temp2=Temp2->Next;
       }
       Temp2->Next=Temp;
       
      
      }
     
     
     
     }
     
    
     return 0;
     
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're trying to reuse the memory for Head. This won't work very well, so you need to call malloc for each new node so that each node is unique. You'll have an easier time working with the structure when you don't have self references all over the place.
    My best code is written with the delete key.

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Thanks Prelude......

    Well thank you, you were right (as always) and without further ado......my linked list....

    Code:
     
     
    #include <stdio.h>
    #include <stdlib.h>
     typedef struct Estudiante 
     {
      char name [25];
      int age;
      struct Estudiante* Next;
      
     }Estudiante;
     
     Estudiante* AddEstudiante(Estudiante* Head)
     {
     
    	 Estudiante* Fulanito;
      Estudiante* Temp;
      
      Fulanito=(Estudiante*)malloc(sizeof (Estudiante));
      Temp=(Estudiante*)malloc(sizeof (Estudiante));
      printf("Name of Student\n");
    	 scanf("%s",&Fulanito->name);
    	 fflush(stdin);
      printf("Age of Student\n");
      scanf("%d",&Fulanito->age);
      fflush(stdin);
    	 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(Estudiante* Head)
     {
      Estudiante* Temp;
      Temp=(Estudiante*)malloc(sizeof (Estudiante));
      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(Estudiante* Head)
     {
      if (Head==NULL)
      {
       printf("\nList is empty\n");
       return 1;
      }
      
      Estudiante* Temp;
      Estudiante* Temp2;
      Temp=(Estudiante*)malloc(sizeof (Estudiante));
      Temp2=(Estudiante*)malloc(sizeof (Estudiante));
      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;
     
     
     Estudiante* 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=AddEstudiante(Head);
    	
    	  
    	  break;
      case 2:    
    	   ViewList(Head);
    	   break;
      case 3: DeleteStudent(Head);
    	break;
     
      case 4: Done=true;
    	break;
      }
      if (Done)
       break;
     }
     
     
    
     return 0;
    
    }
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >fflush(stdin);
    Bad.

    >delete Temp;
    This is C. That line shouldn't even compile. Also, I don't see where you free() all of the memory you allocated in the program.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Quote Originally Posted by XSquared
    >fflush(stdin);
    Bad.

    >delete Temp;
    This is C. That line shouldn't even compile. Also, I don't see where you free() all of the memory you allocated in the program.
    Well it does, and I didn't really notice it until now, as you can probably guess I am more used to C++.

    About that memory freeing stuff, that would be on my todo list. I also checked for the list being empty and people trying to delete stuff while it's empty, but it's not posted on there.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    int DeleteStudent(Estudiante* Head)
     {
      if (Head==NULL)
      {
       printf("\nList is empty\n");
       return 1;
      }
      
      Estudiante* Temp; // Not C!
      Estudiante* Temp2; // Not C!
      Temp=(Estudiante*)malloc(sizeof (Estudiante));
      Temp2=(Estudiante*)malloc(sizeof (Estudiante));
      Temp=Head;
      while (Temp->Next!=NULL)
      {
       Temp2=Temp;
       Temp=Temp->Next;
      }
    
      delete Temp; // Not C!
    Unless you're using C99 (unlikely), you cannot declare variables except a the beginning of a block. And delete is not an operator that frees memory in C.

    >bool Done=false;
    Unless you're using C99, C does not have a bool type. I get the distinct feeling that you are trying to write C but compiling as C++. This is hiding a lot of the legitimate C errors that you would get.
    My best code is written with the delete key.

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I get the distinct feeling that you are trying to write C but compiling as C++. This is hiding a lot of the legitimate C errors that you would get.
    Oops.........and you'd be right...I guess college professor would not like that.......need to change that.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM