写出带虚拟头结点的循环双链表的创建链表(输入结点的整形值,输入-1代表链表创建完成)和打印链表的两个函数

动态 未结 0 123
小小草
小小草 LV1 2023年9月23日 23:40 发表
好的,以下是带虚拟头结点的循环双链表创建和打印函数的示例代码: ```c #include #include // 定义循环双链表节点结构体 typedef struct node { int data; struct node *prev; struct node *next; } Node; // 初始化循环双链表 void initList(Node **head) { *head = (Node *)malloc(sizeof(Node)); (*head)->data = 0; (*head)->prev = *head; (*head)->next = *head; } // 带虚拟头结点的循环双链表尾插法建立 void createList(Node *head) { int value; Node *tail = head; while (1) { printf("请输入节点的值(输入-1结束):"); scanf("%d", &value); if (value == -1) { break; } Node *newNode = (Node *)malloc(sizeof(Node)); newNode->data = value; newNode->prev = tail; newNode->next = head; tail->next = newNode; head->prev = newNode; tail = newNode; } } // 打印循环双链表 void printList(Node *head) { Node *p = head->next; while (p != head) { printf("%d ", p->data); p = p->next; } printf("\n"); } int main() { Node *head; initList(&head); createList(head); printList(head); return 0; } ``` 这段代码中,我们首先定义了一个带虚拟头结点的循环双
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复