does anyone know how to pop off a stack?

This is a discussion on does anyone know how to pop off a stack? within the C Programming forums, part of the General Programming Boards category; I have a linked list, that I want to remov things off the top like a stack.... I made this ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    84

    does anyone know how to pop off a stack?

    I have a linked list, that I want to remov things off the top like a stack....

    I made this function but it doesn't work...

    does anyone know what I'm doing wrong?

    Code:
    STUDENT* remv_student( STUDENT* ptr ) {                
                                                   
            static STUDENT popped_student;        
                                                   
             popped_student = *top;                
                                                 
             ptr = top;
             
             top = top -> studentPtr;
             
             free (ptr);
             
             --students_enrolled;
             
             return &popped_student;
             
             }

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    First, top is never defined. I'm going to assume that it's a global variable. I'd name it something a little more... informative, myself. "student_list", etc.

    Your code seems fine to me. (Though no error checking for end of list.) However, why return a pointer? (To a variable inside a function... although static, seems a fishy thing to do...) Couldn't you just return the structure?

    Otherwise, is top (and it's members) valid? What exactly "doesn't work"? Does it crash? If so, where?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I agree. You shouldn't be using the static variable at all. Just return the node you're popping. Oh, also, you can't update a what a pointer points at inside another function unless you're passing a pointer to the pointer you want to update. It should then be something like:
    Code:
    STUDENT *pop( STUDENT **stack )
    {
        STUDENT *n = NULL;
        if( students_enrolled )
        {
            n = *stack;
            *stack = *stack->next;
            --students_enrolled;
        }
        return n;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21