Thread: pointer of current node should be point to first node

  1. #1
    Registered User
    Join Date
    Feb 2022
    Posts
    73

    pointer of current node should be point to first node

    I am trying to make circuler linkel list. I have created single linked list. In a code, pointer of current node point to previous node. I want to change pointer so that pointer of current node will point to first node in the list.

    Code:
    #include<stdio.h>
    
    #include<stdlib.h>
      
    struct node 
    {
        int data;
        struct node * next;
    };
    
    struct node * ADD ( struct node *Head, int value)
    {
        struct node * new = malloc( sizeof(*new));
        
        if( new != NULL )
        {
            new -> data = value;
            new -> next = Head;
        }
        
        return new;
    }
    
    void show ( struct node* head)
    {
        struct node* current;
        
        for ( current = head; current != NULL; current = current -> next )
        {
            printf("%d ", current -> data);
        }
    }    
     
    int main()
    {
        struct node *head = NULL;
        
        head = ADD( head, 1);
        head = ADD( head, 2);
        head = ADD( head, 3);
        
        show(head);
        
        return 0;
    }
    3 2 1
    Last edited by Dadu@; 03-16-2022 at 11:09 PM.

  2. #2
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    You must check if head is NULL in your ADD function.

    Something like below:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    void check_allocation(void *ptr) {
      if (!ptr) {
        fputs("Allocation failed!\n", stderr);
        exit(EXIT_FAILURE);
      }
    }
    
    struct node {
      int data;
      struct node * next;
    };
    
    /*to make the list circular*/
    struct node * ADD ( struct node *Head, int value){
      struct node * new = malloc( sizeof(*new));
      check_allocation(new);
      new->data = value;  
      if (Head) {//check if Head is NULL
        new->next = Head->next;
        Head->next = new;
      }else {
        new->next = new;
      }
      return new;
    }
    
    ....
    Last edited by G4143; 03-17-2022 at 12:13 AM.

  3. #3
    Registered User
    Join Date
    Feb 2022
    Posts
    73
    Thank you so much

    Code:
     #include<stdio.h>
    
    #include<stdlib.h>
      
    struct node 
    {
        int data;
        struct node * next;
    };
    
    
    void check_allocation(void *ptr) {
      if (!ptr) {
        fputs("Allocation failed!\n", stderr);
        exit(EXIT_FAILURE);
      }
    }
    
    
    
    
    struct node * ADD ( struct node *Head, int value)
    {
        struct node * new = malloc( sizeof(*new));
        
       check_allocation(new);
      new->data = value;  
      if (Head) {//check if Head is NULL
        new->next = Head->next;
        Head->next = new;
      }else {
        new->next = new;
      }
      return new;
    }
    
    
    void show ( struct node* head)
    {
        struct node* current;
        
        for ( current = head; current != NULL; current = current -> next )
        {
            printf("%d ", current -> data);
        }
    }    
     
    int main()
    {
        struct node *head = NULL;
        
        head = ADD( head, 1);
        head = ADD( head, 2);
        head = ADD( head, 3);
    	head = ADD( head, 4);
    	head = ADD( head, 5);
        
        show(head);
        
        return 0;
    }
    Output

    Code:
     5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > for ( current = head; current != NULL; current = current -> next )
    Your end condition is getting back to head, not NULL.

    It's a circular list remember.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Simplier:

    Code:
    // Circular single linked list test.
    
    #include <stdio.h>
    #include <stdlib.h>
    
    // Macro to initialize a list.
    #define CSLIST_INIT(list__) { &(list__) }
    
    // base structure of a single linked circular list.
    struct cslist_head {
      struct cslist_head *next_;
    };
    
    // Add a node after an element in the list.
    static inline void cslist_add_next( struct cslist_head *node, struct cslist_head *prev )
    {
      node->next_ = prev->next_;
      prev->next_ = node;
    }
    
    // Deletes a node after an element in the list.
    static inline struct cslist_head *cslist_del_next( struct cslist_head *prev )
    {
      struct cslist_head *node;
    
      node = prev->next_;
      prev->next_ = node->next_;
    
      return node;
    }
    
    // Macro to iterate to the list.
    #define cslist_foreach( iter__, head__ ) \
      for ( iter__ = (head__)->next_; iter__ != head__; iter__ = (iter__)->next_ )
    
    // Simple test.
    int main( void )
    {
      // Derived from base structure...
      struct mynode {
        struct cslist_head *next_;    // must be here!
    
        // data.
        int x;
      };
    
      // auxiliary pointer and the empty list.
      struct cslist_head *t, list = CSLIST_INIT(list);
    
      struct mynode *node;
    
      // Add 3 nodes.
      node = malloc( sizeof *node );
      node->x = 1;
      cslist_add_next( (struct cslist_head *)node, &list );
      t = ( struct cslist_head *)node;    // keep pointer to tail.
    
      node = malloc( sizeof *node );
      node->x = 2;
      cslist_add_next( (struct cslist_head *)node, t );
      t = ( struct cslist_head *)node;    // keep pointer to tail.
    
      node = malloc( sizeof *node );
      node->x = 3;
      cslist_add_next( (struct cslist_head *)node, t );
    
      // List all nodes.
      cslist_foreach( t, &list )
      {
        node = ( struct mynode *)t;
    
        printf( "%d\n", node->x );
      }  
    
      // while the list still have nodes, free them.
      while ( list.next_ != &list )
      {
        t = cslist_del_next( &list );
        free( t );
      }
    }
    Last edited by flp1969; 03-17-2022 at 11:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding element before current node
    By sigur47 in forum C Programming
    Replies: 4
    Last Post: 12-05-2011, 01:50 AM
  2. Quadtree Node -> pointers to the children node
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 04-13-2009, 06:38 PM
  3. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  4. NODE *RemoveDuplicates(NODE *p, NODE *q);
    By xnicky in forum C Programming
    Replies: 3
    Last Post: 12-03-2006, 05:30 PM
  5. Replies: 5
    Last Post: 10-04-2001, 03:42 PM

Tags for this Thread