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

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++
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...
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...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT