Question

Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulati

Organize your class so that all data is in one section, then group all constructors/destructor, all mutators, and then all ac

2, 3 to mean tower A, B, C and the destination parameter value can also be 1, 2, or 3 to mean A, B, or C tower. Depending on

Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each of the toweґs pegs Nodes of the linked lists will be dvnamically instantiated objects of the Disk class. There is NO inheritance relationship between these two classes Disk Class Place the class definition in prog8disk h file. Implement all of this class' functions as inline Include only the necessary include files in the prog8diskh file. This class will define a single disk that is used in the puzzle. The disk will have a certain weight (larger disks will have larger weight, smaller disks will have smaller weight). If the puzzle has 4 disks, we will assign largest disk weight 4, then next largest 3, etc. down to the smallest disk which will have weight 1. Use an appropriate private data member to represent this disk weight. Also provide an accessor function to return the disk weight. Since objects of this class will be nodes in a linked list, add another private data member that will be able to "point" to the next Disk node. Provide mutator and accessor functions to get/set this pointer data member Provide a default constructor that will set all data members to values that make sense such as 0 and nullptr; Provide an overloaded constructor that will accept ONLY the weight as a parameter setting data member to the given weight and pointer to nullptr Provide a destructor for the class. It will be empty
Organize your class so that all data is in one section, then group all constructors/destructor, all mutators, and then all accessors. Docume nt your class sections TowersOfHanoi Class Implement the TowersOfHanoi class in prog8tower h and prog8tower.cpp files Implement the overloaded constructor and the destructor as inline functions. This class will implement the puzzle itself. Class data will consist of a static moveCount data member, 3 head pointers (pAHead, pBHead, pCHead) that will represent linked lists of Disk objects for peg A, B, and C respectively. Utilize three tail pointers that will point to the last node in each toweґs list of Disk objects initialize these appropriately in the constructor The static moveCount data member should be incremented in the recursive method that solves the should be declared in prog8tower.cpp file and set to 1 The variable Below is a representation of the TowersOfHanoi object upon creation... Disk nodes need to be created dynamically puzzle coun pAHead pBHead (nullptr) pCHead (nullptr) width (2) pNext width (1) pNext width (3) pNext width (4) pNext This class should NOT have a default constructor Instead you should provide an overloaded constructor that takes 4 parameters: number of disks in the initial tower, i.e., tower A followed by three pointers to tower linked lists. All of the parameters should have default values, 0 for the first one and nullptr for all others. This constructor should create the linked list represented above and set the pAHead pointer to point to the list's first node/disk. The client will only provide the first parameter value, i.e., number of disks. Use these parameters to initialize all data members of your class. Also provide the following methods display Tower: should produce current state of the tower puzzle, i.е., nodes in each of the towers. Below is an example of the initial state of the puzzle and is also represented by the picture above, i.e., all 4 disks on peg A. A: 4321 At some point during the solution this function may produce the following "snapshot" of the tower (after disk from A tower was moved to C tower). A: 432 moveDisk: this function should accept two integer parameters. The first one will be the tower source which a disk is to be removed from and the second one will be the destination tower where the disk will be moved to. The source value can be 1
2, 3 to mean tower A, B, C and the destination parameter value can also be 1, 2, or 3 to mean A, B, or C tower. Depending on values of the source and the destination values you will need to remove the last disk node from list (A, B, or C) and APPEND that node to the END of the destination linked list of disk nodes. Here the tail pointer will be useful to eliminate the need to traverse from the head node to the end of the list each time... also remember that you need to disconnect the last node from the source list. This function is void. appropriate tower linked moveDisk(1, 3) would mean move top disk from tower A to be the top disk on tower C. moveTower: this function will be the recursive solution to the puzzle. It will have two integer parameters that will represent the source, and destination tower. Height of the example tower in this description document is 4, we are always at first requesting that the tower be moved from peg A to peg C, the other two parameters would initially be 1 and 3. Initial call to this recursive function from your client code will look like this... (pPuzzle is an instance of TowersOfHanoi class). pPuzzle-moveTower (3, 1, 3, 2); // 3 disks, move from 1 to 3 using 2 as temp Look at the book's example of tower of Hanoi and pay particular attention to the recursive calls and the order of the last three parameters. Make your solution match that order This class MUST have a destructor that will delete all nodes from any of non-empty tower linked lists of nodes You will also need a "helper" function that is private to this class and is described belo convert: this function should be called only from other member functions of this class. It will help to format the output of the class to console window. It will be accepting a parameter that is 1, 2, or 3 and will returm "A," "B," or "C" respectively Main - client code In you main0 you will ask the user for the height of the tower A, i.e number of disks the puzzle contains. Number of steps to solve a given puzzle is related to the number of disks, i.e., 4 disks would be solved in 24-1 moves which is 15. Validate this disk input and only accept values between 1 and 9 (inclusive). Provide appropriate message if user enters values outside of this range and force them to enter agai Instantiate the TowersOfHanoi class dynamically passing the number of disks to the overloaded constructor Call the displayTower) member function to display the initial tower state Call moveTower0 recursive function to solve the puzzle passing the number of disks and 1 and 3 for tower numbers (as shown above) Make sure to delete the TowersOfHanoi object once the puzzle is solved.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
struct node1
{
int data1;
node1 *next1;
}*top1 = NULL, *p1 = NULL, *np1 = NULL;
struct node2
{
int data2;
node2 *next2;
}*top2 = NULL, *p2 = NULL, *np2 = NULL;
struct node3
{
int data3;
node3 *next3;
}*top3 = NULL, *p3 = NULL, *np3 = NULL;
void push1(int data)
{
np1 = new node1;
np1->data1 = data;
np1->next1 = NULL;
if (top1 == NULL)
{
top1 = np1;
}
else
{
np1->next1 = top1;
top1 = np1;
}
}
int pop1()
{
int b = 999;
if (top1 == NULL)
{
return b;
}
else
{
p1 = top1;
top1 = top1->next1;
return(p1->data1);
delete(p1);
}
}
void push2(int data)
{
np2 = new node2;
np2->data2 = data;
np2->next2 = NULL;
if (top2 == NULL)
{
top2 = np2;
}
else
{
np2->next2 = top2;
top2 = np2;
}
}
int pop2()
{
int b = 999;
if (top2 == NULL)
{
return b;
}
else
{
p2 = top2;
top2 = top2->next2;
return(p2->data2);
delete(p2);
}
}
void push3(int data)
{
np3 = new node3;
np3->data3 = data;
np3->next3 = NULL;
if (top3 == NULL)
{
top3 = np3;
}
else
{
np3->next3 = top3;
top3 = np3;
}
}
int pop3()
{
int b = 999;
if (top3 == NULL)
{
return b;
}
else
{
p3 = top3;
top3 = top3->next3;
return(p3->data3);
delete(p3);
}
}
int top_of_stack()
{
if (top1 != NULL && top1->data1 == 1 )
{
return 1;
}
else if (top2 != NULL && top2->data2 == 1)
{
return 2;
}
else if (top3 != NULL && top3->data3 == 1)
{
return 3;
}
}
void display1()
{
cout<<endl;
node1 *p1;
p1 = top1;
cout<<"Tower1 -> "<<" t";
while (p1 != NULL)
{
cout<<p1->data1<<" t";
p1 = p1->next1;
}
cout<<endl;
}
void display2()
{
node2 *p2;
p2 = top2;
cout<<"Tower2-> "<<" t";
while (p2 != NULL)
{
cout<<p2->data2<<" t";
p2 = p2->next2;
}
cout<<endl;
}
void display3()
{
node3 *p3;
p3 = top3;
cout<<"Tower3-> "<<" t";
while (p3 != NULL)
{
cout<<p3->data3<<" t";
p3 = p3->next3;
}
cout<<endl;
cout<<endl;
}
void toh(int n)
{
int i, x, a, b;
for (i = 0; i < (pow(2,n)); i++)
{
display1();
display2();
display3();
x = top_of_stack();
if (i % 2 == 0)
{
if (x == 1)
{
push3(pop1());
}
else if (x == 2)
{
push1(pop2());
}
else if (x == 3)
{
push2(pop3());
}
}
else
{
if (x == 1)
{
a = pop2();
b = pop3();
if (a < b && b != 999)
{
push3(b);
push3(a);
}
else if (a > b && a != 999)
{
push2(a);
push2(b);
}
else if (b == 999)
{
push3(a);
}
else if (a == 999)
{
push2(b);
}
}
else if (x == 2)
{
a = pop1();
b = pop3();
if (a < b && b != 999)
{
push3(b);
push3(a);
}
else if (a > b && a != 999)
{
push1(a);
push1(b);
}
else if (b == 999)
{
push3(a);
}
else if (a == 999)
{
push1(b);
}
}
else if (x == 3)
{
a = pop1();
b = pop2();
if (a < b && b != 999)
{
push2(b);
push2(a);
}
else if (a > b && a != 999)
{
push1(a);
push1(b);
}
else if (b == 999)
{
push2(a);
}
else if (a == 999)
{
push1(b);
}
}
}
}
}
int main()
{
int n, i;
cout<<"enter the number of disks N = ";
cin>>n;
for (i = n; i >= 1; i--)
{
push1(i);
}
toh(n);
return 0;
}

