⑴随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。 ⑵遍历单向链表。 ⑶把单向链表中元素逆置(不允许申请新的结点空间)。 ⑷在单向链表中删除所有的偶数元素结点。 ⑸编写在非递减有序链表中插入一个元素使链表元素仍有序的函数,并利用该函数建立一个非递减有序单向链表。 ⑹利用算法5建立两个非递减有序单向链表,然后合并成一个非递增链表。 ⑺利用算法5建立两个非递减有序单向链表,然后合并成一个非递减链表。 ⑻利用算法1建立的链表,实现将其分解成两个链表,其中一个全部为奇数,另一个全部为偶数(尽量利用已知的存储空间)。 在主函数中设计一个简单的菜单,分别调试上述算法。
时间: 2023-04-03 12:03:56 浏览: 129
我可以回答你的问题。
⑴随机产生或键盘输入一组元素,建立一个带头结点的单向链表(无序)。
可以使用以下代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node, *LinkedList;
LinkedList createList() {
LinkedList head = (LinkedList) malloc(sizeof(Node));
head->next = NULL;
return head;
}
void insert(LinkedList head, int data) {
Node *p = (Node*) malloc(sizeof(Node));
p->data = data;
p->next = head->next;
head->next = p;
}
void traverse(LinkedList head) {
Node *p = head->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void reverse(LinkedList head) {
Node *p = head->next;
head->next = NULL;
while (p != NULL) {
Node *temp = p->next;
p->next = head->next;
head->next = p;
p = temp;
}
}
void deleteEven(LinkedList head) {
Node *p = head->next;
Node *prev = head;
while (p != NULL) {
if (p->data % 2 == 0) {
prev->next = p->next;
free(p);
p = prev->next;
} else {
prev = p;
p = p->next;
}
}
}
void insertInOrder(LinkedList head, int data) {
Node *p = head->next;
Node *prev = head;
while (p != NULL && p->data < data) {
prev = p;
p = p->next;
}
Node *newNode = (Node*) malloc(sizeof(Node));
newNode->data = data;
newNode->next = prev->next;
prev->next = newNode;
}
LinkedList mergeDescending(LinkedList list1, LinkedList list2) {
LinkedList result = createList();
Node *p1 = list1->next;
Node *p2 = list2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->data > p2->data) {
insert(result, p1->data);
p1 = p1->next;
} else {
insert(result, p2->data);
p2 = p2->next;
}
}
while (p1 != NULL) {
insert(result, p1->data);
p1 = p1->next;
}
while (p2 != NULL) {
insert(result, p2->data);
p2 = p2->next;
}
return result;
}
LinkedList mergeAscending(LinkedList list1, LinkedList list2) {
LinkedList result = createList();
Node *p1 = list1->next;
Node *p2 = list2->next;
while (p1 != NULL && p2 != NULL) {
if (p1->data < p2->data) {
insert(result, p1->data);
p1 = p1->next;
} else {
insert(result, p2->data);
p2 = p2->next;
}
}
while (p1 != NULL) {
insert(result, p1->data);
p1 = p1->next;
}
while (p2 != NULL) {
insert(result, p2->data);
p2 = p2->next;
}
return result;
}
void splitList(LinkedList head, LinkedList oddList, LinkedList evenList) {
Node *p = head->next;
Node *odd = oddList;
Node *even = evenList;
while (p != NULL) {
if (p->data % 2 == 0) {
even->next = p;
even = p;
} else {
odd->next = p;
odd = p;
}
p = p->next;
}
even->next = NULL;
odd->next = NULL;
}
int main() {
LinkedList list = createList();
int n;
printf("请输入元素个数:");
scanf("%d", &n);
printf("请输入元素:");
for (int i = 0; i < n; i++) {
int data;
scanf("%d", &data);
insert(list, data);
}
printf("链表元素为:");
traverse(list);
reverse(list);
printf("逆置后的链表元素为:");
traverse(list);
deleteEven(list);
printf("删除偶数元素后的链表元素为:");
traverse(list);
LinkedList orderedList = createList();
insertInOrder(orderedList, 3);
insertInOrder(orderedList, 1);
insertInOrder(orderedList, 2);
insertInOrder(orderedList, 5);
insertInOrder(orderedList, 4);
printf("非递减有序链表为:");
traverse(orderedList);
LinkedList orderedList2 = createList();
insertInOrder(orderedList2, 7);
insertInOrder(orderedList2, 6);
insertInOrder(orderedList2, 8);
printf("另一个非递减有序链表为:");
traverse(orderedList2);
LinkedList mergedList = mergeDescending(orderedList, orderedList2);
printf("合并后的非递增链表为:");
traverse(mergedList);
LinkedList mergedList2 = mergeAscending(orderedList, orderedList2);
printf("合并后的非递减链表为:");
traverse(mergedList2);
LinkedList oddList = createList();
LinkedList evenList = createList();
splitList(list, oddList, evenList);
printf("奇数链表为:");
traverse(oddList);
printf("偶数链表为:");
traverse(evenList);
return 0;
}
```
运行程序后,可以按照菜单提示选择要调试的算法。
阅读全文