Question

in C++, please help. Not only do we need to create an ADT but create a...

in C++, please help. Not only do we need to create an ADT but create a main file as well to implement everything. For this program you will be creating a stack ADT to allow the client program to pick up treasures for a Star Wars scavenger game to get everyone ready for the opening of Galaxy’s Edge in 2020. Each treasure should have a (a) name, (b) description, (c) category, (d) what it is used for, and (e)if this is something the user wants to keep after the treasure hunt is over. You might also want to include which civilization the item is from. Each person doing the treasure hunt must find something in each of the following categories:

1.Food(3)

2.Weapon (1)

3.Clothing(2)

4.Artifact(2)

5.Pick one other

The stack will be implemented using a flexible array (a linear linked list of arrays). Each element of the array will be a treasure. Create an ordered list to keep track of the required categories (don’t just hard code these) using any data structure you would like (array, linear linked list, or other). For the stack, implement push, pop, peek, and is_empty. Please keep in mind that because we are creating ADTs, the user of the program must not be aware that a stack and ordered list is being used –this is the job of the application program(your test program)!

Please keep in mind that because we are creating ADTs, the

user of the program must not be aware that a stack and ordered list is being used –this is the job of the application program(your test program)! Things you should know...as part of your program:

1)Do not use statically allocated arrays in your classes or structures.The client may use them.

2)All data members in a class must be private

3)Never perform input operations from your class in CS163

4)Global variables are not allowed in CS163

5)Do not use the String class! (use arrays of characters instead!) You may use the cstring class.

6) Use modular design, separating the .h files from the .cpp files. Remember, .h files should contain the class header and any necessary prototypes. The .cpp files should contain function definitions. You must have at least 1.h file and 2.cpp

files. Never "#include" .cpp files!

7)Use the iostream library for all I/O; do not use stdio.h.

8)Make sure to define a   constructor and destructor for your class. Your destructor must deallocate all dynamically allocated memory.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include "Stack.h"
#include <iostream>
#include <string>

using namespace std;

class Treasure
{
public:
   Treasure() = default;
   Treasure(string name, string description, string category, string use, bool keep)
   {
       this->name = name;
       this->description = description;
       this->category = category;
       this->use = use;
       this->keep = keep;
   }
   string name;
   string description;
   string category;
   string use;
   bool keep;
   friend ostream& operator<<(ostream& os, const Treasure& Obj);
};
ostream& operator<<(ostream& os, const Treasure& Obj)
{
   cout << "Name: " << Obj.name << endl;
   cout << "Description: " << Obj.description << endl;
   cout << "Category: " << Obj.category << endl;
   cout << "Use: " << Obj.use << endl;
   cout << "Keep?: " << Obj.keep << endl << endl;
   return os;
}

int main()
{
   // Stack Test!. Passed!.
   /*

   Stack<int> S;
   S.display();
   S.Pop();
   S.display();
   S.Push(1);
   S.display();
   S.Push(2);
   S.display();
   S.Push(3);
   S.display();
   S.Pop();
   S.display();
   S.Pop();
   S.display();
   S.Pop();
   S.display();
   S.Pop();
   S.display();

   */
  
   Treasure T1("A", "B", "C", "D", false);
   Treasure T2("E", "F", "G", "H", false);
   Treasure T3("I", "J", "K", "L", true);

   Stack<Treasure> S2;
   S2.Push(T1);
   cout << S2.Top();
   S2.Push(T2);
   cout << S2.Top();
   S2.Push(T3);
   cout << S2.Top();
   S2.Pop();
   cout << S2.Top();

   return 0;
}

#pragma once
#include"LinkedList.h"
#include <iostream>
using namespace std;
template <typename T>
class Stack
{
private:
   LinkedList<T>Pointer;
public:
   // Cinstructor For Stacks
   Stack() = default;

   // Returns If Stack is Having Any Vacant Slot To Push A Value!.
   bool IsStackEmpty() const { return Pointer.IsEmptyList(); }

   // Push A Value To The Next Index Of Stack!.
   void Push(T N) { Pointer.InsertAtHead(N); }

   // Move Back And Index, No Need To Change The Value As It Can Be OverWritten Next Time Push Is Called!.
   void Pop() { Pointer.DeleteFromHead(); }

   // Returns Top Most Value Of The Stack!.
   T Top() { return Pointer.GetHead()->GetData(); }

   void display() { Pointer.Print(); }
  
   // Destructor For Stack!.
   ~Stack() = default;
};