OUTPUT:

enter the number of disks N = 5

Tower1 -> t1 t2 t3 t4 t5 t
Tower2-> t
Tower3-> t


Tower1 -> t2 t3 t4 t5 t
Tower2-> t
Tower3-> t1 t


Tower1 -> t3 t4 t5 t
Tower2-> t2 t
Tower3-> t1 t


Tower1 -> t3 t4 t5 t
Tower2-> t1 t2 t
Tower3-> t


Tower1 -> t4 t5 t
Tower2-> t1 t2 t
Tower3-> t3 t


Tower1 -> t1 t4 t5 t
Tower2-> t2 t
Tower3-> t3 t


Tower1 -> t1 t4 t5 t
Tower2-> t
Tower3-> t2 t3 t


Tower1 -> t4 t5 t
Tower2-> t
Tower3-> t1 t2 t3 t


Tower1 -> t5 t
Tower2-> t4 t
Tower3-> t1 t2 t3 t


Tower1 -> t5 t
Tower2-> t1 t4 t
Tower3-> t2 t3 t


Tower1 -> t2 t5 t
Tower2-> t1 t4 t
Tower3-> t3 t


Tower1 -> t1 t2 t5 t
Tower2-> t4 t
Tower3-> t3 t


Tower1 -> t1 t2 t5 t
Tower2-> t3 t4 t
Tower3-> t


