双链表实现
来源:互联网 发布:伊人独泪谁人知 编辑:程序博客网 时间:2024/06/06 12:22
一、实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二、实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
三、源代码
#include<iostream>const int MaxSize=20;using namespace std;template<typename T>struct Node{T data;Node<T> *prior,*next,*first;};template<typename T>class Linklist{private:Node<T> *prior,*next,*first;int length;public:Linklist();Linklist(T a[],int n);~Linklist();int Length();T Get(int i);//按位查找int Locate(T x);//按值查找void Insert(int i,T x);//插入T Delete(int i);//删除void PrintList();//遍历};template<typename T>Linklist<T>::Linklist(){first=new Node<T>;first->next=NULL;first->prior=NULL:}template<typename T>Linklist<T>::Linklist(T a[],int n){first=new Node<T>;first->next=NULL;for(int i=0;i<n;i++){Node<T> *s;s=new Node<T>;s->data=a[i];s->next=first->next;first->next=s;s->prior=first;}}template<typename T>Linklist<T>::~Linklist(){while(first!=NULL){Node<T> *q;q=first;first=first->next;delete q;}}template<typename T>int Linklist<T>::Length(){Node<T> *p;p=first->next;int count=0;while(p!=NULL){p=p->next;count++;}return count;}template<typename T>T Linklist<T>::Get(int i){ Node<T> *p;p=first->next;int count=1;while(p!=NULL&&count<i) {p=p->next;count++;}if(p==NULL) throw"位置";else return p->data;}template<typename T>int Linklist<T>::Locate(T x){Node<T> *p;p=first->next;int count=1;while(p!=NULL){if(p->data==x)return count;p=p->next;count++;}return 0;}template<typename T>void Linklist<T>::Insert(int i,T x){Node<T> *p;p=first;int count=0;while(p!=NULL&&count<i-1){p=p->next;count++;}if(p==NULL) throw"位置非法";else{Node<T> *s;s=new Node<T>;s->prior=p;s->next=p->next;p->next->prior=s;p->next=s;s->data=x;}}template<typename T>T Linklist<T>::Delete(int i){Node<T> *p;p=first;int count=0;while(p!=NULL&&count<i-1){p=p->next;count++;}if(p==NULL||p->next==NULL)throw"位置非法";else{T x;x=p->data;Node<T> *q;q=p->next;(q->prior)->next=q->next;(q->next)->prior=q->prior;delete q;return x;}}template<typename T>void Linklist<T>::PrintList(){Node<T> *p;p=first->next;while(p!=NULL){cout<<p->data;cout<<" ";p=p->next;}}void main(){int b[10]={90,98,99,94,89,87,86,80,84,82};Linklist<int>c(b,10);cout<<"显示所有学生的成绩"<<endl;c.PrintList();cout<<endl;cout<<"学生总数为:"<<c.Length()<<endl;cout<<"查找的第三位学生的成绩为:"<<endl;cout<<c.Get(3)<<endl;cout<<"查找成绩为99的学生:"<<endl;cout<<c.Locate(99)<<endl;cout<<"在第二个学生前插入成绩为78的学生"<<endl;try{c.Insert(2,78);}catch(char *s){cout<<s<<endl;}cout<<"插入后显示所有学生成绩"<<endl;c.PrintList();cout<<endl;cout<<"删除第一个学生成绩后剩下学生的成绩为:"<<endl;try{c.Delete(1);}catch(char *s){cout<<s<<endl;}c.PrintList();}四、运行结果
五、实验心得
因为双链表的求表长、按位查找等操作的实现与单链表的差不多,所以搞懂单链表后再去做双链表,会比较容易一点,花费的时间相对会短一点。但最后运行的时候,在插入成绩那里卡到了,后面删除的操作都运行不出来。检查了几遍插入Insert外函数的语句,都没有找到错误。最后放弃(烦到了),第二天早上才继续看双链表,想到可能是构造函数那里出错,最后加上s->prior=first,运行成功。还是要多加练习,多理解,才能减少错误。多练习。
阅读全文
0 0
- 双链表实现
- 双链表实现
- 双链表实现
- 双链表实现
- 双链表实现
- 双链表实现
- 双链表实现
- 双链表实现
- LRU实现(基于双链表和HashTable实现)
- 循环双链表的实现
- 数据结构实现循环双链表
- C语言实现双链表
- C实现双链表
- JAVA双链表的实现
- 双链表的实现
- 双链表的实现
- Java双链表的实现
- C实现循环双链表
- MySQL DELETE 语句整理
- 第四周 项目 3
- 11.错误记录(stl,resize)
- While() , do{}while(); , for(){} 循环语句的用法
- Linux学习1-分区&格式化(慕课网学习)
- 双链表实现
- 关于IE 6、7、8中透明度的兼容问题
- windows 系统监控工具和本地资源搜索软件
- 十进制小数转二进制
- 阿里云centos安装subversion记录
- 第二次作业
- JavaScript设计模式
- matlab之在输出的数组中加入空格
- 卡特兰数学习