用C语言链式存储方式实现以下要求:1.实现单链表链式存储结构,并利用文件内容初始化2.实现单链表的顺序输出,获取长度,查找,插入,删除3.删除所有score低于s的元素,然后顺序输出其他元素4.倒序输出链表内容5.将以上单链表改为双链表,并倒序输出链表内容
时间: 2024-10-07 10:05:13 浏览: 40
关于线性表的顺序存储和链式存储结构的实验报告
在C语言中,我们可以使用链表数据结构来实现上述需求。首先,我们定义一个节点结构体,包含数据域和指向下一个节点的指针:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义单链表节点结构
typedef struct Node {
int data;
struct Node* next;
} ListNode;
// 从文件中初始化单链表
void initListFromFile(char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening file.\n");
return;
}
ListNode* head = NULL;
ListNode* current = NULL;
int data;
while (fscanf(file, "%d", &data) != EOF) {
ListNode* newNode = malloc(sizeof(ListNode));
newNode->data = data;
newNode->next = head;
head = newNode;
}
fclose(file);
}
// 单链表操作函数
ListNode* printList(ListNode* head) {
ListNode* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
return head;
}
int listLength(ListNode* head) {
int count = 0;
ListNode* temp = head;
while (temp != NULL) {
count++;
temp = temp->next;
}
return count;
}
ListNode* searchAndInsert(int value, ListNode** head_ref) {
ListNode* current = *head_ref;
if (current == NULL || current->data >= value) {
ListNode* newNode = malloc(sizeof(ListNode));
newNode->data = value;
newNode->next = *head_ref;
*head_ref = newNode;
} else {
while (current->next != NULL && current->next->data < value) {
current = current->next;
}
if (current->next != NULL)
current->next = malloc(sizeof(ListNode));
current->next->data = value;
}
return *head_ref;
}
ListNode* deleteBelowScore(ListNode** head_ref, int s) {
ListNode* current = *head_ref;
ListNode* prev = NULL;
while (current != NULL && current->data < s) {
prev = current;
current = current->next;
free(prev);
}
if (prev != NULL)
prev->next = current;
else
*head_ref = current;
return *head_ref;
}
// 将单链表转换为双链表
struct DoublyLinkedListNode {
int data;
struct DoublyLinkedListNode* prev;
struct DoublyLinkedListNode* next;
};
void convertToDoublyLinkedList(ListNode* head) {
ListNode* temp = head;
DoublyLinkedListNode* newHead = NULL;
DoublyLinkedListNode* newNode = NULL;
while (temp != NULL) {
newNode = malloc(sizeof(DoublyLinkedListNode));
newNode->data = temp->data;
newNode->prev = NULL;
newNode->next = newHead;
if (newHead != NULL) {
newHead->prev = newNode;
}
newHead = newNode;
temp = temp->next;
}
}
// 双链表倒序输出
void printDoublyLinkedListInReverse(DoublyLinkedListNode* head) {
DoublyLinkedListNode* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->prev;
}
printf("%d -> NULL\n", head->data);
}
int main() {
char filename[] = "input.txt"; // 假设文件存放数据
initListFromFile(filename);
ListNode* head = printList(head); // 输出初始化后的链表
printf("Length: %d\n", listLength(head));
int value = 10; // 示例值
head = searchAndInsert(value, &head); // 插入新元素
printf("After insertion: ");
printList(head);
int scoreThreshold = 8; // 示例分数
head = deleteBelowScore(&head, scoreThreshold); // 删除低分元素
printf("After deleting elements below %d: ", scoreThreshold);
printList(head);
convertToDoublyLinkedList(head); // 转换为双链表
DoublyLinkedListNode* doubleHead = (DoublyLinkedListNode*) head;
printDoublyLinkedListInReverse(doubleHead); // 倒序输出双链表
return 0;
}
```
阅读全文