main函数的
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "DoubleList.h"
int main(int argc, char *argv[])
{
DList* L = (DNode*)malloc(sizeof(DNode));
ElemType op;
do{
printf("-------双向循环链表-------\n");
printf("\t 1. 初始化\n");
printf("\t 2. 在头部插入元素\n");
printf("\t 3. 在尾部插入元素\n");
printf("\t 4. 在位置i插入元素\n");
printf("\t 5. 查找并删除元素\n");
printf("\t 6. 查找元素\n");
printf("\t 7. 删除位置i元素\n");
printf("\t 8. 输出元素\n");
printf("\t 9. 判断链表是否为空\n");
printf("\t 10. 求列表长度\n");
printf("\t 11. 销毁列表\n");
printf("\t ..........\n");
printf("\t 0. 退出\n");
printf("请选择:");
scanf("%d", &op);
if (op == 0)//退出
{
printf("感谢使用!\n");
}
else if (op == 1)//初始化
{
if (Init(&L))
{
printf("初始化成功!\n");
}
else
{
printf("初始化失败!\n");
}
}
else if (op == 2)//在头部插入元素
{
ElemType x;
char opp;
do {
printf("请输入在头部插入的元素:");
scanf("%d", &x);
if (InsertHead(L, x) == OK)
{
printf("插入头部成功!\n");
}
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 3)//在尾部插入元素
{
ElemType x;
char opp;
do {
printf("请输入在尾部插入的元素:");
scanf("%d", &x);
if (Append(L, x) == OK)
{
printf("插入尾部成功!\n");
}
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 4)//在位置i插入元素
{
ElemType x;
int i;
char opp;
do
{
printf("请输入插入结点的位置、x值(i,x):");
scanf("%d,%d", &i, &x);
/*调用*/
if (InsertAt(L, i, x) == OK)
{
printf("插入结点成功!\n");
}
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 5)//查找并删除元素
{
char opp;
int x;
do {
printf("请输入要查找并删除的元素:");
scanf("%d", &x);
Delete(L, x);
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 6)//查找元素
{
char opp;
int x;
do {
printf("请输入要查找的元素:");
scanf("%d", &x);
Find(L, x);
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 7)//删除位置i元素
{
char opp;
int i;
do {
printf("请输入删除的位置i:");
scanf("%d", &i);
if (DeleteAt(L, i))
{
printf("删除成功!\n");
}
printf("是否继续(Y/N,Y)?:");
getchar();
scanf("%c", &opp);
opp = (opp == 10) ? 'y' : opp;
} while (opp == 'y' || opp == 'Y');
}
else if (op == 8)//输出元素
{
Output(L);
}
else if (op == 9)//判断链表是否为空
{
if (ListLength(L) == 0)
{
printf("该列表为空!\n");
}
else
{
printf("该列表有%d个元素!\n", ListLength(L));
}
}
else if (op == 10)//求列表长度
{
printf("该列表共有%d个元素!\n", ListLength(L));
}
else if (op == 11)//销毁列表
{
Destroy(L);
}
else
{
printf("其他功能还在待开发阶段!\n");
}
} while (op != 0);
return 0;
}
DoubleList.c的
#define _CRT_SECURE_NO_WARNINGS 1
/*
DoubleList.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "DoubleList.h"
/*双向链表初始化*/
Status Init(DList** L)
{
DNode* head = (DNode*)malloc(sizeof(DNode));
head->prev = head;
head->next = head;
*L = head;
return OK;
}
/*插入操作,新元素放入第一个位置*/
Status InsertHead(DList* L, ElemType x)
{
DNode* dnode = (DNode*)malloc(sizeof(DNode));
dnode->data = x;
dnode->prev = L;
dnode->next = L->next;
L->next->prev = dnode;
L->next = dnode;
return OK;
}
/*扩展元素,在尾部插入元素*/
Status Append(DList* L, ElemType x)
{
DNode* newNode = (DNode*)malloc(sizeof(DNode));
DNode* p;
newNode->data = x;
p = L;
while (p->next != L)
{
p = p->next;
}
newNode->next = L;
newNode->prev = p;
p->next = newNode;
return OK;
}
/*在位置i插入元素x*/
Status InsertAt(DList* L, int i, ElemType x)
{
DNode* newNode = (DNode*)malloc(sizeof(DNode));
DNode* p=L;
int n;
if (i<1 || i>ListLength(L)+1)
{
printf("越界了,该链表只有%d个元素!", ListLength(L));
return ERROR;
}
for (n = 0; n < i; n++)
{
p = p->next;
}
newNode->data = x;
newNode->prev = p->prev;
p->prev->next = newNode;
newNode->next = p;
p->prev = newNode;
return OK;
}
/*查找并删除元素x*/
Status Delete(DList* L, ElemType x)
{
DNode* p;
p = L;
while (p->next != L)
{
p = p->next;
if (p->data == x)
{
p->next->prev = p->prev;
p->prev->next = p->next;
free(p);
printf("元素%d已删除!\n", x);
return OK;
}
}
printf("元素%d不存在\n", x);
return ERROR;
}
/*删除位置i元素*/
Status DeleteAt(DList* L, int i)
{
DNode* p=L;
int n;
if (ListLength(L) == 0)
{
printf("这个列表没有元素,请先添加元素!\n");
return ERROR;
}
if(i< 1|| i>ListLength(L))
{
printf("没有%d这个位置!\n", i);
return ERROR;
}
for (n = 0; n < i;n++)
{
p = p->next;
}
p->next->prev = p->prev;
p->prev->next = p->next;
free(p);
return OK;
}
/*输出*/
void Output(DList* L)
{
DNode* p;
int i = 0;
p = L;
while (p->next != L)
{
i++;
p = p->next;
printf("Node %d. %d\n", i, p->data);
}
}
/*查找元素*/
Status Find(DList* L, ElemType x)
{
DNode* p;
ElemType i=0;
p = L;
while (p->next != L)
{
p = p->next;
i++;
if (p->data == x)
{
printf("元素%d是第%d个元素\n", x,i);
return OK;
}
}
printf("元素%d不存在\n", x);
return ERROR;
}
/*列表长度*/
Status ListLength(DList* L)
{
DNode* p;
ElemType n = 0;
p = L;
while (p->next != L)
{
p = p->next;
n++;
}
return n;
}
/*销毁列表*/
void Destroy(DList* L)
{
DNode *p, *q;
p = L->next;
q = L;
while (q->next != L)
{
q = p->next;
free(p);
p = q;
}
free(L);
free(q);
printf("销毁成功!\n");
}
头文件DoubleList.h的
/*
DoubleList.h
*/
#define ERROR 0
#define OK 1
#define Overflow 2
#define Underflow 3
#define NotPresent 4
#define Duplicate 5
typedef int Status;
typedef int ElemType;
/* 双向链表结点*/
typedef struct dnode
{
ElemType data;
struct dnode* prev, * next;
}DNode, DList;
/*双向链表初始化*/
Status Init(DList** L);
/*插入操作,新元素放入第一个位置*/
Status InsertHead(DList* L, ElemType x);
/*扩展元素,在尾部插入元素*/
Status Append(DList* L, ElemType x);
/*在位置i插入元素x*/
Status InsertAt(DList* L, int i, ElemType x);
/*查找并删除元素x*/
Status Delete(DList* L, ElemType x);
/*删除位置i元素*/
Status DeleteAt(DList* L, int i);
/*输出*/
void Output(DList * L);
/*查找元素*/
Status Find(DList* L, ElemType x);
/*列表长度*/
Status ListLength(DList* L);
/*销毁列表*/
void Destroy(DList* L);
不知道为什么我的销毁列表有问题,必须列表有两个或两个以上的元素才能成功,否则就会断点报错,还请大神指导指导!!!