实验二

一、

(1)顺序栈

#include<iostream>
using namespace std;
const int StackSize=10;

template<class DataType>
class SeqStack
{
public:
       SeqStack();
       ~SeqStack(){}
       void push(DataType x);
       DataType pop();
private:
       DataType data[StackSize];
       int top;
};

template<class DataType>
SeqStack<DataType>::SeqStack()
{
   top=-1;
}

template<class DataType>
void SeqStack<DataType>::push(DataType x)
{
if(top==StackSize-1)throw;
top++;
data[top]=x;
}
template<class DataType>
DataType SeqStack<DataType>::pop()

  DataType x;
  if(top==-1)throw;
  x=data[top--];
  return x;
}

void main()
{
SeqStack<int>S;
cout<<"对15和5执行入栈操作"<<endl;
S.push(15);
S.push(5);
cout<<"执行一次出栈操作:"<<endl;
S.pop();
}

(2)链栈

#include<iostream>
using namespace std;


template<class DataType>
class LinkStack
{
public:
      LinkStack();
       ~LinkStack(){}
       void push(DataType x);
       DataType pop();
private:
      Node<DataType>*top;
};


template<class DataType>
LinkStack<DataType>::LinkStack()
{
   top=NULL;
}


template<class DataType>
void LinkStack<DataType>::push(DataType x)
{
s=new Node;s=->data=x;
s->next=top;
top=s;
}
template<class DataType>
DataType LinkStack<DataType>::pop()

  if(top=NULL)throw"下溢";
  x=top->data;p=top;
  top=top->next;
  delete p;
  return x;
}


void main()
{
LinkStack<int>S;                  
cout<<"对15和5执行入栈操作"<<endl;
S.push(15);
S.push(5);
cout<<"执行一次出栈操作:"<<endl;
S.pop();
}

(3)顺序队列

#include<iostream>
using namespace std;
const int QueueSize=10;

template<class DataType>
class CirQueue
{
public:
       CirQueue();
       ~CirQueue(){}
       void EnQueue(DataType x);
       DataType DeQueue();
private:
       DataType data[QueueSize];
       int front,rear;
};


template<class DataType>
CirQueue<DataType>::CirQueue()
{
  rear=front=QueueSize-1;
}


template<class DataType>
void CirQueue<DataType>::EnQueue(DataType x)
{
if((rear+1)%QueueSize==front)throw"上溢";
rear=(rear+1)%QueueSize;
data[rear]=x;
}
template<class DataType>
DataType CirQueue<DataType>::DeQueue()

if(rear==front)throw"下溢";
front=(front+1)%QueueSize;
return data(front);
}


void main()
{
     CirQueue<int>Q;
     cout<<"元素10和15执行入队操作:"<<endl;
     Q.EnQueue(10);
Q.EnQueue(15);
}
     cout<<"执行出队操作"<<endl;
     Q.DeQueue();
}
 


 

(4)链队列

 

#include<iostream>

using namespace std;

//#include"s.cpp"

 

 

struct Node

{

       DataTypedata;

       Node<DataType>*next;

};

template<class DataType>

class LinkQueue

{

       public:

              LinkQueue();

              ~LinkQueue();

              voidEnQueue(DataType x);

              DataTypeDeQueue();

              DataTypeGetQueue();

              intEmpty();

       private:

              Node<DataType>*front,*rear;

};

 

template<class DataType>

 

LinkQueue<DataType>::LinkQueue()

{

       Node<DataType>*s=NULL;

       s=newNode<DataType>;

       s->next=NULL;

       front=rear=s;

}

template<class DataType>

LinkQueue<DataType>::~LinkQueue()

{

       Node<DataType>*p=NULL;

       while(front!=NULL)

       {

              p=front->next;

              deletefront;

              front=p;

       }

}

template<class DataType>

voidLinkQueue<DataType>::EnQueue(dATAtYPE X)

{

       Node<DataType>*s=NULL;

       s=newNode<DataType>;

       s->data=x;

       s->next=NULL;

       rear->next=s;rear=s;

}

template<class DataType>

DataTypeLinkQueue<DataType>::DeQueue()

{

       Node<DataType>*p=NULL;

       intx;

       if(rear==front)throw"下溢";

       p=front->next;

       x=p->data;

       front->next=p->next;

       if(p->next==NULL)rear=front;

       deletep;

       returnx;

}

template<class DataType>

DataTypeLinkQueue<DataType>::GetQueue()

{

       if(front!=rear)

              returnfront->next->data;

}

template<class DataType>

int LinkQueue<DataType>::Empty()

{

       if(front==rear)

              return1;

       else

              return0;

}

 

void main()

{

       LinkQueue<int>Q;

       if(Q.Empty())

              cout<<"队列为空"<<endl;

       else

              cout<<"队列非空"<<endl;

       cout<<"元素10和15执行入队操作:"<<endl;

       try

       {

              Q.EnQueue(10);

              Q.EnQueue(15);

       }

       catch(char*wrong)

       {

              cout<<wrong<<endl;

       }

       cout<<"查看队头元素:"<<endl;

       cout<<Q.GetQueue()<<endl;

       cout<<"执行出队操作"<<endl;

       try

       {

              Q.DeQueue();

       }

       catch(char*wrong)

       {

              cout<<wrong<<endl;

       }

       cout<<"查看队头元素"<<endl;

       cout<<Q.GetQueue()<<endl;

}*/

二、

#include<iostream>

using namespace std;

const int Size=50;

class Conversion

{

public:

       Conversion(){top=-1;}

       ~Conversion(){}

       voidPush(int x);

       voidPop();

private:

       intdata[Size];

       inttop;

};

 

void Conversion::Push(int x)

{

       top=-1;

       inty;

       intm=2;

       if(top==Size-1)throw"上溢";

       while(x!=0)

       {

              y=x%m;

              data[++top]=y;

              x=x/m;

       };

}

void Conversion::Pop()

{

       if(top==-1)throw"下溢";

              while(top!=-1)

              {

                     intx=data[top--];

                     cout<<x;

              };

 

}

 

void mian()

{

       inti=1;

       intnumber;

       Conversionn;

       do

       {     cout<<"请输入一个十进制整数"<<endl;

              cin>>number;

              n.Push(number);

              n.Pop();

              cout<<endl;

       }while(i==1);

}

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值