用数据结构解一个复杂点的约瑟夫环 需要用到队列 栈 链表 大体上写出来了 还没写算法 想试一下能不能运行 结果。。。。
附上源代码吧
//op.h
enum Boolean
{
False,
Ture,
};
class Clist;
class Stack;
class Queue;
class node
{
public:
friend class Clist;
friend class Stack;
friend class Queue;
node();
node(int);
~node() {}
private:
int data;
node *Cnext;
node *Cfront;
node *Slink;
node *Qnext;
node *Qfront;
int SaveTime;
int DeathTime;
Boolean JustSave;
};
class Clist
{
public:
Clist();
~Clist();
void Insert(int);
void Insert(int,node*);
node *Find(int);
node *Del(int);
void MakeEmpty();
void Joseph();
private:
node *first;
node *last;
node *current;
int Cnum;
};
class Stack
{
public:
Stack();
~Stack();
void MakeEmpty();
void Push(node*);
node *Pop();
Boolean IsEmpty();
private:
node *top;
int Snum;
};
class Queue
{
public:
Queue();
~Queue();
void MakeEmpty();
void EnQueue(node*);
node *DeQueue();
Boolean IsEmpty();
private:
node *rear,*front;
int Qnum;
};
#include<iostream>
#include"op.h"
using namespace std;
node::node()
{
data=0;
Cnext=0;
Cfront=0;
Slink=0;
Qnext=0;
Qfront=0;
SaveTime=DeathTime=0;
JustSave=False;
}
node::node(int val)
{
data=val;
Cnext=0;
Cfront=0;
Slink=0;
SaveTime=DeathTime=0;
JustSave=False;
}
Clist::Clist()
{
first=new node(0);
current=0;
last=0;
Cnum=0;
}
Clist::~Clist()
{
MakeEmpty();
}
void Clist::Insert(int val)
{
if(Cnum==0)
{
node *p=new node(val);
first->Cnext=p;
p->Cfront=p;
p->Cnext=p;
last=p;
Cnum++;
}
else
{
node *p=new node(val);
p->Cfront=last;
p->Cnext=first->Cnext;
last=p;
Cnum++;
}
}
void Clist::Insert(int val, node *now)
{
if(now==last)
{
Insert(val);
}
else
{
node *p=new node(val);
p->Cfront=now;
p->Cnext=now->Cnext;
now->Cnext->Cfront=p;
now->Cnext=p;
Cnum++;
}
now->SaveTime++;
now->JustSave=Ture;
}
node *Clist::Find(int val)
{
node *p=first->Cnext;
while(p!=last || p->data!=val)
{
p=p->Cnext;
}
if(p->data==val)
{
return p;
}
else
{
cout<<"Can not find the value!!"<<endl;
return first;
}
}
node *Clist::Del(int val)
{
node *p=Find(val);
if(p==first)
{
return p;
}
else
{
p->Cfront->Cnext=p->Cnext;
p->Cnext->Cfront=p->Cfront;
Cnum--;
p->Cfront=0;
p->Cnext=0;
return p;
}
}
void Clist::MakeEmpty()
{
node *p=first->Cnext;
node *q=p;
while(p!=last)
{
p=p->Cnext;
delete q;
q=p;
}
delete first;
delete last;
delete current;
delete p;
delete q;
Cnum=0;
}
void Clist::Joseph()
{
for (int i=1;i<=30;i++)
{
Insert(i);
}
current=first->Cnext;
while(current!=last)
{
cout << current->data<<" ";
current=current->Cnext;
}
}
Stack::Stack()
{
top=0;
Snum=0;
}
Stack::~Stack()
{
MakeEmpty();
}
void Stack::MakeEmpty()
{
node *p=top;
while(Snum!=0)
{
top=top->Slink;
delete p;
p=top;
}
delete p;
delete top;
}
void Stack::Push(node *now)
{
now->Slink=top;
top=now;
now->DeathTime++;
now->JustSave=False;
Snum++;
}
node *Stack::Pop()
{
node *p=top;
top=top->Slink;
Snum--;
p->Slink=0;
return p;
}
Boolean Stack::IsEmpty()
{
if(Snum==0)
return Ture;
else
return False;
}
Queue::Queue()
{
rear=front=0;
Qnum=0;
}
Queue::~Queue()
{
MakeEmpty();
}
void Queue::EnQueue(node *now)
{
if(Qnum==0)
{
rear=front=now;
}
else
{
now->Qnext=front;
front->Qfront=now;
front=now;
}
now->DeathTime++;
now->JustSave=False;
Qnum++;
}
node *Queue::DeQueue()
{
node *p=rear;
rear=rear->Qfront;
rear->Qnext=0;
p->Qfront=0;
p->Qnext=0;
Qnum--;
return p;
}
Boolean Queue::IsEmpty()
{
if(Qnum==0)
return Ture;
else
return False;
}
void Queue::MakeEmpty()
{
node *p=front;
while(Qnum!=0)
{
front=front->Qnext;
delete p;
p=front;
Qnum--;
}
}
4 个解决方案
#1
没有main()函数
例如
void main()
{
Queue q;
}
例如
void main()
{
Queue q;
}
#2
没有 main函数
#3
!!!!汗颜。。。谢了
#4
难道我眼花了吗?没有main函数啊
#1
没有main()函数
例如
void main()
{
Queue q;
}
例如
void main()
{
Queue q;
}
#2
没有 main函数
#3
!!!!汗颜。。。谢了
#4
难道我眼花了吗?没有main函数啊