Main.cpp文件:
#include"DList.h"
void main()
{
List mylist;
InitDList(&mylist);
ElemType Item;
int select = 1;
while (select)
{
printf("*************************************************\n");
printf(" [1] push_back [2] push_front *\n");
printf(" [3] show_list [4] pop_back *\n");
printf(" [5] pop_front [6] insert_val *\n");
printf(" [7] find [8] length *\n");
printf(" [9] delete_val [10] sort *\n");
printf(" [11] reserve [12] clear *\n");
printf(" [13*] destory [0] quit_system *\n");
printf("*************************************************\n");
printf("请选择:");
scanf("%d", &select);
if (select == 0)
break;
switch (select)
{
case 1:
printf("请输入要插入的数据(-1结束)");
while (scanf("%d", &Item), Item != -1)
{
push_back(&mylist, Item);
}
break;
case 2:
printf("请输入要插入的数据(-1结束)");
while (scanf("%d", &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:
printf("请输入要插入的数值"); //升序的顺序进行插入
scanf("%d", &Item);
insert_val(&mylist, Item);
break;
case 7:
printf("请输入要查找的数据");
scanf("%d", &Item);
Node* p;
p = find(&mylist, Item);
if (p == NULL)
{
printf("查找的数据在链表中不存在\n");
}
break;
case 8:
printf("链表的长度为:%d\n", length(&mylist));
break;
case 9:
printf("请输入要删除的数据:");
scanf("%d", &Item);
delete_val(&mylist, Item);
break;
case 10:
sort(&mylist);
break;
case 11:
reserve(&mylist);
break;
case 12:
clear(&mylist);
break;
case 13:
/*destory(&mylist);*/
break;
default:
printf("输入的命令错误,请重新输入!");
break;
}
}
destory(&mylist);
}
DList.h文件:
#ifndef _DLIST_H_
#define _DLIST_H_
#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#define ElemType int
typedef struct Node
{
ElemType data;
struct Node* next;
struct Node* prio;
}Node;
typedef struct List
{
Node* first;
Node* last;
size_t size;
}List;
void InitDList(List *list);
void push_back(List *list, ElemType x);
void push_front(List *list, ElemType x);
void show_list(List *list);
void pop_back(List *list);
void pop_front(List *list);
void insert_val(List *list, ElemType val);
Node* find(List *list, ElemType x);
int length(List *list);
void delete_val(List *list, ElemType x);
void sort(List *list);
void reserve(List *list);
void clear(List *list);
void destory(List *list);
///////////////////////////////////////////////////////////////////////
Node* buyNode(ElemType x);
#endif
Dlist.cpp文件:
#include"DList.h"
void InitDList(List *list)
{
Node* s = (Node *)malloc(sizeof(Node));
assert(s != NULL);
list->first = list->last = s;
list->last->next= NULL;
list->first->prio = NULL;
list->size = 0;
}
Node* buyNode(ElemType x)
{
Node* p = (Node *)malloc(sizeof(Node));
assert(p != NULL);
p->next = p->prio = NULL;
p->data = x;
return p;
}
void push_back(List *list, ElemType x)
{
Node* p = buyNode(x);
p->prio = list->last;
list->last->next = p;
list->last = p;
list->size++;
}
void push_front(List *list, ElemType x)
{
Node* p = buyNode(x);
if (list->first == list->last)
{
list->last = p;
}
else
{
p->next = list->first->next;
p->next->prio = p;
}
p->prio = list->first;
list->first->next = p;
list->size++;
}
void show_list(List *list)
{
Node* p = list->first->next;
while (p !=NULL)
{
printf("%d-->", p->data);
p = p->next;
}
printf("Null.\n");
}
void pop_back(List *list)
{
if (list->size == 0)
return;
Node* p = list->first;
while (p->next != list->last)
p = p->next;
free(list->last);
list->last = p;
list->last->next = NULL;
list->size--;
}
void pop_front(List *list)
{
if (list->size == 0)
return;
Node* p = list->first->next;
if (list->first->next==list->last)
{
list->last = list->first;
list->last->next = NULL;
}
else
{
p->next->prio = list->first;
list->first->next = p->next;
}
free(p);
list->size--;
}
void insert_val(List *list, ElemType val) //按升序插入
{
Node* p = buyNode(val);
Node* s = list->first;
while (s->next != NULL&&s->next->data < val)
s = s->next;
if (s->next==NULL) //尾插
{
//push_back(list,x);
p->prio = list->last;
list->last->next = p;
list->last = p;
list->last->next = NULL;
}
else
{
p->next = s->next;
p->next->prio = p;
p->prio = s;
s->next = p;
}
list->size++;
}
Node* find(List *list, ElemType x)
{
Node* p = list->first->next;
while (p != NULL&&p->data != x)
p = p->next;
return p;
}
int length(List *list)
{
return list->size;
}
void delete_val(List *list, ElemType x)
{
if (list->size == 0)
return;
Node* p = find(list, x);
if (p == NULL)
{
printf("要删除的数据不存在!\n");
return;
}
if (p == list->last)
{
list->last=p->prio ;
list->last->next = NULL;
}
else
{
p->next->prio = p->prio;
p->prio->next = p->next;
}
free(p);
list->size--;
}
void sort(List *list)
{
if (list->size == 0 || list->size == 1)
return;
Node* s = list->first->next;
Node* q = s->next;
list->last = s;
list->last->next = NULL;
while (q != NULL)
{
s = q;
q = q->next;
Node* p = list->first;
while (p->next != NULL&&p->next->data < s->data)
p = p->next;
if (p->next == NULL) //尾插
{
s->next = NULL;
s->prio = list->last;
list->last->next = s;
list->last = s;
}
else
{
s->next = p->next;
s->next->prio = s;
s->prio = p;
p->next = s;
}
}
}
void reserve(List *list)
{
if (list->size == 0 || list->size == 1)
return;
Node* s = list->first->next;
Node* q = s->next;
list->last = s;
list->last->next = NULL;
//头插
while (q != NULL)
{
s = q;
q = q->next;
s->next = list->first->next;
s->next->prio = s;
s->prio = list->first;
list->first->next = s;
}
}
void clear(List *list)
{
if (list->size == 0)
return;
Node* p = list->first->next;
while (p != NULL)
{
if (p == list->last)
{
list->last = list->first;
list->last->next = NULL;
}
else
{
p->next->prio=list->first;
list->first->next=p->next;
}
free(p);
p = list->first->next;
}
list->size == 0;
}
void destory(List *list)
{
clear(list);
free(list->first);
list->first = list->last = NULL;
}