#pragma once
#include"Node.h"
#include<iostream>
using namespace std;
template <typename T>
class LinkedList // LinkedList Using Head And Tail.
{
private:
   // Top / Front / Head.
   Node<T>*Head;
   // Tail / Back / Current.
   Node<T>*Current;
   // Set Head As It Is A Private Member.
   void SetHead(Node<T> *Val) { this->Head = Val; }
   // Set Tail As It Is A Private Member.
   void SetCurrent(Node<T> *Val) { this->Current = Val; }
   // Get Tail As It Is A Private Member.
   Node<T>* GetCurrent() { return this->Current; }
public:
   // Get Head As It Is A Public Member.
   Node<T>* GetHead() { return this->Head; }
   // Constructor Sets Head And Tail To Null.
   LinkedList() { SetHead(NULL); SetCurrent(NULL); }
   // Copy Constructor.
   LinkedList(LinkedList &obj) // Yes , I Googled This! , Used To Make A New Copy Of Linked List When Passing It To A Function Without Reference So The Orignal List Is Does Not Get Deleted!.
   {
       Node<T>dummy;
       for (Node<T>* o = obj.GetHead(), *n = &dummy; o; o = o->GetNext())
       {
           n->SetNext(new Node<T>(o->GetData()));
           n = n->GetNext();
       }
       this->Head = dummy.GetNext();
   }
   // Returns If List Is Empty Or Not.
   bool IsEmptyList() { if (GetHead() == NULL) return true; return false; }
   // Returns The Data Of Current List From Head.
   T GetHeadData() { return GetHead()->GetData(); }
   // Returns The Data Of Current List From Tail.
   T GetTailData() { return GetCurrent()->GetData(); }
   // Prints The Current List From Head To Tail.
   void Print()
   {
       Node<T>*Temp = GetHead();
       cout << "Current List : ";
       while (Temp != NULL)
       {
           cout << Temp->GetData() << " ";
           Temp = Temp->GetNext();
       }
       cout << endl;
   }
   // Inserts The New Value At Head.
   void InsertAtHead(T Val)
   {
       Node<T>*Temp = new Node<T>;
       Temp->SetData(Val);
       Temp->SetNext(this->Head);
       if (GetHead() == NULL)
       {
           SetHead(Temp);
           SetCurrent(Head);
       }
       else
       {
           Temp->SetNext(GetHead());
           SetHead(Temp);
       }
   }
   // Inserts The New Value At Tail.
   void InsertAtTail(T Val)
   {
       Node<T>*Temp = new Node<T>;
       Temp->SetData(Val);
       Temp->SetNext(NULL);
       if (Head == NULL)
       {
           SetHead(Temp);
           SetCurrent(Head);
       }
       else
       {
           GetCurrent()->SetNext(Temp);
           SetCurrent(Temp);
       }
   }
   // Deletes The Value At Head.
   void DeleteFromHead()
   {
       if (!IsEmptyList())
       {
           if (GetHead()->GetNext() != NULL)
           {
               Node<T>*Temp = GetHead();
               SetHead(GetHead()->GetNext());
               delete Temp;
           }
           else
           {
               Node<T>*Temp = GetHead();
               delete Temp;
               SetHead(NULL);
           }
       }  
   }
   // Deletes The Value At Tail.
   void DeleteFromTail()
   {
       if (!IsEmptyList())
       {
           SetCurrent(GetHead());
           if (GetCurrent() == GetHead() && GetCurrent()->GetNext() == NULL)
           {
               delete GetCurrent();
               SetHead(NULL);
           }
           else
           {
               while (GetCurrent()->GetNext()->GetNext() != NULL)
               {
                   SetCurrent(GetCurrent()->GetNext());
               }
               delete GetCurrent()->GetNext();
               GetCurrent()->SetNext(NULL);
           }
       }
   }
   // Searches The List For A Value, And Returns The Address Of Node Which Contains The Value.
   Node<T>* Search(T Val)
   {
       SetCurrent(GetHead());
       if (IsEmptyList()) return NULL;
       else
       {
           while (GetCurrent() != NULL)
           {
               if (GetCurrent()->GetData() == Val) return GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
           }
           return NULL;
       }
   }
   // Insert A Value, After A Specific Value.
   void InsertAfter(T Check, T Val)
   {
       SetCurrent(GetHead());
       Node<T>* Ptr = Search(Check);
       if (!Ptr) cout << "Value Not Found!" << endl;
       else
       {
           Node<T>* Temp = new Node<T>;
           Temp->SetData(Val);
           Temp->SetNext(Ptr->GetNext());
           Ptr->SetNext(Temp);
       }
   }
   // Insert A Value, Before A Specific Value.
   void InsertBefore(T Check, T Val)
   {
       SetCurrent(GetHead());
       Node<T>* Ptr = Search(Check);
       if (!Ptr) cout << "Value Not Found!" << endl;
       else
       {
           Node<T>* Temp = new Node<T>;
           Temp->SetData(Ptr->GetData());
           Ptr->SetData(Val);
           Temp->SetNext(Ptr->GetNext());
           Ptr->SetNext(Temp);
       }
   }  
   // Sorts The List Value By Value In Descending, Not Node By Node, Until The List Is Fully In Order.
   void SortDescending()
   {
       Node<T>* Temp;
       bool Swap = true;
       while(Swap)
       {
           Swap = false;
           SetCurrent(Head);
           while (GetCurrent())
           {
               Temp = GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
               if (GetCurrent() && Current->GetData() > Temp->GetData())
               {
                   Swap = true;
                   T Val;
                   Val = Temp->GetData();
                   Temp->SetData(GetCurrent()->GetData());
                   GetCurrent()->SetData(Val);
               }
           }
       }
   }
   // Sorts The List Value By Value In Ascending, Not Node By Node, Until The List Is Fully In Order.
   void SortAscending()
   {
       Node<T>* Temp;
       bool Swap = true;
       while (Swap)
       {
           Swap = false;
           SetCurrent(Head);
           while (GetCurrent())
           {
               Temp = GetCurrent();
               SetCurrent(GetCurrent()->GetNext());
               if (GetCurrent() && Current->GetData() < Temp->GetData())
               {
                   Swap = true;
                   T Val;
                   Val = Temp->GetData();
                   Temp->SetData(GetCurrent()->GetData());
                   GetCurrent()->SetData(Val);
               }
           }
       }
   }
   // Default Destructor.
   ~LinkedList() = default;
};
#pragma once
template <typename T>
class Node
{
private:
   T Data;
   Node* Next;
public:
   T GetData() { return this->Data; }
   void SetData(T Val) { this->Data = Val; }
   Node* GetNext() { return this->Next; }
   void SetNext(Node* Val) { this->Next = Val; }
   // SetData Cannot Be NULL For Some Structs, So Remove This Statement.
   Node() { this->SetNext(nullptr); }
   Node(T Val) { this->SetData(Val); }
   ~Node() = default;
};

IF THERE IS ANY PROBLEM OR YOU HAVE ANY QUERY REGARDING THE SOLUTION KINDLY FEEL FREE TO ASK, AND LEAVE A THUMBS UP WHEN YOU ARE SATISFIED. THANKS!.

Add a comment
Know the answer?
Add Answer to:
in C++, please help. Not only do we need to create an ADT but create a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT....

    Please help with this Java Program. Thank you! we will be implementing an Ordered List ADT. Our goal is to implement the interface that is provided for this ADT. Notice that there are two interfaces: OrderedListADT builds on ListADT. In this homework, you'll only be responsible for the OrderedListADT. Figure 1: UML Overview 1 Requirements Create a doubly linked implementation of the OrderedListADT interface. Note that the book includes most of the source code for a singly linked implementation of...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability...

    ***************C PROGRAMMING ONLY************* Demonstrate the ability to create an array on the stack Demonstrate the ability to create an array on the heap allowing user to choose the number of values to store. Demonstrate the ability to store an array of Struct values on both the stack and the heap. Program Specifications: 1. Create a struct with at least 3 fields - any struct you want but explain it in your comments in the code. Populate at least 10 elements...

  • I NEED HELP with this. please create a UML diagram. I need a simple code to...

    I NEED HELP with this. please create a UML diagram. I need a simple code to solve the problem.   The ADT Bag is a group of items, much like what you might have with a bag of groceries. In a software development cycle, specification, design, implementation, test/debug, and documentation are typical activities. The details are provided in the rest of the document. ADT Bag Specification: (Note: You should not change the names of the operations in your program. This should...

  • Create a Program in C++ We need commentaries in the program setter and getter we need...

    Create a Program in C++ We need commentaries in the program setter and getter we need the Output of the program and the code compiling and use a file. You will design a program that plays hangman. The rules are explained here. The program must at least include one class and client code. It does not require graphical representation but to keep track of the player misses. The host is the computer. The user has eight chances for error (otherwise,...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT