循环链表
来源:互联网 发布:手机淘宝延长收货时间 编辑:程序博客网 时间:2024/06/03 21:21
#include<iostream> using namespace std; struct Node { int data; Node *rear,*next; }; class CircleList { private: Node *first; public: CircleList(); CircleList(int a[],int n); ~CircleList(); int Locate(int x); void Insert(int i,int x); int Delete(int i); void PrintList(); }; CircleList::CircleList() { first=new Node; first->next=NULL; } CircleList::CircleList(int a[],int n) { Node *r,*s; first=new Node; r=first; for(int i=0;i<n;i++) { s=new Node; s->data=a[i]; s->next=NULL; r->next=s; s->rear=r; r=s; } r->next=NULL; } CircleList::~CircleList() { Node *q=NULL; while(first!=NULL) { q=first; first=first->next; delete q; } } void CircleList::Insert(int i,int x) { /*Node *p=first,*s=NULL,*rear; int count=1; while(p!=NULL&&count<i-1) {p=p->next; count++;} if(p==NULL)throw"位置"; else{ s=new Node;s->data=x; s->rear=p; s->next=p->next; p->next->rear=s; p->next=s; */Node *p=first,*s=NULL,*rear; p = first->next; int count = 1; while (p != first && count < i-1) { p = p->next; count++; } if (p == first)throw "插入位置异常"; else { s = new Node; s->data = x; s->next = p->next; p->next = s; length++; } } int CircleList::Delete(int i) { Node *p=first; int count=0; while(p!=NULL&&count<i) { p=p->next; count++; } if(p==NULL) throw"位置"; else{ (p->rear)->next=p->next; (p->next)->rear=p->rear; free(p); } return 0; } int CircleList::Locate(int x) { Node *p=first->next; int count=1; while(p!=NULL) { if(p->data==x) return count; p=p->next; count++; } return 0; } void CircleList::PrintList() { Node *p=first->next; while(p!=NULL) { cout<<p->data<<" "; p=p->next; } cout<<endl; } int main() { int r[5]={88,92,52,68,78}; CircleList L(r,5); cout<<"执行插入操作前数据为:"<<endl; L.PrintList(); cout<<"在第三个位置插入成绩93"<<endl; try{L.Insert(3,78);} catch(char *s) { cout<<s<<endl; } cout<<"执行插入操作后数据为:"<<endl; L.PrintList(); cout<<"成绩为92的学生位置为:"; cout<<L.Locate(92)<<endl; cout<<"执行删除操作前数据为:"<<endl; L.PrintList(); cout<<"删除第一个成绩"<<endl; try { L.Delete(1); } catch(char *s) { cout<<s<<endl; } cout<<"执行删除操作后数据为:"<<endl; L.PrintList(); return 0; }
阅读全文
0 0
- 循环链表实现循环队列
- 循环链表与循环队列
- 循环链表
- 循环链表
- 双向循环链表
- 双向循环链表
- 循环链表实验
- 链表::循环链表
- 双向循环链表
- 双向循环链表
- 循环链表
- 循环链表
- 循环链表
- 循环链表建立
- 数据结构 循环链表
- 双向循环链表
- 循环链表详解
- 循环链表
- (刷题日志)最小生成树
- CSS hack
- 1087 1 10 100 1000
- 初赛日志
- Docker原理
- 循环链表
- linux中查看nginx、apache、php、mysql配置文件路径
- 在64-bit 驱动中支持32-bit I/O 操作
- 命令模式
- 证券交易中各类词汇的解释
- 初识SDR
- Java学习之路——数组排序法(冒泡、直选、插入、反转)
- 利用reduce端的GroupingComparator来实现将一组bean看成相同的key
- codevs 1961 躲避大龙 bfs