双向循环链表
截屏2020-04-06下午6.13.49.png
链表初始化
截屏2020-04-06下午8.48.50.png
Status initList(LinkList *L){
*L = (LinkList)malloc(sizeof(Node));
if(*L == NULL) return ERROR;
(*L)->next = (*L);
(*L)->prior = (*L);
return OK;
}
链表插入结点
截屏2020-04-06下午8.51.04.png
Status insertList(LinkList *L, int i, ElemType data){
if (*L == NULL) return ERROR;
if (i<1) return ERROR;
LinkList target = *L;
int j = 1;
while (j < i && target->next != *L) {
target = target->next;
j++;
}
if (j>i) return ERROR;
LinkList temp = (LinkList)malloc(sizeof(Node));
if (temp == NULL) return ERROR;
temp->data = data;
temp->prior = target;
temp->next = target->next;
target->next = temp;
temp->next->prior = temp;
return OK;
}
链表删除结点
截屏2020-04-06下午8.52.48.png
Status deleteList(LinkList *L ,int i){
if (*L == NULL) return ERROR;
if (i<1) return ERROR;
LinkList temp = (*L)->next;
if (temp->next == *L) {
*L = NULL;
free(*L);
return OK;
}
int j = 1;
while (j < i) {
temp = temp->next;
j++;
}
if (j<i&&temp->next == *L) {
//输入的位置超过了链表的长度
return ERROR;
}
temp->prior->next = temp->next;
temp->next->prior = temp->prior;
free(temp);
return OK;
}
LinkList2