Thread: Help with linked list

Threaded View

Previous Post Previous Post   Next Post Next Post
  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)

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