Like Tree5Likes

Linked Lists please help!!!

This is a discussion on Linked Lists please help!!! within the C Programming forums, part of the General Programming Boards category; Hey everyone, i am sending this message because i have a problem with my program in Linked Lists. All of ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    49

    Linked Lists please help!!!

    Hey everyone, i am sending this message because i have a problem with my program in Linked Lists. All of a sudden i can't print my list. Actually i have my insert function where i add the numbers and it seems to work ok.

    I tried so many times to change my function but still nothing. I have my head and tail pointing to NULL (struct Node *head = NULL, struct Node *tail = NULL) outside main(). I have my code and please i will appreciate it so much if you explain me what is the problem here and every time my function print_list() says the list is empty.

    Code:
    struct Node //My struct
    {
         int value;
         struct Node *next;
    };
    
    struct Node *head = NULL;
    struct Node *tail = NULL;
    
    void insert(struct Node *, int);
    void Delete(struct Node *);
    void empty_list(struct Node *);
    void print_list(struct Node *);
    struct Node *reverse(struct Node *);
    int size(struct Node *);
    
    int main() //Start of main() function
    {
           int choice, pl;
           int done = -1;
           int num, rev;
          
           while(done != 0) // I want to have a menu so the user has to choose one of the options
           {
             printf("                                MENU                \n");
             printf("                    ---------------------------    \n");
             printf(" 1. Insert an element in the list \n");
             printf("\n");
             printf(" 2. Delete an element from the list \n");
             printf("\n");
             printf(" 3. Print the length of the list \n");
             printf("\n");
             printf(" 4. Print if the list is empty or not \n");
             printf("\n");
             printf(" 5. Print the elements of the list \n");
             printf("\n");
             printf(" 6. Reverse list\n");
             printf(" \n");
             printf(" 7. Exit \n");
             printf("\n");
    
             printf(">> Give your choice: ");
             scanf("%d", &choice);
             printf("\n");
    
             switch(choice)
             {
                   case 1:
    
                       printf("\nGive a number: ");
                       scanf("%d", &num);
                       printf("\n");
                       insert(&head, num);
    
                  break;
    
                  case 2:
    
                      Delete(&head);
    
                  break;
    
                  case 3:
    
                      pl = size(&head);
                      printf("\nThe list has : %d elements", pl);
                      printf("\n\n");
    
                  break;
    
                  case 4:
    
                      empty_list(&head);
    
                  break;
    
                  case 5:
    
                      print_list(head);
    
                  break;
    
                  case 6:
    
                      rev = reverse(head);
                      printf("The list in reverce order is: %d ", rev);
                      printf("\n");
                      printf("\n");
    
                  break;
    
                  case 7:
    
                      done == 0;
    
                  break;
    
                  default:
                      printf("Wrong choice");
                      printf("\n");
           }
       }
    return 0;
    }//end of main() function
    
    //From this part and so on i started to create my functions
      
    void insert(struct Node *head, int num) //insert function
    {
           struct Node *temp1;
           temp1=(struct Node*)malloc(sizeof(struct Node)); 
           temp1 = head;
    
           while(temp1->next!=NULL)
               temp1 = temp1->next; 
    
               struct Node *temp; 
               temp = (struct Node*)malloc(sizeof(struct Node);
               temp->value = num; 
               temp->next = NULL; 
               temp1->next = temp; 
    
               printf("\nYour number has been inserted in your list \n");
               printf("\n\n");
    }
    
    void Delete(struct Node *head)//Delete function
    {
              struct Node *last;
              last = (struct Node *)malloc(sizeof(struct Node));
              last = head;
    
              struct Node *previous_last;
              previous_last = (struct Node *)malloc(sizeof (struct Node));
    
             while(last->next != NULL)
             {
                 previous_last = last;
                 last = previous_last->next;
             }
        
             previous_last->next = NULL;
             free(last);
             printf("The element has been deleted  \n");
             printf("\n");
    
    }
    
    void empty_list(struct Node *head)//empty_list funcion
    {
               struct Node *pointer = head;
    
               if (pointer == NULL)
               {
                    printf("The list is empty");
                    printf("\n");
               }
               else
               {
                   printf("The list is not empty");
                   printf("\n");
               }
    }
    
    struct Node *reverse(struct Node *head) //reverse function
    {
             struct Node *current = NULL;
             struct Node *temp;
             struct Node *first;
    
            while (first!= NULL)
            {
                temp = first;
                first = first->next;
                temp->next = current;
                current =temp;
            }
            return current;
    }
    
    void print_list(struct Node *head)//print_list fuction
    {
          struct Node *pointer = head;
    
           if(pointer==NULL)
           {
              printf("The list is empty");
              printf("\n");
           }
           else 
           {
               printf("The list is:");
               while(pointer->next != NULL)
              {
                 ("%d", pointer->value);
                 printf("\n");
                 pointer = pointer->next;
              }
           }
    }
    
    int size(struct Node *head)//size (of the list, how many elements are in the list) function
    {
            struct Node *temp = head;
            int counter = 0;
    
            while(temp)
            {
                counter = counter + 1;
                temp = temp->next;
            }
                return counter;
    }
    Thank you
    Last edited by ValL; 11-08-2012 at 07:22 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Take a quick few minutes and indent your code better - that's bizarre and difficult to study.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by ValL View Post
    Actually i have my insert function where i add the numbers and it seems to work ok.
    Unfortunately it doesn't.
    You need to pass head by reference to both Insert() and Delete() to be able to update the global var head.
    Kurt

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Sorry my friend i change it several times and it still doesn't work. Is it possible to explain what do you mean by passing the head by reference? In my main function i did this insert(&head, num), delete(&head). This is all wrong? I thought that if i give to the function insert() the address of head will be ok.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Turn up your compiler warnings and pay attention to them. You're passing in a node**, but accepting a node*.

    Of course C doesn't have references, but passing the pointer itself by pointer is only half the fix anyway. It still never updates head.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I have tried to make it wright but it doesn't change. Can you tell me how i have to make my function to work properly? The compiler won't let me know what is the mistake so i can not understand it


    • Let's take the first function insert()


    i have this:

    Code:
    void insert(struct Node *head, int num)
    {
        struct Node *temp1;
        temp1=(struct Node*)malloc(sizeof(struct Node)); 
        temp1 = head; 
    
        while(temp1->next!=NULL)
          temp1 = temp1->next; 
    
          struct Node *temp; 
          temp = (struct Node*)malloc(sizeof(struct Node));         
        
          temp->value = num;
          temp->next = NULL; 
          temp1->next = temp; 
    
          printf("\nThe element has inserted to the list \n");
          printf("\n\n");
    
    }
    In this function how it should change to be ok ?
    i have to do that? void insert(struct Node **head, int num) and then struct Node *temp1 = *head?

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,514
    You declare head here; where in your code does head stop being NULL?

    Code:
    struct Node *head = NULL;
    I did not see anywhere it stops being NULL.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I don't know how to do it. How i am suppose to make head stop being NULL??

  9. #9
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You assign a non-NULL value to it. When you create the first node in the list, it becomes the head and it's location should be assigned to head. Somewhere in your code you should be saying "head = ..."
    stahta01 likes this.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    I understand what do you mean my friend but still i can't do it. I don't know what to assign to head to change it Maybe i have to change my hole function (insert) now (i have to admit that i am not so expert in pointers that's why maybe i can't understand the hole thing ). I have tried it so many times to change it but still nothing. Although, i appreciate your help very much.

    Thank you.
    Last edited by ValL; 11-08-2012 at 03:30 PM.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    ValL i suggest you take a piece of paper and a pencil and draw what your code does... run your code in paper.I think this will help you realize what is happening

  12. #12
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The trick is that inserting a node isn't always exactly the same. Most of the time, you want to seek through the list and append a new node (which is what you're doing). Your first time is different. If the list is empty, you need to initialize certain values in your list (like head). The same goes for removing a node - deleting the last node requires you to do certain things that you wouldn't have to do most of the time.

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    49
    Thank you very much for your help. I admit that i had to work very hart to find the problem. Finally i end up with changing my hole program to make it work ok. Here i give you an example of what i did and tell me if is ok now or not.

    • This is my insert function: I have now my head pointer in main pointing to NULL and not outside main like a global variable.


    Code:
    struct Node
    {
        int Data;
        struct Node *Next;
    }*Head;
    
    
    void insert_end(int num)
    {
      struct Node *temp1, *temp2;
    
      temp1=(struct Node *)malloc(sizeof(struct Node));
      temp1->Data=num;
    
      temp2=Head;
    
      if(Head == NULL)
      {
         Head=temp1;
         Head->Next=NULL;
      }
      else
      {
         while(temp2->Next != NULL)
         temp2=temp2->Next;
    
         temp1->Next=NULL;
         temp2->Next=temp1;
      }
    }
    
    
    • This is my delete function now one small change only here (last = Head)
    void Delete() { struct Node *last; last = (struct Node *)malloc(sizeof(struct Node)); last = Head; struct Node *previous_last; previous_last = (struct Node *)malloc(sizeof (struct Node)); while(last->Next != NULL) { previous_last = last; last = previous_last->Next; } previous_last->Next = NULL; free(last); printf("The element has been deleted \n"); printf("\n"); }
    • and now this is my main function
    int main()//start of main { int num; Head=NULL; //Head is now pointing to NULL inside main. ... ... //I failed to put all the lines here because it's a big code and ... i just wanted to see and understand what i have done and tell me if is better now. case 1: //for adding new numbers { printf("\nPlease give a number for insertion: "); scanf("%d", &num); printf("\n"); insert_end(num); break; } //And so on for all the other functions ... ... ... ... ... return 0; }//End of main
    Thank you
    Last edited by ValL; 11-09-2012 at 05:02 AM.

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    49

    Sparse Matrices in C help

    Can anyone tell me how a sparse matrix work with linked lists? I truly can't understand. Now i have to make the array (insert values in it) and print it.
    I have tried to find out about it but nothing.
    All help will be appreciate.

    Thanx!!

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    With not many details because i do not remember.A sparse matrix has many zeros.Assume you have an array 100 x 100 and the 80% of the elements are zero.Why do store the whole matrix (i mean declare a 2D array of 100 x 100 ) and waste so much memory?You can create a list,which for example would have in every node
    • value
    • index i
    • index j
    • pointer to next node

    and only store the nodes that have non-zero value.

Page 1 of 5 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 12:07 PM
  2. Replies: 4
    Last Post: 05-01-2010, 10:19 PM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 01:49 AM
  4. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 11:08 AM
  5. Linked List of Linked lists Revisited.
    By Qui in forum C++ Programming
    Replies: 11
    Last Post: 04-11-2004, 09:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21