Thread: not sure why links in this program are not always null

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2023
    Posts
    33

    not sure why links in this program are not always null

    So laserlight asked me to talk about "my" linked list in the most recent thread i posted here about NULL pointers.

    However, I have only made edits to someone elses linked list, and i have been torturing the code (and subsequently, myself) to try and figure out how the program actually works.

    So my understanding of a linked list, is the nodes created with a struct type, and the last item on the list must always contain the NULL pointer...or else there would be no indicators of where the list begins and ends:

    Code:
    struct node
    {
      int value;
      struct node *next;
    };
    so we would have a series of nodes connected by the memory addresses for next, each link in the chain meaning two nodes are connected by next.

    However, I'm still not getting why sometimes the current->next node is not NULL, but only if you have created more than one node in the program:

    Code:
      //shows position and value of nodes at this point in the program
      //For some reason, curent->next stops being displayed as null after
      //you make the first node.
      printf("head value is %d, current value is %d\n", first->value, current->value);
      printf("first link: %p, current->next:"
             " %p, current: %p.\n", first->next, current->next, current);
    Output:

    S)how, A)dd, R)emove, Q)uit: A
    Type a value: 1
    head value is 1, current value is 1
    first link: (nil), current->next: (nil), current: 0x55723cd1bac0.
    S)how, A)dd, R)emove, Q)uit: A
    Type a value: 1
    head value is 1, current value is 1
    first link: 0x55723cd1bae0, current->next: 0x55723cd1bae0, current: 0x55723cd1bac0.
    S)how, A)dd, R)emove, Q)uit:
    I know that using casts this way violates the strict C standards you get when running -Wpedantic, but this is just an educational program. It has no practical value whatsoever, even the possible applications of this type of linked list are clear. You can just run this to better understand what the program does, i created the code above myself and the diagram aspect of "show()" just to demonstrate to a user how a linked list works...but i don't understand why current->next isn't null after we have created a node. It's null when you run through the list in show:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<smanage.h>
    struct node
    {
      int value;
      struct node *next;
    };
    //pointers declared with file scope
    struct node *first;
    struct node *current;
    struct node *previous;
    
    
    struct node *create()
    {
      struct node *space; 
      space = malloc(sizeof(struct node)); 
    
    
      return (space); 
    }
    
    
    int menu()
    {
      char choice;
    
    
      printf("S)how, A)dd, R)emove, Q)uit: ");
      choice = getchar();
    
    
      purge(); 
    
    
      return (toupper(choice)); 
    }
    
    
    void add()
    {
      //if first is NULL
      if (!first)
      {
        //make head node
        first = create(); 
        //Link current and first together
        current = first; 
      }
      else
      {
        current = first; 
        //link or re-link the nodes together until we
        //reach the end of the list
        while (current->next)
        {
          //connect current->next with current
          current = current->next; 
        }  
    
    
        //makes new current node
        current->next = create(); 
      }
      printf("Type a value: ");
      scanf("%d", &current->value); 
      purge(); 
    
    
      //shows position and value of nodes at this point in the program
      //For some reason, curent->next stops being displayed as null after
      //you make the first node.
      printf("head value is %d, current value is %d\n", first->value, current->value);
      printf("first link: %p, current->next:"
             " %p, current: %p.\n", first->next, current->next, current);
    }
    
    
    void show(void)
    {
      if (!first)
      {
        puts("No nodes to show.");
    
    
        return;
      }
    
    
      current = first; 
    
    
      puts("Showing all nodes: "); 
    
    
      int count = 1; 
      //loop until there aren't any more nodes
      while (current)
      {
        //don't show diagram if end of list is reached
        if(!current->next)
        {
          printf("Record %i: %i at %p and %p\n"
          , count, current->value, current, current->next);
    
    
          current = current->next; 
          count++;
        }
        else 
        {
          //show diagram if loop hasn't reached the end
          printf("Record %i: %i at %p and %p\n"
                 "                                    /\n"
                 "                                   /\n"
                 "                                  /\n" 
                 "                                 /\n"
                 "                                /\n"
                 "                               /\n" 
                 "                              /\n"
                 "                             /\n" 
          , count, current->value, current, current->next);
    
    
          current = current->next; 
          count++;
        }
      }
    }
    
    
    
    
    void delrec(void)
    {
      if (!first)
      {
        puts("No nodes to remove"); 
        return;
      }
      //use show to display node list
      puts("Choose node to remove: "); 
      show(); 
    
    
      int node, count; 
      //select node to remove, store value
      printf("node: ");
      scanf("%i", &node); 
      purge(); 
      puts("");
    
    
      count = 1; 
      //start list at first node like in
      //other functions
      current = first; 
    
    
      while (count != node)
      {
        //when user selected node is
        //reached, previous is linked with
        //current, current is linked with current->next
        previous = current; 
        current = current->next; 
        
        count++;
        //if number chosen doesn't reflect
        //real number of nodes...
        if (!current)
        {
          puts("node not found"); 
    
    
          return;
        }
      }
      //remove node by linking either
      //first or previous-> node with node past
      //the one user selected to remove
      if (!previous)
      {
        first = current->next; 
      }
      else
      {
        previous->next = current->next; 
      }  
      //user confirmation message
      printf("Node %i removed.\n", node); 
      //allow the memory allocated for that
      //node to be allocated again
      free(current); 
    }
    
    
    int main()
    {
      int choice = 0;
    
    
      while (choice != 'Q')
      {
        choice = menu(); 
    
    
        switch (choice)
        {
          case 'S':
            show(); 
            break;
          case 'A':
            add(); 
            break;
          case 'R':
            delrec(); 
            break;
          case 'Q':
            break; 
        }
      }
    
    
      return 0; 
    }
    P.S.: sorry if this is too much code at once for you, but i'm not telepathic and don't know which parts of this program i should post here. Also, smanage just contains functions i use for string input, one of those functions being "purge()" which clears the buffer:

    Code:
    void purge()
    {
    while(getchar != '\n');
    }
    Last edited by C_me_run; 12-09-2023 at 03:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Program, null terminator
    By dexstar in forum C Programming
    Replies: 2
    Last Post: 08-01-2013, 08:34 AM
  2. Replies: 1
    Last Post: 11-09-2010, 01:01 AM
  3. Why Do i get NULL pointer assignment In this program?
    By chottachatri in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2008, 05:18 AM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM

Tags for this Thread