1. 程式人生 > >資料結構|鏈棧的實現(實驗3.2)

資料結構|鏈棧的實現(實驗3.2)

一、實驗目的

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

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

二、實驗內容

1、自己確定結點的具體資料型別和問題規模,建立一個鏈棧,實現棧的壓棧和出棧操作。

三、實驗步驟

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

template < typename T >
class LinkStack{
	public:
		LinkStack(T array[],int n);  //建構函式
		~LinkStack();   //解構函式
		void push(T x);   //入棧操作
		T pop();   //出棧操作
		void print();   //遍歷操作
		T gettop() { if(top!=NULL) return top->data;}   //取棧頂元素
		int Empty() ;   //判斷是否為空
	private:
		Node <T> *top;
};

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

2.1 建構函式

    1.棧頂指標 top 置為 NULL 
    2.建立一個結點 node 置為 NULL 
    3.迴圈: 
     ①.為結點 node 申請空間 
     ②.把陣列的值儲存在結點 node 的 data 域中 
     ③.把棧頂指標 top 指向的結點的地址儲存在 結點 node 的 next 域中 
     ④.棧頂指標 top 指向結點 node 

2.2解構函式: 
   1.建立一個結點 deleteNode 置為 NULL 
   2.迴圈: 
     ①.結點 deleteNode 指向棧頂結點 
     ②.棧頂指標 top 指向棧頂結點的下一個結點 
     ③.刪除棧頂結點

2.3入棧操作函式: 
   1.建立一個結點 s ,申請空間 
   2.把 x 儲存在結點 s 的 data 域中 
   3.結點 s 與原來的棧頂元素相連 
   4.棧頂指標 top 指向結點 s ,即結點 s 成為棧頂元素

2.4出棧操作函式: 
   1.判斷是否棧空,若是則丟擲“下溢”,否則 
   2.建立一個結點 p ,其指向棧頂結點 
   3.建立一個變數 x ,其記錄棧頂結點儲存的元素 
   4.棧頂指標 top 指向棧頂結點的下一個結點 
   5.刪除棧頂結點 p  
   6.返回棧頂結點儲存的元素 x

2.5輸出操作函式:按照順序,輸出棧元素

3、完整程式;

由於結點元素型別不確定,因此採用C++模板機制。

原始碼如下:

#include<iostream>
using namespace std;

template < typename T >
struct Node
{
	T data;
	Node<T> *next;
};

template < typename T >
class LinkStack {
public:
	LinkStack(T array[], int n)
	{
		top = NULL;
		Node<T> *node = NULL;
		for (int i = 0; i < n; i++) {
			node = new Node<T>;
			node->data = array[i];
			node->next = top;
			top = node;
		}
	}

	~LinkStack()
	{
		Node<T> *deleteNode = NULL;
		deleteNode = top;
		top = top->next;
		delete deleteNode;
	}

	void push(T x);
	T pop();
	void print();
	T gettop() { if (top != NULL) return top->data; }
	int Empty() { top == NULL ? return 1 : return 0; }
private:
	Node <T> *top;
};

template <typename T>  
bool LinkStack<T>::Empty(){  
    return top == NULL ? true : false;  
}  
template < typename T >
void LinkStack<T>::push(T x)
{
	Node<T> *s;
	s = new Node<T>;
	s->data = x;
	s->next = top;
	top = s;
}

template < typename T >
T LinkStack<T>::pop()
{
	T x;
	Node<T> *p;
	if (top == NULL)throw"下溢";
	x = top->data; p = top;
	top = top->next;
	delete p;
	return x;
}

template <class T>
void LinkStack<T>::print()
{
	Node<T> *node = top;
	while (node->next != NULL) {
		cout << node->data << " ";
		node = node->next;
	}
	cout << node->data << endl;
}

void main()
{
	int arr[] = { 0,1,2,3,4 };
	cout << "建立物件:0、1、2、3、4 依次入棧 " << endl;
	LinkStack<int> a(arr, 5);
	cout << "遍歷棧內的元素:";
	a.print();
	cout << "出棧一個元素" << a.pop() << "結果如下:" << endl;
	a.print();
	cout << "現在棧頂元素為:" << a.gettop();
	cout << "入棧元素6~10:" << endl;
	for (int i = 6; i <= 10; i++)
	{
		a.push(i);
	}
	cout << "遍歷棧內的元素:";
	a.print();
}

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

①總結

   在程式內定義物件為int型,將元素1~5按順序入棧。再出棧一個元素,後6~10依次入棧。

   通過呼叫成員函式,實現入棧、出棧、輸出等基本功能。

②執行結果如下:


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

  經過上一次實驗,我對棧的相關知識已經有所瞭解。本次試驗較為簡單,因為實驗內容較少,且對書本知識掌握得有點熟悉。但本次試驗還是發現了自己的不足之處。經過改進、詢問同學,成功完成了實驗。收穫頗豐。總的來說,我對理論知識有了更充分的理解,也能較大程度明白執行原理。