Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include "Nodes.h"
- #include <windows.h>
- using namespace std;
- class Stack{
- private:
- ListNode *first;
- public:
- Stack(){
- first = NULL;
- }
- ListNode* getFirst(){
- return(first);
- }
- void Insert(int v) {
- ListNode *temp,*curr;
- if(first == NULL){
- first = new ListNode(v,NULL);
- }
- else {
- curr=first;
- while(curr->getNext()!=NULL) {
- curr=curr->getNext();
- }
- curr->setNext(new ListNode(v,NULL));
- }
- }
- void Remove(){
- int v;
- ListNode *temp,*curr,*temp1;
- curr = first;
- while(curr->getNext() !=NULL) {
- curr=curr->getNext();
- }
- curr->setNext(NULL);
- }
- };
- class Tower{
- private:
- Stack s1, s2, s3;
- ListNode *curr;
- public:
- Tower(int v){
- for(int i = 0;i<v;i++){
- s1.Insert(v-i);}
- if(v==1){move1(s1, s2);}
- if(v==2){move2(s1, s2, s3);}
- if(v==3){move3(s1, s2, s3);}
- if(v==4){move4(s1, s2, s3);}
- if(v==5){move5(s1, s2, s3);}
- }
- void move1(Stack&s, Stack&e){
- int temp;
- ListNode *curr= s.getFirst();
- while(curr->getNext() !=NULL) {
- curr=curr->getNext();
- }
- temp = curr->getData();
- s.Remove();
- e.Insert(temp);
- print();
- cout<<endl;
- Sleep(1000);
- }
- void move2(Stack s,Stack e,Stack t){
- move1(s,t);
- move1(s,e);
- move1(t,e);
- }
- void move3(Stack s,Stack e,Stack t){
- move2(s,t,e);
- move1(s,e);
- move2(t,e,s);
- }
- void move4(Stack s,Stack e,Stack t){
- move3(s,t,e);
- move1(s,e);
- move3(t,e,s);
- }
- void move5(Stack s,Stack e,Stack t){
- move4(s,t,e);
- move1(s,e);
- move4(t,e,s);
- }
- void print(){
- cout<<"S1:";
- curr = s1.getFirst();
- while(curr!=NULL){
- cout<<curr->getData()<<" ";
- curr = curr->getNext();
- }cout<<endl;
- cout<<"S2:";
- curr = s2.getFirst();
- while(curr!=NULL){
- cout<<curr->getData()<<" ";
- curr = curr->getNext();
- }cout<<endl;
- cout<<"S3:";
- curr = s3.getFirst();
- while(curr!=NULL){
- cout<<curr->getData()<<" ";
- curr = curr->getNext();
- }
- }
- };
- int main(){
- int Size,v;
- cout<<"Enter The Tower Size: ";
- cin>>Size;
- Tower t(Size);
- t.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement