Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Mandatory Instructions Develop a C++ object oriented solution to the Towers of H

ID: 3694664 • Letter: M

Question

Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. You solution will involve designing two classes Disk and TowersOfHanoi. TowersOfHanoi class will implement the game and will include linked lists representing disks on each of the tower's pegs... Nodes of the linked lists will be instantiated objects of the Disk class Disk Class Implement this class in prog8disk.h file Implement all of this class functions as inlin Include only the necessary include files in the prog8disk.h file This class will define a singlé disk that is used in the puzzle. The disk will have a certain weight (larger disks wil 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 appropriate data member to represent this disk weight. Also provide an accessor function to get the disk weight. Since objects of this class will be nodes in a linked list, add a 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

Explanation / Answer

This C++ program displays the iterative solution to the Tower of Hanoi problem. Tower Of Hanoi consists of three rods and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack.
No disk may be placed on top of a smaller disk

Implementation of Tower Of Hanoi using linked list:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<iostream.h>

struct node

{

int data;

struct node *link;

};

typedef struct node node;

node *getnode() //Allocate memory for a new node

{

node *p;

p=(node *)malloc(sizeof(node));

return(p);

}

freenode(node *p) //Frees a node

{

free(p);

}

initializellist(node **first) //Initialize the list

{

(*first)=NULL;

}

traversellist(node *first) //Traverse the list

{

cout<<" Contents of a linked list are as....";

cout<<" Start";

while(first!=NULL)

{

cout<<"->"<<first->data;

first=first->link;

}

cout<<"->End ";

}

llistcreate(node **f,int n) //Create the list

{

node *temp, *current;

int i,item;

for(i=1;i<=n;i++)

{

cout<<"Enter the data field of node#"<<i<<":";

cin>>item;

temp=getnode();

temp->data=item;

temp->link=NULL;

if((*f)==NULL)

{

(*f)=temp;current=(*f);

}

else

current->link=temp;

current=temp;

}

}

void movenode(node **first,node **second)

{

node *temp,*curr;

if((*second)==NULL)

{return;}

temp=(*first);

curr=(*second)->link;

(*first)=(*second);

(*first)->link=temp;

(*second)=curr;

}

void towerofhanoi(int n,node **first,node **second,node **third)

{

if(n==1)

{

movenode(third,first);

cout<<" Move plate "<<n<<" from "<<first<<"ll to "<<third<<"ll";

return;

}

else

{

towerofhanoi(n-1,first,third,second);

movenode(third,first);

cout<<" Move plate "<<n<<" from "<<first<<"ll to "<<third<<"ll";

towerofhanoi(n-1,second,first,third);

}

}

main()

{

node *first,*second,*third;

clrscr();

int choice,item,pos,n;

initializellist(&first);

initializellist(&second);

initializellist(&third);

cout<<" "<<"Creating a singly linked list. ";

cout<<" How many nodes do you want to create?:";

cin>>n;

llistcreate(&first,n);

cout<<" Contents of peg1....";

traversellist(first);

cout<<"Contents of peg2....";

traversellist(second);

cout<<"Contents of peg3....";

traversellist(third);

towerofhanoi(n,&first,&second,&third);

cout<<" Contents of peg1....";

traversellist(first);

cout<<"Contents of peg2....";

traversellist(second);

cout<<"Contents of peg3....";

traversellist(third);

getch();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote