双层循环链表
头文件DCList.h
#ifndef __DCLIST_H__
#define __DCLIST_H__
#include<iostream>
#include<assert.h>
using namespace std;
#define ElemType int
typedef struct ListNode
{
ElemType data;
ListNode *prio;
ListNode *next;
}ListNode;
typedef struct List
{
ListNode *first;
ListNode *last;
size_t size;
}List;
//
void InitList(List *list);
bool push_back(List *list,ElemType x);
void show_list(List *list);
bool push_front(List *list,ElemType x);
bool pop_back(List *list);
bool insert_val(List *list,ElemType x);
bool delete_val(List *list,ElemType x);
ListNode* find_key(List *list,ElemType x);
void clear_list(List *list);
void destory_list(List *list);
void reverse_list(List *list);
void sort_list(List *list);
//
void InitList(List *list)
{
ListNode *s = (ListNode *)malloc(sizeof(ListNode));
assert(s != NULL);
list->first=list->last=s;
list->first->prio = list->last;
list->last->next = list->first;
list->size = 0;
}
bool push_back(List *list,ElemType x)
{
ListNode *s = (ListNode *)malloc(sizeof(ListNode));
if(s == NULL)
{
return false;
}
s->data = x;
list->last->next = s;
s->prio = list->last;
list->last = s;
list->last->next = list->first;
list->first->prio = list->last;
list->size++;
return true;
}
void show_list(List *list)
{
ListNode *p = list->first->next;
while(p != list->first)
{
cout<<p->data<<"->";
p = p->next;
}
cout<<"Over!"<<endl;
}
bool push_front(List *list,ElemType x)
{
ListNode *s = (ListNode *)malloc(sizeof(ListNode));
if(s == NULL)
{
return false;
}
s->data = x;
s->next = list->first->next;
s->next->prio = s;
s->prio = list->first;
list->first->next = s;
if(list->last == list->first)
{
list->last = s;
}
list->size++;
return true;
}
ListNode* find_key(List *list,ElemType x)
{
if(list->size == 0)
{
return NULL;
}
ListNode *p = list->first->next;
while(p != list->first && p->data != x)
{
p = p->next;
}
if(p == list->first)
{
return NULL;
}
return p;
}
bool delete_val(List *list,ElemType x)
{
if(list->size == 0)
{
return false;
}
ListNode *p = find_key(list,x);
if(p == NULL)
{
return false;
}
p->next->prio = p->prio;
p->prio->next = p->next;
if(p == list->last)
{
list->last = p->prio;
}
free(p);
list->size--;
return true;
}
bool insert_val(List *list,ElemType x)
{
if(list->size == 0)
{
return false;
}
ListNode *s = (ListNode *)malloc(sizeof(ListNode));
if(s == NULL)
{
return false;
}
s->data = x;
ListNode *p = list->first->next;
while(s->data >= p->data &&p->next != list->first)
{
p = p->next;
}
p->prio->next = s;
s->next = p;
list->size++;
}
#endif
源文件
DCList.cpp
#include"DCList.h"
#include<iostream>
using namespace std;
int main()
{
ListNode *p = NULL;
List mylist;
InitList(&mylist);
int select = 1;
ElemType item;
//int pos;
while(select){
cout<<"**************************************************************"<<endl;
cout<<"*[1]push_back [2]push_front *"<<endl;
cout<<"*[3]show_list [0]quit_system *"<<endl;
cout<<"*[4]pop_back [5]pop_front *"<<endl;
cout<<"*[6]insert_pos [7]insert_val *"<<endl;
cout<<"*[8]delete_pos [9]delete_val *"<<endl;
cout<<"*[10]find_key [11]length *"<<endl;
cout<<"*[12]reverse_list [13]sort *"<<endl;
cout<<"*[14]clear_list [15]destory_list *"<<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>>pos;
//cout<<"请输入要插入的值:>";
//cin>>item;
//insert_pos(&mylist,pos,item);
case 7:
cout<<"请输入要插入的值"<<endl;
cin>>item;
insert_val(&mylist,item);
break;
case 8:
//cout<<"请输入要删除的位置:>";
//cin>>pos;
//delete_pos(&mylist,pos);
break;
case 9:
cout<<"请输入要删除的值:>";
cin>>item;
delete_val(&mylist,item);
break;
case 10:
cout<<"请输入要找到的值:>";
cin>>item;
p = find_key(&mylist,item);
if(p == NULL)
{
cout<<"要查找的值:"<<item<<"不存在!"<<endl;
}
break;
case 11:
//cout<<"SeqList Length = "<<Length(&mylist)<<endl;
break;
case 12:
// reverse_list(&mylist);
case 13:
// sort_list(&mylist);
break;
case 14:
// clear_list(&mylist);
break;
case 0:
break;
}
system("pause");
system("cls");
}
//destory_list(&mylist);
return 0;
}