LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Linked Lists: Pointers sent to functions as arguments do not change their valur (https://www.linuxquestions.org/questions/programming-9/linked-lists-pointers-sent-to-functions-as-arguments-do-not-change-their-valur-134145/)

Jose Muņiz 01-12-2004 06:44 PM

Linked Lists: Pointers sent to functions as arguments do not change their valur
 
After long C vacations, I decided to create a linked list to see if I remembered things right. I see I don't. My linked list is supposed to be a queue.


In function mk_item, if tail equals NULL, then it means there is no linked list created. Hence, a list is created and both head and tail are forced to point into that direction. However, when this function exits, the value of head returns to the original NULL. I don't understand why, ,considering it is pointers we are talking about. What I am doing wrong? And how can I solve it? Thank you very much for your help :)

Code:


#include <stdio.h>
#include <stdlib.h>


/* List of structures */
struct list
{
  int ID;
  int value;
  struct list* pointer;
};
typedef struct list list;

/* Function Prototypes */
void show_menu(void);
list* mk_item(list*, list*);
list* rm_item(list*, list*);
void print_list(list*);

/* Global variables  */
int ID_assigner = 0;

/* main()              */
int main()
{
  char option;
  list* tail = NULL;
  list* head = NULL;
 
  while (option != 'd')
  {         
    show_menu();
    scanf("%s", &option);

    switch (option)
    {
      case 'a': case 'A':
        tail = mk_item(tail, head);
        printf("The value of head is %p\n" ,head);
        break;
      case 'b': case 'B':
        head = rm_item(tail, head);
        break;
      case 'c': case 'C':
        printf("%s \t\t %s \n", "ID", "Value");
        print_list(tail);
        break;
      case 'd': case 'D':
        break;
      default :
        printf("\aOoops! Invalid option!\n");
        break;
    }   
  }

  printf("You have made a small program very happy :D Thank you for using me. \n");
 
  return 0;
}


/* show_menu()          */
void show_menu(void)
{
  printf("Choose the correct option \n"
        " a)  Create a new item    \n"
        " b)  Delete last item    \n"
        " c)  Print items          \n"
        " d)  Exit                \n"
        " $ ");
       
}


list* mk_item(list* tail, list* head)
{

 
  if (tail == NULL)
  {
    tail = (list*) malloc(sizeof(list));
    head = tail;

    tail -> ID = ++ID_assigner;
    tail -> pointer = NULL;

    printf("Please provide the value of item ID %d: ", ID_assigner);
    scanf("%d", &tail -> value);

    printf("Value of tail is %p and value of head is %p\n", tail, head);

  }

  else
  {
    list* temp = NULL;

    temp = (list*) malloc(sizeof(list));

    temp -> ID = ++ID_assigner;
    temp -> pointer = NULL;

    printf("Please provide the value of item ID %d: ", ID_assigner);
    scanf("%d", &temp -> value);

    head -> pointer = temp;
    head = temp;
  }
 
  return tail;
}


list* rm_item(list* tail, list* head)
{
  list* temp = tail;

  if (temp -> pointer == NULL || temp == NULL)
    tail = NULL;
  else
  {
    while ((temp -> pointer)-> pointer != NULL)
    {
      temp = temp -> pointer; 
    }

    head = temp;
  }
 
  return head;
}

void print_list(list* tail)
{
  if (tail == NULL)
  {
    printf("All printed\n");
  }
  else
  {
    printf("%d \t\t %d \n", tail -> ID, tail -> value);
    print_list(tail -> pointer);
  }
 
}


jtshaw 01-12-2004 07:02 PM

I am slightly confused by your logic here. But I did go out with the guys after work today so that might be why.....

The way I do link lists is to declare a structure (I will call it NODE) like the one you have created with some kind of data inside it and a next pointer. I start the program with head and tail (both NODE's) pointing to null. My routine that adds data would first check to see if head is NULL. If it is it creates a new NODE (mallocs data, sets the data value, and sets the next value to NULL) and points head to this node and points tail to head->next. If head isn't null it checks to see if tail is NULL, if it is it creates a new node where tail is currently pointing. If both head and tail aren't null then tail = tail->next and we create a new node where tail is currently pointing. To pull data off the queue we simply save the data from head, and point head to head->next. This of course assumes a FIFO queue.

wapcaplet 01-12-2004 07:18 PM

I think maybe what you need to do is to pass the pointers to mk_item by reference, rather than by value. The two parameters to mk_item (list * tail, list * head) are local copies that are lost once the function returns, so if you set "tail = " something, it's only for the scope of that function.

So try this:

list * mk_item( list * &tail, list * &head )
...

kev82 01-12-2004 07:45 PM

wapcaplet is right but references are C++ if your doing it in C(seems like you are) you'll have to do it with a double pointer, redeclare mk_item like this

list* mk_item(list* tail, list** head) {

and call it with

mk_item(tail, &head)


All times are GMT -5. The time now is 12:39 AM.