-1

Q please help me this program is not working properly. not displaying the value.this program is an example of singly linked list which I am trying to run on c. `

#include<stdio.h>
 #include<stdlib.h>        //malloc defined
struct node       
{
       int data;
       struct node *next;      
       };
 add()      //add function
  {
      int value;
       struct node *n;
       n=(struct node*)malloc(sizeof(struct node));  //mem allocation
       printf("enter the value to add\n");
       scanf("%d",&value);
       n->data=value;
       n->next=NULL;
      // n=n->next;
      // n->next=NULL;
       }  
  delete()     //delete function
  {
       //   n=n->next;
          struct node *n;    //declaration
          printf("the node deleted is %d",n->data);
          free(n);
          }

  display()          //display function
  {
           struct node *n;
           while(n!=NULL)
             {
             printf("%d",n->data);
             n=n->next;

             }
           }             
int main()
{
     int ch;
     while(1)
     {
             printf("do you want to add node press 1\n");

             printf("do you want to delete node press 2\n");

             printf("do you want to display node press 3\n");

             printf("do you want to exit press 4\n");
             scanf("%d",&ch);
     switch(ch)
     {
        case 1:add();
               break;

        case 2:delete();
               break;

        case 3:display();
               break;

        case 4:exit(0);


        default: printf("wrong choice!!!\n");                                      

     }
     }
     return 0;
     getch();
     }
 please help me this program is not working properly.

not displaying the value.this program is an example of singly linked list which I am trying to run on c.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
prajyot
  • 1
  • 3
  • Local variables defined inside function are, well, *local*, and exist only inside the functions they are declared in. A variable named `n` inside the function `add` is different from a variable with the same name in another function. – Some programmer dude Feb 25 '16 at 06:59
  • 2
    It also looks to me like you need to find a good beginners book, so I suggest you check out [The Definitive C Book Guide and List](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – Some programmer dude Feb 25 '16 at 07:00
  • Yes sir I need to work more on my C skills thanks for your recommendation about the book – prajyot Feb 26 '16 at 08:00

3 Answers3

0

printf("%d",n->data); is not printed because:

struct node *n;  // n is not determined.
while(n != NULL) // undefined behaviour

n is different from the other n that is in the add() function. You never passed the struct to the other functions so it will not do what you wanted it to do.

Andreas DM
  • 10,685
  • 6
  • 35
  • 62
  • One small problem: Local variables are not initialized, so saying that `n` is `NULL` is wrong. The value of `n` is *indeterminate* (and will in reality seem random). Using such variables except to initialize them leads to *undefined behavior*. – Some programmer dude Feb 25 '16 at 07:06
  • @JoachimPileborg Thanks :) – Andreas DM Feb 25 '16 at 07:06
0
  1. in the function display(), the local variable 'n' is only declared but not defined, so here 'n' is only a wild pointer. It's happened in the function delete() as well.
  2. it's not a correct way to implement the single linked list. In the function add() what you wrote is only create a node, but not add a node to linked list.
0
 #include<stdio.h>
 #include<stdlib.h>        //malloc defined
struct node       
    {
       int data;
       struct node *next;      
       }*n,*p;

 create()      //add function
  {
      int value;

       n=(struct node*)malloc(sizeof(struct node));  //mem allocation
       printf("enter the value to add\n");
       scanf("%d",&value);
       n->data=value;
       n->next=NULL;

        }  
   add()
   {    
   int value;
//    struct node *p;
     p=(struct node*)malloc(sizeof(struct node));
    printf("enter the value to add next\n");
    scanf("%d",&value);
   n->next=p;
   p->data=value;
   p->next=NULL;

    }    
  delete()     //delete function
  {

          printf("the node deleted is %d",p->data);
           n->next=NULL;
          free(p);   

            }

      display()          //display function
      {

               while(n!=NULL)
                 {
                 printf("%d\n",n->data);
                 n=n->next;

             }
           }             
int main()
{
     int ch;
     while(1)
     {
         printf("do you want to create node press 1\n");
         printf("do you want to add node press 2\n");
         printf("do you want to delete node press 3\n");
         printf("do you want to display node press 4\n");
         printf("do you want to exit press 5\n");
         scanf("%d",&ch);
 switch(ch)
 {
    case 1:create();
           break;

    case 2:add();
           break;

    case 3:delete();
           break;

    case 4:display();
           break;

    case 5:exit(0);


    default: printf("wrong choice!!!\n");                                      

 }
 }
 return 0;
 getch();
 }
prajyot
  • 1
  • 3