1. 程式人生 > >實驗二(2)用單鏈表實現資訊操作

實驗二(2)用單鏈表實現資訊操作

一、實驗目的

鞏固線性表的資料結構的儲存方法和相關操作,學會針對具體應用,使用線性表的相關知識來解決具體問題。

二、實驗時間

準備時間為第3周到第4周,具體集中實驗時間為第4周第2次課。2個學時。

實驗內容

          建立一個由n個學生資訊的單鏈表,n的大小由自己確定,每一個學生的成績資訊由自己確定,實現資料的對錶進行插入、刪除、查詢等操作。分別輸出結果。
四、程式碼實現

         1、Linklist.h

//Linklist.h
#pragma once
template<class DataType>
struct Node             //用結構體定義節點
{
	DataType data;
	Node<DataType> *next;
};

template<class DataType>
class Slinklist {              //定義單鏈表類模板
public:
	Slinklist();
	Slinklist(DataType a[], int n);
	~Slinklist();
	int Length();
	void Get(int n);
	int Locate(char a[]);
	void Insert(int i, DataType x);
	DataType Delete(int i);
	void Printlist();
private:
	Node<DataType> *first;
};

struct Student          //定義學生資訊結構體陣列
{
	char name[5];
	char sno[14];
	char sex[4];
	char age[4];
	char Class[12];
};

       2、各功能函式實現:Linklist.cpp
//Linklist.cpp
#include"Linklist.h"
#include<iostream>
using namespace std;
template<class DataType>
Slinklist<DataType>::Slinklist()   //無參建構函式定義
{
	first = new Node<DataType>;      //生成頭節點
	first->next = NULL;              //頭節點的指標域置空
}

template<class DataType>
Slinklist<DataType>::Slinklist(DataType a[], int n)    //有參建構函式定義,用頭插法建立單鏈表
{
	first = new Node<DataType>; first->next = NULL;
	for (int i = 0; i < n; i++)
	{
		Node<DataType> *s;
		s = new Node<DataType>; s->data = a[i];
		s->next = first->next; first->next = s;
	}
}

template<class DataType>
int Slinklist<DataType>::Length()      //計算單鏈表長度
{
	p = new Node<DataType>;
	p = first->next; count = 0;
	while (p != NULL)
	{
		p = p->next;
		count++;
	}
	return count;
}

template<class DataType>
void Slinklist<DataType>::Get(int i)    //查詢在第i位的學生資訊
{
	Node<DataType> *p;
	p = first->next; int count = 0;
	while(p->data.name != NULL&&count < i - 1)
	{
		p = p->next;
		count++;
	}
	if (p->data.name == NULL)throw"位置";
	else cout << p->data.name << " " << p->data.sno << " " << p->data.sex << " " << p->data.age << " " << p->data.Class << endl;
}

template<class DataType>
int Slinklist<DataType>::Locate(char a[])    //查詢學生資訊,返回學生所在位置
{
	Node<DataType> *p;
	p = new Node<DataType>;
	p = first->next; int count = 1;
	while (p->data.name != NULL)
	{
		if ((string)p->data.name == (string)a)return count;
		p = p->next;
		count++;
	}
	return 0;
}

template<class DataType>
void Slinklist<DataType>::Insert(int i, DataType x)   //從第i位插入學生資訊
{
	Node<DataType> *p;
	p = new Node<DataType>;
	p = first; int count = 0;
	while (p->data.name != NULL&&count < i - 1)
	{
		p = p->next;
		count++;
	}
	if (p->data.name == NULL)throw"位置";
	else {
		Node<DataType> *s;
		s = new Node<DataType>; s->data = x;
		s->next = p->next; p->next = s;
	}
}

template<class DataType>
DataType Slinklist<DataType>::Delete(int i)     //刪除第i位學生資訊
{
	Node<DataType> *p;
	p = new Node<DataType>;
	p = first; int count = 1;
	while (p->data.name != NULL&&count < i)
	{
		p = p->next;
		count++;
	}
	if (p->data.name == NULL||p->data.name == NULL )throw"位置";
	else {
		Node<DataType> *q;
		q = new Node<DataType>;
		q = p->next; DataType x = q->data;
		p->next = q->next;
		delete q;
		return x;
	}
}

template<class DataType>
void Slinklist<DataType>::Printlist()     //單鏈表遍歷資訊
{
	Node<DataType> *p;
	p = new Node<DataType>;
	p = first->next;
	while (p != NULL)
	{
		cout << p->data.name << " " << p->data.sno << " " << p->data.sex << " " << p->data.age << " " << p->data.Class << endl;
		p = p->next;
	}
	cout << endl;
}

template<class DataType>
Slinklist<DataType>::~Slinklist()     //解構函式
{
	while (first != NULL)
	{
		Node<DataType> *q;
		q = new Node<DataType>;
		q = first;
		first = first->next;
		delete q;
	}
}

       3、主程式對資訊進行基本操作:linklistmain.cpp
//linklistmain.cpp
#include"Linklist.cpp"
#include<iostream>
using namespace std;
int main()
{
	Student student[5] = {  { "小波","201611671119","男","20","信管1161" },
							{ "小明","201611671114","男","19","信管1161" },
							{ "小紅","201611671121","女","18","信管1161" },
							{ "小芳","201611671127","女","17","信管1161" },
							{ "小李","201611671125","男","18","信管1161" }  };
	Slinklist<Student>score(student, 5);
	cout << ">>>顯示所有學生資訊:" << endl;
	score.Printlist();
	cout << ">>>將“小剛”的資訊插入第二位後顯示所有學生資訊:" << endl;
	score.Insert(2, { "小剛","201611671101","男","15","信管1161" });
	score.Printlist();
	cout << ">>>獲取第三位學生的資訊:"<<endl;
	score.Get(3) ;
	cout << ">>>查詢學生“小明”資訊所在的位置:" << endl;
	cout << score.Locate("小明") << endl;
	cout << ">>>刪除資料前所有學生資料:" << endl;
	score.Printlist();
	score.Delete(2);
	cout << ">>>刪除第二位學生資料後所有學生資料:" << endl;
	score.Printlist();
	return 0;
}

五、執行結果如下:


六、總結

       在能夠完成基本操作的同時對操作資料稍微複雜化了一點。