Thread: Circular linked list

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    54

    Circular linked list

    Hi

    Is this circular linked list ? if not How it can be circular linked list ?

    Code:
    #include<stdio.h>
    
    #include<stdlib.h> 
     
    struct Node{
      int X;
      struct Node *Next;
    };
     
    struct Node* New_Node(int Number, struct Node *next) {
        struct Node *New = malloc(sizeof(*New));
               New -> X = Number;
               New -> Next = next;
        return New;
    }
          
    int main (void ) {
       struct Node *Head = NULL;  
       
    Head = New_Node(11, Head);
    Head = New_Node(12, Head);
    Head = New_Node(13, Head);
    
    
          
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,077
    For it to be Circular the new node needs to have the node before pointing to it.
    Note: I am far from being a expert of Circular link lists.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2021
    Posts
    54
    Quote Originally Posted by stahta01 View Post
    For it to be Circular the new node needs to have the node before pointing to it.
    Note: I am far from being a expert of Circular link lists.

    Tim S.
    Can you describe little bit ? perhaps pseudo code will help a lot

  4. #4
    Registered User
    Join Date
    Apr 2021
    Posts
    68
    For a list to be circular, you have to be able to start as some point P, and follow the list in a single direction, and eventually end up back at the very same point P.

    Your example code is not circular.

    To make it circular, follow the next pointers until you end up almost back at the start, and then insert the new node.

    Code:
    HEAD --> 11 --> 12 --> 13 -+
              ^                |
              |                |
              +----------------+

  5. #5
    Registered User
    Join Date
    May 2021
    Posts
    54
    Quote Originally Posted by aghast View Post
    For a list to be circular, you have to be able to start as some point P, and follow the list in a single direction, and eventually end up back at the very same point P.

    Your example code is not circular.

    To make it circular, follow the next pointers until you end up almost back at the start, and then insert the new node.

    Code:
    HEAD --> 11 --> 12 --> 13 -+
              ^                |
              |                |
              +----------------+
    Can you give pseudo code for function ? or algorithm for function

  6. #6
    Registered User
    Join Date
    Apr 2021
    Posts
    68
    Can you give pseudo code for function ? or algorithm for function
    I already did: to make it circular, follow the next pointers until you are almost back where you started. Then insert your new value.

    Start with the simple case: how do you insert a single new node into a list where head is NULL?

    Here are some rules:

    1. When you call a list function, you pass in the "head" pointer as a pointer.
    2. When a function updates the list, it always returns a new value for the "head" pointer, even if that doesn't change. The caller has to take the update.
    3. When you return from a "public API" function, the list is always circular or the head pointer is NULL.

    So:

    Code:
        struct Node *
        insert_new_node_into_empty_list(struct Node * head, NodeValueT value)
        {
            assert( head == NULL ); // otherwise it's not an empty list
    
            // ... code goes here ...
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Circular Linked List.
    By TheGreekMan2000 in forum C Programming
    Replies: 6
    Last Post: 05-23-2020, 09:29 AM
  2. circular linked->list help
    By The Brain in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2004, 11:12 AM
  3. Circular linked list
    By campermama in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2004, 11:53 AM
  4. Circular Linked list ....
    By yescha in forum C Programming
    Replies: 1
    Last Post: 11-17-2001, 03:41 AM
  5. Again, Circular Linked list ???
    By yescha in forum C Programming
    Replies: 2
    Last Post: 11-16-2001, 08:35 PM

Tags for this Thread