双向链表Test1

来源:互联网 发布:gta5低配优化补丁 编辑:程序博客网 时间:2024/06/05 23:58
一.实验目的
     巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
 
二..实验内容
  建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
(用双向链表来实现)。

三..源代码
//DoubleList.h文件
#include<iostream>using namespace std;template<class T>struct Node{T data;Node<T> *pNext;Node<T> *pPrior;};template<class T>class List{private:Node<T> *pHead;Node<T> *pTail;int length;public:List(int length);void traverseList();void traverseListReturn();void sortList();bool insert(int index, T x);bool ChangeList(int postion, int num); //修改链表中指定位置的节点void ClearList();int DeleteList(int postion);int GetList(int postion);~List();};template<class T>List<T>::List(int length){this->length = length;pHead = new Node<T>;pHead->pPrior = NULL;pTail = pHead;for (int i = 0; i < length; i++){Node<T> *temp = new Node<T>;cout << "Please enter the no." << i + 1 << "Node's data:";cin >> temp->data;temp->pNext = NULL;temp->pPrior = pTail;pTail->pNext = temp;pTail = temp;}}template<class T>void List<T>::traverseList(){Node<T> *p = pHead->pNext;while (p != NULL){cout << p->data << "  ";p = p->pNext;}cout << endl;}template<class T>void List<T>::traverseListReturn(){Node<T>*p = pTail;while (p->pPrior != NULL){cout << p->data << "  ";p = p->pPrior;}cout << endl;}template<class T>void List<T>::sortList(){Node<T> *p = new Node<T>;Node<T> *q = new Node<T>;T temp;for (p = pHead->pNext; p->pNext != NULL; p = p->pNext){for (q = p->pNext; q != NULL; q = q->pNext){if (q->data < p->data){temp = q->data;q->data = p->data;p->data = temp;}}}}template<class T>bool List<T>::insert(int index, T x){if (index<1 || index>length){return false;}Node<T> *p = new Node<T>;p->data = x;Node<T> *q = pHead;for (int i = 0; i < index; i++){q = q->pNext;}p->pPrior = q->pPrior;q->pPrior->pNext = p;p->pNext = q;q->pPrior = p;length++;return true;}template<class T>bool List<T>::ChangeList(int postion, int num){Node<T> *p = pHead->pNext;if (postion > length || postion < 1){return false;}for (int i = 0; i < postion - 1; i++){p = p->pNext;}p->data = num;}template<class T>void List<T>::ClearList(){Node<T> *q;Node<T> *p = pHead->pNext;while (p != NULL){q = p;p = p->pNext;delete q;}p = NULL;q = NULL;}template<class T>int List<T>::DeleteList(int postion){Node<T> *p = pHead->pNext;if (postion > length || postion < 1){return -1;}for (int i = 0; i < postion - 1; i++){p = p->pNext;}p->pPrior->pNext = p->pNext;p->pNext->pPrior = p->pPrior;delete p;length--;}template<class T>int List<T>::GetList(int postion){Node<T> *p = pHead->pNext;if (postion > length || postion < 1){return -1;}for (int i = 0; i < postion - 1; i++){p = p->pNext;}return p->data;}template<class T>List<T>::~List(){Node<T> *q;Node<T> *p = pHead->pNext;while (p != NULL){q = p;p = p->pNext;delete q;}p = NULL;q = NULL;}
//源.cpp
#include"DoubleList.h"#include<iostream>using namespace std;int main(void){List<float> Score(4);Score.traverseList(); //输入99.5.98.94.5.88,遍历Score.traverseListReturn(); //逆向遍历cout << "---------------------------" << endl;Score.sortList(); //排序Score.traverseList(); //再次遍历cout << "---------------------------" << endl;Score.insert(3, 96); //在第三个位置插入96Score.traverseList(); //再次遍历cout << "---------------------------" << endl;Score.ChangeList(2, 100); //将第二个位置改为100Score.traverseList(); //再次遍历cout << "---------------------------" << endl;Score.sortList(); //排序Score.traverseList(); //再次遍历cout << "---------------------------" << endl;cout << Score.GetList(2) << endl; //获取第二个位置的元素Score.DeleteList(3); //删除第三个位置的元素Score.traverseList();//再次遍历cout << "---------------------------" << endl;system("pause");return 0;}
实验结果如下:
经验证,结果无误
四、实验心得
双向链表的一个特点就是有着prior和next两个指针;在双链表中,求表长、按位查找、按值查找、遍历等操作的实现与单链表基本相同。所以没什么大问题,在插入和删除操作操作时应该注意,把节点的前驱指针和后继指针都连接上,并且要注意指针修改的先后顺序,我就应该修改顺序颠倒而出现错误。
通过这次实验,掌握了双向链表的使用,也更加巩固了对线性表的深入了解。

原创粉丝点击