Thread: Passing lists to functions

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17

    Passing lists to functions

    First of all i would like to greet the forum,i just registered.Hope someday to be able to help here too,like you.

    My assignment is about implementing a linked stack and a linked queue and my program should be able to enqueue-dequeue/push-pop,show and retrieve data from them (seperate operations for each).i have created all this using global variables to avoid double and triple pointers, but professor wants it with pointers.we are learning C this semester together with data structures,so i have zero experience with pointers.it took me days to write these 360 lines and i want it to be as it must be.i have noone to cooperate with,since i am the only one that had managed to do it...

    my problem is passing lists to functions.this is the small part of my program ( enqueue a node to a linked queue) that i am working on to solve the problem,but i do not know what goes wrong with the pointers:

    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    struct node
    {
     int info;
     struct node *next;
    };
    
    void enqueue(struct node **fro,struct node **rea,struct node **tmp,int *qs);
    struct node newnode(int value);
    
    
    main()
    {       
      int value,qsize=0; 
      struct node *front=NULL,*rear=NULL,*temp;
      do{
       scanf("&#37;d",&value);
       *temp = newnode(value);
       enqueue(&front,&rear,&temp,&qsize);
      } while (value!=0);
    }
    
    struct node newnode(int item)
    {
     struct node *tmp;
     tmp=(struct node *)malloc(sizeof(struct node));
     tmp->info=item;
     tmp->next=NULL;
     return *tmp; 
    }
      
      
    void enqueue(struct node **fro,struct node **rea,struct node **tmpe,int *qs)
    {
      if(rea == NULL)
      {
      rea = tmpe;
      fro= rea;
      *qs++;
      }
      else
      {
      (*rea)->next = *tmpe;
      rea = tmpe;
      *qs++;
      }
    }
    Thanks in advance.I would really appreciate it ,if you could suggest me a book or give me a link that could help me.
    Last edited by Griever; 11-13-2008 at 08:33 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Since no one else is touching this, here's a rewrite. Study the changes carefully, especially those involving pointers.
    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    struct node
    {
      int info;
      struct node *next;
    };
    typedef struct node Node;
    
    Node *newnode( int value);
    void enqueue( Node **fro, Node **rea, Node *tmpe, int *qs);
    
    int main()
    {
        int value, qsize = 0;
        Node *front = NULL, *rear = NULL, *temp;
    
        while (1)
        {
            scanf( "%d", &value);
            if (value == 0) break;
            temp = newnode( value);
            enqueue( &front, &rear, temp, &qsize);
        }
    
        for (temp = front; temp; temp = temp->next)
            printf( "%d ", temp->info);
        printf( "\n");
    
      system("pause");
    }
    
    Node *newnode( int item)
    {
        Node *tmp;
        tmp = (Node*) malloc( sizeof( Node));
        tmp->info = item;
        tmp->next = NULL;
        return tmp;
    }
    
    void enqueue( Node **fro, Node **rea, Node *tmpe, int *qs)
    {
        if (*rea == NULL)
        {
            *rea = tmpe;
            *fro = *rea;
            *qs++;
        }
        else
        {
            (*rea)->next = tmpe;
            *rea = tmpe;
            *qs++;
        }
    }

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    looking at it for days,never thought of using a pointer with a function.also the use of pointers in enqueue function make sense now.thank you very much for your time,you indirectly helped the whole class.i will go apply this to all the code.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    here's my 2c and btw what does system("pause") do as I couldn't get it to compile on my machine.
    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    struct node
    {
        int info;
        struct node *next;
    };
    
    struct node *enqueue(struct node *fro, struct node *tmpe, int *qs);
    struct node *newnode(int value);
    
    int main(void)
    {       
        int value, qs=0; 
        struct node *front=NULL,*rear=NULL,*temp=NULL;
    
        do {
            scanf("&#37;d",&value);
            temp = newnode(value);
            front = enqueue(front, temp, &qs);
        } while (value != 0);
    
        for (front=front->next; front; front=front->next)
            printf("front->info is %d\n", front->info);
    }
    
    struct node *newnode(int item)
    {
        struct node *tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=item;
        tmp->next=NULL;
        return tmp; 
    }
      
    struct node *enqueue(struct node *fro, struct node *tmpe, int *qs)
    {
        struct node *x ;
        if (fro) {
            x = fro;
            fro = tmpe;
            tmpe = x;
            fro->next = tmpe;
        }
        else
            fro = tmpe;
        (*qs)++;
        return fro;
    }
    Last edited by itCbitC; 11-13-2008 at 01:25 PM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    A few problems:
    * You entered the terminating 0 into the queue.
    * You've ignored the rear pointer.
    * You've created a stack, not a queue (i.e., following the "front" pointer prints out the input backwards).

    system("pause") is a magical command that only compiles on my machine, apparently. What could it possibly do?. I assume you're joking. However, I should have removed it before posting.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by nucleon View Post
    A few problems:
    * You entered the terminating 0 into the queue.
    that's easily undone.
    Quote Originally Posted by nucleon View Post
    * You've ignored the rear pointer.
    didn't ignore it but there's no use for it so I took it out.
    Quote Originally Posted by nucleon View Post
    * You've created a stack, not a queue (i.e., following the "front" pointer prints out the input backwards).
    correct it's a stack as the OP wanted a queue and a stack so now he's got it all covered.
    Quote Originally Posted by nucleon View Post
    system("pause") is a magical command that only compiles on my machine, apparently. What could it possibly do?. I assume you're joking. However, I should have removed it before posting.
    jokes apart, your source didn't compile so I took a stab at the program.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nucleon
    system("pause") is a magical command that only compiles on my machine, apparently.
    Yes, that is quite possible, since you did not #include <stdlib.h>, and <malloc.h> should be removed in favour of <stdlib.h> anyway.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2008
    Location
    Greece,Athens
    Posts
    17
    from the little i know,system("PAUSE") works only on windows systems (DOS command) and on C.

    and itCbitC thanks for your time too.i see you are passing addresses than the whole pointers.i tried to do that too but still had no idea about putting a pointer with a function.thanks guys.
    Last edited by Griever; 11-13-2008 at 05:04 PM.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Thank you Griever for clarifying the system("pause") call. I didn't know that it was a DOS/Windows thing.
    I was baffled by its inclusion though in a way it made me give your program a try.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    I didn't realize this place was full of a bunch of unix snobs.
    Goodbye.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by nucleon View Post
    I didn't realize this place was full of a bunch of unix snobs.
    Goodbye.
    Eh? Chill out. I'd describe this place as a pretty good mix. We are merely pointing out that a particular piece of code is nonstandard and won't work everywhere. Given that this board is C/C++ centric, not centric to any particular platform, this is not an unreasonable thing to do.

    Some people (not pointing fingers) might seem rude from time to time. Maybe they are. Who cares. If you can't stand it, go ahead and leave. Otherwise take a pill and relax a little okay?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nucleon
    I didn't realize this place was full of a bunch of unix snobs.
    Perhaps you failed to notice that I said nothing about running the program, and that itCbitC's problem was about compiling the program. The system() function itself is in the standard library, under the <stdlib.h> header, but when adding that function call, you forgot to #include <stdlib.h> so you were relying on that header being included by another, which is not guaranteed. It is as simple as that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by nucleon View Post
    I didn't realize this place was full of a bunch of unix snobs.
    Goodbye.
    I sincerely apologize if I inadvertently came across as rude or a Unix snob. I was merely stating that I couldn't compile your program.

    Cool beans!!!

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    i have a question. LIFO is a stack right so what is a que because i was told that u can work at any end but the end u chose u have to continue using that end throughout the code.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by frassunit
    LIFO is a stack right so what is a que because i was told that u can work at any end but the end u chose u have to continue using that end throughout the code.
    A queue is a FIFO data structure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. structure question: passing to functions
    By kocika73 in forum C Programming
    Replies: 4
    Last Post: 03-07-2005, 08:58 PM
  3. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM