Write a C++ function to implement push operation on a stack implemented using a
ID: 671483 • Letter: W
Question
Write a C++ function to implement push operation on a stack implemented using a linked list. The push function takes in an integer argument and inserts it onto the top of the stack.
void Stack::push(int value)
struct LinkedListNode {
int data;
LinkedListNode* next_node;
};
class Stack
{
public:
// CONSTRUCTOR and DESTRUCTOR
Stack( );
~Stack( );
// Stack Operations
void push(int value);
int pop( );
int top();
bool isEmpty( );
private:
LinkedListNode *top_ptr; // Points to top of stack
};
Explanation / Answer
void Stack::push(int value){
LinkedListNode *newnode = new LinkedListNode;
newnode->data = value;
newnode->next_node = top_ptr;
top_ptr = newnode;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.