#include<stdio.h>
#include<stdlib.h>
#define ElemType int
#define MaxSize 10
#define true 1
#define false 0
typedef int bool;
//定义单链表节点
typedef struct DLNode {
ElemType data;
struct DLNode* prior,* next;
}DLNode, * DLinkList;
//初始化链表
bool InitList(DLinkList* L) {
*L = (DLNode*)malloc(sizeof(DLNode));
if (*L == NULL) {
return false;
}
(*L)->prior = NULL;
(*L)->next = NULL;
return true;
}
//头插法建立链表
void List_HeaderInsert(DLinkList L) {
DLNode* p;
int n, t = 1, data;
printf("请输入你要添加元素的个数:");
scanf_s("%d", &n);
while (n) {
scanf_s("%d", &data);
p = (DLNode*)malloc(sizeof(DLNode));
p->data = data;
p->prior = L;
p->next = L->next;
L->next = p;
if (t) {
t = 0;
n--;
continue;
}
p->next->prior=L->next;
n--;
}
}
//获取指定节点
DLNode* GetElem(DLinkList L, int i) {
if (i < 0) {
return NULL;
}
int n = 1;
if (i == 0) {
return L;
}
DLNode* p = L->next;
while (p) {
if (i == n) {
return p;
}
n++;
p = p->next;
}
return NULL;
}
//链表打印
void Print_LinkList(DLinkList L) {
if (Is_Empty(L)) {
printf("链表为空");
return;
}
DLNode* p = L->next;
int n = 0;
while (p != NULL) {
if (n % 10 == 0 && n / 10 != 0) {
printf("\n");
}
printf("%d ", p->data);
p = p->next;
n++;
}
}
void Reverse_Print(DLinkList L) {
if (Is_Empty(L)) {
printf("链表为空");
return;
}
DLNode* p = L->next;
int n = 0;
while (p != NULL) {
n++;
if (p->next == NULL) {
break;
}
p = p->next;
}
for (int i = 0; i < n; i++) {
if (n % 10 == 0 && n / 10 != 0) {
printf("\n");
}
printf("%d ", p->data);
p = p->prior;
}
}
//判断是否为空
bool Is_Empty(DLinkList L) {
if (L->next == NULL) {
return true;
}
return false;
}
//按位插入节点
bool ListInsert(DLinkList L, int index, ElemType data) {
if (Is_Empty(L) || index < 1) {
return false;
}
DLNode* p = L->next;
int n = 1;
while (p) {
if (n == index) {
InsertPriorNode(p, data);
return true;
}
p = p->next;
n++;
}
return false;
}
//在指定节点后面插入
bool InsertNextNode(DLNode* s, ElemType data) {
if (s == NULL) {
return false;
}
DLNode* p = (DLNode*)malloc(sizeof(DLNode));
p->data = data;
p->prior = s;
p->next = s->next;
s->next = p;
p->next->prior = p;
return true;
}
//在指定节点前插入
bool InsertPriorNode(DLNode* s, ElemType data) {
if (s == NULL) {
return false;
}
DLNode* q = s->prior;
InsertNextNode(q, data);
return true;
}
//按位删除节点
bool ListDelete(DLinkList L, int index) {
if (Is_Empty(L) || index < 1) {
return false;
}
if (index == 1) {
DLNode* p = L->next;
p->next->prior = L;
L->next = p->next;
free(p);
return true;
}
int n = 1;
DLNode* p = L->next;
while (p) {
if (n == index - 1) {
if (p->next == NULL) {
return false;
}
DeleteNode(p);
return true;
}
p = p->next;
n++;
}
return false;
}
//删除指定节点
bool DeleteNode(DLNode* p) {
if (p == NULL) {
return false;
}
DLNode* q = p->next;
p->next = q->next;
q->next->prior = p;
free(q);
return true;
}
//链表反转
bool ListReverse(DLinkList* L) {
if (Is_Empty(L)) {
return false;
}
int n = 1;
DLinkList K;
InitList(&K);
DLNode* p = (*L)->next;
while (p) {
if (n == 1) {
DLNode* q = (DLNode*)malloc(sizeof(DLNode));
q->data = p->data;
q->next = NULL;
K->next = q;
q->prior = K;
n = 2;
}
else {
InsertPriorNode(K->next, p->data);
}
p = p->next;
}
free(*L);
*L = K;
return true;
}
int main() {
DLinkList L;
InitList(&L);
List_HeaderInsert(L);
Print_LinkList(L);
/*printf("\n");
Reverse_Print(L);*/
printf("\n");
printf("%d", (GetElem(L, 1))->data);
printf("\n");
ListInsert(L, 2, 5);
Print_LinkList(L);
/*printf("\n");
Reverse_Print(L);*/
printf("\n");
ListDelete(L, 2);
Print_LinkList(L);
printf("\n");
ListReverse(&L);
Print_LinkList(L);
printf("\n");
Reverse_Print(L);
}