1. 程式人生 > >棧和佇列的基本操作實現及其應用

棧和佇列的基本操作實現及其應用

實驗2:棧和佇列的基本操作實現及其應用

一、實驗目的

1、   熟練掌棧和佇列的結構特點,掌握棧和佇列的順序儲存和鏈式儲存結構和實現。

2、      學會使用棧和佇列解決實際問題。

二、實驗內容

1、自己確定結點的具體資料型別和問題規模:

分別建立一個順序棧和鏈棧,實現棧的壓棧和出棧操作。

分別建立一個順序佇列和鏈佇列,實現佇列的入隊和出隊操作。

順序棧

#include<iostream> 

using namespacestd; 

const intStackSize=10; 

class SeqStack 

public: 

    SeqStack(){top=-1;}

    ~SeqStack(){} 

    void Push(int x); 

    int Pop(); 

    int GetTop(); 

    int Empty(); 

private: 

    int data[StackSize]; 

    int top; 

}; 

voidSeqStack::Push(int x) 

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

    data[++top]=x; 

intSeqStack::Pop() 

    int x; 

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

    x=data[top--]; 

    return x; 

intSeqStack::GetTop() 

    if(top!=-1) 

        return data[top]; 

intSeqStack::Empty() 

    if(top==-1) return 1; 

    else return 0; 

void main() 

    SeqStack s; 

    if(s.Empty()) 

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

    else 

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

    cout<<"進行1,2入棧操作"<<endl; 

    s.Push(1); 

   s.Push(2); 

    cout<<"棧頂元素:"<<endl; 

    cout<<s.GetTop()<<endl; 

    cout<<"出棧:"<<endl; 

    s.Pop(); 

    cout<<"棧頂元素:"<<endl; 

    cout<<s.GetTop()<<endl; 

 

 

#include<iostream.h> 

struct node 

    int data; 

    node *next; 

}; 

classLinkStack 

public: 

    LinkStack(){top=NULL;} 

    ~LinkStack(){} 

    void Push(int x); 

    int Pop(); 

    int getTop(){if(top!=NULL)returntop->data;} 

    int Empty(); 

private: 

    node *top; 

}; 

voidLinkStack::Push(int x) 

    node *s; 

    s=new node;s->data=x; 

    s->next=top;top=s; 

intLinkStack::Pop() 

    int x; 

    node *p; 

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

    x=top->data;p=top; 

    top=top->next; 

    delete p; 

    return x; 

intLinkStack::Empty() 

    if(top==NULL) return 1; 

    else return 0; 

void main() 

    LinkStack S; 

    if(S.Empty()) 

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

    else 

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

    cout<<"對1和2執行入棧操作"<<endl; 

    S.Push(1); 

    S.Push(2); 

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

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

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

    S.Pop(); 

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

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

 

 

 

#include<iostream> 

using namespacestd; 

const intQueueSize=100; 

class CirQueue 

public: 

    CirQueue(){front=rear=QueueSize-1;} 

    ~CirQueue(){} 

    void Enqueue(int x); 

    int DeQueue(); 

    int GetQueue(); 

    int Empty(); 

private: 

    int data[QueueSize]; 

    int front,rear; 

}; 

void CirQueue::Enqueue(intx) 

    if((rear+1)%QueueSize==front) throw"上溢"; 

    rear=(rear+1)%QueueSize; 

    data[rear]=x; 

intCirQueue::DeQueue() 

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

    front=(front+1)%QueueSize; 

    return data[front]; 

intCirQueue::GetQueue() 

    int i; 

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

    i=(front+1)%QueueSize; 

    return data[i]; 

intCirQueue::Empty() 

    if(front==rear) 

        return 1; 

    else 

        return 0; 

void main() 

    CirQueue c; 

    if(c.Empty()) 

        cout<<"佇列為空"<<endl; 

    else 

        cout<<"佇列為非空"<<endl; 

    cout<<"對1和2執行入隊操作"<<endl; 

    c.Enqueue(1); 

    c.Enqueue(2); 

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

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

    cout<<"執行一次出隊操作"<<endl; 

    c.DeQueue(); 

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

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

 

 

#include<iostream> 

using namespacestd; 

struct node 

    int data; 

    node *next; 

}; 

classLinkQueue 

