-
November 8th, 2007, 11:37 PM
#1
Senior Member
Problem with linked list in C
Hi,
When I call the function Print it loops in endless cycle. I can't solve this problem. Hope somebody can help. Here is the code:
Code:
#include <stdio.h>
typedef struct curr
{
char name[50];
char code[3];
float value;
int number;
struct curr *next;
}currency;
currency *start_ptr = NULL;
int cnt = 0;
void Print(currency *list)
{
currency *temp = list;
if (temp!=NULL)
while(temp!=NULL)
{
printf("%d.\t%s\t%s\t%f\n", temp->number, temp->name, temp->code, temp->number);
temp->next;
}
else
printf("Spisakat e prazen.\n");
}
void InsertItem(currency **list)
{
currency temp;
fflush(stdin);
printf("Vavedi valuta: ");
gets(temp.name);
printf("Vavedi kod na valutata: ");
gets(temp.code);
printf("Vavedi stoinost za 1USD: ");
scanf("%f", &temp.value);
temp.number=++cnt;
temp.next=NULL;
*list=&temp;
}
void main()
{
currency *list = NULL;
InsertItem(&list);
Print(list);
}
-
November 9th, 2007, 01:11 AM
#2
Couple of things, think you are trying merge the actual list and the actual node together in one struct and I don't know if that will work quite right...especially if you start manipulating the list....
I would suggest something to the effect of your current struct being the 'node' and then having a controlling struct that is your list, with essentially two pointers, one to the start of the list, and one as kind of a current location in the list...
The problem with the way you are doing it, as you progress through the list, you will eventually lose the first node in your list....Anyway, it certainly had me having to think about it for a minute...
To answer your specific question...this is why:
Code:
while(temp!=NULL)
{
printf("%d.\t%s\t%s\t%f\n", temp->number, temp->name, temp->code, temp->number);
temp->next;
}
You are never changing the pointer of temp....you are referencing temp->next but never setting temp to it...change to:
And I believe your infinite loop will go away; however, as I mentioned above, I think you have some other larger logical issues going on there...not to mention problems with using gets and static buffer sizes
Last edited by nebulus200; November 9th, 2007 at 01:59 AM.
There is only one constant, one universal, it is the only real truth: causality. Action. Reaction. Cause and effect...There is no escape from it, we are forever slaves to it. Our only hope, our only peace is to understand it, to understand the 'why'. 'Why' is what separates us from them, you from me. 'Why' is the only real social power, without it you are powerless.
(Merovingian - Matrix Reloaded)
-
November 9th, 2007, 10:15 AM
#3
nebulus2000 is right, you should have two separate ones.
-
November 12th, 2007, 03:30 PM
#4
Senior Member
First, I am sorry for my later reply, but I was out of town for a while
nebulus200, I didn't get you ... I have two structures - temp and list. list i poiting to the start of the list and temp - to the end. Do you mean something like this:
Code:
struct *ctrl, *node;
node=list;
ctrl=node;
ctrl->next=temp
-
November 13th, 2007, 05:44 PM
#5
Senior Member
I googled for this problem and changed the functions in this way:
Code:
void Print(currency *list)
{
if (list!=NULL)
while(list)
{
printf("%d\t%s\t%s\t%.4f\n", list->number, list->name, list->code, list->number);
list=list->next;
}
else
printf("Spisakat e prazen.\n");
}
Code:
void InsertItem(currency *list)
{
currency temp, tmp;
fflush(stdin);
printf("Vavedi valuta: ");
scanf("%s", &temp.name);
printf("Vavedi kod na valutata: ");
scanf("%s", &temp.code);
printf("Vavedi stoinost za 1USD: ");
scanf("%f", &temp.value);
temp.number=++cnt;
temp.next=NULL;
list=&temp;
}
But now it gives me exception on this (in print() function):
Code:
printf("%d\t%s\t%s\t%.4f\n", list->number, list->name, list->code, list->number);
-
November 14th, 2007, 06:22 AM
#6
when you use currency *list, you are actually using a pointer to a pointer or in other words a double pointer so that you can actually modify the pointer's location and move it around the list instead of doing a return everytime to the single pointer.
Now coming to the display function, I see no need to use a double pointer as you are not modifying the list pointer.
I will be pasting a code here
Code:
void display(currency list)
{
currency temp=list;
while(temp!=NULL)
{
cout<<temp->info<<"\t"; //put your structure members here
temp=temp->next;
}
}
the problem with your code was that you were using a double pointer and instead of storing the pointer to list in a temp variable, you were doing
Code:
if (list!=NULL)
while(list)
{
printf("%d\t%s\t%s\t%.4f\n", list->number, list->name, list->code, list->number);
list=list->next;
Since you were using a pointer to the pointer list, the above function should use something like (*list)->number and *list=(*list)->next. But even this is not recommended, as it takes list for a nice trip through the linked list and it would end up at the very last element of the list. So after displaying if you run another function like insertion, it may end up inserting at the rear instead of at the front.
Hope this makes things clear
Last edited by pi><boy; November 14th, 2007 at 06:27 AM.
-
November 14th, 2007, 07:31 AM
#7
Senior Member
But now it gives me error "binary '=' : no operator defined which takes a right-hand operand of type 'struct curr *' (or there is no acceptable conversion)" on
I tried in this way:
bu it gives me exception on this row at runtime.
-
November 14th, 2007, 08:41 AM
#8
oops, I have made a mistake here. Extremely sorry for that. I didn't notice that the typedef was only for the structure, not for a pointer to the structure.
So everything I wrote in my previous post wont help in this context. Just doing the following SHOULD solve it.
Code:
void display(currency *list)
{
currency *temp=list;
while(temp!=NULL)
{
cout<<temp->number<<temp->name<<"\n";
temp=temp->next;
}
}
Again extremely sorry for the confusion I created.
Last edited by pi><boy; November 14th, 2007 at 08:46 AM.
-
November 14th, 2007, 09:09 AM
#9
hey I decided to compile your code and I think you had a logical mistake in the insert function. I wrote it in c++, but I think you won't have much problem in porting it to c.
Code:
void InsertItem(currency **list)
{
currency *temp;
cout<<"Enter name: ";
gets(temp->name);
cout<<"Enter code: ";
gets(temp->code);
cout<<"Enter value: ";
cin>>temp->value;
temp->number=++cnt;
temp->next=*list;
*list=temp;
}
when I use this with the display function I posted above, it works perfectly fine.
Note that this function is for insertion at the front. For insertion at the rear, you just have to do the following:
Code:
curreny *cur;
cur=*list;
while(cur->next!=NULL)
cur=cur->next; //this finds out the last node
cur->link=temp;
temp->link=NULL;
And one more thing, I think allocating a fixed memory space for the nodes using malloc or new would result in better memory management.
-
November 14th, 2007, 12:36 PM
#10
Senior Member
pi><boy, I tried your way, but when I debug it gives me error "Please enter path for GETS.H"
Similar Threads
-
By IcSilk in forum Operating Systems
Replies: 8
Last Post: October 30th, 2005, 11:01 PM
-
By XTC46 in forum Site Feedback/Questions/Suggestions
Replies: 15
Last Post: August 24th, 2005, 07:52 PM
-
By journy101 in forum Programming Security
Replies: 3
Last Post: August 13th, 2003, 05:31 PM
-
By pctwister in forum Hardware
Replies: 4
Last Post: July 24th, 2003, 08:27 AM
-
By sweet_angel in forum *nix Security Discussions
Replies: 4
Last Post: October 17th, 2002, 12:48 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|