实验二——双向链表
来源:互联网 发布:python sum axis 1 编辑:程序博客网 时间:2024/06/06 15:49
一、实验目的
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二、 实验内容
巩固线性表的数据结构的存储方法和相关操作,学会针对具体应用,使用线性表的相关知识来解决具体问题。
二、 实验内容
建立一个由n个学生成绩的顺序表,n的大小由自己确定,每一个学生的成绩信息由自己确定,实现数据的对表进行插入、删除、查找等操作。分别输出结果。
(用双向链表实现)
三、源代码
DulList.h
#ifndef DulList_H #define DulList_H struct Node { int data; Node *prior,*next; }; class DulList { public: DulList(int length); ~DulList(){} void Insert(int i,int x); int Delete(int i); int Locate(int x); int Get(int i); void PrintList();void PrintListRE();private: Node *phead,*ptail; int length; }; #endifDulList.cpp
#include <iostream>using namespace std;#include "DulList.h"DulList::DulList(int length){this->length=length;phead=new Node;phead->prior=NULL;ptail=phead;for(int i=0;i<length;i++){Node *t=new Node;cout<<"the no." << i+1 << " Node's data:";cin>>t->data;t->next=NULL;t->prior=ptail;ptail->next=t;ptail=t;}}int DulList::Locate(int x) { Node *p=phead->next; int count=1; while(p!=NULL) { if(p->data==x) return count; p=p->next; count++; } return 0; } int DulList::Get(int i){Node *p =phead->next;if(i>length||i<1) return -1;for(int j= 0;j<i-1;j++) p = p->next;return p->data;}void DulList::Insert(int i,int x){Node *p=phead;for(int j=0;j<i;j++) p=p->next;Node *t=new Node;t->data=x;t->prior=p->prior;p->prior->next=t;t->next=p;p->prior=t;length++;}int DulList::Delete(int i){Node *p=phead->next;if(i>length||i< 1) return -1;for (int j=0;j<i-1;j++) p=p->next;p->prior->next=p->next;p->next->prior=p->prior;Node *d=p;delete p;length--;return d->data;}void DulList::PrintList(){Node *p=phead->next;while(p!=NULL){cout<<p->data<<" ";p=p->next;}cout<<endl;}void DulList::PrintListRE(){Node *p=ptail;while (p->prior!=NULL){cout<<p->data<<" ";p=p->prior;}cout<<endl;}DulList_main.cpp
#include <iostream>using namespace std;#include "DulList.h"int main(){DulList D(10);cout<<"所有成绩"<<endl;cout<<"正向遍历:";D.PrintList();cout<<"反向遍历:";D.PrintListRE();cout<<"第6个学生成绩:"<<D.Get(6)<<endl;cout<<"成绩为84的学生位置:"<<D.Locate(84)<<endl;cout<<"在第四个位置插入学生成绩100: ";D.Insert(4,100);D.PrintList();cout<<"删除第三个学生成绩: ";D.Delete(3);D.PrintList();}结果
阅读全文
0 0
- 实验二——双向链表
- 数据结构实验 双向链表
- Linux双向链表(二)——逻辑判断
- 双向链表——单链表改进之二
- 实验二 循环链表和双向链表的设计和实现
- 实验二——静态链表
- SDUT—2054数据结构实验之链表九:双向链表 (基本建立)
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 数据结构实验之链表九:双向链表
- 机器学习元知识
- Linux系统虚拟机的安装原理
- 普通工厂模式and抽象工厂模式
- Java的安全组成
- Ⅴ vue2.0 项目实战
- 实验二——双向链表
- oracle数据库写入数据的过程
- 【CodeForces
- 讲解基本自然语言处理NLP
- Apache Maven Assembly插件详解
- c primer plus 第十一章学习小结
- springmvc异常StandardWrapper.Throwable
- map中[]运算符和find的区别
- Map集合