LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-12-2004, 07:44 PM   #1
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 31
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);
  }
  
}
 
Old 01-12-2004, 08:02 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 66
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.
 
Old 01-12-2004, 08:18 PM   #3
wapcaplet
Guru
 
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
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 )
...
 
Old 01-12-2004, 08:45 PM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists: java vs c kpachopoulos Programming 4 10-30-2005 05:41 PM
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 02:59 AM
Linked Lists leonidg Programming 7 03-10-2005 03:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 11:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 03:40 PM


All times are GMT -5. The time now is 01:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration