Link to home
Start Free TrialLog in
Avatar of ksanand_be
ksanand_be

asked on

Problem with single-linked list

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}

struct account
{
 char *name;
 //float cur_exp,prev_debit, prev_credit;
 struct account *next;
 };
typedef struct account acc;
display(acc *);
add(acc *);
int main()
{
int i;
clrscr();
acc *list_acc;
list_acc = NULL;
clrscr();
//Print the various options available to the user
printf("1. View the records\n");
printf("2. Calcuate current per head expenditure\n");
printf("3. View total expenditure till date\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
switch(i)
 {
   case 1: printf ("Viewing records");
         add(list_acc);
         display(list_acc);
         break;

   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   default:
   printf("Input data unknown\n");
  };
getch();
}
display(acc *list_ptr)
 {
    while(list_ptr != NULL) //This is where the problem is
                      // the base address of the list still remains empty
                      // i.e. list_acc: {NULL, NULL}
    {
      printf("\n %s" ,list_ptr->name);
      list_ptr  = list_ptr->next;
     }
  }
add(acc *list_ptr) // this function works to the perfection.
 {
   acc *temp, *new_blk;
   temp = list_ptr;
   int i,number;
   printf(" Enter the number of people in your house");
   scanf("%d", &number);
   for(i=1;i<=number;i++)
   {
       if(list_ptr == NULL)
      {
       temp = (acc *)malloc(sizeof(acc));
       printf("\nEnter the name of person %d", i);
       scanf("%s",temp->name);
       temp->next = NULL;
       list_ptr = temp;
       }
      else
       {
        new_blk = (acc *)malloc(sizeof(acc));
        printf("\nEnter the name of person %d", i);
        scanf("%s",new_blk->name);
        new_blk->next = NULL;
        temp->next = new_blk;
       }

     }
   }

I reckon it's kind of local variable problem. However I am not able to exactly pin point the error. Kindly help. The program runs with out error but there is error in the functionality of the program. i.e. the display fucntion doesn't work.

Thnks

ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ksanand_be
ksanand_be

ASKER

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}

struct account
{
 char *name;
 //float cur_exp,prev_debit, prev_credit;
 struct account *next;
 };
typedef struct account acc;
display(acc * );
acc add(acc *);
int main()
{
int i;
clrscr();
acc *list_acc;
list_acc = NULL;
clrscr();
//Print the various options available to the user
printf("1. View the records\n");
printf("2. Calcuate current per head expenditure\n");
printf("3. View total expenditure till date\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
switch(i)
 {
   case 1: printf ("Viewing records");
         list_acc = add(list_acc);          // LINE 34
         display(list_acc);
         break;

   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   default:
   printf("Input data unknown\n");
  };
getch();
}
display(acc *list_ptr)
 {
    while(list_ptr != NULL)    {
      printf("\n %s" ,list_ptr->name);
      list_ptr  = list_ptr->next;
     }
  }
acc add(acc *list_ptr)
 {
   acc *temp, *new_blk;
   temp = list_ptr;
   int i,number;
   printf(" Enter the number of people in your house");
   scanf("%d", &number);
   for(i=1;i<=number;i++)
   {
        new_blk = (acc *)malloc(sizeof(acc));
        printf("\nEnter the name of person %d", i);
        scanf("%s",new_blk->name);
        new_blk->next = NULL;
        if(list_ptr == NULL)
         list_ptr = new_blk;
        else
         temp->next = new_blk;
       return (list_ptr);                            // LINE 75
       }


   }

As you told me I have made the changes but I get an error saying
"cannot convert account  to account * " line 34
"cannot convert account * to account"  line 75

Should I be returning list_ptr from the function add()??????

Thnks
I tried using passing by reference and the program runs without any error. However only the first member and last member of the list are printed in the display function correctly. For instance if i input number = 4 (4 names -a,b,c,d) only a and d gets printed but the inbetween values r lost. kindly help. For your reference find the code below:

extern"C" {
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
}

struct account
{
 char *name;
 //float cur_exp,prev_debit, prev_credit;
 struct account *next;
 };
typedef struct account acc;
display(acc * );
acc add(acc **);
int main()
{
int i;
clrscr();
acc *list_acc;
list_acc = NULL;
clrscr();
//Print the various options available to the user
printf("1. View the records\n");
printf("2. Calcuate current per head expenditure\n");
printf("3. View total expenditure till date\n");
printf("4. Who spends the most\n");
printf("5. Graphical representation\n");
printf("6. Add/Delete a member\n");
scanf("%d", &i);
switch(i)
 {
   case 1: printf ("Viewing records");
         add(&list_acc);
         display(list_acc);
         break;

   case 2:
   case 3:
   case 4:
   case 5:
   case 6:
   default:
   printf("Input data unknown\n");
  };
getch();
}
display(acc *list_ptr)
 {
    while(list_ptr != NULL) //This is where the problem is
                      // the base address of the list still remains
                      // i.e. list_acc: {NULL, NULL}
    {
      printf("\n %s" ,list_ptr->name);
      list_ptr  = list_ptr->next;
     }
  }
acc add(acc **list_ptr)
 {
   acc *temp, *new_blk;
   temp = *list_ptr;
   int i,number;
   printf(" Enter the number of people in your house");
   scanf("%d", &number);
   for(i=1;i<=number;i++)
   {
        new_blk = (acc *)malloc(sizeof(acc));
        printf("\nEnter the name of person %d", i);
        scanf("%s",new_blk->name);
        new_blk->next = NULL;
        if(*list_ptr == NULL)
         {
         temp = new_blk;
         *list_ptr = temp;
        }
        else
         temp->next = new_blk;
       }

   }

Thankyou.