


#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...
c++ only 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 Node { int number; Node * nextNode;...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
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...
This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.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. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...
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...
CS 215 Program Design, Abstraction, and Problem Solving Programming Project #3 INTRODUCTION The goal of this programming project is to enable the student to practice designing a program that solves a problem using a class and a linked-list and developing a C++ program to implement the solution. PROJECT TASKS 1. Read the problem definition below and then analyze it before designing a solution. You will produce a document (external documentation) of this definition, analysis, and design. 2. Write a C++...