一、Sysutil.h//系统头文件
#ifndef _SYSUTIL_H_
#define _SYSUTIL_H_
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<stdbool.h>
#pragma warning(disable:4996)
#include<vld.h> //内存泄漏工具的头文件
#endif
二、DCList.h//函数实现头文件
#include"Sysutil.h"
typedef int ElemType;
typedef struct DCListNode
{
ElemType data;
struct DCListNode *prev;
struct DCListNode *next;
}DCListNode;
typedef struct DCList
{
DCListNode *first;
DCListNode *last;
size_t size;
}DCList;
DCListNode* _BuyDCListNode(ElemType x)
{
DCListNode *s = (DCListNode*)malloc(sizeof(DCListNode));
assert(s != NULL);
s->next = s->prev = NULL;
s->data = x;
return s;
}
void DCListInit(DCList *plist);
void DCListPushBack(DCList *plist, ElemType x);
void DCListShow(DCList *plist);
void DCListPushFront(DCList *plist, ElemType x);
void DCListPopBack(DCList *plist);
void DCListPopFront(DCList *plist);
size_t DCListLength(DCList *plist);
void DCListClear(DCList *plist);
void DCListDestroy(DCList *plist);
DCListNode* DCListFind(DCList *plist, ElemType key);
void DCListDeleteByVal(DCList *plist, ElemType key);
void DCListReverse(DCList *plist);
void DCListInsertByVal(DCList *plist, ElemType x);
void DCListSort(DCList *plist);
void DCListInit(DCList *plist){
assert(plist);
DCListNode * s = _BuyDCListNode(0);
plist->first = plist->last = s;
plist->first->next = plist->last;
plist->last->prev = plist->first;
plist->size = 0;
}
void DCListPushBack(DCList *plist,ElemType x){
assert(plist);
DCListNode *s = _BuyDCListNode(x);
s->prev = plist->last;
plist->last->next = s;
s->next = plist->first;
plist->first->prev = s;
plist->last = s;
plist->size++;
}
void DCListShow(DCList *plist)
{
DCListNode *p = plist->first->next;
while (p != plist->first)
{
printf("%d-->", p->data);
p = p->next;
}
printf("Over.\n");
}
void DCListPushFront(DCList *plist, ElemType x){
assert(plist);
DCListNode *s = _BuyDCListNode(x);
s->next = plist->first->next;
plist->first->next->prev = s;
s->prev = plist->first;
plist->first->next = s;
if (plist->last == plist->first)
plist->last = s;
plist->size++;
}
void DCListPopBack(DCList *plist){
assert(plist);
DCListNode *p = plist->last;
if (p != plist->first){
plist->last = p->prev;
p->prev->next = plist->first;
plist->first->prev = p->prev;
free(p);
plist->size--;
}
}
void DCListPopFront(DCList *plist){
assert(plist);
DCListNode *p;
if (plist->size == 0)
return;
p = plist->first->next;
plist->first->next = p->next;
p->next->prev = plist->first;
free(p);
plist->size--;
if (plist->size == 0)
plist->last = plist->first;
}
size_t DCListLength(DCList *plist){
return plist->size;
}
void DCListClear(DCList *plist){
assert(plist);
DCListNode *p = plist->first->next;
if (plist->size == 0){
return;
}
while (p != plist->first){
p->prev->next = p->next;
p->next->prev =p->prev;
free(p);
p = plist->first->next;
}
plist->size = 0;
plist->last = plist->first;
}
void DCListDestroy(DCList *plist)
{
DCListClear(plist);
free(plist->first);
plist->first = plist->last = NULL;
plist->size = 0;
}
DCListNode* DCListFind(DCList *plist, ElemType key){
assert(plist);
DCListNode *p = plist->first->next;
while (p != plist->first&&p->data!=key){
p = p->next;
}
if (p == plist->first)
return NULL;
return p;
}
void DCListDeleteByVal(DCList *plist, ElemType key){
assert(plist);
DCListNode *p = DCListFind(plist, key);
if (p != NULL)
{
if (p == plist->last) //需要判断是否是最后一个节点
plist->last = p->prev;
p->prev->next = p->next;
p->next->prev = p->prev;
free(p);
plist->size--;
}
}
void DCListReverse(DCList *plist){
DCListNode *p, *q;
if (plist->size <= 1)
return;
p = plist->first->next;
q = p->next;
plist->last = p;
plist->last->next = plist->first;
plist->first->prev = plist->last;//断开第一个有效节点
while (q != plist->first)
{
//向后顺序进行
p = q;
q = q->next;
//插入排序
p->next = plist->first->next;
p->next->prev = p;
p->prev = plist->first;
p->prev->next = p;
}
}
void DCListInsertByVal(DCList *plist, ElemType x){
assert(plist);
DCListNode *s = _BuyDCListNode(x);
DCListNode *p = plist->first->next;
while (p != plist->first && x>p->data)//寻找插入的位置
p = p->next;
if (p == plist->first)//如果只有头节点
plist->last = s;
s->next = p;
s->prev = p->prev;
s->prev->next = s;
s->next->prev = s;
plist->size++;
}
三、TestMain.c源文件
#include"DCList.h"
int main()
{
DCList mylist;
DCListInit(&mylist);
ElemType item, key;
int pos;
DCListNode *p;
int select = 1;
while (select)
{
printf("********************************************\n");
printf("* [1] push_back [2] push_front *\n");
printf("* [3] show_list [0] quit_system *\n");
printf("* [4] pop_back [5] pop_front *\n");
printf("* [6] length [*7] capacity *\n");
printf("* [8] insert_val [*9] insert_pos *\n");
printf("* [10] delete_val [*11] delete_pos *\n");
printf("* [12] find [*13] sort *\n");
printf("* [14] reverse [*15] remove_all *\n");
printf("* [16] clear [*17] destroy *\n");
printf("* [*18] find_binary *\n");
printf("********************************************\n");
printf("请选择:>");
scanf("%d", &select);
switch (select)
{
case 1:
printf("请输入要插入的数据(以-1结束):>");
while (scanf("%d", &item), item != -1) //逗号表达式
DCListPushBack(&mylist, item);
break;
case 2:
printf("请输入要插入的数据(以-1结束):>");
while (scanf("%d", &item), item != -1) //逗号表达式
DCListPushFront(&mylist, item);
break;
case 3:
DCListShow(&mylist);
break;
case 4:
DCListPopBack(&mylist);
break;
case 5:
DCListPopFront(&mylist);
break;
case 6:
printf("表长为:>%d\n", DCListLength(&mylist));
break;
case 7:
//printf("表的容量为:>%d\n", SeqListCapacity(&mylist));
break;
case 8:
printf("请输入要插入的值:>");
scanf("%d", &item);
DCListInsertByVal(&mylist, item);
break;
case 9:
printf("请输入要插入的位置:>");
scanf("%d", &pos);
printf("请输入要插入的值:>");
scanf("%d", &item);
//SeqListInsertByPos(&mylist, pos, item);
break;
case 10:
printf("请输入要删除的值:>");
scanf("%d", &key);
DCListDeleteByVal(&mylist, key);
break;
case 11:
printf("请输入要删除的位置:>");
scanf("%d", &pos);
//SeqListDeleteByPos(&mylist, pos);
break;
case 12:
printf("请输入要查找的值:>");
scanf("%d", &key);
p = DCListFind(&mylist, key);
if (p == NULL)
printf("要查找的值%d不存在.\n", key);
else
printf("要查找的值%d存在.\n", key);
break;
case 13:
DCListSort(&mylist);
break;
case 14:
DCListReverse(&mylist);
break;
case 15:
printf("请输入要删除的值:>");
scanf("%d", &key);
//SListRemoveAll(&mylist, key);
break;
case 16:
DCListClear(&mylist);
break;
case 18:
printf("请输入要查找的值:>");
scanf("%d", &key);
//pos = SeqListFindByBinary(&mylist, key);
if (pos == -1)
printf("要查找的值%d不存在.\n", key);
else
printf("要查找的值%d在下标为%d的位置.\n", key, pos);
break;
}
system("pause");
system("cls");
}
DCListDestroy(&mylist);
return 0;
}