Question

In: Computer Science

towers of hanoi c++ program using stacks and singly linked lists.

towers of hanoi c++ program using stacks and singly linked lists.

Solutions

Expert Solution

#include <iostream>

#include <cmath>

#include <cstdlib>

#include <climits>

using namespace std;

struct Stack

{

unsigned capacity;

int top;

int *array;

};

Stack* createStack(unsigned capacity)

{

Stack* stack = (Stack*) malloc(sizeof(Stack));

stack->capacity = capacity;

stack->top = -1;

stack->array = new int[capacity];

return stack;

}

int isFull(Stack* stack)

{

return (stack->top == stack->capacity - 1);

}

int isEmpty(Stack* stack)

{

return (stack->top == -1);

}

void pushDisk(Stack *stack, int disk)

{

if (isFull(stack))

return;

stack->array[++stack -> top] = disk;

}

int popDisk(Stack* stack)

{

if (isEmpty(stack))

return INT_MIN;

return stack->array[stack -> top--];

}

void moveDisk(char fromTower, char toTower, int disk)

{

printf("Move the disk %d from Tower %c to Tower %c\n", disk, fromTower, toTower);

}

void moveDisksBetweenTwoTowers(Stack *src, Stack *dest, char s, char d)

{

int tower1TopDisk = popDisk(src);

int tower2TopDisk = popDisk(dest);

// when tower 1 is empty

if (tower1TopDisk == INT_MIN)

{

pushDisk(src, tower2TopDisk);

moveDisk(d, s, tower2TopDisk);

}

// when tower 2 is empty

else if (tower2TopDisk == INT_MIN)

{

pushDisk(dest, tower1TopDisk);

moveDisk(s, d, tower1TopDisk);

}

// when top disk of tower 1 is greater than top disk of tower 2

else if (tower1TopDisk > tower2TopDisk)

{

pushDisk(src, tower1TopDisk);

pushDisk(src, tower2TopDisk);

moveDisk(d, s, tower2TopDisk);

}

// when top disk of tower 1 is less than top disk of tower 2

else

{

pushDisk(dest, tower2TopDisk);

pushDisk(dest, tower1TopDisk);

moveDisk(s, d, tower1TopDisk);

}

}

void towersOfHanoi(int numDisks, Stack *start, Stack *middle, Stack *end)

{

int i, totalMoves;

char startSymbol = '1', endSymbol = '3', midSymbol = '2';

if (numDisks % 2 == 0)

{

char temp = endSymbol;

endSymbol = midSymbol;

midSymbol = temp;

}

totalMoves = pow(2, numDisks) - 1;

for (i = numDisks; i >= 1; i--)

{

pushDisk(start, i);

}

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

{

if (i % 3 == 1)

moveDisksBetweenTwoTowers(start, end, startSymbol, endSymbol);

else if (i % 3 == 2)

moveDisksBetweenTwoTowers(start, middle, startSymbol, midSymbol);

else if (i % 3 == 0)

moveDisksBetweenTwoTowers(middle, end, midSymbol, endSymbol);

}

}

int main()

{

unsigned numDisks;

Stack *start, *end, *mid;

cout << endl << "How many disks? ";

cin >> numDisks;

// create three stacks of size 'numDisks'

start = createStack(numDisks);

mid = createStack(numDisks);

end = createStack(numDisks);

cout << endl;

// start the puzzle

towersOfHanoi(numDisks, start, mid, end);

return 0;

}

******************************************************************* SCREENSHOT ********************************************************


Related Solutions

Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The...
Data Structures Homework – Singly Linked Lists Create a singly linked that represents a school. The school has multiple classes. Each class has a different number of students. class student { Long ID; string Name; string Address; float grades[3]; student *below; }; class Node // the node represents a class in school { int ID; int NoOfStudents; int NoOfQuizes; student *t;// a linked list of students is allocated dynamically Node *Next; }; class school { string Name; Node *Head; int...
Write a java program to solve Towers of Hanoi with the condition that there are "m"...
Write a java program to solve Towers of Hanoi with the condition that there are "m" number of rods and "n" number of disks. Where m >= 3 and n >=1.
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas:...
Program a solver for the Towers of Hanoi problem presented below. Submit the following to canvas: Hanoi.java, Driver.java (contains your main method). This is an individual project. Students may discuss solutions to the problem but may not share code with one another. I will discuss this project during our next lecture. Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner...
i want it in C++.You will solve the Towers of Hanoi problem in an iterative manner (using Stack) in C++(using data structure). Note: you have to solve for N number of disks, 3 Towers (Stacks). Do not use recursion. For better understanding play the game at least once. Link:https://www.mathsisfun.com/games/towerofhanoi.html
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features...
By using javaFX, write and design the Towers of Hanoi game with Redo and Undo features with play and solve options.
JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java,...
JAVA Program a solver for the Towers of Hanoi problem presented below. Submit the Following: Hanoi.java, Driver.java (contains your main method). Rules: a) There are three Pillars (Pillar1, Pillar2, Pillar3). b) There are N number of disks of increasing size (disk size is indicated by an integer). c) At the start, all disks are stacked on one of the pillars. d) At no point can a disk of larger size be placed above a disk of smaller size (including the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT