Class LinkedStack The next class you will be implementing is the linked-list based stack itself, it...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Class LinkedStack The next class you will be implementing is the linked-list based stack itself, it is similar to the implementation we described in class. This class is implementing the interface provided called StackADT. The elements in our linked list are pointers to Tile objects. To maintain the stack, each push will insert a new node containing the element at the start/beginning of the linked list (the head of the linked list, at top), and each pop operation will remove the node at the head of the linked list, returning its data. The LinkedStack has two private attributes: • TileNode* top - A pointer to the head of the linked list, the top of the stack. int count The number of elements in the stack. You are not to modify the code of LinkedStack.h. For the LinkedStack class, you must implement all the following public methods: 1. LinkedStack::LinkedStack(): A default constructor, here you will initialize the head node called top to null and the number of elements, count, is set to zero. 2. LinkedStack::~LinkedStack(): The deconstructor for the linked list, which is responsible for de-allocating for memory dynamically allocated using the new keyword. Create a pointer variable (TileNode*) that is assigned top, then traverse the linked list, from top to the end of the linked list, deleting each successive node using the delete keyword. You are not responsible for deleting the Tile pointed to by the data attribute, it will be assumed that upon removing data from the stack, those elements will be deleted by the user. 3. void LinkedStack::push (Tile* elem): Create a new TileNode whose data is a Tile* named elem (the input) and insert this node at the head of the linked list (i.e. the beginning of the linked list). Make sure to update the number of elements in the stack appropriately, and when you update the linked list that the insertion is performed correctly. 4. Tile* LinkedStack::pop(): Remove and return element at the top of the stack. As this is a linked list, this means performing a removal of a node at the head of the linked list. If the stack is empty, return null. Make sure to update the number of elements in the stack, and to delete the node (, only the node itself, not any of the data its contents are pointing to,) removed from the linked list (, and the linked list is correctly updated). 5. Tile* LinkedStack::peek (): Return the element at the top of the stack. Do not remove this element (or its node) from the linked list. If the stack is empty, return null. 6. int LinkedStack::size(): Return the size of the stack, the number of elements it contains. 7. bool LinkedStack::isEmpty(): Are there any elements in our stack? False if yes, true otherwise. Make sure to #include any header files of classes used in LinkedStack.cpp so that compiles. TileNode.h 1 #ifndef _NODE 2 #define _NODE 3 4 5 567 7 8 9 10 11 12 13 14 15 565 16 17 x 18 19 20 21 22 23 #include "Tile.h" Node class This is a node for a linked list. class TileNode{ private: Tile* data;//the data for the node TileNode* next;//the next node public: }; #endif TileNode (Tile* d);//constructor //note: there is nothing to deallocate (for a deconstructor), let the linked list take care of it. Tile* getData();//get the data of the node. TileNode* getNext();//get the next node in the linked list void setData(Tile* newData);//set the data for the node void setNext (TileNode* newNext);//set the next node LinkedStack.h x 1 #ifndef _LINKED_STACK #define _LINKED_STACK 2 3 4 #include "StackADT.h" 5 #include "Tile.h" #include "TileNode.h" 679 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LinkedStack class, it is a subclass of the abstract class StackADT (an interface) */ class LinkedStack: public StackADT{ private: TileNode* top; //the top of the stack int count;//number of elements public: }; #endif LinkedStack();//Constructor (sets top to nullptr and count=0) ~Linkedstack(); //Deconstructor (deletes each node in the linked list) void push(Tile* elem);//push make a new node, insert at the head of the linked list with elem in it (top of the stack). Tile* pop();//pop the stack: remove the first node in the linked list, return its data. If the stack is empty, return nullptr. Tile* peek();//return the top element of the stack, do not remove the node that has the top element. int size();//return the number of elements in the stack bool isEmpty();//is the stack empty? StackADT.hx 1 2 3 4 5 6 7 */ 8 class StackADT{//An example Stack ADT coa 9 10 11 12 13. 14 15 #ifndef _STACK_ADT #define _STACK_ADT #include "Tile.h" /* 16 StackADT public: }; #endif 0;//push elem onto the top virtual void push(Tile* elem) of the stack. virtual Tile* pop() = 0;//pop the top element off the stack and return it. = virtual Tile* peek() = 0;//return the element that is on the top of the stack. virtual int size() = 0;//how many elements are in the stack? virtual bool isEmpty() = 0;//is the stack empty? 14 Class LinkedStack The next class you will be implementing is the linked-list based stack itself, it is similar to the implementation we described in class. This class is implementing the interface provided called StackADT. The elements in our linked list are pointers to Tile objects. To maintain the stack, each push will insert a new node containing the element at the start/beginning of the linked list (the head of the linked list, at top), and each pop operation will remove the node at the head of the linked list, returning its data. The LinkedStack has two private attributes: • TileNode* top - A pointer to the head of the linked list, the top of the stack. int count The number of elements in the stack. You are not to modify the code of LinkedStack.h. For the LinkedStack class, you must implement all the following public methods: 1. LinkedStack::LinkedStack(): A default constructor, here you will initialize the head node called top to null and the number of elements, count, is set to zero. 2. LinkedStack::~LinkedStack(): The deconstructor for the linked list, which is responsible for de-allocating for memory dynamically allocated using the new keyword. Create a pointer variable (TileNode*) that is assigned top, then traverse the linked list, from top to the end of the linked list, deleting each successive node using the delete keyword. You are not responsible for deleting the Tile pointed to by the data attribute, it will be assumed that upon removing data from the stack, those elements will be deleted by the user. 3. void LinkedStack::push (Tile* elem): Create a new TileNode whose data is a Tile* named elem (the input) and insert this node at the head of the linked list (i.e. the beginning of the linked list). Make sure to update the number of elements in the stack appropriately, and when you update the linked list that the insertion is performed correctly. 4. Tile* LinkedStack::pop(): Remove and return element at the top of the stack. As this is a linked list, this means performing a removal of a node at the head of the linked list. If the stack is empty, return null. Make sure to update the number of elements in the stack, and to delete the node (, only the node itself, not any of the data its contents are pointing to,) removed from the linked list (, and the linked list is correctly updated). 5. Tile* LinkedStack::peek (): Return the element at the top of the stack. Do not remove this element (or its node) from the linked list. If the stack is empty, return null. 6. int LinkedStack::size(): Return the size of the stack, the number of elements it contains. 7. bool LinkedStack::isEmpty(): Are there any elements in our stack? False if yes, true otherwise. Make sure to #include any header files of classes used in LinkedStack.cpp so that compiles. TileNode.h 1 #ifndef _NODE 2 #define _NODE 3 4 5 567 7 8 9 10 11 12 13 14 15 565 16 17 x 18 19 20 21 22 23 #include "Tile.h" Node class This is a node for a linked list. class TileNode{ private: Tile* data;//the data for the node TileNode* next;//the next node public: }; #endif TileNode (Tile* d);//constructor //note: there is nothing to deallocate (for a deconstructor), let the linked list take care of it. Tile* getData();//get the data of the node. TileNode* getNext();//get the next node in the linked list void setData(Tile* newData);//set the data for the node void setNext (TileNode* newNext);//set the next node LinkedStack.h x 1 #ifndef _LINKED_STACK #define _LINKED_STACK 2 3 4 #include "StackADT.h" 5 #include "Tile.h" #include "TileNode.h" 679 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 LinkedStack class, it is a subclass of the abstract class StackADT (an interface) */ class LinkedStack: public StackADT{ private: TileNode* top; //the top of the stack int count;//number of elements public: }; #endif LinkedStack();//Constructor (sets top to nullptr and count=0) ~Linkedstack(); //Deconstructor (deletes each node in the linked list) void push(Tile* elem);//push make a new node, insert at the head of the linked list with elem in it (top of the stack). Tile* pop();//pop the stack: remove the first node in the linked list, return its data. If the stack is empty, return nullptr. Tile* peek();//return the top element of the stack, do not remove the node that has the top element. int size();//return the number of elements in the stack bool isEmpty();//is the stack empty? StackADT.hx 1 2 3 4 5 6 7 */ 8 class StackADT{//An example Stack ADT coa 9 10 11 12 13. 14 15 #ifndef _STACK_ADT #define _STACK_ADT #include "Tile.h" /* 16 StackADT public: }; #endif 0;//push elem onto the top virtual void push(Tile* elem) of the stack. virtual Tile* pop() = 0;//pop the top element off the stack and return it. = virtual Tile* peek() = 0;//return the element that is on the top of the stack. virtual int size() = 0;//how many elements are in the stack? virtual bool isEmpty() = 0;//is the stack empty? 14
Expert Answer:
Answer rating: 100% (QA)
To implement the LinkedStack class you need to create a new file called LinkedStackcpp and include t... View the full answer
Related Book For
Posted Date:
Students also viewed these accounting questions
-
This is a puzzlejust for fun. Lewis Carroll (1832-1898), author of Alice in Wonderland and Through the Looking Glass, was a mathematician, logician, and political scientist. Carroll loved careful...
-
This is a question for my statistics class, please help me solve it. Thanks! 6. Which of the following about the random variables and probability distributions is a true statement? Explain why other...
-
Show that this is a basis for R4. (We can use the results of this subsection to simplify this job.) 0000
-
Donna is a Malaysian tax resident, working as a manager in the human resource department of a telecommunication company. Her remuneration yearly salary amounted to RM82,860. Her husband, Don service...
-
The following summarized Cash T-account reflects the total debits and total credits to the Cash account of Texas Corporation for calendar year 2011. (1) Use this information to prepare a complete...
-
Required The following information is from the records of attorney Glenn Price. Write a brief explanation of the accounting event represented in each of the general journal entries. Debit 40,000...
-
The WACC is an average cost because it is a weighted average of the firm's component costs of capital. However, each component cost is a marginal cost; that is, the cost of new capital. Thus, the...
-
Crab Beach Systems, a U.S. multinational producer of computer hardware, has subsidiaries located throughout the world. The company recently received year-end financial statements from its Swiss...
-
a. How much will $100 grow to if invested at a continuously compounded interest rate of 9.5% for 8 years? (Do not round intermediate calculations. Round your answer to 2 decimal places.) Future value...
-
Roger Ginde is developing a program in supply chain management certification for managers. Ginde has listed a number of activities that must be completed before a training program of this nature...
-
Perform the indicated operations and write the result in standard form 20 50 60 20 50 60 Type an exact answer using radicals as needed Type your answer in the form a bi
-
5. A 72.0 cm long stretched string is resonating at the third harmonic. If the source frequency is 460 Hz, then what is the speed of the wave? Answer in m/s.
-
2. Perform the following tasks: a) Consider a portfolio of three funds, namely funds 1, 2 and 3, with weights W, W and W3, respectively. Construct the portfolio variance. [4 marks] b) Construct in...
-
Suppose the government gives a guaranteed minimum price for sugar of $.25 cents a pound - and agrees to purchase any surplus which is subsequently destroyed. Demand and Supply are: Qd 200,000...
-
In a substitution reaction, C8H9Br, a compound that contains a phenyl group, reacts with NaSH with H2O being the solvent. The original compound has placed the leaving group, Br, on an in plane bond....
-
3. Show what is printed by the following pseudocode. Begin and End delimit a block. So, the statements between the begin and end are all part of the code to be executed each time the For loop is...
-
Which of the following would federal fair housing laws prohibit? A 49-year-old, single woman describing herself as such in an advertisement for a roommate An ad stating families with small children ne
-
Design an experiment to demonstrate that RNA transcripts are synthesized in the nucleus of eukaryotes and are subsequently transported to the cytoplasm.
-
Burts utility function is U(x1, x2) = (x1 +2)(x2 +6), where x1 is the number of cookies and x2 is the number of glasses of milk that he consumes. (a) What is the slope of Burts indifference curve at...
-
The Miss Manners Refinery in Dry Rock, Oklahoma, converts crude oil into gasoline. It takes 1 barrel of crude oil to produce 1 barrel of gasoline. In addition to the cost of oil there are some other...
-
Four students, Art, Bob, Cal, and Dan, must be assigned rooms in a college dorm. The dean of housing would like to assign them in stable partnerships, so that no two students would prefer each other...
-
illustrate cultural and personal factors that create variations in ethics, and
-
discuss factors that promote organizational justice and ethics.
-
How are ethics and values related?
Study smarter with the SolutionInn App