0

While inserting node at end in linked list ,my code is running in infinite loop.
IDE Used-Eclipse 64 bit OS

 #include<stdio.h>
 #include<stdlib.h>
 typedef struct Node
   {
     int info;
     struct Node *next;

   }node;
   node *head;
   node *ptr1;
   void insert(int x);
   void show();
   int main()
   {
     int i,x,n;
     puts("Enter number of elements\n");
     scanf("%d",&n);
     for(i=0;i<n;i++)
       {
       puts("Enter elements");
       scanf("%d",&x);
       insert(x);

       }
    show();
    return 0;
     }

//To insert the data in linked list

    void insert(int x)
    {
      node *ptr;
      ptr1=head;
      ptr=(node*)malloc(sizeof(node));
      ptr->info=x;
      if(head==NULL)
        {
         ptr->next=head;
         head=ptr;
        }
      else
        {
        ptr1->next=NULL;
        ptr1=ptr;
        }
       }

//To print the details of list //Unable to figure out this function

    void show()
    {
    while(ptr1->next!=NULL)
      {
      printf("%d\n",ptr1->info);
      ptr1=ptr1->next;
      }



}
Dumb
  • 43
  • 1
  • 1
  • 9

2 Answers2

0

The ptr1 is set to head each time your code enters the insert function then setting it to ptr in the else subsection but nothing pointing to any previous items. Here is an example in case you need one.

typedef struct Node
{
 int info;
 struct Node *next;
}node;

node *head = NULL;
void insert(int x);
void show();
int main()
{
 int i,x,n;
 puts("Enter number of elements\n");
 scanf("%d",&n);
 for(i=0;i<n;i++)
   {
   puts("Enter elements");
   scanf("%d",&x);
   insert(x);

   }
   show();
   return 0;
 }

void insert(int x)
{
  node *ptr = (node*)malloc(sizeof(node));
  ptr->info=x;
  ptr->next=head; /* this will always add the new entry at the beginning of the list all you need it to initialize the head to NULL*/ 
  head = ptr; /* move the head so that it points to the newly created list element */
} 

void show()
{
    node *ptr1 = head;
    printf("%d\n",ptr1->info); /* print the head */
    while(ptr1->next!=NULL) /* now walk the list remember it first looks if the next pointer in the list is null first then it jumps on next element in case it is not*/
    {
        ptr1=ptr1->next;
        printf("%d\n",ptr1->info);
    }
}

Remember to create a function to free up the list elements before exiting the main.

weaz
  • 35
  • 8
  • This code is not inserting node to the end ,it is inserting it to the front. – Dumb Feb 18 '15 at 14:57
  • As stated in the comments :). It is all up to you to sort the elements and even more as exercises. – weaz Feb 21 '15 at 05:17
0

Your two functions can be simplified. A singly linked list is very easy to implement. I would also improve the functions by taking the list pointer as an argument, and in the case of insert() return the new head of the list: but get it working first! Note there is no reason or need to declare global variables when their only use is local to a function.

// insert new node at head of the list
void insert(int x) {
    node *ptr = malloc(sizeof(node));
    if (ptr == NULL) {
        printf ("malloc failure\n");
        exit (1);
    }
    ptr->info = x;
    ptr->next = head;           // append existing list
    head = ptr;                 // new head of list
    }

// show the linked list
void show() {
    node *ptr = head;           // start at head of list
    while (ptr != NULL) {
        printf("%d\n", ptr->info);
        ptr = ptr->next;        // follow the link chain
    }
}

EDIT here is code to add to the tail of a linked list

// insert new node at tail of the list
void insert2(int x) {
    node *tail = head;
    node **last = &head;
    node *ptr;
    while (tail) {
        last = &tail->next;
        tail = tail->next;
        }
    ptr = malloc(sizeof(node));
    if (ptr == NULL) {
        printf ("malloc failure\n");
        exit (1);
    }
    ptr->info = x;
    ptr->next = NULL;
    *last = ptr;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • This is not inserting node to the end ,this is rather inserting it to the front. – Dumb Feb 18 '15 at 14:58
  • That's how you use a linked list. It's a LIFO stack. – Weather Vane Feb 18 '15 at 15:47
  • No but I am trying to print nodes in Linked list in FIFO way and I am unable to figure out How? – Dumb Feb 18 '15 at 16:00
  • I have added another function, you can see it makes life a lot harder by working with the tail of a list. Another way would be to create the list by adding to the head, then create another list from that, which will reverse the sequence. – Weather Vane Feb 18 '15 at 16:09