Thread: Help with linked list

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Unhappy Help with linked list

    Hi!

    I have problem with the "ReadList" and "WriteList" function.

    Code:
    # include <stdio.h>
    # include <stdlib.h>
    # include <ctype.h>
    # include <conio.h>
    
    # define FALSE 0
    
    typedef struct 
    {  
    	int Value;
    	struct List *link;
    } List;
    
    // other functions
    void clrscr (void);
    void Menu (int *Choose);
    // functions for the list
    List *AddElement (List *start, int Data);
    void ReadList (List *start, char File01[]);
    void WriteList (List *start, char File01[]);
    void PrintList (List *start);
    
    int main()
    {
    	int Choose, Data;
    	List *start = NULL;
                   
    	start = ReadList(start, "data.txt");
    	do
    	{
    		Menu(&Choose);
    		switch (Choose)
    		{
    			case 1:
    				printf("\nEnter number: ");
    				scanf("%d", &Data);
    				start = AddElement(start, Data); 
    				break;
    			case 2:
    				if (start != NULL)
    				{
    					PrintList(start);
    				}
    				else
    				{
    					printf("\nList is empty.");
    				}
    				getch();
    				break;
    			case 3:
    				break;
    			default:
    				printf("\nWrong selection. Try again.");
    				getch();
    				break;
    		}
    	} while (Choose != 3);
                    WriteList (start, "data.txt");
    	return 0;	
    }
    
    void Menu (int *Choose) 
    {
        unsigned char Char;
    
        do 
    	{
    		clrscr();
    		printf("List");
    		printf("\n\n1 - Add element into sorted list");
    		printf("\n2 - Print elements on the screen");
    		printf("\n3 - Exit");
    		printf("\n\nChoose: ");
    		Char = getchar();
    		if ((isdigit (Char) == FALSE) && (Char != '\n')) 
    		{
    			printf ("\nYou must enter number."); getch();
    		}
        } while (isdigit (Char) == FALSE);
        *Choose = (int) Char - '0'; 
    }
    
    void clrscr()
    {
    	system("cls");
    }
    
    List *AddElement (List *start, int Data)
    {
    	List *pom, *New;
    	
    	New = (List *) malloc(sizeof(List));
    	New->Value = Data;
    	if ( (start == NULL) || (New->Value <= start->Value) ) 
    	{
    		(List *)New->link = start;
    		start = New;
    	}
    	else
    	{
    		pom = start;	
    		while ( (pom->link != NULL) && (New->Value > ((List *)pom->link)->Value) )
    		{
    			pom = (List *)pom->link;
    		}
    		New->link = pom->link;
    		(List *)pom->link = New;
    		
    	}
    	return start;
    }
    
    void PrintList (List *start)
    {
    	while (start != NULL)
    	{
    		printf("%d\t", start->Value);
    		start = (List *)start->link;
    	}
    }
    Thnx in advance .
    Last edited by GaPe; 02-08-2002 at 03:02 AM.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Uf, I totally forgot to copy/paste these two functions:

    Code:
    List *ReadList (List *start, char File01[])
    {
    	FILE *fin;
    	List *New;
    
    	fin = fopen(File01, "rb");
    	while ( !feof(fin) )
    	{
    		New = (List *) malloc (sizeof(List));
    		fread(New, sizeof(List), 1, fin);
    		(List *)New->link = start;
    		start = New;
    	}
    	fclose(fin);
    	return start;
    }
    
    void WriteList (List *start, char File01[])
    {
    	FILE *fout;
    
    	fout = fopen(File01, "wb+");
    	while (start != NULL)
    	{
    		fwrite(start, sizeof(List), 1, fout);
    		start = (List *)start->link;
    	}
    	fclose(fout);
    }
    The problem is that when I exit my program, the WriteList function always adds 0 to the "data.txt" file. What is wrong?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    typedef struct
    {
    int Value;
    struct List *link;
    } List;

    The 0 is the value of link for the last element.... the value after the last element is NULL, and NULL is 0. So... the last value in your data.txt is going to be 0.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    But why the program doesn't print out the 0 on the screen when I use function "PrintList"?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    char File01[100], *File01Ptr;

    File01Ptr = File01;
    File01Ptr = "data.txt";
    Didn't get to the linked list part of your code because after reading the first few lines I noticed this illegal code.

    Should be:
    strcpy(File01Ptr,"data.txt");

    because you can't assign a whole string safely to a single character such as:
    File01Ptr = "data.txt";

    Better yet to just use:
    strcpy(File01,"data.txt");

    I'm wondering why you called the char pointer File01Ptr, have you used file pointers before?

    FILE *File01Ptr;

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Yes I used file pointers before and this code is right. I have a string and a pointer to that string (File01[100] and *File01Ptr). In my case a file pointer is "fin" and "fout".
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    The char pointer points to a single character, it should be used with strcpy if you want to assign a string to that position. I have been doing C for 3 years. I know this for a fact. I can write linked lists in my sleep.

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    If I write "printf("%s", File01Ptr);" I get "data.txt" which is totally right.

    If you write linked lists in your sleep than, please, help me out and tell me why do I get those 0 in my file.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    No, I am aware that some compilers will allow you to assign a string to a pointer to char. I am familiar with how it works, but it is poor code and can cause problems depending on how the OS manages memory.

    If you change it than I'll help you with the list. There is no point in helping someone with partly retarded code in their program.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    //call
    start = ReadList(start, File01Ptr);

    //prototype
    void ReadList (List *start, char File01[]);

    Same situation, the code will compile but it is confused, specify the pointer for what it is.

    void ReadList (List *,char *);

    Now I'll keep going.

  11. #11
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You are working with a binary file. Send it to me so I don't have to create it myself.

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Ok I changed the code.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    while ( !feof(fin) )
    {

    }

    Take this out of the ReadList function and put it in main like so:

    //Open the file
    int count =0;
    while(! feof(fin))
    {
    ReadList(fin,start);
    count ++;
    }


    Than in ReadList do this:

    List *New, *Next;

    declare another pointer in the readlist function where you create the list. You did the allocation correctly but than you need to build the list like this for example.

  14. #14
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Here's the file and change it's extension to .txt.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  15. #15
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    void OpenFile(FILE **fptr, char path[], char mode[]);
    
    int main()
    {
    	int Choose, Data;
    	List *start = NULL;
    	FILE *fin;
    
    	OpenFile(&fin,"data.txt","rb");
    
    	int count = 0;
    	while ( !feof(fin))
    	{
    		start = ReadList(fin,start);
    		count ++;
    	}
    	//...
    	return 0;
    }
    
    void OpenFile(FILE **fptr, char path[], char mode[])
    {
    	*fptr = fopen(path,mode);
    	if (*fptr == NULL) 
    	{
    		fprintf(stderr,"Error: File a:\\name?? is missing!\n");
    		exit(1);
    	}
    }
    This is cleaner. Okay now for the ReadList function.
    Last edited by Troll_King; 02-08-2002 at 03:22 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM