Thread: Linked List from Standard Input

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    18

    Unhappy Linked List from Standard Input

    Hi all,

    for any of you who shiver at ignorant, terrible code, just press 'back' now.

    Assignment where I have to use a header and .c file to implement an abstract data type for lists containing ints.

    Anywho, in the end, it's supposed to be able to do several things,
    but the first is reading a set of ints from standard input, and adding them to the linked list in correct order, using EOF to stop.

    The code I have so far is largely copied from my notes,
    but a few conventions used, such as '->' don't seem to work.
    Was it pseudo code, and if so, for what!

    Basically.. I'm completely lost and in a panic...


    list.c *I'm updating this when I make changes.. and there are many to be made!*
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "list.h"
    #define SIZE (sizeof( struct _LIST_ ))
    
    int j, input;
    int* list, tmp;
    
    
    
     struct _LIST_ {
      int number;
      struct _LIST_ *next;
     };
    
     typedef struct _LIST_ *LIST;
    
    
    
    
    
    extern void print_stuff( int i )
    	{
    	printf("i is %d \n", i);
    	}
    
    
    extern void create_list ()
    	{
    	list = malloc( SIZE );
    	scanf ("d%", &input);
    	if ( input != EOF)
    		{
    		list -> next = NULL;
    		list -> number = input;
    		}
    	else
    		{
    	printf ("no input");
    		}
    	scanf ("d%", &input);
    	while ( input = !EOF )
    		{
    		tmp = malloc( SIZE );
    		tmp -> next = list;
    		tmp -> number = input;
    		list = tmp;
    		}
    	}
    
    extern void free_list (void* list)
    	{
    	                      
    	while (list != NULL) 
    		{
       		tmp = list;
       		list = list->next;
       		free( tmp );
    		}
    
    
    	}
    
    
    extern void reverse_list (void* list);
    	{
    	}
    
    
    
    
    extern void print_list( void* list );
    	{
    	int search( int key, LIST list )
    		 {
      		while ((list != NULL) && (list->number != key))
       		 printf (list);
    		list = list->next;
     		 return (list != NULL);
    		}
    	}
    
    
    
    extern void read_list( void* *plist );
    	{
    	}
    
    
    extern void append_lists (void *list1, void *list2)
    	{	
    	}
    main.c
    Code:
    #include <stdio.h>
    #include "list.h"
    
    int main (){
    
    printf("Main is working\n");
    print_stuff(2);
    create_list();
    }
    list.h
    Code:
    #ifndef _LIST_H_
    #define _LIST_H_
    
    
    extern void print_list( void* list );
    extern void read_list( void* *plist );
    extern void free_list( void* list );
    extern void create_list ();
    extern void reverse_list(void* list);
    extern void append_lists(void* list1, void* list2);
    
    
    
    extern void print_stuff( int i );
    
    #endif /* list.h */
    Last edited by mercuryfrost; 08-22-2009 at 03:53 PM. Reason: updated whilst working

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > if ( input !EOF)

    > scanf ("d%", &input);
    > while ( input = !EOF )

    Try posting something which compiles to begin with.

    This is riddled with stupid syntax errors, unless that is your question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    I did say it was 'terrible, ignorant' code.

    Ya, I guess stupid syntax errors are my first problem... amongst many.

    If I could make it compile I would. I'm very new to C, and I apologise for my ignorance.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Dunno - I'm done for the day.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Most of what you have is probably copied down correctly. Of course what you don't appear to have copied are all the variable declarations, and those are kind of important. So look at those again, or if they're not in your notes, think about what they have to be.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Here are a selection of problems

    > int j, input;
    > int* list, tmp;
    Avoid global variables. j and input especially should be local to whatever functions that need them
    Your list needs to be global given your current code, so leave it as it is.
    But it needs to be
    Code:
    struct _LIST_ *list;
    That'll probably fix most of the -> errors you see

    > scanf ("d%", &input);
    > while ( input = !EOF )
    1. d% is not a conversion, %d is
    2. scanf itself returns EOF, not a converted value.

    Buf it scanf() does return EOF, then so will all future attempts at reading input.
    Consider perhaps a sentinel value like say -1
    Code:
    while ( scanf("%d",&input) != EOF && input != -1 )
    > printf (list);
    Be very careful what you pass to printf.
    The first parameter must always be a control string.

    >extern void reverse_list (void* list);
    A lot of empty functions (not a problem).
    The ; at the end of the line however is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    Quote Originally Posted by Salem View Post
    But it needs to be
    Code:
    struct _LIST_ *list;
    That'll probably fix most of the -> errors you see


    > printf (list);
    Be very careful what you pass to printf.
    The first parameter must always be a control string.

    Many thanks for feedback Tabstop and Salem.

    I've attemtped to implement the changes you suggested, but was unsure of where or how to implement the two mentioned above.

    The current god-awful-mess is
    Code:
    #include <stdio.h>
    #include "list.h"
    #include <stdlib.h>
    #define SIZE sizeof struct _LIST_
    
    
    int* input, tmp, tail, node;
    
    
    
     struct _LIST_
    	{
      	int number;
      	struct _LIST_ *next;
     	}
    
     typedef struct _LIST_ *LIST;
    
    
    
    
    
    extern void print_stuff( int i )
    	{
    	printf("i is %d \n", i);
    	}
    
    
    extern void create_list ()
    	{
    	list = malloc( SIZE );
    	scanf ("%d", &input);
    	if ( input != EOF)
    		{
    		list -> next = NULL;
    		list -> number = input;
    		}
    	else
    		{
    	printf ("no input");
    		}
    	while ( scanf("%d",&input) != EOF && input != -1 )
    		{
    		tmp = malloc( SIZE );
    		tmp->next = list;
    		tmp->number = input;
    		list = tmp;
    		}
    	}
    
    
    
    extern void free_list (void* list)
    	{
    	                      
    	while (list != NULL) 
    		{
       		tmp = list;
       		list = list->next;
       		free( tmp );
    		}
    
    
    	}
    
    
    extern void reverse_list (void* list)
    	{
    	list *reverse (list *node);
    	if (node->next == NULL)
     		{
       			tail = node;
        			return;
      		}
      	else
      		{
        			reverse (node->next);
        			node->next->next = node;
        			node->next = NULL;
      		}
    		
    	}
    
    
    
    
    extern void print_list( void* list )
    	{
    	int search( int key, LIST list )
    	while ((list != NULL) && (list->number != key))
       		{
    		printf (list);
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    
    extern void read_list( void* *plist )
    	{
    	int search( int key, LIST list ) 
    	while ((list != NULL) && (list->number != key))
        		{
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    extern void append_lists (void *list1, void *list2)
    	{	
    	if (list1 == NULL)
       	list1 = list2;
    	else 
    		{
       		tmp = list1;
       		while (tmp->next != NULL)
           		tmp = tmp->next;
      		tmp->next = list2;
    		}
    
    	}

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Change
    typedef struct _LIST_ *LIST;
    to simply
    struct _LIST_ *LIST;

    Also the format for the printf is

    Say you want to print an integer called as "int i" using printf so you will use it like this

    printf("\n %d", i);

    \n is for the new line

    %d is the format specifier

    printf("format specifier 1, ...., format specifier n", value1,.... value n);

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by roaan
    Change
    typedef struct _LIST_ *LIST;
    to simply
    struct _LIST_ *LIST;
    Look carefully: the name of the variable is list, not LIST.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    Changes (attempted!) made!

    Thanks guys for your time!


    Code:
    #include <stdio.h>
    #include "list.h"
    #include <stdlib.h>
    #define SIZE sizeof struct _LIST_
    
    
    int* input, tmp, tail, node, list;
    
    
    
     struct _LIST_
    	{
      	int number;
      	struct _LIST_ *next;
     	}
    
    struct _LIST_ *LIST;
    
    
    
    
    
    extern void print_stuff( int i )
    	{
    	printf("i is %d \n", i);
    	}
    
    
    extern void create_list ()
    	{
    	list = malloc( SIZE );
    	scanf ("%d", &input);
    	if ( input != EOF)
    		{
    		list -> next = NULL;
    		list -> number = input;
    		}
    	else
    		{
    	printf ("no input");
    		}
    	while ( scanf("%d",&input) != EOF && input != -1 )
    		{
    		tmp = malloc( SIZE );
    		tmp->next = list;
    		tmp->number = input;
    		list = tmp;
    		}
    	}
    
    
    
    extern void free_list (void* list)
    	{
    	                      
    	while (list != NULL) 
    		{
       		tmp = list;
       		list = list->next;
       		free( tmp );
    		}
    
    
    	}
    
    
    extern void reverse_list (void* list)
    	{
    	list *reverse (list *node);
    	if (node->next == NULL)
     		{
       			tail = node;
        			return;
      		}
      	else
      		{
        			reverse (node->next);
        			node->next->next = node;
        			node->next = NULL;
      		}
    		
    	}
    
    
    
    
    extern void print_list( void* list )
    	{
    	int search( int key, LIST list )
    	while ((list != NULL) && (list->number != key))
       		{
    		printf ("%d", list);
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    
    extern void read_list( void* *plist )
    	{
    	int search( int key, LIST list ) 
    	while ((list != NULL) && (list->number != key))
        		{
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    extern void append_lists (void *list1, void *list2)
    	{	
    	if (list1 == NULL)
       	list1 = list2;
    	else 
    		{
       		tmp = list1;
       		while (tmp->next != NULL)
           		tmp = tmp->next;
      		tmp->next = list2;
    		}
    
    	}
    Compiler errors are as follows
    Code:
    list.c:17: error: two or more data types in declaration specifiers
    list.c: In function ‘create_list’:
    list.c:31: error: expected expression before ‘struct’
    list.c:33: warning: comparison between pointer and integer
    list.c:36: warning: assignment makes integer from pointer without a cast
    list.c:42: warning: comparison between pointer and integer
    list.c:44: error: expected expression before ‘struct’
    list.c:44: warning: assignment makes integer from pointer without a cast
    list.c:45: error: invalid type argument of ‘->’
    list.c:46: error: invalid type argument of ‘->’
    list.c:47: warning: assignment makes pointer from integer without a cast
    list.c: In function ‘free_list’:
    list.c:58: warning: assignment makes integer from pointer without a cast
    list.c:59: warning: dereferencing ‘void *’ pointer
    list.c:59: error: request for member ‘next’ in something not a structure or union
    list.c:60: warning: passing argument 1 of ‘free’ makes pointer from integer without a cast
    list.c: In function ‘reverse_list’:
    list.c:69: error: invalid operands to binary *
    list.c:69: error: invalid operands to binary *
    list.c:70: error: invalid type argument of ‘->’
    list.c:77: error: invalid type argument of ‘->’
    list.c:78: error: invalid type argument of ‘->’
    list.c:79: error: invalid type argument of ‘->’
    list.c: In function ‘print_list’:
    list.c:89: error: expected declaration specifiers or ‘...’ before ‘LIST’
    list.c:90: warning: ISO C forbids nested functions
    list.c: In function ‘search’:
    list.c:90: error: expected declaration specifiers before ‘while’
    list.c:96: error: expected declaration specifiers before ‘}’ token
    list.c:101: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    list.c:112: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    list.c:123: error: expected ‘{’ at end of input
    list.c: In function ‘print_list’:
    list.c:123: error: expected declaration or statement at end of input
    Last edited by mercuryfrost; 08-23-2009 at 08:13 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    At a glance, an error that you should immediately fix is the missing semi-colon at the end of your struct definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > int* input, tmp, tail, node, list;
    And this is just getting worse and worse.

    None of them should by type 'int*'
    All of them except list should be local to whatever functions that use them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mercuryfrost View Post
    The current god-awful-mess is
    Do not write programs this way. You should check and see if everything compiles without error every few lines when you write. If you copied this down as notes, that's great, but you will be much better off if you start writing something from scratch using it as notes, possibly cut n' pasting a line at a time, etc.

    Rewriting bad or partial code is almost by definition going to be easier and produce a better result than "salvaging it" like this. Whatever is valid and useful in it is easy to just copy, and you can forget the rest. This is especially true if the code never worked in the first place. You may think it will be quicker to just "fix things", but you are WRONG. Things like missing semi-colons can cause cascading errors that are very awkward to solve IF you were not test compiling while writing. In that case, you will know immediately when you've added a problem. Working backward like this, you will solve one mysterious error only to expose another. By the time you get to the end, you will have thrown a large portion of this out, and have had to completely rewrite a majority of the lines anyway, from the looks of things.

    Of course, I don't expect you to change your mind at this point, but honestly, if you value your time and what comes of it, don't do it this way again!

    Can I ask what this is suppose to represent:
    Code:
    int search( int key, LIST list )
    Last edited by MK27; 08-23-2009 at 08:46 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    18
    Quote Originally Posted by laserlight View Post
    At a glance, an error that you should immediately fix is the missing semi-colon at the end of your struct definition.
    Fixed! Thanks!

    Quote Originally Posted by Salem View Post
    > int* input, tmp, tail, node, list;
    And this is just getting worse and worse.

    None of them should by type 'int*'
    All of them except list should be local to whatever functions that use them.
    Sorry about that. I've put them all into their own functions now

    Quote Originally Posted by MK27 View Post
    Of course, I don't expect you to change your mind at this point, but honestly, if you value your time and what comes of it, don't do it this way again!
    Totally right... lesson learned.

    Quote Originally Posted by MK27 View Post
    Can I ask what this is suppose to represent:
    Code:
    int search( int key, LIST list )
    Copied again from my notes,
    it was to be something that searched through each value in the linked list.

    Code:
    #include <stdio.h>
    #include "list.h"
    #include <stdlib.h>
    #define SIZE sizeof struct _LIST_
    
    
    int list;
    
    
    
     struct _LIST_;
    	{
      	int number;
      	struct _LIST_ *next;
     	}
    
    struct _LIST_ *LIST;
    
    
    
    
    
    extern void print_stuff( int i )
    	{
    	printf("i is %d \n", i);
    	}
    
    
    extern void create_list ()
    	{
    	int input,tmp;
    	list = malloc( SIZE );
    	scanf ("%d", &input);
    	if ( input != EOF)
    		{
    		list -> next = NULL;
    		list -> number = input;
    		}
    	else
    		{
    	printf ("no input");
    		}
    	while ( scanf("%d",&input) != EOF && input != -1 )
    		{
    		tmp = malloc( SIZE );
    		tmp->next = list;
    		tmp->number = input;
    		list = tmp;
    		}
    	}
    
    
    
    extern void free_list (void* list)
    	{
    	int tmp;                     
    	while (list != NULL) 
    		{
       		tmp = list;
       		list = list->next;
       		free( tmp );
    		}
    
    
    	}
    
    
    extern void reverse_list (void* list)
    	{
    	int node, tail;
    	list *reverse (list *node);
    	if (node->next == NULL)
     		{
       			tail = node;
        			return;
      		}
      	else
      		{
        			reverse (node->next);
        			node->next->next = node;
        			node->next = NULL;
      		}
    		
    	}
    
    
    
    
    extern void print_list( void* list )
    	{
    	int search( int key, LIST list )
    	while ((list != NULL) && (list->number != key))
       		{
    		printf ("%d", list);
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    
    extern void read_list( void* *plist )
    	{
    	int search( int key, LIST list ) 
    	while ((list != NULL) && (list->number != key))
        		{
    		list = list->next;
     		return (list != NULL);
    		}
    	}
    
    
    extern void append_lists (void *list1, void *list2)
    	{	
    	if (list1 == NULL)
       	list1 = list2;
    	else 
    		{
       		tmp = list1;
       		while (tmp->next != NULL)
           		tmp = tmp->next;
      		tmp->next = list2;
    		}
    
    	}

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    int list;
    struct _LIST_;

    Sorry, but this kind of dumb stuff is going backwards quicker than going forwards.
    It seems like you just invent random syntax, press compile, then press copy/paste to the forum.

    It's like trying to teach millions of monkeys to type Shakespeare. Eventually you'll stumble on an answer, but it will take far too long to be interesting.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  4. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM