1. 程式人生 > >順序棧以及鏈棧驗證實驗

順序棧以及鏈棧驗證實驗

一、實驗目的

1.掌握棧的順序儲存結構和鏈式儲存結構;

2..驗證順序棧以及鏈棧及其基本操作的實現;

3驗證棧的操作特性。

二、實驗內容

1.建立一個空棧;

2.對已建立的棧進行插入、刪除、去棧頂元素等基本操作。

三、設計與編碼

1.本實驗用到的理論知識。

涉及了棧的後進先出的特性,棧的順序儲存結構—順序棧和棧的鏈式儲存結構—鏈棧。

2.演算法設計

(1)順序棧驗證實驗:

SeqStack.h

#ifndef SeqStack_H

#define SeqStack_H

const int StackSize=10;

template<class DataType>

class SeqStack

{

public:

       SeqStack();

       ~SeqStack(){};

       voidPush(DataType x);

       DataTypePop();

       DataTypeGetTop();

       intEmpty();

private:

       DataTypedata[StackSize];

       inttop;

};

#endif

 

SeqStack.cpp

#include"SeqStack.h"

 

template<class DataType>

SeqStack<DataType>::SeqStack()

{

       top=-1;

}

 

template<class DataType>

voidSeqStack<DataType>::Push(DataType x)

{

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

       top++;

       data[top]=x;

}

 

template<class DataType>

DataType SeqStack<DataType>::Pop()

{

       DataTypex;

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

       x=data[top--];

       returnx;

}

 

template<class DataType>

DataType SeqStack<DataType>::GetTop()

{

       if(top!=-1)

              returndata[top];

}

 

template<class DataType>

int SeqStack<DataType>::Empty()

{

       if(top==-1)return1;

       elsereturn 0;

}

 

#include<iostream>

using namespace std;

#include "SeqStack.cpp"

 

void main()

{

       SeqStack<int>S;

       if(S.Empty())

              cout<<"棧已空"<<endl;

       else

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

       cout<<"對56,65,10執行入棧操作"<<endl;

       S.Push(56);

       S.Push(65);

       S.Push(10);

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

       cout<<S.GetTop()<<"執行操作"<<endl;

       cout<<"執行一次出棧操作並對69執行入棧"<<endl;

       S.Pop();

       S.Push(69);

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

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

}


(2)鏈棧驗證實驗

#include<iostream.h> 

struct Node 

   int data; 

   Node *next; 

}; 

class LinkStack 

public: 

   LinkStack()  {  top=NULL; } 

   ~LinkStack(); 

   void Push(int x); 

   int Pop(); 

   int GetTop()  {  if(top!=NULL)return top->data;  } 

   bool Empty() 

   { 

       if(top==NULL)return 1; 

       else return 0; 

   } 

private: 

   Node *top; 

}; 

 

LinkStack::~LinkStack()   

{   

   Node *p;   

   while(top!=NULL)   

   {   

       p=top;   

       top=top->next;   

       delete p;   

   }   

void LinkStack::Push(int x)   

{   

   Node *s=new Node;   

   s->data=x;   

   s->next=top;   

   top=s;   

}  

int LinkStack::Pop()   

{   

   Node *p;   

   int x;   

   if(top==NULL)cout<<"Stack is empty"<<endl;   

   else   

   {   

    x=top->data;   

    p=top;   

    top=p->next;   

    delete p;   

    return x;   

   }   

}     

int main()   

{      

   LinkStack s; 

   cout<<"對56、69和94執行壓棧操作"<<endl; 

   s.Push(56);   

   s.Push(69);   

   s.Push(94); 

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

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

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

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

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

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

   return 0;   

}    

四、執行與測試

1.遇到的問題是結構性問題,通過百度以及查閱書本解決。

2.設計的測試資料及結果如上。