Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: Problem with linked list in C

  1. #11
    the code I posted was a c++ code, did you first change it into a standardized c code? It seems it can't find the header file for gets().

    #include<stdio.h> should be enough. Dunno about what's wrong now.

  2. #12
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    Yes, I changed it. Here it is:
    Code:
    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=*list;
    	*list=temp;
    }
    I included <strdio.h>

    If I do it in this way:
    Code:
    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=list;
    	list=&temp;
    }
    it works. But still have problem with the print function
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  3. #13
    Hey how does the insert function work?
    You have changed the argument to a single pointer. So it should be
    Code:
    currency* InsertItem(currency *list)
    {
    //.......
    
    return list;
    }
    and in main() it should be called as
    Code:
    list=InsertItem(list);
    [EDIT]

    here is the whole code after I made some changes. It works totally fine for me except for the fact that it can't do scanf("%f",&temp->value). It shows some "floating point formats not linked" on the borland compiler. But I think you won't be seeing that on something like cc or gcc.

    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;
    	    while(temp!=NULL)
    	    {
    	      printf("%d",temp->number);
    	      temp=temp->next;
    	     }
    
    }
    
    void InsertItem(currency **list)
    {
    	currency *temp;
            temp=(currency*)malloc(sizeof(curr));
    	fflush(stdin);
    	printf("Vavedi valuta: ");
    	gets(temp->name);
    	printf("Vavedi kod na valutata: ");
    	gets(temp->code);
    
    	temp->number=++cnt;
    	temp->next=*list;
    	*list=temp;
    
    }
    
    void main()
    {
    	currency *list = NULL;
    	InsertItem(&list);
    	Print(list);
    }
    Last edited by pi><boy; November 14th, 2007 at 02:46 PM.

  4. #14
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    Ok, but now I get back to the previous problem - the cycle in the print function loop forever. And another thing - if I call Print(), without called InserItem before, it don't show me "Spisakat e prazen", but gives me an exception on:
    Code:
    while(temp->next!=NULL)
    Last edited by Barov4e; November 14th, 2007 at 02:21 PM.
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  5. #15
    Hey see the above edited post ^^

  6. #16
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    pi><boy, you r the Man! Thanks a lot ... it is done!

    Best regards!
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  7. #17
    you were segfaulting

  8. #18
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    argh ... another stupid exception. I want to add new items to the edn of the list and resource the function:
    Code:
    void InsertItem(currency **list)
    {
    	currency *temp, *node;
    	temp=(currency*)malloc(sizeof(curr));
    	node=*list;
    	while(node)
    		node=node->next;
    	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;
    	node->next=temp;
    	*list=node;
    }
    And now it gives me exception on this row:
    Code:
    	node->next=temp;
    Hmmm ... obviously I have problems with these pointers
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

  9. #19
    yeah you are having problems with pointers. I suggest you to do a bit more of reading up.

    I think it would be
    Code:
    while(node->next!=NULL)
                node=node->next;
    Your code is while(node). That wouldn't take the pointer node to the last node in the list. Rather it would take you to the link of the last node i.e. NULL and only then will the while loop stop working.

  10. #20
    Senior Member
    Join Date
    Jul 2003
    Posts
    166
    Nope, I tried it already and gives me exception on:
    Code:
    while(node->next!=NULL)
    It don't allows me to change node->next. I can change it only before node=*list;
    BGDevS
    [gloworange]www.peaksoft.info [/gloworange]

Similar Threads

  1. Serious Problem
    By IcSilk in forum Operating Systems
    Replies: 8
    Last Post: October 30th, 2005, 11:01 PM
  2. Tips
    By XTC46 in forum Site Feedback/Questions/Suggestions
    Replies: 15
    Last Post: August 24th, 2005, 07:52 PM
  3. Parse a null delimited list of strings
    By journy101 in forum Programming Security
    Replies: 3
    Last Post: August 13th, 2003, 05:31 PM
  4. Replies: 4
    Last Post: July 24th, 2003, 08:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •