In: Computer Science
Description( IN C++)!!
The purpose of this challenge is to implement a stack using a Linked List as a backing data structure
Requirements
struct Location { string name; string address; }; struct VisitNode { Location loc; VisitNode * next; };
VisitNode * create() { VisitNode * newnode; try { newnode = new VisitNode; } catch (bad_alloc) { newnode = NULL; } return newnode; } // USAGE: To create a new node, use as follows // VisitNode * newnode = create();
Sample main()
int main() { Stack browser; // simulate a browser history browser.push("Google", "https://google.com"); browser.push("Amazon", "https://amazon.com"); browser.push("LinkedIn", "https://LinkedIn.com"); browser.push("Reddit", "https://reddit.com"); show(browser); // this should show the entire history // simulate clicking Back button string top = browser.pop(); if (top != "") cout << endl << "Clicked back from " << top << endl; show(browser); // simulate clicking Back button top = browser.pop(); if (top != "") cout << endl << "Clicked back from " << top << endl; show(browser); // simulate clicking Back button top = browser.pop(); if (top != "") cout << endl << "Clicked back from " << top << endl; show(browser); return 0; }
please please thumbs up!!!
hope it will help uh out!!
Code::
(IN C++ PROGRAMMING LANGUAGE)
-------------------------------------------------------
#include <iostream>
using namespace std;
struct Location{ // create structure to represent data in
node
string name;
string address;
};
// create structure to represent node in linked list
struct VisitNode {
Location loc; // data stored in node
VisitNode* next; // pointer to next node
};
// create class to represent stack
class Stack {
private:
VisitNode* head; // top of the stack
// function to create new node
VisitNode* create() {
VisitNode* newNode;
try {
// dynamically allocte memory to new node
newNode = new VisitNode;
}
catch (bad_alloc) {
// if allocation fail set new node to NULL
newNode = NULL;
}
return newNode;
}
// delete all nodes from stack
void deallocate() {
// create a temparory node to delete current node
VisitNode* temp = head; // assign temp to head to delete top
node
while (head != NULL) {
// move head to next node
head = head->next;
// delete current node
delete temp;
// re assign temp to head to delete next node
temp = head;
}
}
public:
// destructor
~Stack() {
deallocate();
head = NULL;
}
// place a new node on topof stack
bool push(string name, string address) {
// create a new node
VisitNode* newNode = create();
// check if newNode created, before addiing data to it
if (newNode == NULL) {
return false;
}
// add data to new node
newNode->loc.name = name;
newNode->loc.address = address;
newNode->next = NULL;
// now assign new node at top of stack
// check if stack have any nodes
if (head == NULL) {
// newNode is first node to be added
head = newNode;
}
else {
// assign newNode to top of head
newNode->next = head;
// re-assign head at top node
head = newNode;
}
}
// removes top node and returns name from loc data
// if stack is empty returns empty string
string pop() {
// check if stack is empty
if (head == NULL) {
// return empty string
return "";
}
else {
// copy the data from top node
string data = head->loc.name;
// create a temporary node to delete first node
VisitNode* temp = head;
// move head to next node in stack
head = head->next;
// delete the top node
delete temp;
return data;
}
}
// create a friend function
// friend function can access the private data of the class
member
friend void show(Stack& s) {
// traverse the entire list and print each node's loc.name
// in sigle line
// to traverse the list create a pointer(iterator)
VisitNode* itr = s.head;
while (itr != NULL) { // loop till end of list
// print each node's data
cout << itr->loc.name << " ";
// move iterator to next node
itr = itr->next;
}
}
};
// main function as given
int main() {
Stack browser;
// simulate browser history
browser.push("Google", "https://google.com");
browser.push("Amazon", "https://amazon.com");
browser.push("LinkedIn", "https://LinkedIn.com");
browser.push("Reddit", "https://reddit.com");
show(browser); // this should show the entire history
// simulate clicking Back button
string top = browser.pop();
if (top != "") {
cout << endl << "Clicked back from " << top
<< endl;
}
show(browser);
// simulate clicking Back button
top = browser.pop();
if (top != "") {
cout << endl << "Clicked back from " << top
<< endl;
}
show(browser);
// simulate clicking Back button
top = browser.pop();
if (top != "") {
cout << endl << "Clicked back from " << top
<< endl;
}
show(browser);
return 0;
}
-----------------------------------------------------------
output::