单链表的基本操作
2015-04-30 00:04
363 查看
单链表所尊崇的结点如图
所实现的功能如下:
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 sort(List *list);//排序
void resver(List *list);//逆置(值不相同)
int length(List *list);//长度
ElemType Next(List *list , ElemType key);//后继的值
ElemType Prio(List *list , ElemType key);//前驱的值
具体的代码如下:
头文件:
#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 sort(List *list);//排序 void resver(List *list);//逆置(值不相同) int length(List *list);//长度 ElemType Next(List *list , ElemType key);//后继的值 ElemType Prio(List *list , ElemType key);//前驱的值 #endif
函数定义文件:
<span style="font-size:18px;">#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; } 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; } Node* Find(List *list, ElemType key) { Node *p = list->first->next; while(p != NULL && p->data != key) { p = p->next; } return p; } 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; } 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; } bool modify(List *list, ElemType key,ElemType x) { Node *pos = Find(list,key); if(pos == NULL) { return false; } pos->data = x; return true; } void ShowList(List *list) { Node *p = list->first ->next ; while(p != NULL) { cout<<p->data <<"-->"; p = p->next ; } cout<<"NULL"<<endl; } void clear(List *list) { Node *p = list->first->next; while(p != NULL) { list->first->next = p->next; free(p); p = list->first->next; } list->last = list->first ; list->size = 0; } void destroy(List *list) { clear(list); free(list->first); list->first = list->last = NULL; } 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--; } } 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; } int length(List *list) { return list->size; } ElemType Prio(List *list , ElemType key) { Node* pos = FindPrio(list,key); if(pos == NULL) { return -1; } return pos->data; } ElemType Next(List *list , ElemType key) { Node *pos = Find(list,key); if(pos == NULL) { return -1; } return pos->next->data; }</span>
测试文件:
<span style="font-size:18px;">#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; } } } </span>
对于具体的操作,测试文件写得很清楚,在这里便不用截图说明了。代码有什么问题,希望大家可以指出,谢谢。
注:对于上述代码进行改进
1.bool insert_val(List *list, ElemType x)
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 != NULL) { if(x < p->next->data) { break; } p = p->next ; } s->next = p->next ; p->next = s; if(p == list->last) { list->last = s; } list->size++; return true; }
此函数在这时是按照插入的数来自己确定位置插入的。
测试函数如下:
cout<<"请输入要插入的值:>"; cin>>item; insert_val(&mylist,item);
2.bool resver(List *list)
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 = NULL; list->last = p; while(q != NULL) { p = q; q = q->next; p->next = list->first->next; list->first->next = p; } return true; }