Tower1 -> t2 t5 t
Tower2-> t3 t4 t
Tower3-> t1 t


Tower1 -> t5 t
Tower2-> t2 t3 t4 t
Tower3-> t1 t


Tower1 -> t5 t
Tower2-> t1 t2 t3 t4 t
Tower3-> t


Tower1 -> t
Tower2-> t1 t2 t3 t4 t
Tower3-> t5 t


Tower1 -> t1 t
Tower2-> t2 t3 t4 t
Tower3-> t5 t


Tower1 -> t1 t
Tower2-> t3 t4 t
Tower3-> t2 t5 t


Tower1 -> t
Tower2-> t3 t4 t
Tower3-> t1 t2 t5 t


Tower1 -> t3 t
Tower2-> t4 t
Tower3-> t1 t2 t5 t


Tower1 -> t3 t
Tower2-> t1 t4 t
Tower3-> t2 t5 t


Tower1 -> t2 t3 t
Tower2-> t1 t4 t
Tower3-> t5 t


Tower1 -> t1 t2 t3 t
Tower2-> t4 t
Tower3-> t5 t


Tower1 -> t1 t2 t3 t
Tower2-> t
Tower3-> t4 t5 t


Tower1 -> t2 t3 t
Tower2-> t
Tower3-> t1 t4 t5 t


Tower1 -> t3 t
Tower2-> t2 t
Tower3-> t1 t4 t5 t


Tower1 -> t3 t
Tower2-> t1 t2 t
Tower3-> t4 t5 t


Tower1 -> t
Tower2-> t1 t2 t
Tower3-> t3 t4 t5 t


Tower1 -> t1 t
Tower2-> t2 t
Tower3-> t3 t4 t5 t


Tower1 -> t1 t
Tower2-> t
Tower3-> t2 t3 t4 t5 t


Tower1 -> t
Tower2-> t
Tower3-> t1 t2 t3 t4 t5 t

Add a comment
Know the answer?
Add Answer to:
Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...
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++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and In...

    C++ program, item.cpp implementation. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The majority of implementation will be done...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • 8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code...

    8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....

  • c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file...

    c++ File name: 2170 107b.ee Purpose: This program demonstrates the use of multi-linked lists. //This file contains the implementation file for the 2170_10_7b.h header file. This implementation uses a dynamic linked list structure implementing the // Multi ListClass object. The implementation uses nodes which have two pointer fields one to point to the next record by name (nextName) and one to point Ito the next record by account number(nextNum). The linked list is also maintained I with a dummy header...

  • This is a c++ class utilizing class templates and linked lists and Nodes. I need to...

    This is a c++ class utilizing class templates and linked lists and Nodes. I need to implement the following member function(s) to LinkedBag.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. In this case, I need to join the original items with the user items(a_bag). So if the original has {1,2,3} and a_bag has...

  • IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as...

    IntList Recursion Assignment Specifications: You will add some additional recursive functions to your IntList class as well as make sure the Big 3 are defined. IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1"...

    Simple test in text: int main() { Deque dq1; cout << dq1.empty() << " - 1" << endl; dq1.insertFront(42); dq1.insertBack(216); cout << dq1.peekFront() << " - 42" << endl; cout << dq1.peekBack() << " - 216" << endl; cout << dq1.size() << " - 2" << endl; Deque dq2(dq1); Deque dq3; dq3 = dq1; cout << dq1.removeFront() << " - 42" << endl; cout << dq1.removeBack() << " - 216" << endl; cout << dq2.peekFront() << " - 42" <<...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

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