双向循环链表的基本操作
来源:互联网 发布:蒋晓华卫星大数据 编辑:程序博客网 时间:2024/05/17 02:12
#ifndef _DCLIST_H #define _DCLIST_H #include<iostream> #include<assert.h> using namespace std; typedef int ElemType; typedef struct Node { ElemType data; struct Node *next; struct Node *prev; }Node; typedef struct List { Node *first; Node *last; size_t size; }List; void InitList(List *list); void push_back(List *list, ElemType x); void push_front(List *list, ElemType x); void show_list(List *list); bool pop_front(List *list); bool pop_back(List *list); int length(List *list); void clear(List *list); void destroy(List *list); bool sort(List *list); bool modify(List *list, ElemType key,ElemType x); ElemType Next(List *list , ElemType key); ElemType Prio(List *list , ElemType key); Node* find(List *list, ElemType key); bool delete_val(List *list, ElemType key); bool insert_val(List *list, ElemType x); bool resver(List *list); #endif #include"DCList.h" void InitList(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); list->first = list->last = s; list->first->prev = list->last; list->last->next = list->first; list->size = 0; } void push_back(List *list, ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->data = x; s->prev = list->last; list->last->next = s; list->last = s; list->last->next = list->first; list->first->prev = list->last; list->size++; } void push_front(List *list, ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->data = x; s->prev = list->first; s->next = list->first->next; list->first->next = s; s->next->prev = s; if(list->size == 0) list->last = s; list->size++; } bool pop_back(List *list) { if(list->size == 0) { return false; } Node *p = list->first; while(p->next != list->last) { p = p->next; } p->next = list->first ; free(list->last); list->last = p; list->last->next = list->first ; list->first->prev = list->last; list->size--; return true; } bool pop_front(List *list) { if(list->size == 0) { return false; } Node *p = list->first->next ; if(list->size == 1) { free(list->last); list->last = list->first ; list->first ->next = list->last; list->last->prev = list->first ; } else { list->first->next = p->next ; p->next ->prev = list->first ; free(p); } list->size--; return true; } void show_list(List *list) { Node *p = list->first->next; while(p != list->first) { cout<<p->data<<"-->"; p = p->next; } cout<<"Over!"<<endl; } Node* find(List *list, ElemType key) { Node *p = list->first->next; while(p != list->first && p->data != key) p = p->next; if(p != list->first) return p; return NULL; } bool delete_val(List *list, ElemType key) { Node *q = find(list,key); if(q == NULL) return false; if(q == list->last) list->last = q->prev; q->next->prev = q->prev; q->prev->next = q->next; free(q); list->size--; return true; } bool insert_val(List *list, ElemType x) { Node *p = find(list,x); if(p != NULL) return false; Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->data = x; p = list->first; while(p->next != list->first) { if(x<p->next->data) break; p = p->next; } s->next = p->next; p->next->prev = s; s->prev = p; p->next = s; if(p == list->last) { list->last = s; } list->size++; return true; } bool resver(List *list) { if(list->size == 0) { return false; } if(list->size == 1) { return true; } Node *p = list->first->next; Node *q = p->next; p->next = list->first; list->first->prev = p; list->last = p; while(q!=list->first) { p = q; q = q->next; p->next = list->first->next; p->next->prev = p; p->prev = list->first; list->first->next = p; } return true; } int length(List *list) { return list->size ; } void clear(List *list) { Node *p = list->first->next; while(p != list->first) { list->first->next = p->next; free(p); p = list->first->next; } list->first = list->last ; list->last->next = list->first; list->first->prev = list->last; list->size = 0; } void destroy(List *list) { clear(list); free(list->first); list->first = list->last = NULL; } bool sort(List *list)//数值不重复 { int count = list->size; Node *pre = list->first->next; Node *p = pre->next; if(list->size == 0) { return false; } if(list->size == 1) { return true; } while(count) { if(p->data >= pre->data ) { p = p->next ; pre = pre->next ; } else { ElemType item = p->data;p->data = pre->data ; pre->data = item; } count--; } return true; } bool modify(List *list, ElemType key,ElemType x) { Node *pos = find(list,key); if(pos == NULL) { return false; } pos->data = x; return true; } ElemType Prio(List *list , ElemType key) { Node* pos = find(list,key); if(pos == NULL) { return -1; } return pos->prev->data; } ElemType Next(List *list , ElemType key) { Node *pos = find(list,key); if(pos == NULL) { return -1; } return pos->next->data; } #include "DCList.h" void main() { List mylist; InitList(&mylist); int select = 1; ElemType item; ElemType pos; Node *p = NULL; while(select) { cout<<"************************************"<<endl; cout<<"* [0] quit_system [1] push_back *"<<endl; cout<<"* [2] push_front [3] show_seqlist*"<<endl; cout<<"* [4] pop_back [5] pop_front *"<<endl; cout<<"* [6] insert_val [7] delete_val *"<<endl; cout<<"* [8] find [9] modify *"<<endl; cout<<"* [10] sort [11] clear *"<<endl; cout<<"* [12] destroy [13] length *"<<endl; cout<<"* [14] resver [15] next *"<<endl; cout<<"* [16] prio *"<<endl; cout<<"************************************"<<endl; cout<<"请选择:>"; cin>>select; switch(select) { case 1: cout<<"请输入要插入的数据(-1结束):>"; while(cin>>item,item!=-1) { push_back(&mylist,item); } break; case 2: cout<<"请输入要插入的数据(-1结束):>"; while(cin>>item,item!=-1) { push_front(&mylist,item); } break; case 3: show_list(&mylist); break; case 4: pop_back(&mylist); break; case 5: pop_front(&mylist); break; case 6: cout<<"请输入要插入的值:>"; cin>>item; insert_val(&mylist,item); break; case 7: cout<<"请输入要删除的值:>"; cin>>item; delete_val(&mylist,item); break; case 8: cout<<"请输入要查找的值:>"; cin>>item; p = find(&mylist,item); cout<<"所在查找值的地址为:"<<p<<endl; break; case 9: cout<<"请输入要修改的值:>"; cin>>item; cout<<"请输入改变后的值:>"; cin>>pos; modify(&mylist,item,pos); break; case 10: sort(&mylist); break; case 11: clear(&mylist); break; case 12: destroy(&mylist); break; case 13: cout<<"单链表的长度为:"<<length(&mylist)<<endl; break; case 14: resver(&mylist); break; case 15: cout<<"请输入要查找的值:>"; cin>>item; cout<<"所在查找值的后继为:"<<Next(&mylist,item)<<endl; break; case 16: cout<<"请输入要查找的值:>"; cin>>item; cout<<"所在查找值的前驱为:"<<Prio(&mylist,item)<<endl; break; default: break; } } destroy(&mylist); }
0 0
- 双向循环链表的基本操作
- 双向循环链表的基本操作
- 双向循环链表的基本操作
- 双向循环链表的基本操作
- 双向循环链表的基本操作
- 循环双向链表的基本操作
- 不带头节点的双向循环链表基本操作
- 双向循环链表的头插尾插中间插基本操作
- 双向循环链表基本操作
- 双向循环链表基本操作
- 双向循环链表基本操作
- C语言单链表,双向链表,循环单链表,循环双链表的基本操作
- 双向循环链表基本操作(C语言)
- 双向循环链表的操作源码
- 双向循环链表操作的实现
- 双向循环链表操作的实现
- 双向循环链表的操作
- 不带头节点的双向循环链表的基本操作
- 设计模式之禅学习笔记--责任链模式
- 讲解SysWOW64文件夹
- IOS 遇到问题 记录
- java 计算百分比值
- linux块设备驱动
- 双向循环链表的基本操作
- mac 10.10.3 安装 php5.6.7和apache2.4.12
- popupWindow
- C++11模版元编程
- 高斯消元模板
- 多线程编程1 - NSThread
- 并发性能优化 – 降低锁粒度
- js随机跳转网址
- MATLAB 画图 入门