Question: Here is some code that i need help with completing in the break sections. It is with stacks using linked lists. #include using namespace std;
Here is some code that i need help with completing in the break sections. It is with stacks using linked lists.
#include
using namespace std;
// A linked list node
class Node
{
public:
int data;
Node *next;
Node()
{
data = 0;
next = nullptr;
}
Node(int data)
{
this->data = data;
next = nullptr;
}
};
class List
{
public:
struct Node* head;
struct Node* tail;
List()
{
head = tail = nullptr;
}
};
void ListPrint(List *list)
{
Node *tempNode = list->head;
while(tempNode != nullptr){
cout<
tempNode = tempNode->next;
}
}
void ListPrepend(List *list, Node *newNode) {
if (list->head == nullptr) { // list empty
list->head = newNode;
list->tail = newNode;
}
else {
newNode->next = list->head;
list->head = newNode;
}
}
void StackPush(List *stack, Node *newItem) {
// Insert as list head (top of stack)
ListPrepend(stack, newItem);
}
int main() {
List *myStack = new List;
int data;
char input;
do
{
cout<<" Current Stack: ";
ListPrint(myStack);
cout< cout<<"Enter Stack operation: "; cout<<"Push(u), Pop(o), Peek(e), quit(q): "; cin>>input; switch(input) { case 'u': cout<<"enter data to push: "; cin>>data; StackPush(myStack, new Node(data)); break; case 'o': /* if(!isEmpty(myStack)){ int poppedItem = StackPop(myStack); cout<<"Popped: "< } else cout<<"Stack Empty"; */ break; case 'e': /* if(!isEmpty(myStack)){ int peekedItem = StackPeek(myStack); cout<<"Peeked: "< } else cout<<"Stack Empty"; */ break; } }while(input != 'q'); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
