1. 程式人生 > >實驗二:棧與佇列的實現與應用

實驗二:棧與佇列的實現與應用

1、順序棧:

#include <iostream>

using namespace std;

const int StackSize=10;

class SeqStack{

public:

SeqStack(); //建構函式,初始化一個空棧

~SeqStack(){} 

void Push(int x); 

int Pop(); 

int GetTop();

int Empty();

private:

int data[StackSize];//存放棧元素的陣列

int top; //棧頂指標,為棧頂元素在陣列中的下標

};

SeqStack::SeqStack(){

top=-1;}

void SeqStack::Push(int x){

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

data[++top]=x;}

int SeqStack::Pop(){

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

int x=data[top--];

return x;}

int SeqStack::GetTop(){

if(top!=-1)

return data[top];}

int SeqStack::Empty()

if(top==-1)return 1;

else return 0;

}

void main(){

SeqStack k1;

if(k1.Empty())

cout<<"棧為空"<<"\n"<<endl;

else

cout<<"棧非空"<<endl;

cout<<"對99和88執行入棧操作"<<"\n"<<endl;

k1.Push(99);

k1.Push(88);

cout<<"棧頂元素為:"<<"\n"<<endl;

cout<<k1.GetTop()<<"\n"<<endl;

cout<<"執行一次出棧操作"<<"\n"<<endl;

k1.Pop();

cout<<"棧頂元素為:"<<"\n"<<endl;

cout<<k1.GetTop()<<"\n"<<endl;}


2、鏈棧:

#include<iostream>

using namespace std;

struct Node

{

int data;

Node *next;

};

class LinkStack{

public:

LinkStack();

~LinkStack();

void Push(int x);

int Pop();

int GetTop();

int Empty();

private:

Node *top;

};

//建構函式

LinkStack::LinkStack(){

top=new Node;

top=NULL;}

//壓棧函式

void LinkStack::Push(int x){

Node *s=NULL;

s=new Node;

s->data=x;

s->next=top;

top=s;}

//彈棧函式

int LinkStack::Pop(){

if(top==NULL)throw"下溢";

else

int x;Node *p=NULL;

p=top;

x=top->data;

top=top->next;

delete p;

return x;}

//取棧頂元素

int LinkStack::GetTop(){

if(top!=NULL)

return top->data;}

//判空函式

int LinkStack::Empty(){

if(top==NULL)

return 1;

else return 0;}

//解構函式

LinkStack::~LinkStack(){

Node *q=NULL;

while(top!=NULL)

q=top;

top=top->next;

delete q;}

//主函式

void main(){

LinkStack s;

if(s.Empty())

cout<<"此棧為空!"<<"\n"<<endl;

else

cout<<"此棧不為空"<<"\n"<<endl;

cout<<"對99和88進行壓棧操作""\n"<<endl;

s.Push(99);

s.Push(88);

cout<<"棧頂元素為:"<<"\n"<<endl;

cout<<s.GetTop()<<"\n"<<endl;

cout<<"執行一次彈棧操作"<<"\n"<<endl;

s.Pop();

cout<<"棧頂元素為:"<<"\n"<<endl;

cout<<s.GetTop()<<"\n"<<endl;}

3、順序佇列

#include <iostream> 

using namespace std; 

const int MAX=10; 

class Queue{ 

private: 

   int front; 

   int rear; 

   int data[MAX]; 

public: 

    Queue(){front=rear=MAX-1;} 

   ~Queue(){} 

   void EnQueue(int x); 

   int DeQueue(); 

   int GetQueue(){if (front!=rear) return data[(front+1)%MAX];}; 

   int Empty(); 

}; 

void Queue::EnQueue(int x){ 

   if ((rear-1)%MAX==front) throw "上溢"; 

   rear=(rear+1)%MAX; 

   data[rear]=x; 

int Queue::DeQueue(){ 

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

   int x=data[front]; 

   front=(front+1)%MAX; 

   return x; 

int Queue::Empty(){ 

   if (front==rear) return 1; else return 0; 

int main(){ 

   Queue S; 

   if (S.Empty()) 

       cout<<"隊為空!"<<endl; 

   else 

       cout<<"隊不為空!"<<endl; 

   cout<<"1入隊:"<<endl; 

   S.EnQueue(1); 

   cout<<"2入隊:"<<endl;

 S.EnQueue(2); 

   cout<<"取隊頭:"<<endl; 

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

   cout<<"一次出隊:"<<endl; 

   S.DeQueue(); 

   cout<<"取隊頭:"<<endl; 

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

   return 0; 

}  

4、鏈佇列

#include <iostream> 

using namespace std; 

struct Data { 

   int data; 

   struct Data *next; 

} *q; 

class Queue{ 

private: 

    Data *rear,*front; 

public: 

   Queue(){rear=NULL;front=NULL;} 

   ~Queue(){} 

   void EnQueue(int x); 

   int DeQueue(); 

    intGetQueue(); 

   int Empty(); 

}; 

void Queue::EnQueue(int x){ 

   q=new  Data; 

   q->data=x; 

   q->next=NULL; 

   if (Empty()) 

       front=rear=q;     //若隊為空,讓隊頭和隊尾指標都指向新增空間 

   else { 

       rear->next=q; 

       rear=q;} 

int Queue::DeQueue(){ 

   if (Empty()) throw "下溢"; 

   q=front; 

   int x=front->data; 

   front=front->next; 

   delete q; 

   return x; 

 

int Queue::GetQueue(){ 

   if (!Empty()) 

       return front->data; 

 

int Queue::Empty(){ 

   if (front==NULL&&rear==NULL) return 1;  

   else return 0; 

int main(){ 

   Queue S; 

   if (S.Empty()) 

       cout<<"隊為空!"<<endl; 

   else 

       cout<<"隊不為空!"<<endl; 

   cout<<"10入隊:"<<endl; 

   S.EnQueue(10); 

   cout<<"5入隊:"<<endl; 

   S.EnQueue(5); 

   cout<<"取隊頭元素:"<<endl; 

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

   cout<<"一次出隊:"<<endl; 

   S.DeQueue(); 

   cout<<"取隊頭元素:"<<endl; 

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

   return 0; 

}