单链表的基本操作
来源:互联网 发布:个人陈述 知乎 编辑:程序博客网 时间:2024/05/18 23:26
头文件#ifndef _LIST_H #define _LIST_H #include<iostream> #include<assert.h> using namespace std; #define ElemType int typedef struct Node { ElemType data; struct Node *next; }Node, *PNode; typedef struct List { PNode first; PNode last; size_t size; }List; void InitList(List *list);//初始化 bool push_back(List *list, ElemType x);//尾插 bool push_front(List *list, ElemType x);//头插 bool pop_back(List *list);//尾删 bool pop_front(List *list);//头删 Node* Find(List *list, ElemType key);//按值找地址 Node* FindPrio(List *list, ElemType key);//按值找前驱地址 bool insert_val(List *list, ElemType key,ElemType x);//按值找位置并插入值 bool delete_val(List *list, ElemType key);//按值找值删除 bool modify(List *list, ElemType key,ElemType x);//按值找值修改 void ShowList(List *list);//打印 void clear(List *list);//清除 void destroy(List *list);//摧毁 void resver(List *list);//逆置(值不相同) int length(List *list);//长度 ElemType Next(List *list , ElemType key);//后继的值 ElemType Prio(List *list , ElemType key);//前驱的值 void sort(List *list);//排序 #endif 各个函数的定义#include "List.h"void InitList(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->next = NULL; list->first = list->last = s; list->size = 0; } Node* Find(List *list, ElemType key) { Node *p = list->first->next; while(p != NULL && p->data != key) { p = p->next; } return p; } bool push_back(List *list, ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); if(s == NULL) { return false; } s->data = x; s->next = NULL; list->last ->next = s; list->last = s; list->size++; return true; } bool push_front(List *list, ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); if(s == NULL) { return false; } s->data = x; s->next = list->first->next ; list->first->next = s; if(list->size == 0) { list->last = s; } list->size ++; return true; } bool pop_back(List *list) { if(list->size == 0) { return false; } Node *pre = list->first ; while(pre->next != list->last) { pre = pre->next ; } pre->next = NULL; free(list->last); list->last = pre; 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(p); list->first->next = NULL; list->last = list->first ; } else { list->first->next = p->next ; free(p); } list->size--; return true; } void ShowList(List *list) { Node *p = list->first ->next ; while(p != NULL) { cout<<p->data <<"-->"; p = p->next ; } cout<<"NULL"<<endl; } bool insert_val(List *list, ElemType key,ElemType x) { Node *pos = Find(list,key); if(pos == NULL) { return false; } Node *s = (Node *)malloc(sizeof(Node)); s->data = x; s->next = pos->next ; pos->next = s; return true; }bool delete_val(List *list, ElemType key) { Node *pos = FindPrio(list,key); if(pos == NULL) { return false; } Node *s = pos->next; pos->next = s->next; free(s); list->size--; return true; } Node* FindPrio(List *list, ElemType key) { Node *pre = list->first; Node *p = pre->next ; while(p !=NULL && p ->data!=key) { p = p->next; pre = pre->next; } return pre; }void clear(List *list) { Node *p = list->first->next; while(p != NULL) { list->first->next = p->next; free(p); p = list->first->next; } list->first = list->last ; list->size = 0; } bool modify(List *list, ElemType key,ElemType x) { Node *pos = Find(list,key); if(pos == NULL) { return false; } pos->data = x; return true; }void destroy(List *list) { clear(list); free(list->first); list->first = list->last = NULL; } int length(List *list) { return list->size; } void resver(List *list)//数值不重复 { Node* s = (Node*)malloc(sizeof(Node)); if(list->size == 0) { return ; } if(list->size == 1) { cout<<"逆置后值为:"<<list->last->data <<endl; } s->next = list->last ; while(list->first->next != list->last) { list->last->next = FindPrio(list,list->last->data); list->last = list->last->next; } list->last->next = NULL; free(list->first); list->first = s; } ElemType Next(List *list , ElemType key) { Node *pos = Find(list,key); if(pos == NULL) { return -1; } return pos->next->data; } ElemType Prio(List *list , ElemType key) { Node* pos = FindPrio(list,key); if(pos == NULL) { return -1; } return pos->data; } void sort(List *list)//数值不重复 { int count = list->size; Node *pre = list->first->next; Node *p = pre->next; if(list->size == 0) { return ; } if(list->size == 1) { cout<<"排序后为:"<<list->last->data<<endl; } 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--; }}测试函数#include "List.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] findprio [9] modify *"<<endl; cout<<"* [10] sort [11] clear *"<<endl; cout<<"* [12] destroy [13] length *"<<endl; cout<<"* [14] resver [15] next *"<<endl; cout<<"* [16] prio [17] find *"<<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: ShowList(&mylist); break; case 4: pop_back(&mylist); break; case 5: pop_front(&mylist); break; case 6: cout<<"请输入在何值后插入值:>"; cin>>item; cout<<"请输入要插入的值:>"; cin>>pos; insert_val(&mylist,item,pos); break; case 7: cout<<"请输入要删除的值:>"; cin>>item; delete_val(&mylist,item); break; case 8: cout<<"请输入要查找的值:>"; cin>>item; p = FindPrio(&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; case 17: cout<<"请输入要查找的值:>"; cin>>item; p = Find(&mylist,item); cout<<"所在查找值的地址为:"<<p<<endl; break; default: break; } }}
0 0
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作!
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- 单链表的基本操作
- Android 判断程序处于前台或者后台
- linux下socket编程实例
- HDU 3341 Lost's revenge
- 我的第一个svm程序:手写字识别
- DAO设计模式
- 单链表的基本操作
- HDU 3247 Resource Archiver
- C++中随机函数rand()和srand()的用法
- 第三章:UVa227 解题报告
- poj 杂题 - 1083 Moving Tables
- mixin
- 特殊数据类型成员变量的初始化(转)
- Shell基础-Bash变量-环境变量(上)
- Objective-C中设计模式总结