Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
////ACTUAL PROGRAM//
C++
#include <stdio.h>;
#include <stdlib.h>

struct node
{
    int data;
    struct node *link;
};
 
struct node *insert(struct node *p, int n)
{
    struct node *temp;
    /* if the existing list is empty then insert a new node as the starting node */
    if(p==NULL)
    {
        p=(struct node *)malloc(sizeof(struct node)); /* creates new node data value passes as parameter */
        if(p==NULL)
        {
            printf("Error\n");
            exit(0);
        }
        p-> data = n;
        p-> link = p; /* makes the pointer pointing to itself because it is a circular list*/
    }
    else
    {
        temp = p;
        /* traverses the existing list to get the pointer to the last node of it */
        while (temp-> link != p)
            temp = temp-> link;
        temp-> link = (struct node *)malloc(sizeof(struct node)); 
        if(temp -> link == NULL)
        {
            printf("Error\n");
            exit(0);
        }
        temp = temp-> link;
        temp-> data = n;
        temp-> link = p;
    }
    return (p);
}
 
   void printlist ( struct node *p )
   {
   struct node *temp;
    temp = p;
   printf("The data values in the list are\n");
      if(p!= NULL)
      {
      do
            {
            printf("%d\t",temp->data);
            temp=temp->link;
            } while (temp!= p);
      }
      else
         printf("The list is empty\n");
   }
 
   void main()
   {
      int n;
      int x;
      struct node *start = NULL ;
 
      printf("Enter the nodes to be created \n");
      scanf("%d",&n);
      while ( n -- > 0 )
      {
   printf( "Enter the data values to be placed in a node\n");
         scanf("%d",&x);
         start = insert ( start, x );
      }
      printf("The created list is\n");
      printlist ( start );
 
   }

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
 else
      {
      temp = p;
   /* traverses the existing list to get the pointer to the last node of it */
      while (temp-> link != p)
         temp = temp-> link;
            temp-> link = (struct node *)malloc(sizeof(struct node)); 
           if(temp -> link == NULL)
           {
         printf("Error\n");
              exit(0);
           }
           temp = temp-> link;
           temp-> data = n;
           temp-> link = p;
          }
          return (p);
   }
Posted
Updated 15-Oct-11 21:45pm
v4
Comments
OriginalGriff 15-Oct-11 7:34am    
Sorry - your actual question got truncated because you wrote it all in the subject line.. Edit your question, and put a short, descriptive subject. Then put the long description of the problem in the body of the question.
Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 15-Oct-11 7:38am    
Why are you using malloc() to create new nodes when traversing the tree in search mode?
Chuck O'Toole 15-Oct-11 17:19pm    
perhaps because the function he's doing the traversing in is called "insert()" :)
Richard MacCutchan 16-Oct-11 3:10am    
The code is supposed to be looking for the end of the list, but adds a new struct to each link as it traverses in the while loop.
Fixed the indentation so I can see what's going on. :(

1 solution

You should understand that the pointer points to NULL only when there is no memory allocated to the structure node, in this case it's because it is a circular list..

Therefore temp->link will be equal to NULL only when there was no more memory that could be allocated to create a node, which would be considered as an error in our case.

I hope that answers your question. It would be better if you could make your question more clear.. I guess it was truncated coz it was in the subject field..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900