Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; //STEP1: Create a conceptual idea of a chunk. We'll build a stack of chunks class chunk { public: int value;

#include #include using namespace std;

//STEP1: Create a conceptual "idea" of a chunk. We'll build a stack of chunks class chunk { public: int value; chunk *next; //give our building blocks a default look and feel chunk() { value = 0; next = NULL; } };

class Stack { public: chunk *top; //gateway pointer to the structure

//constructor Stack() { top = NULL; //start with an empty stack } //Functions, with an underlying philosophy: LIFO //Function 1: Add to the stack, popularly called 'push' void push(int x) { //get a new chunk, we are going to add this chunk *temp; temp = new chunk; temp->value = x;

//is my stack empty? if(top == NULL) { //new element becomes the top top = temp; } else { //stack has more than one chunk in there already temp->next = top; top = temp; } }

//Function 2: Delete from stack, popularly called 'pop' void pop() { if(top == NULL) { cout << "Empty stack, nothing to delete" << endl; } else { chunk *temp; temp = top; top = temp->next; //top points to the second element cout << "About to delete: " << temp->value << endl; delete temp; } }

void display() { chunk *traverse = top; if(top == NULL) { cout << "Empty Stack. Nothing to display" << endl; } else { while(traverse != NULL) { int i; cout << traverse->value << "-->" << endl; traverse = traverse->next;

} } }

};

int main() {

Stack ourStack; int choice = 0;

while(1) { cout << "Press 1 to add to stack" << endl; cout << "Press 2 to delete from stack" << endl; cout << "Press 3 to display" << endl; cout << "Anything else to quit" << endl; cin >> choice;

switch(choice) { case 1: cout << "Add what?" << endl; int value; cin >> value; ourStack.push(value); break;

case 2: ourStack.pop(); break;

case 3: ourStack.display(); break;

default: exit(1); } }

}

Please base on this stack code, wirte a Queue code and Linked list code. Thank you!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Intelligent Information And Database Systems 6th Asian Conference Aciids 2014 Bangkok Thailand April 7 9 2014 Proceedings Part I 9 2014 Proceedings Part 1 Lnai 8397

Authors: Ngoc-Thanh Nguyen ,Boonwat Attachoo ,Bogdan Trawinski ,Kulwadee Somboonviwat

2014th Edition

3319054759, 978-3319054759

More Books

Students also viewed these Databases questions

Question

Calculate the lifetime value (LTV) of a loyal customer.

Answered: 1 week ago

Question

Use service tiering to manage the customer base and build loyalty.

Answered: 1 week ago