linked list

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

linked list

 
0
  #1
Mar 4th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: linked list

 
0
  #2
Mar 4th, 2007
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' );
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

Re: linked list

 
0
  #3
Mar 4th, 2007
[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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 27
Reputation: donaldunca is an unknown quantity at this point 
Solved Threads: 0
donaldunca's Avatar
donaldunca donaldunca is offline Offline
Light Poster

Re: linked list

 
0
  #4
Mar 5th, 2007
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!!
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: linked list

 
0
  #5
Mar 5th, 2007
Originally Posted by donaldunca View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 134
Reputation: mathematician is an unknown quantity at this point 
Solved Threads: 3
mathematician mathematician is offline Offline
Junior Poster

Re: linked list

 
0
  #6
Mar 5th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1488 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC