943,587 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1852
  • C RSS
Mar 4th, 2007
0

linked list

Expand Post »
I have some problem with linked list FIFO(first in first out).
This is my code:
  1. typedef int datatype;
  2. typedef struct node *nodep;
  3. struct node *first,*last,*p;
  4. struct node
  5. {
  6. datatype data;
  7. nodep *next;
  8. };
  9. void create();
  10. void list();
  11. void create()
  12. { char *tl;
  13. do
  14. {
  15. p=(struct node*)calloc(5,sizeof(struct node));
  16. p->data=random(100);
  17. p->next=NULL;
  18. if(first==NULL) first=p;
  19. else last->next=p;
  20. last=p;
  21. printf("do you still want to create?(y/n):");scanf("%s",&tl);
  22. getch();
  23. }while(*tl!='n');
  24. }
  25. void list()
  26. {
  27. while(p!=NULL)
  28. {
  29. printf("%3d",p->data);
  30. p=p->next;
  31. }
  32. getch();
  33. }
  34. main()
  35. {
  36. first=NULL;
  37. create();
  38. list();
  39. getch();
  40. }
But my program doesn't run. Can you help me to slove this problem?
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Mar 4th, 2007
0

Re: linked list

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' );
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Mar 4th, 2007
0

Re: linked list

[quote=donaldunca;324408]
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.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006
Mar 5th, 2007
0

Re: linked list

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!!
Reputation Points: 10
Solved Threads: 0
Light Poster
donaldunca is offline Offline
27 posts
since Sep 2006
Mar 5th, 2007
0

Re: linked list

Click to Expand / Collapse  Quote originally posted by donaldunca ...
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');
}
[/code]
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
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 12:08 pm.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 5th, 2007
0

Re: linked list

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.
Reputation Points: 14
Solved Threads: 4
Junior Poster
mathematician is offline Offline
149 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: open a file and divide its data in arrays
Next Thread in C Forum Timeline: ootball league table - add data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC