已知两个线性表a和b,试设计循环链表,其尾指针分别为taila和tailb,编写代码实现:两个线性表的合并,并删除自第i个元素起,长度为len个元素。
时间: 2023-04-12 08:03:27 浏览: 69
设计循环链表的思路如下:
1. 定义循环链表节点结构体,包含数据域和指针域。
2. 定义循环链表结构体,包含头指针和尾指针。
3. 初始化循环链表,将头指针和尾指针都指向空节点。
4. 插入节点,将新节点插入到尾指针后面。
5. 删除节点,将要删除的节点的前一个节点的指针指向要删除的节点的后一个节点。
6. 合并两个循环链表,将一个循环链表的尾指针指向另一个循环链表的头指针,形成一个新的循环链表。
7. 删除自第i个元素起,长度为len个元素,先找到第i个节点,然后遍历len个节点,将要删除的节点的前一个节点的指针指向要删除的节点的后一个节点。
以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
typedef struct list {
Node *head;
Node *tail;
} List;
// 初始化循环链表
void initList(List *list) {
list->head = NULL;
list->tail = NULL;
}
// 插入节点
void insertNode(List *list, int data) {
Node *node = (Node *)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
if (list->head == NULL) {
list->head = node;
list->tail = node;
list->tail->next = list->head;
} else {
list->tail->next = node;
list->tail = node;
list->tail->next = list->head;
}
}
// 删除节点
void deleteNode(List *list, int i, int len) {
Node *p = list->head;
Node *q = NULL;
int j = 1;
while (p != NULL && j < i) {
q = p;
p = p->next;
j++;
}
if (p == NULL || j > i) {
printf("删除位置不合法\n");
return;
}
j = ;
while (p != NULL && j < len) {
Node *temp = p;
p = p->next;
free(temp);
j++;
}
if (q == NULL) {
list->head = p;
} else {
q->next = p;
}
}
// 合并两个循环链表
void mergeList(List *list1, List *list2) {
list1->tail->next = list2->head;
list1->tail = list2->tail;
list1->tail->next = list1->head;
}
// 打印循环链表
void printList(List *list) {
Node *p = list->head;
while (p != NULL && p != list->tail) {
printf("%d ", p->data);
p = p->next;
}
if (p != NULL) {
printf("%d ", p->data);
}
printf("\n");
}
int main() {
List list1, list2;
initList(&list1);
initList(&list2);
insertNode(&list1, 1);
insertNode(&list1, 2);
insertNode(&list1, 3);
insertNode(&list2, 4);
insertNode(&list2, 5);
insertNode(&list2, 6);
printf("list1: ");
printList(&list1);
printf("list2: ");
printList(&list2);
mergeList(&list1, &list2);
printf("list1 + list2: ");
printList(&list1);
deleteNode(&list1, 2, 2);
printf("list1 + list2 删除自第2个元素起,长度为2个元素: ");
printList(&list1);
return ;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)