/*1*/
#include <stdio.h>
#include <stdlib.h>
// 定义链表结点
struct Node {
int data;
struct Node* next;
};
// 创建一个新结点
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在链表尾部添加新结点
void appendNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
(*head)->next = *head; // 创建循环
} else {
struct Node* tail = *head;
while (tail->next != *head) {
tail = tail->next;
}
newNode->next = *head;
tail->next = newNode;
}
}
// 删除结点s的直接前驱结点
void deletePredecessor(struct Node** head, struct Node* s) {
if (*head == NULL || s == NULL) {
printf("链表为空或结点s为NULL。\n");
return;
}
struct Node* current = *head;
struct Node* predecessor = NULL;
while (current->next != *head) {
if (current->next == s) {
predecessor = current;
break;
}
current = current->next;
}
if (predecessor != NULL) {
struct Node* toDelete = predecessor->next;
predecessor->next = toDelete->next;
free(toDelete);
} else {
printf("未找到结点s的前驱结点。\n");
}
}
// 打印链表
void printList(struct Node* head) {
if (head == NULL) {
printf("链表为空\n");
return;
}
struct Node* current = head;
do {
printf("%d -> ", current->data);
current = current->next;
} while (current != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
int n, data;
printf("请输入链表长度: ");
scanf("%d", &n);
if (n <= 1) {
printf("链表长度必须大于1。\n");
return 1;
}
printf("请输入链表数据: ");
for (int i = 0; i < n; i++) {
scanf("%d", &data);
appendNode(&head, data);
}
printf("原始链表:\n");
printList(head);
int s_data;
printf("输入要删除前驱结点的结点的数据: ");
scanf("%d", &s_data);
struct Node* s = head;
while (s->data != s_data) {
s = s->next;
if (s == head) {
printf("未找到匹配的结点。\n");
return 1;
}
}
deletePredecessor(&head, s);
printf("删除直接前驱结点后的链表:\n");
printList(head);
return 0;
}
/*2*/
#include <stdio.h>
#include <stdlib.h>
// 定义链表结点
struct Node {
char data;
struct Node* next;
};
// 创建一个新结点
struct Node* createNode(char data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在链表尾部添加新结点
void appendNode(struct Node** head, char data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
// 将原链表中的数据按类别分为三个循环链表
void splitList(struct Node* original, struct Node** letters, struct Node** digits, struct Node** others) {
struct Node* current = original;
while (current != NULL) {
if (isalpha(current->data)) {
appendNode(letters, current->data);
} else if (isdigit(current->data)) {
appendNode(digits, current->data);
} else {
appendNode(others, current->data);
}
current = current->next;
}
}
// 打印链表
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%c -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* original = NULL;
struct Node* letters = NULL;
struct Node* digits = NULL;
struct Node* others = NULL;
char input[100];
printf("请输入包含三类字符的线性表,以回车结束:\n");
fgets(input, sizeof(input), stdin);
for (int i = 0; input[i] != '\0'; i++) {
if (input[i] != '\n') {
appendNode(&original, input[i]);
}
}
splitList(original, &letters, &digits, &others);
printf("字母表:\n");
printList(letters);
printf("数字表:\n");
printList(digits);
printf("其他字符表:\n");
printList(others);
return 0;
}