对循环双链表实现下述功能:
void meau(); //菜单函数 void Initlist(List *list); //初始化 void show(List *list); //打印链表内容 bool Push_back(List *list,ElemType x); //尾插法 bool Push_front(List *list,ElemType x);//头插法 bool Isempty(List *list); //判断链表是否为空 bool Pop_back(List *list); //尾删法 bool Pop_front(List *list); //头删法 Node *Find_val(List *list,ElemType x); //按值查找 bool Delete_val(List *list,ElemType x);//按值删除 bool modify(List *list,ElemType x); //修改 void clear(List *list); //清空链表 void destory(List *list); //摧毁链表 void reverse(List *list); //逆置链表 Node *prio(List *list,ElemType x); //求某个值的前驱 Node *next(List *list,ElemType x); //求某个值的后继 bool Insert_val(List *list,ElemType x);//按值插入 void sort(List *list); //排序(升序)
DCList.h:
#ifndef __DCLIST_H__ #define __DCLIST_H__ #include<assert.h> #include<iostream> using namespace std; typedef int ElemType; typedef struct Node { struct Node *pre; struct Node *next; ElemType data; }Node; typedef struct List { Node *first; Node *last; int size; }List; void meau(); //菜单函数 void Initlist(List *list); //初始化 void show(List *list); //打印链表内容 bool Push_back(List *list,ElemType x); //尾插法 bool Push_front(List *list,ElemType x);//头插法 bool Isempty(List *list); //判断链表是否为空 bool Pop_back(List *list); //尾删法 bool Pop_front(List *list); //头删法 Node *Find_val(List *list,ElemType x); //按值查找 bool Delete_val(List *list,ElemType x);//按值删除 bool modify(List *list,ElemType x); //修改 void clear(List *list); //清空链表 void destory(List *list); //摧毁链表 void reverse(List *list); //逆置链表 Node *prio(List *list,ElemType x); //求某个值的前驱 Node *next(List *list,ElemType x); //求某个值的后继 bool Insert_val(List *list,ElemType x);//按值插入 void sort(List *list); //排序(升序) #endifDCList.cpp:
#include"DCList.h" /*菜单函数*/ void meau() { cout<<"*****************SeqList********************"<<endl; cout<<" ---zyh_helen"<<endl; cout<<"*[1]InitSeqList [2]Push_back *"<<endl; cout<<"*[3]Push_front [4]Pop_back *"<<endl; cout<<"*[5]Pop_front [6]Find_val *"<<endl; cout<<"*[7]show [0]Quit_syatem*"<<endl; cout<<"*[8]modify [9]Delete_val *"<<endl; cout<<"*[10]clear [11]destory *"<<endl; cout<<"*[12]reverse [13]prio *"<<endl; cout<<"*[14]next [15]sort *"<<endl; cout<<"*[16]Insert_val *"<<endl; } /*初始化链表*/ void Initlist(List *list) { Node *s = (Node *)malloc(sizeof(Node)); assert(s != NULL); s->next = s->pre = s; list->first = list->last = s; list->size = 0; } /*尾插法*/ bool Push_back(List *list,ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); if(s != NULL) { s->data = x; list->last->next = s; s->pre = list->last; list->last = s; list->last->next = list->first; list->first->pre = list->last; //s->next = list->first; //list->last = s; list->size++; return true; } else return false; } /*打印链表内容*/ void show(List *list) { Node *s = list->first->next; while(s != list->first) { cout<<s->data<<"-->"; s = s->next; } cout<<"NULL"<<endl; } /*头插法*/ bool Push_front(List *list,ElemType x) { Node *s = (Node *)malloc(sizeof(Node)); if(s != NULL) { s->data = x; /*和后面元素(原来的第一个元素)连接*/ s->next = list->first->next; list->first->next->pre = s; /*和头结点连接*/ list->first->next = s; s->pre = list->first; /*若是第一个节点,尾指针该指向它*/ if(list->size == 0) { list->last = s; } list->size++; return true; } else { cout<<"申请空间失败!"<<endl; return false; } } /*判断链表是否为空*/ bool Isempty(List *list) { return (list->size == 0); } /*尾删法*/ bool Pop_back(List *list) { if(Isempty(list)) { cout<<"链表已空!"<<endl; return false; } Node *s = list->last->pre;//找到最后一个节点的前驱 free(list->last);//释放最后一个节点 /*连接*/ s->next = list->first; list->last = s; list->size--; return true; } bool Pop_front(List *list) { if(Isempty(list)) { cout<<"链表已空!"<<endl; return false; } Node *s = list->first->next;//找到第一个节点(要释放的) /*连接*/ list->first->next = s->next; s->next->pre = list->first; /*如果要删除的结点是第一个节点,尾指针要改变指向*/ if(list->size == 1) { list->last = list->first; } free(s); list->size--; return true; } /*查找函数:找到指定元素:返回指向它的指针,找不到:返回NULL*/ Node *Find_val(List *list,ElemType x) { Node *s = list->first->next; while(s != list->first) { if(x == s->data) return s; else s = s->next; } return NULL; } /*按值删除结点*/ bool Delete_val(List *list,ElemType x) { Node *s = Find_val(list,x); if(s != NULL) { /*连接:头删,尾删,都适用*/ s->pre->next = s->next; s->next->pre = s->pre; /*释放*/ free(s); list->size--; /*如果释放的是最后一个节点,尾指针需要改变指向,其余情况无需改变*/ if(s == list->last) { list->last = s->pre; } return true; } else { cout<<"the item is not exist!"<<endl; return false; } } /*bool Delete_val(List *list,ElemType x) { Node *s = Find_val(list,x); if(s != NULL) { if(s == list->first->next) { Pop_front(list); } else if(s == list->last)//为什么没加else,删除头结点会出错??? { Pop_back(list); } else { s->pre->next = s->next; s->next->pre = s->pre; free(s); list->size--; } return true; } else { cout<<"the item is not exist!"<<endl; return false; } }*/ /*修改链表中指定的值*/ bool modify(List *list,ElemType x) { Node *s = Find_val(list,x); ElemType item; if(s != NULL) { cout<<"please input a new item:"; cin>>item; s->data = item; return true; } else { cout<<"the item is not exist!"<<endl; return false; } } /*清空链表*/ void clear(List *list) { Node *s = list->first->next;//s总是指向链表中的第一个节点 while(s != list->first) { list->first->next = s->next;//将链表中的第一个结点空出来 free(s);//释放第一个节点 s = list->first->next;//重新指向新的第一个节点 } list->last = list->first; list->size = 0; } /*销毁链表*/ void destory(List *list) { clear(list); free(list->first); list->first = list->last = NULL; } /*链表逆置:保留第一个结点,将剩余的结点游离出来,然后依次头插到保留的结点中*/ void reverse(List *list) { Node *s = list->first->next;//第一个结点 Node *p = s->next;//分离出剩余的结点 /*第一个结点逆置后成为最后一个结点*/ s->next = list->first; list->last = s; while(p != list->first) { s = p; //保存游离出来的第一个节点 p = p->next;//为下一次头插做准备 /*将游离出的第一个节点--->头插-->进去*/ s->next = list->first->next; list->first->next->pre = s; s->pre = list->first; list->first->next = s; /* Push_front(list,s->data); free(s);调用Push_front函数,会自动创建结点,所以得释放原来的结点 */ } } /*求指定元素的前驱*/ Node *prio(List *list,ElemType x) { Node *s = Find_val(list,x); if(s != NULL) { if(s == list->first->next) { cout<<"it doesn't have prio!"<<endl; } else { return s->pre; } } else { cout<<"the item is not exist!"<<endl; return NULL; } } /*求指定元素的后继*/ Node *next(List *list,ElemType x) { Node *s = Find_val(list,x); if(s != NULL) { if(s == list->last) { cout<<"it doesn't have next!"<<endl; } else { return s->next; } } else { cout<<"the item is not exist!"<<endl; return NULL; } } /*按值插入:若插入的值已经存在,返回NULL,否则将值插入(假设链表数据有序:升序)*/ bool Insert_val(List *list,ElemType x) { Node *s = Find_val(list,x); if(s != NULL) { cout<<"the item is already exist!"<<endl; return false; } /*创建要插入的结点*/ Node *p = (Node *)malloc(sizeof(Node)); p->data = x; /*查找要插入位置的前驱*/ s = list->first; while(s != list->last) { if(s->next->data > x) break;//要插入的位置找到(在s之后插入) else s = s->next; } /*插入*/ p->next = s->next; s->next->pre = p; p->pre = s; s->next = p; /*若要插入位置的前驱是最后一个结点,即尾插,则尾指针需要改变指向*/ if(s == list->last) { list->last = p; } list->size++; return true; } /*升序*/ void sort(List *list) { Node *s = list->first->next;//第一个结点 Node *p = s->next;//分离出剩余的结点 /*第一个结点逆置后成为最后一个结点*/ s->next = list->first; list->last = s; while(p != list->first) { s = p; p = p->next; /*将游离出来的第一个节点按值插入到保存结点中*/ Insert_val(list,s->data); free(s);//调用Insert_val()会创建结点,所以应该原来的结点释放 } }
main.cpp:
#include"DCList.h" int main() { List mylist; Node *s; Initlist(&mylist); ElemType item; int choice = 1; while(choice) { meau(); cout<<"input you choice:"<<endl; cin>>choice; switch(choice) { case 1: Initlist(&mylist); break; case 2: cout<<"input the item you want to push_back:-1 as a end"<<endl; while(cin>>item,item != -1) Push_back(&mylist,item); break; case 3: cout<<"input the item you want to push_back:-1 as a end"<<endl; while(cin>>item,item != -1) Push_front(&mylist,item); break; case 4: Pop_back(&mylist); break; case 5: Pop_front(&mylist); break; case 6: cout<<"input the item you want to find:"<<endl; cin>>item; Find_val(&mylist,item); if(Find_val(&mylist,item) != NULL) cout<<"the item is found!"<<endl; else cout<<"the item is not exist:"<<endl; break; case 7: show(&mylist); break; case 8: cout<<"input the item you want to modify:"<<endl; cin>>item; modify(&mylist,item); break; case 9: cout<<"input the item you want to delete:"<<endl; cin>>item; Delete_val(&mylist,item); break; case 10: clear(&mylist); break; case 11: destory(&mylist); break; case 12: reverse(&mylist); break; case 13: cout<<"input the item you want to find it's prio:"<<endl; cin>>item; s = prio(&mylist,item); if(s != NULL) cout<<"it's prio is:"<<s->data<<endl; break; case 14: cout<<"input the item you want to find it's next:"<<endl; cin>>item; s = next(&mylist,item);; if(s != NULL) cout<<"it's next is:"<<s->data<<endl; break; case 15: sort(&mylist); break; case 16: cout<<"input the item you want to insert:"<<endl; cin>>item; Insert_val(&mylist,item); break; default: break; } } destory(&mylist); return 0; }
具体功能:望读者自行测试,如有错误欢迎提出修改意见----->>>zyh_helen
原文地址:http://blog.csdn.net/zongyinhu/article/details/45490133