Hi everyone, I've worked on this thing for about three days now and am really getting frustrated. I need to make a stack using linear linked lists of arrays. I've got my class to compile without complaining, but am really struggling with the function definitons of push and peek. Any help would be greatly appreciated. Here is what I have so far:

//function to "push", move data to the stack
void Stack::push(char newItem[])
{
        //create new node
        node *newPtr = new node;
        assert(newPtr != NULL);
        newPtr->item = newItem;
        //inserts new node
        newPtr->next = topPtr;
        topPtr = newPtr;
}

 
//function to return the top item in stack
void Stack::peek(char stackTop[]) const
{
         stackTop[] = topPtr->item;
}

Edited 6 Years Ago by Nick Evan: n/a

probably want something like this. The function should return a const char*

const char* Stack:peek(void) const
{
    return topPtr->item;
}

// example calling 
Stack stack;
const char* item = stack.peek();

I figured out how to use the peek function by changing a few things in my class, but I'm still lost on how to assign an array of characters to a node in a linked list. I keep getting the compiler error "incompatible types of assignment char to char[]" Here is the code I have... I'm not sure I've changed it at all, most of the changing has been within my class.

void Stack::push(char newItem[])
{
     node *newPtr = new node;
     assert(newPtr != NULL);
 
     newPtr->item = newItem; //setting char item[] to newItem[]
     topPtr = newPtr;
}
This article has been dead for over six months. Start a new discussion instead.