public: 

    LinkQueue(); 

    ~LinkQueue(); 

    void EnQueue(int x); 

    int DeQueue(); 

    int GetQueue(); 

    int Empty(); 

private: 

    node *front,*rear; 

}; 

LinkQueue::LinkQueue() 

    node *s=NULL; 

    s=new node; 

    s->next=NULL; 

    front=rear=s; 

LinkQueue::~LinkQueue() 

    node *p=NULL; 

    while(front!=NULL) 

    { 

        p=front->next; 

        delete front; 

        front=p; 

    } 

voidLinkQueue::EnQueue(int x) 

    node *s=NULL; 

    s=new node; 

    s->data=x; 

    s->next=NULL;

      rear->next=s;

    rear=s; 

intLinkQueue::DeQueue() 

    node *p=NULL; 

    int x; 

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

    p=front->next; 

    x=p->data; 

    front->next=p->next; 

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

    delete p; 

    return x; 

intLinkQueue::GetQueue() 

{  

    if(front!=rear) 

        return front->next->data; 

intLinkQueue::Empty() 

    if(front==rear) 

        return 1; 

    else 

        return 0; 

void main() 

    LinkQueue l; 

    if(l.Empty()) 

        cout<<"佇列為空"<<endl; 

    else 

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

    cout<<"對1和2執行入隊操作"<<endl; 

    try 

    { 

        l.EnQueue(1); 

        l.EnQueue(2); 

    } 

    catch(char *wrong) 

    { 

        cout<<wrong<<endl; 

    } 

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

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

    cout<<"執行一次出隊操作"<<endl; 

    try 

    { 

        l.DeQueue(); 

    } 

    catch(char *wrong) 

    { 

        cout<<wrong<<endl; 

    } 

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

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

#include<iostream.h>

const intStackSize=100;

class SeqStack

{

public:

      SeqStack(){top=-1;}

      ~SeqStack(){}

      int Empty();

      int data[StackSize];

      int top;

};

 

int main()

{

      int x;

      SeqStack s;

      s.top=-1;

      cout<<"輸入一個十進位制數字:"<<endl;

      cin>>x;

    

      do{

            s.top++;

            s.data[s.top]=x%2;

            x=x/2;

      }while(x!=0);

      cout<<"該數字的二進位制為:"<<endl;

      do

      {

            cout<<s.data[s.top];

            s.top--;

      }while (s.top!=-1);

      cout<<endl;

      return 0;

}

 

2、設計演算法並寫出程式碼,實現一個十將二進位制轉換成2進位制數。

 

3、選做題(*

設計一個模擬飯堂排隊打飯管理軟體,實現“先來先打飯”的排號叫號管理。

三、實驗步驟

1、依據實驗內容分別說明實驗程式中用到的資料型別的定義;

2、相關操作的演算法表達;

3、完整程式;

4、總結、執行結果和分析。

5、總體收穫和不足,疑問等。

四、實驗要求

1、   按照資料結構實驗任務書,提前做好實驗預習與準備工作。

2、   加“*”為選做題。做好可加分。

3、   嚴格按照資料結構實驗報告模板和規範,及時完成實驗報告。

4、   在個人主頁上發文章提交作業。

5、   實驗課會抽查3-5人,希望你可以被查到!

#include<iostream.h> 

struct node 

    int data; 

    node *next; 

}; 

classSeqStack 

public: 

    SeqStack(){top=NULL;} 

    ~SeqStack(){} 

    void Push(int x); 

    int Pop(); 

private: 

    node *top; 

}; 

voidSeqStack::Push(int x) 

    node *s; 

    s=new node;s->data=x; 

    s->next=top;top=s; 

intSeqStack::Pop() 

    int x; 

    node *p; 

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

    x=top->data;p=top; 

    top=top->next; 

    delete p; 

    return x; 

int main() 

    int n,a,b,length=0; 

    cout<<"請輸入一個十進位制整數:"<<endl; 

    cin>>n; 

    a=n; 

    SeqStack s1; 

    while(a!=0) 

    {  

        b=a%2;

        a=a/2;

        s1.Push(b);

        length++; 

    } 

    try{ 

        cout<<"其二進位制為:"<<endl; 

       while(length!=0){cout<<s1.Pop();length--;} 

        cout<<endl; 

    } 

    catch(char *wrong) 

    {cout<<wrong<<endl;} 

    return 0;