c++实现双循环链表完整代码
时间: 2023-05-30 11:04:26 浏览: 101
以下是C语言实现双循环链表的完整代码:
```
#include <stdio.h>
#include <stdlib.h>
// 双循环链表结点的定义
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 双循环链表的定义
typedef struct DoubleLinkedList {
Node* head;
int size;
} DoubleLinkedList;
// 创建一个新结点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// 初始化双循环链表
void initDoubleLinkedList(DoubleLinkedList* list) {
list->head = NULL;
list->size = 0;
}
// 在双循环链表的末尾插入一个结点
void insertNodeAtEnd(DoubleLinkedList* list, int data) {
Node* newNode = createNode(data);
if (list->head == NULL) {
list->head = newNode;
newNode->prev = newNode;
newNode->next = newNode;
} else {
Node* lastNode = list->head->prev;
lastNode->next = newNode;
newNode->prev = lastNode;
newNode->next = list->head;
list->head->prev = newNode;
}
list->size++;
}
// 在双循环链表的指定位置插入一个结点
void insertNodeAtIndex(DoubleLinkedList* list, int index, int data) {
if (index < 0 || index > list->size) {
printf("Invalid index!\n");
return;
}
if (index == 0) {
insertNodeAtFront(list, data);
return;
}
if (index == list->size) {
insertNodeAtEnd(list, data);
return;
}
Node* newNode = createNode(data);
Node* curNode = list->head;
for (int i = 0; i < index - 1; i++) {
curNode = curNode->next;
}
newNode->prev = curNode;
newNode->next = curNode->next;
curNode->next->prev = newNode;
curNode->next = newNode;
list->size++;
}
// 在双循环链表的头部插入一个结点
void insertNodeAtFront(DoubleLinkedList* list, int data) {
insertNodeAtIndex(list, 0, data);
}
// 在双循环链表的末尾删除一个结点
void deleteNodeAtEnd(DoubleLinkedList* list) {
if (list->head == NULL) {
printf("The list is empty!\n");
return;
}
if (list->size == 1) {
free(list->head);
list->head = NULL;
} else {
Node* lastNode = list->head->prev;
lastNode->prev->next = list->head;
list->head->prev = lastNode->prev;
free(lastNode);
}
list->size--;
}
// 在双循环链表的指定位置删除一个结点
void deleteNodeAtIndex(DoubleLinkedList* list, int index) {
if (index < 0 || index >= list->size) {
printf("Invalid index!\n");
return;
}
if (index == 0) {
deleteNodeAtFront(list);
return;
}
if (index == list->size - 1) {
deleteNodeAtEnd(list);
return;
}
Node* curNode = list->head;
for (int i = 0; i < index; i++) {
curNode = curNode->next;
}
curNode->prev->next = curNode->next;
curNode->next->prev = curNode->prev;
free(curNode);
list->size--;
}
// 在双循环链表的头部删除一个结点
void deleteNodeAtFront(DoubleLinkedList* list) {
deleteNodeAtIndex(list, 0);
}
// 打印双循环链表
void printDoubleLinkedList(DoubleLinkedList* list) {
if (list->head == NULL) {
printf("The list is empty!\n");
return;
}
Node* curNode = list->head;
for (int i = 0; i < list->size; i++) {
printf("%d ", curNode->data);
curNode = curNode->next;
}
printf("\n");
}
int main() {
DoubleLinkedList list;
initDoubleLinkedList(&list);
insertNodeAtEnd(&list, 1);
insertNodeAtEnd(&list, 2);
insertNodeAtEnd(&list, 3);
insertNodeAtFront(&list, 0);
insertNodeAtIndex(&list, 3, 4);
printDoubleLinkedList(&list);
deleteNodeAtEnd(&list);
deleteNodeAtIndex(&list, 2);
deleteNodeAtFront(&list);
printDoubleLinkedList(&list);
return 0;
}
```