I have some problem with linked list FIFO(first in first out).
This is my code:

typedef int datatype;
typedef struct node *nodep;
struct node *first,*last,*p;
struct node
{
    datatype data;
    nodep *next;
};
void create();
void list();
void create()
{       char *tl;
    do
    {
        p=(struct node*)calloc(5,sizeof(struct node));
        p->data=random(100);
        p->next=NULL;
        if(first==NULL)   first=p;
        else  last->next=p;
        last=p;
        printf("do you still want to create?(y/n):");scanf("%s",&tl);
        getch();
    }while(*tl!='n');
}
void list()
{
    while(p!=NULL)
    {
        printf("%3d",p->data);
        p=p->next;
    }
    getch();
}
main()
{
    first=NULL;
    create();
    list();
    getch();
}

But my program doesn't run. Can you help me to slove this problem?

Recommended Answers

All 5 Replies

A few instant problems

1. Over reliance on global variables.

2. Your print routine starts at 'p', not first. Your p variable is the last node created.

3. scanf("%s",&tl);
t1 doesn't point at any memory.
At a push, the minimum would be these snippets

char t1[10];
scanf("%s",t1);
while( t1[0] != 'n' );

printf("do you still want to create?(y/n):");scanf("%s",&tl);
getch();


It isn't obvious why you need getch() there if you are using scanf to read from the keyboard. That in itself probably explains why your program appears not to run.

The easiest thing you could do would be to redefine t1 as a character (not a pointer), and then use:

t1 = getch();

or

t1 = getchar();

In fact you seem to have calls to getch() sprinkled throughout your program, without any corresponding prompts being displayed on the screen. The only thing that is likely to do is bring the program to a grinding halt.

My mistakes of program:
- Can not convert 'node*' to 'node**'
- Can not convert 'node**' to 'node*'
- Function should return a value
I don't understand these mistakes. Could you help me?Thank you!!

start-quote

void create()
{ 
    char *tl;
    do
    {
        p=(struct node*)calloc(5,sizeof(struct node));
        p->data=random(100);
        p->next=NULL;
        if(first==NULL) first=p;
        else last->next=p;
        last=p;
        printf("do you still want to create?(y/n):");scanf("%s",&tl);
        getch();
    }while(*tl!='n');
}

end-quote

calloc will allocate 5 node structs. p will point to the first of the five elements. On the second loop, last will be assigned the new allocate 5 struct node objects, but then last will be assigned p. Second loop; Since last at this point points to the first allocated 5 objects, the "last=p" would overwrite the first allocate 5 objects with the secondly new allocated 5 objects; The "else last->next" really is irrelevant at this point. You might want to use malloc instead of calloc because malloc could be used to allocate one object at a time instead of 5 using calloc. Or you can get the user's input and calloc that number of objects and loop through initializing them. Let me know.

Good luck, LamaBot

To append a comment to the previous. If you allocate an array of structures by using calloc you do not really need a linked list at all - unless you are, or might be, allocating more structures subsequently. The point of a linked list is link together structures which may not be in a single contiguous block of memory. If they are known to be in a single contiguous block of memory, it is easier to address them as an array.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.