#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
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...
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 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 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 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 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 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 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" << 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 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 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;...