Thread: Printing a reverse linked list

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    Post Printing a reverse linked list

    hey guys so far my code works good but when i enter a code and press '\n' the code statements are executed twice....

    Code:
    .//Filename: rllst.c
    //This program prints a reverse linked list.
    //Author: Nyah check, chairman, INK Corp..
    //Licence: Freeware
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int print_list( struct node);
    int insert_list(void);
    struct node
    {
        int num;
        struct node *next;
    }values;
    
    struct node *first = NULL;
    struct node *p, *q;
    
    int main(void)
    {
        char code;
        int n;
    
        printf("\nThis program stores variables in  a linked list and prints all of them in reverse order.");
        printf("\nPress '0': QUIT, 'c': CREATE LIST, 'p': PRINT REVERSED LIST.");
    
        for ( ; ; )
        {
            printf("\nEnter code: ");
            code = getchar();
            getc(stdin);
    
            switch(code)
            {
                case '0':   printf("\nThanks for using our software.");
                            printf("\n We would like to receive your comments, suggestions to better our products.");
                            printf("\nINK Corp.. 2012.");
                            exit(EXIT_SUCCESS);
    
                case 'C':
                case 'c':   insert_list();
                            break;
    
                case 'P':
                case 'p':   print_list(values);
                            break;
    
                default:    printf("\nInvalid Command.");
                            break;
            }
        }
        return 0;
    }
    
    //insert_list(): This function adds variables to a linked list and returns if it was unsuccessful.
    int insert_list(void)
    {
        int n;
        struct node *new_node;
        new_node =(struct node *)malloc(sizeof(struct node));
    
        if ( new_node == NULL)
        {
            printf("\nNo memory to add data.");
            exit(EXIT_FAILURE);
        }
    
        else
    
            printf("\nEnter number: ");
            scanf("%d", &n);
    
            new_node->num = n;
            p == first;
            new_node->next = first;
            q = new_node;
    
        return 0;
    }
    
    //print_list(): This function prints all the values in the linked list in reverse order.
    //              starting from the last element.
    int print_list( struct node)
    {
        printf("\nReverse list");
        printf("\n-------------");
        for ( int i = 1; q != p; q = q->next, i++)
            printf("\n%d. %d ",i, q->num);
    
        return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    How does your code work? It doesn't even compile!
    Code:
    $ make list
    gcc -Wall -g -std=c99  -lm -lpthread -lcurses -lefence  list.c   -o list
    list.c:9: warning: ‘struct node’ declared inside parameter list
    list.c:9: warning: its scope is only this definition or declaration, which is probably not what you want
    list.c: In function ‘main’:
    list.c:46: error: type of formal parameter 1 is incomplete
    list.c:23: warning: unused variable ‘n’
    list.c: In function ‘insert_list’:
    list.c:75: warning: statement with no effect
    list.c: At top level:
    list.c:84: error: conflicting types for ‘print_list’
    list.c:9: note: previous declaration of ‘print_list’ was here
    list.c: In function ‘print_list’:
    list.c:84: error: parameter name omitted
    make: *** 
    [list] Error 1

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It compiles OK - if you use a C++ compiler.
    Code:
    $ g++ -Wall foo.cpp
    foo.cpp: In function ‘int main()’:
    foo.cpp:23: warning: unused variable ‘n’
    foo.cpp: In function ‘int insert_list()’:
    foo.cpp:75: warning: statement has no effect
    Where "for ( int i" is supported, along with unnamed parameters, and necessary casting of malloc.

    The OP should really start to use the right compiler for the right language. You need to make sure you learn the language, and not just to accept whatever you can get past the compiler.

    Plus the line 75 issue.


    > but when i enter a code and press '\n' the code statements are executed twice
    That would be line 31 picking up the trailing \n left behind by line 72.
    scanf() is pretty awful at the best of times, but trying to mix it with other input methods (like getchar) is a disaster area.

    Replace your two lines of getchar/getc with
    scanf(" %c", &code); // note the leading space
    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.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok i'll d that but what kind of compiler is @anduril462 using i use the codeblocks gcc compiler and my code runs so you better check yours.....

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Nyah Check View Post
    ok i'll d that but what kind of compiler is @anduril462 using i use the codeblocks gcc compiler and my code runs so you better check yours.....
    My compiler is fine (I use gcc too). You posted invalid C code in the C forum. Your code only compiles if you're using a C++ compiler (see the very first line of Salem's reply). You need to post in the appropriate forum next time.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think the code is right for the forum - it's just Nyah needs to make sure they're using gcc rather than g++ to compile code.
    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
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    ok i'll check the code but i don't understand why its happening that way with your compiler

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    my compiler is codeblocks c /c++ compiler...

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Code::Blocks is an IDE, not a compiler. You should be able to configure it to use gcc instead of g++. Also, naming your source files as ".c" may help.
    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
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197
    thanks @laserlight do that right away had no idea........

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Buea, Cameroon
    Posts
    197

    new code.

    setup my compiler corectly now.... anyway here is my new code.

    Code:
    //new code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int print_list( struct node);
    int insert_list(void);
    struct node
    {
        int num;
        struct node *next;
    }values;
    
    struct node *first = NULL;
    struct node *q;
    
    int main(void)
    {
        char code;
    
        printf("\nThis program stores variables in  a linked list and prints all of them in reverse order.");
        printf("\nPress '0': QUIT, 'c': CREATE LIST, 'p': PRINT REVERSED LIST.");
    
        for ( ; ; )
        {
            printf("\nEnter code: ");
            code = getchar();
            getc(stdin);
    
            switch(code)
            {
                case '0':   printf("\nThanks for using our software.");
                            printf("\n We would like to receive your comments, suggestions to better our products.");
                            printf("\nINK Corp.. 2012.");
                            exit(EXIT_SUCCESS);
    
                case 'C':
                case 'c':   insert_list();
                            break;
    
                case 'P':
                case 'p':   print_list(values);
                            break;
    
                default:    printf("\nInvalid Command.");
                            break;
            }
        }
        return 0;
    }
    
    //insert_list(): This function adds variables to a linked list and returns if it was unsuccessful.
    int insert_list(void)
    {
        int n;
        struct node *new_node;
        new_node =(struct node *)malloc(sizeof(struct node));
    
        if ( new_node == NULL)
        {
            printf("\nNo memory to add data.");
            exit(EXIT_FAILURE);
        }
    
        else
    
            printf("\nEnter number: ");
            scanf("%d", &n);
    
            new_node->num = n;
            new_node->next = first;
            q = new_node;
    
        return 0;
    }
    
    //print_list(): This function prints all the values in the linked list in reverse order.
    //              starting from the last element.
    int print_list( struct node)
    {
        printf("\nReverse list");
        printf("\n-------------");
        for ( int i = 1; q != NULL; q = q->next, i++)
            printf("\n%d. %d ",i, q->num);
    
        return 0;
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list reverse recursively
    By gaurav_13191 in forum C Programming
    Replies: 4
    Last Post: 12-07-2011, 09:32 AM
  2. how to reverse items in linked list?
    By ilikeapplepie in forum C Programming
    Replies: 8
    Last Post: 04-09-2011, 11:15 PM
  3. Reverse a linked list, probably a simple fix.
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 11-10-2010, 11:54 PM
  4. Linked List(+reverse) trouble
    By killmequick in forum C Programming
    Replies: 8
    Last Post: 09-18-2008, 01:40 PM
  5. reverse a singly linked list
    By ivandn in forum C Programming
    Replies: 15
    Last Post: 12-18-2001, 05:33 PM