Thread: Linked List help!!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    4

    Linked List help!!

    Hi everyone!

    I'm new here, so please excuse any mis-posts or errors that I may have.

    I'm trying to create a Linked List from a text input file, sort the Linked List, then print out the sorted linked list to an output file.

    So far, I'm just trying to get the gist of linked list.
    I haven't completed the sorting method yet.
    I'm just having trouble even creating the linked list!
    I get compiling errors:

    Final.c:23: warning: assignment makes pointer from integer without a cast
    C:\Users\User\Final.c: At top level:
    Final.c:39: error: conflicting types for 'AddToList'
    Final.c:23: error: previous implicit declaration of 'AddToList' was here

    I have no idea what to do


    insert
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    struct node{
        int data;
        struct node *next;
    };
    
    
    /********************************************************************/
    /*            CreateList
    /********************************************************************/
    /*
     * Input:       A pointer to the node that we want to create, a pointer
     					 to the input file and a value for the input file
     * Output:      The pointer to the node is updated.
     * Task:        Create a node linked list
     */
     
    struct node* CreateList(struct node *head, int m, FILE *fp1){
    	do{
    	fscanf(fp1, "%d", &m);
    	head = AddToList(head, m);			// Add to List
    	}
    	while(!feof(fp1));
    
    return head;
    }
    
    /********************************************************************/
    /*            AddToList
    /********************************************************************/
    /*
     * Input:       A pointer to the node that we want to create, 
     					 and a value for the input file
     * Output:      The pointer to the node is updated.
     * Task:        Add a node to the linked list
     */
    struct node* AddToList(struct node *head, int data){
        struct node *tmp;
     
        if(head == NULL){
            head=(struct node *)malloc(sizeof(struct node));
            if(head == NULL){
                printf("Error! memory is not available\n");
                exit(0);
            }
            head-> data = data;
            head-> next = head;
        }
    	 else{
            tmp = head;
             
            while (tmp-> next != head)
                tmp = tmp-> next;
            tmp-> next = (struct node *)malloc(sizeof(struct node));
            if(tmp -> next == NULL)
            {
                printf("Error! memory is not available\n");
                exit(0);
            }
            tmp = tmp-> next;
            tmp-> data = data;
            tmp-> next = head;
        }
        return head;
    }
    
    /********************************************************************/
    /*            WriteList
    /********************************************************************/
    /*
     * Input:       A pointer to the node that we want to write and 
     				    a pointer to the output file
     * Output:      No output
     * Task:        Print the linked list
     */
    void WriteList(struct node *head, FILE *fp3)
    {
        struct node *current;
        current = head;
        if(current!= NULL)
        {
            do
            {
                printf("%d\t",current->data);
    				fprintf(fp3,"%d\n", current->data);
                current = current->next;
            } while (current!= head);
            printf("\n");
        }
        else
            printf("The list is empty\n");
     
    }
    
    /********************************************************************/
    /*            DeleteList
    /********************************************************************/
    /*
     * Input:       A pointer to the node that we want to delete.
     * Output:      No output.
     * Task:        Delete the linked list
     */
    void DeleteList(struct node *head)
    {
        struct node *current, *tmp;
         
        current = head->next;
        head->next = NULL;
        while(current != NULL) {
            tmp = current->next;
            free(current);
            current = tmp;
        }
    }
    
    /*
    WORK IN PROGRESS
    */
    struct node* RemoveFromList(struct node *head, int data){
    
        return head;
    }
    
    /********************************************************************/
    /*            checkFile
    /********************************************************************/
    /*
     * Input:       A pointer to the file that we are reading and a value
     					 to count the error messages. 
     * Output:      The counter
     * Task:        Check input and output files.
     */
    int checkFile (FILE *fp, int count){
    if (count == 0){
    count++;
    
    if (fp == NULL){
    printf("\nError opening input file Input.txt ...Program terminating\n");
    exit(1);
    }
    return count;
    }
    
    if (fp == NULL){
    printf("\nError opening input file Sorted.txt ...Program terminating\n");
    exit(1);
    }
    return count;
    }
    
    
    
    int main()
    {
    	FILE *fp1, *fp3;
    	int m;
    	int count = 0;
    
       struct node *head = NULL;
    	
    
    	// Open file 1 and check for failure 
    	fp1 = fopen("Input.txt", "r");
    	count = checkFile(fp1, count);
    	fp3 = fopen("Sorted.txt", "w");
    	count = checkFile(fp3, count);
    	
    	// Creates List
    	CreateList(head, m, fp1);
    	
    	// Writes to output
    	WriteList(head, fp3);	
    	     
    	// Deletes List
    	DeleteList(head);
    	getchar();			// must press enter to continue
    	 	 
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    If you can figure out how to turn your compiler's warning level up, I'd recommend doing that.

    You're running into an issue that's a vestige of ancient C. In C (not counting C99, which we needn't worry about at the moment), you're allowed to call a function even if the compiler doesn't have any idea what that function looks like. When a compiler sees such a function call, it pretends you did the following:
    Code:
    int function();
    That is, a function that hasn't been declared is assumed to return int and take an unspecified number of arguments. An empty list () in C doesn't mean “no arguments” but “unspecified number of arguments”, yet another relic of the old C days (it means “no arguments” when you're defining a function, but at the same time it means “unspecified arguments”; don't worry about this!)

    So anyway, in CreateList() you're calling AddToList() before the compiler has any idea what that function is. The compiler won't scan the whole file first to find out what functions you're using. It just goes from the top to the bottom, guessing about how functions should look if it's not told otherwise. Then, after it has internally decied that AddToList looks like “int AddToList()”, you go and create an AddToList() that looks totally different. Thus the complaint of your compiler. It is at least smart enough to know that its idea of the function doesn't agree with yours.

    So the goal is to declare the function before you use it. A definition counts as a declaration, so you could move AddToList() above CreateList(). Or you could create just a declaration, like so:
    Code:
    struct node* AddToList(struct node *head, int data);
    Put that above CreateList() and you've given the compiler enough information to properly call the function. This type of declaration is called a prototype, which means it specifies the types of its arguments: remember that C assumes no type information about its arguments.

    If you want to create a prototype for a function that takes no arguments, it must look like this:
    Code:
    void f(void); /* any return type is fine, of course */
    Simply using an empty list () is not the same. It should be (and as I understand it, it is in C++), but due to historical quirks, this is how C works.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    4
    I see. Thanks for the information. The program did compile. :]

    On my way to figuring out how to sort the linked list now. Do you have any suggestions on what the most efficient sort methods for linked lists are?

    I was thinking about a merge sort, but with my little knowledge of linked lists, I don't know if that's the best option.

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

Tags for this Thread