Thread: whats wrong with this program..??

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    whats wrong with this program..??

    Question.. :Write a program to input some integer value from the user and store them into a linked list. The
    user should be presented with the following choice:
    1. Insert a node in the list.
    2. Delete a node from the list.
    3. Display the list.
    4. Quit
    Rules for Insertion and deletion
    Insertion and deletion of the node should follow Last in and First Out Principle i.e.; the inserted
    node should get added towards the head of the list and the node to be deleted should also be
    deleted from the head of the list. If an attempt is done to delete an element from an empty list,
    then the program should prompt the user with an underflow error.

    My code... :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node
    {
    int data;
    struct node *next;
    };
    
    int main()
    {
          int n;
    
            printf("Select a Choice\n");
            printf("press 1 to insert a node in the list\n");
            printf("press 2 to delete a node from the lis\n");
            printf("press 3 to display the list\n");
            printf("press 4 to quit\n");
    
            scanf("%d", &n);
            if (n == 1);
            struct node * insert_begin(struct node *,int);
            if (n == 2);
            struct node * del_begin(struct node *);
            if(n == 3);
            void printnode(struct node *);
            if (n == 4);
            { printf("Bye\n");
              exit;
            }
    
    }
    
    
    struct node * insert_begin(struct node *l,int d)
    {
            struct node *temp;
            temp=(struct node *)malloc(sizeof(struct node));
            temp->data=d;
            temp->next=l;
            l=temp;
            return temp;
    }
    struct node* del_begin(struct node *l)
    {
            struct node *c=l;
            if(l==NULL)
            printf("List is empty\n");
            else
            l=l->next;
            free(c);
            return l;
    }
    
    void printnode(struct node *l)
    {
            printf("\n");
            while(l!=NULL)
            {
            printf("%d -> ",l->data);
            l=l->next;
            }
            printf("NULL");
    }



    P.S : the thing is gcc isnt giving any errors..
    and when i execute this..it simply says bye...!!!
    and i am just a beginner in C..so please pinpoint the errors..thank you..

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You should put your functions' prototypes before main ( as opposed inside main ) and call them properly.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Code:
            scanf("%d", &n);
            if (n == 1);
            struct node * insert_begin(struct node *,int);
            if (n == 2);
            struct node * del_begin(struct node *);
            if(n == 3);
            void printnode(struct node *);
            if (n == 4);
            { printf("Bye\n");
              exit;
            }
    You have extra semi-colon. You are not calling your functions properly. Revise your documentation on both if() and calling functions.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Location
    Stockholm, Sweden
    Posts
    131
    Your if statements are wrong


    Code:
    if (n == 1);
    struct node * insert_begin(struct node *,int);
    actually means
    Code:
    if (n == 1) {
        /* Do nothing */
    }
    struct node * insert_begin(struct node *,int);
    You probably want something like:
    Code:
    if (n == 1) {
        /* insert node */
    } else if (n == 2) {
        /* delete node */
    } ...
    Do not put the semicolon directly after the if statement, and I encourage using curly braces even when they aren't necessary. And using -Wextra when compiling with gcc would have given you hints about this error
    Last edited by iceaway; 10-04-2011 at 10:54 AM.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by tvss View Post
    P.S : the thing is gcc isnt giving any errors..
    Compiles doesn't mean works. The compiler only catches certain types of errors, from which it can't compile. If you intend for your program to add two numbers, but you typed a subtraction sign, the compiler can't catch that sort of thing. You should compile with your warnings turned all the way up for starters (-Wall option for gcc)
    Code:
    $ gcc -Wall list.c
    list.c: In function ‘main’:
    list.c:29: warning: statement with no effect
    list.c:32: warning: control reaches end of non-void function
    exit; just acts as a funciton pointer, kinda like saying a;. You don't actually do anything with exit or a. You need to call exit, like so: exit(0). But you're already in main, and that's at the end of your program, so you don't need to call it explicitly. When main finishes, the exit code is called for you. You just need to return a value from main. You correctly declared it to return an int, but you never return one. return 0; indicates success.

    and when i execute this..it simply says bye...!!!
    Code:
            if (n == 1);
            struct node * insert_begin(struct node *,int);
            if (n == 2);
            struct node * del_begin(struct node *);
            if(n == 3);
            void printnode(struct node *);
            if (n == 4);
    That semicolon at the end of the lines with "if" in them means you just throw away the if statement. The lines in red are actually function prototypes, not function calls. You also never declare a variable to actually store your list.
    and i am just a beginner in C..so please pinpoint the errors..thank you..
    Your n00b status is clear. I wonder why you're working on a linked list assignment when you still don't know how to use conditionals or call functions. Step back to square 1 and go over the basics. We have some tutorials to help you here, and Google has more. Crack open that text book, and read it from the beginning, working through all the sample exercises.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    1.)Remove the semi colons placed after if().
    2.)This program will execute only one time..so 1st you have to use a loop for repeated insertion and deletion.
    3.)You have to use a Head Node that always points towards the 1st node of the list.
    4.)Then instead of calling as insert(struct node*,int) call insert(head,int);
    5.)The way you called the functions are not correct.
    eg:if the function prototype is int add(int,int),then the call is as follows:
    say,sum=add(5,3);
    Last edited by vikasvijayan; 10-04-2011 at 09:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whats wrong with this program?
    By Karpaty in forum C Programming
    Replies: 8
    Last Post: 11-24-2007, 10:29 PM
  2. Whats wrong with my program?
    By Golden Bunny in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2002, 08:08 PM
  3. whats wrong with this program?
    By gamesguy3000 in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2002, 03:08 PM
  4. Whats wrong with this program!?
    By Unregistered in forum C++ Programming
    Replies: 17
    Last Post: 04-05-2002, 01:01 PM
  5. Whats wrong with my program?
    By Ruflano in forum C++ Programming
    Replies: 5
    Last Post: 02-21-2002, 05:09 PM