| | |
linked list
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
I have some problem with linked list FIFO(first in first out).
This is my code:
But my program doesn't run. Can you help me to slove this problem?
This is my code:
C Syntax (Toggle Plain Text)
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(); }
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
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' ); •
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
[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.
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.
•
•
•
•
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]
Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 5th, 2007 at 12:08 pm.
•
•
Join Date: Nov 2006
Posts: 134
Reputation:
Solved Threads: 3
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.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C Forum
- Previous Thread: open a file and divide its data in arrays
- Next Thread: ootball league table - add data
Views: 1488 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include .net ansi array arrays asterisks binarysearch calculate centimeter changingto char command convert copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics hacking hardware histogram homework inches include incrementoperators input iso kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mysql number opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential shape socket socketprograming spoonfeeding standard string strings structures student systemcall testing threads turboc unix user variable voidmain() wab windowsapi






