线性表实验
来源:互联网 发布:淘宝买家注册时间查询 编辑:程序博客网 时间:2024/06/05 00:07
顺序表
#include<iostream> using namespace std; const int Maxsize = 30; class SeqList { public: SeqList(){ length = 0; } SeqList(int a[], int n); ~SeqList(){} void Insert(int i, int x); int Delete(int i); int Locate(int x); void PrintList(); private: int data[Maxsize]; int length; }; SeqList::SeqList(int a[], int n) { if (n > Maxsize) throw "参数非法"; for(int i=0;i<n;i++) data[i] = a[i]; length = n; } void SeqList::Insert(int i, int x) { if (length >= Maxsize) throw "上溢"; if (i<1 || i>length + 1) throw"位置非法"; for (int j = length; j >= i; j--) data[j] = data[j - 1]; data[i - 1] = x; length++; } int SeqList::Delete(int i) { if (length == 0) throw"下溢"; if (i<1 || i>length) throw "位置非法"; int x = data[i - 1]; for (int j = i; j < length; j++) data[j - 1] = data[j]; length--; return x; } int SeqList::Locate(int x) { for (int i = 0; i<length; i++) if (data[i] == x) return i + 1; return 0; } void SeqList::PrintList() { for (int i = 0; i < length; i++) cout << data[i] << " "; cout << endl; } void main(){ int a[5] ={ 1, 2, 3, 4, 5}; SeqList A(a, 5); cout << "执行插入前的数据为:" << endl; A.PrintList(); A.Insert(2, 9); cout << "执行插入操作后的数据为:" << endl; A.PrintList(); cout << "值为9的元素的位置为:"; cout <<A.Locate(9) << endl; cout << "执行删除第一个元素操作,删除前的数据为:" << endl; A.PrintList(); A.Delete(1); cout << "删除后的数据为:" << endl; A.PrintList(); }
2单链表
#include<iostream> using namespace std; template<class T> struct Node { T data; Node<T>* next; }; template<class T> class LinkList { public: LinkList (); LinkList(T a[],int n); ~LinkList(); void Insert (int i,T x); int Locate(T x) ; T Delete(int i); void PrintList(); private: Node<T>* first; }; template<class T> LinkList<T>::LinkList() { first = new Node<T>; first->next=NULL; } template<class T> LinkList<T>::LinkList(T a[],int n) { Node <T> *r,*s; first = new Node<T>; r = first; for(int i=0;i<n;i++) { s = new Node<T>; s->data=a[i]; r->next =s; r=s; } r->next= NULL; } template<class T> LinkList<T>::~LinkList() { Node<T> *q = NULL; while(first != NULL) { q = first; first= first->next; delete q; } } template<class T> void LinkList<T>::Insert(int i,T x) { Node<T> *p = first,*s=NULL; int count = 0; while(p!=NULL&&count< i-1) { p = p->next; count++; } if(p==NULL) throw "位置非法"; else { s = new Node<T>; s->data =x; s->next=p->next; p->next= s; } } template<class T> T LinkList<T>::Delete(int i) { Node<T> *p =first,*q=NULL; T x; int count = 0; while(p!=NULL && count<i-1) { p = p->next; count++; } if(p==NULL ||p->next==NULL) throw"位置"; else { q=p->next; x=q->data; p->next=q->next; return x; } } template<class T> int LinkList<T>::Locate(T x) { Node<T>*p =first->next; int count =1; while(p!=NULL) { if(p->data==x) return count; p=p->next; count++; } return 0; } template<class T> void LinkList<T>::PrintList() { Node<T>*p =first-> next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; } void main(){ int a[5] = {1, 2, 3, 4, 5}; LinkList<int> A(a, 5); cout << "执行插入前的数据为:" << endl; A.PrintList(); A.Insert(2, 9); cout << "执行插入操作后的数据为:" << endl; A.PrintList(); cout << "值为9的元素的位置为:"; cout <<A.Locate(9) << endl; cout << "执行删除第一个元素操作,删除前的数据为:" << endl; A.PrintList(); A.Delete(1); cout << "删除后的数据为:" << endl; A.PrintList(); }
0 0
- 实验二:线性表实验
- 实验二:线性表实验
- 《数据结构》:实验 线性表实验
- 实验二:线性表实验
- 实验二线性表实验
- 实验二 线性表实验
- 实验二、 线性表实验
- 线性表实验
- 线性表实验
- 线性表的实验
- 线性表实验
- 线性表实验
- 线性表实验课
- 线性表实验
- 《数据结构》实验二:线性表的实验
- 《数据结构》实验二:线性表实验
- 《数据结构》实验二: 线性表实验
- 《数据结构》实验二 线性表的实验
- 修改oracle的process和session
- poj 3370 Halloween treats&&poj 2356 Find a multiple
- CocoaPods第三方库的管理找不到.h文件
- 程序流程控制
- selected下拉列表常用取值、赋值方法总结
- 线性表实验
- Github 个人笔记
- ⽤运营的思路来做无线产品测试-第13届BQConf上的分享
- oracle视图总结(创建、查询、修改、删除等)
- VC++中字符串编码的转换
- Codeforces Round #156 (Div. 2)---A. Greg's Workout
- [Node.js]14款最佳的Node.js Web框架
- DataGridView 行、列的隐藏和删除
- Ubuntu 14.04 源码安装Emacs 24.3