views:

75

answers:

4

For starters this is apart of my current homework assignment for my Data Structures class. I am not asking for answers, I am asking for help.

I have a stack class that is implementing a Linked List instead of an array. I am currently trying to write my pop() function. I have a node for my theBack part of the stack.

I am just confused as to getting to the predecesor of my theBack node.

Any help with this would be awesome! Thanks!

A: 

if you're implementing a stack, then you would want to pop the top node anyways, so you would set theTop to be the next node in line (which should be a pointer in theTop node)

localhost
I understand that I want to pop theTop off the stack. I don't know how to get the the predecesor of theTop.
Johnny Whisman
@JCwhisman: Don't you have at least a "next" pointer or something like that in your nodes?
In silico
there is no predecessor, the top is the very top. the actual code will depend on your language (whether you have to delete nodes, etc).
localhost
this is a singly linked list so there is no Pointer to it's predecessor. Only what is next.
Johnny Whisman
+2  A: 

Each node in the singly-linked list links to the previous node. The very first item pushed onto the stack has a NULL, all the others point to the item 'below' them (the predecessor) in the stack.

So before you destroy the top node, you grab the backlink and save it as the new top. Something like this pseudocode, which presumes a stack of int values:

pop()
    ALink *poppedLink = myTop;
    myTop = poppedLink.nextNode; // point to next node

    int returnValue = poppedLink.value; // save the value
    delete poppedLink; // destroy the instance

    return returnValue;

If by "predecessor", you mean: "the thing that was popped before this": that's long gone, isn't it?

egrunin
A: 

What do you mean the "predecessor" of top? Top node is the head of your list, it doesn't have any predecessors.

LKN
I am sorry I didn't clarify what theTop actually is. The top of the stack is the most recently pushed item in the stack. It is the actually the back of my list.
Johnny Whisman
Why are you pushing items at the back of the list in a stack implementation? Anyways what you could do is have a pointer loop through your list till it hits the penulatimate node, or you could have a pointer whose sole job is to keep track of the penultimate node and update it each time you push an element onto the stack
LKN
+2  A: 

A stack is actually reasonably simple to implement as a singly linked list, due to its restricted push and pop operations. It's actually a lot easier if you insert pushed elements at the head of the list. Since it's homework, I'll provide pseudo-code.

To initialise a stack, it's simply creating:

top -> null

with this code:

def init (stk):
    stk->top = null                    # just initialise to empty.

Pushing an item is actually inserting it at the start of the list. So, when you push 3, 4 and 5, you get:

       +---+
top -> | 3 | -> null
       +---+
       +---+    +---+
top -> | 4 | -> | 3 | -> null
       +---+    +---+
       +---+    +---+    +---+
top -> | 5 | -> | 4 | -> | 3 | -> null
       +---+    +---+    +---+

with the following code:

def push (stk, val):
    item = new node                     # Create the node.
    item->value = val                   # Insert value.
    item->next = stk->top               # Point it at current top.
    stk->top = item                     # Change top pointer to point to it.

And popping is then just removing that first node.

def pop (stk):
    if stk->top == null:                # Catch stack empty error.
        abort with "stack empty"
    first = stk->top                    # Save it for freeing.
    val = first->value                  # Get the value from the top.
    stk->top = first->next              # Set top to the top's next.
    free first                          # Release the memory.
    return val                          # Return the value.
paxdiablo