实验 2 线性表的链式存储结构及应用
一、 实验目的
(1) 理解线性逻辑结构的含义及基本特点;
(2) 熟练线性表的链式存储结构的定义;
(3) 熟练掌握线性表的链式存储结构上基本操作实现;
(4) 培养线性表的链式存储结构结构上应用能力;
二、 实验任务
1、 链表定义
2、 链表的基本操作(初始化,求表长,元素输出,查找,插入,删除等)
3、 链表的应用
三、 实验内容及结果
链表的定义及基本操作
代码
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
struct Node {
int data;
struct Node* next;
};
// 初始化链表
void initialize(struct Node** head) {
*head = NULL;
}
// 求表长
int length(struct Node* head) {
int count = 0;
struct Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
// 元素输出
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// 查找元素
struct Node* search(struct Node* head, int key) {
struct Node* current = head;
while (current != NULL) {
if (current->data == key) {
return current;
}
current = current->next;
}
return NULL;
}
// 在位置pos插入元素
void insert(struct Node** head, int data, int pos) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (pos == 0) { // 在头部插入
newNode->next = *head;
*head = newNode;
} else {
struct Node* current = *head;
int i;
for (i = 0; i < pos - 1 && current != NULL; i++) {
current = current->next;
}
if (current != NULL) {
newNode->next = current->next;
current->next = newNode;
} else {
printf("Invalid position\n");
free(newNode);
}
}
}
// 删除元素
void Delete(struct Node** head, int key) {
struct Node* current = *head;
struct Node* prev = NULL;
// 删除头节点
if (current != NULL && current->data == key) {
*head = current->next;
free(current);
return;
}
while (current != NULL && current->data != key) {
prev = current;
current = current->next;
}
if (current == NULL) {
printf("Element not found\n");
return;
}
prev->next = current->next;
free(current);
}
// 示例演示
int main() {
// 初始化链表
struct Node* head;
initialize(&head);
// 插入元素
insert(&head, 10, 0);
insert(&head, 20, 1);
insert(&head, 30, 2);
// 输出链表
printf("List: ");
printList(head);
// 求表长
printf("Length: %d\n", length(head));
// 查找元素
struct Node* element = search(head, 20);
if (element != NULL) {
printf("Element found: %d\n", element->data);
} else {
printf("Element not found\n");
}
// 删除元素
Delete(&head, 20);
// 输出链表
printf("List after deletion: ");
printList(head);
return 0;
运行结果
在这里插入图片描述
链表的应用(大数的阶乘运算)
源代码
#include<stdio.h>
#include<stdlib.h>
struct Nums {
int data;
struct Nums* lLink;
struct Nums* rLink;
};
typedef struct Nums Nums;
Nums* createNode(int data) {
Nums* newNode = (Nums*)malloc(sizeof(Nums));
if (newNode == NULL) {
printf("存储分配出错!\n");
exit(1);
}
newNode->data = data;
newNode->lLink = NULL;
newNode->rLink = NULL;
return newNode;
}
void insertNode(Nums* head, int data) {
Nums* newNode = createNode(data);
if (head->rLink == NULL) {
head->rLink = newNode;
newNode->lLink = head;
}
else {
Nums* lastNode = head->rLink;
while (lastNode->rLink != NULL) {
lastNode = lastNode->rLink;
}
lastNode->rLink = newNode;
newNode->lLink = lastNode;
}
}
void factor(int n) {
Nums* first = createNode(0);
Nums* last = first;
for (int i = 1; i <= n; ++i) {
int flag = 0;
Nums* current = first->rLink;
while (current != NULL) {
current->data = current->data * i + flag;
if (current->data > 999 && current->rLink == NULL) {
Nums* newNode = createNode(0);
newNode->lLink = current;
current->rLink = newNode;
last = newNode;
}
flag = current->data / 1000;
current->data = current->data % 1000;
current = current->rLink;
}
}
Nums* ptr = last;
printf("%d", ptr->data);
ptr = ptr->lLink;
while (ptr != NULL) {
printf("%03d", ptr->data);
ptr = ptr->lLink;
}
}
int main() {
int na;
printf("请输入要计算的数字:\n");
scanf("%d", &na);
printf("n=%d, n!=", na);
factor(na);
printf("\n");
return 0;
}
运行结果
四、 个人体会
链表是一种重要的数据结构,在C语言中使用链表可以高效地存储和操作数据。在上机学习链表时,我遇到了一些挑战,但也学到了很多。首先,理解链表的基本概念是关键,包括节点、头指针和尾指针等。其次,我掌握了链表的插入和删除操作,对于插入操作,需要重新调整节点之间的指针关系;对于删除操作,需要注意释放被删除节点的内存,以防止内存泄漏。此外,我还学会了链表的遍历和查找操作,以及如何处理边界情况和异常情况。通过上机实践,我进一步提升了C语言的编程能力和对链表的理解。我相信,掌握链表的使用对于解决实际问题和编写高效的程序非常重要,我会继续深入学习并应用链表这一数据结构。