Infinite loop problem in linklist

From: vjay (vijayanandham_at_gmail.com)
Date: 10/28/04


Date: Thu, 28 Oct 2004 11:51:26 -0400

I want to just create a linklist.The program below goes into an endless
loop.The srange behaviour is that i can exit from the program if i create
only two nodes.After two goes into infinite loop.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
typedef struct node sn;

int create (sn *);
//int add(sn *);
//int delete(sn *);
//int length(sn *);
int main()
{
sn *head;
printf("Create a list");
head =(sn*)malloc(sizeof(sn));
create(head);
return 0;
}

int create(sn *temp)
{
int i=0;
printf("Enter data element:");
scanf("%d",&temp->data);
printf("To continue press 1:");
scanf("%d",&i);
while (i==1)
{
temp->next = (sn*)malloc(sizeof(sn));
temp = temp->next;
create(temp);
}
temp->next = NULL;
return 0;
}



Relevant Pages

  • LinkedList Pointer (REPOST - diff version)
    ... struct node * next; ... No problem until you are dealing with a pointer variable. ... void Push(struct node** headRef, int newData); ... Given an int and a reference to the head pointer (i.e. a struct ...
    (comp.lang.c)
  • LinkedList Pointer (REPOST - diff version)
    ... struct node * next; ... No problem until you are dealing with a pointer variable. ... void Push(struct node** headRef, int newData); ... Given an int and a reference to the head pointer (i.e. a struct ...
    (comp.lang.c)
  • Amazon Interview Question on Doubly Linked List, Plz help
    ... struct node *prev; ... Memory Allocation for head node ... Save First Node ... temp = head; ...
    (comp.lang.c)
  • Re: malloc problem
    ... > typedef struct node { ... void PrintNodes; ... int main ... int AddNode(lnode **head, const char *data) ...
    (comp.lang.c)
  • Re: deletion in link list
    ... int item,ch; ... the list and should probably be simply "struct node *t". ... void addnode ... This doesn't update the list head that you track in your main routine, ...
    (comp.lang.c)