Thread: linked list implementation

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    12

    linked list implementation

    I have a problem. My program runs, but it scan's only one number, and output's only one number, rest of them zero's........ That's wron? Please help...

    Code:
    # include <stdio.h>
    # include <stdlib.h>
       struct node
       {
           int data;
           struct node *link;
       };
       struct node *insert(struct node *p){
    int n=0;
          
          
          struct node *temp;
          if(p==NULL){
              p=(struct node *)malloc(sizeof(struct node));
             if(p==NULL) {
                 printf("Error\n");
                 exit(0);
             }
             printf("Enter an element : ");
             scanf (" %d", &n);
             p-> data = n;
             p-> link = p;
          } else {
             temp = p;
             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");
       }
       int main(void)
       {
          int n;
          int x;
          struct node *start = NULL ;
              int e=0;
              int i;
    printf("Enter number of element : \n ");
    scanf("%d", &e);
    for (i=0; i<e; i++)
    {
          start = insert ( start);
                              
    };
          printf("The created list is\n");
          printlist ( start );
       
       
       
      system("PAUSE"); 
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    When you add an empty node you set link to p, (the first line below, row #22) then the next entry where p is not NULL temp->link will always be p.

    Code:
             p-> link = p;
          } else {
             temp = p;
             while (temp-> link != p)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for (i=0; i<e; i++)
    {
          start = insert ( start);
                               
    };
    This just sticks a bunch of nodes in a list.
    Code:
        int n=0; /* is set to 0 each time the function is called */
                  
        struct node *temp;
        if(p==NULL){
            p=(struct node *)malloc(sizeof(struct node));
            if(p==NULL) {
                printf("Error\n");
                exit(0);
            }
            printf("Enter an element : ");
            scanf (" %d", &n);
            p-> data = n;
            p-> link = p;
        }
    You only ever enter n if the first node is NULL. After that you never enter n again. How did you expect it to get filled?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    12
    Thank you!! Very much!!!!!! I understand now!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list implementation question
    By somekid413 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2010, 02:33 AM
  2. One more linked list implementation
    By BlackOps in forum C Programming
    Replies: 17
    Last Post: 07-16-2009, 09:34 PM
  3. Linked List implementation
    By jcarouth in forum C Programming
    Replies: 4
    Last Post: 10-05-2005, 10:47 PM
  4. Linked List Queue Implementation help
    By Kenogu Labz in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:14 AM
  5. Linked List implementation of Queue
    By tdm2792 in forum C Programming
    Replies: 5
    Last Post: 11-04-2001, 04:04 PM