目录
1.编写算法查找顺序表中值最小的结点,并删除该结点。
2.编写算法查找单链表中值最大的结点,并将该结点移至链表尾部。
3.编写算法实现顺序表的就地逆置,即利用原表的存储空间将线性表(a1,a2,...an) 逆置为(an,an-1...1) ,并分析设计的算法时间复杂度。
4.编写算法实现链表的就地逆置,即利用原表的存储空间将线性表 逆置为 ,并分析设计的算法时间复杂度。
1.编写算法查找顺序表中值最小的结点,并删除该结点。
具体代码
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 最大容量
// 顺序表类型定义
typedef struct {
int data[MAX_SIZE]; // 存储数据的数组
int length; // 当前长度
} SeqList;
// 初始化顺序表
void initSeqList(SeqList *list) {
list->length = 0;
}
// 插入元素到顺序表
void insertElement(SeqList *list, int value) {
if (list->length >= MAX_SIZE) {
printf("Sequence list is full.\n");
return;
}
list->data[list->length++] = value;
}
// 查找并删除顺序表中值最小的节点
void removeMin(SeqList *list) {
if (list->length == 0) {
printf("Sequence list is empty.\n");
return;
}
int minIndex = 0; // 默认第一个元素是最小值
for (int i = 1; i < list->length; i++) {
if (list->data[i] < list->data[minIndex]) {
minIndex = i;
}
}
// 删除最小值节点
for (int i = minIndex; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
}
// 打印顺序表的内容
void printSeqList(const SeqList *list) {
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
SeqList list;
initSeqList(&list);
// 用户输入顺序表中的元素
int n;
printf("Enter the number of elements in the sequence list: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
printf("Too many elements!\n");
return 1;
}
printf("Enter the elements of the sequence list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertElement(&list, value);
}
printf("Original sequence list: ");
printSeqList(&list);
// 移除顺序表中最小的元素
removeMin(&list);
printf("Sequence list after removing the minimum element: ");
printSeqList(&list);
return 0;
}详细解释该代码
详细解释:
数据结构定义
// 顺序表类型定义
typedef struct {
int data[MAX_SIZE]; // 存储数据的数组
int length; // 当前长度
} SeqList;
data[MAX_SIZE]
:一个固定大小的数组,用于存储顺序表中的元素。length
:记录当前顺序表中元素的数量。
初始化顺序表
// 初始化顺序表
void initSeqList(SeqList *list) {
list->length = 0;
}
- 将顺序表的长度初始化为0,表示顺序表为空。
list
是一个SeqList
类型的变量,用于表示一个顺序表。它包含一个固定大小的数组data
和一个记录当前长度的整数length
。
插入元素到顺序表
// 插入元素到顺序表
void insertElement(SeqList *list, int value) {
if (list->length >= MAX_SIZE) {
printf("Sequence list is full.\n");
return;
}
list->data[list->length++] = value;
}
- 检查顺序表是否已满(即
length
是否达到MAX_SIZE
)。如果已满,输出提示信息并返回。 - 否则,将新元素插入到顺序表的末尾,并增加
length
。
查找并删除顺序表中值最小的节点
// 查找并删除顺序表中值最小的节点
void removeMin(SeqList *list) {
if (list->length == 0) {
printf("Sequence list is empty.\n");
return;
}
int minIndex = 0; // 默认第一个元素是最小值
for (int i = 1; i < list->length; i++) {
if (list->data[i] < list->data[minIndex]) {
minIndex = i;
}
}
// 删除最小值节点
for (int i = minIndex; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
}
- 检查顺序表是否为空。如果为空,输出提示信息并返回。
- 初始化
minIndex
为0,表示默认第一个元素是最小值。 - 遍历顺序表,找到最小值的索引
minIndex
。 - 使用循环将最小值后的所有元素向前移动一位,覆盖掉最小值。
- 减少
length
,表示顺序表中元素数量减少一个。
打印顺序表的内容
// 打印顺序表的内容
void printSeqList(const SeqList *list) {
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
- 遍历顺序表中的所有元素,并逐个打印。
- 打印完所有元素后,输出一个换行符。
主函数
int main() {
SeqList list;
initSeqList(&list);
// 用户输入顺序表中的元素
int n;
printf("Enter the number of elements in the sequence list: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
printf("Too many elements!\n");
return 1;
}
printf("Enter the elements of the sequence list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertElement(&list, value);
}
printf("Original sequence list: ");
printSeqList(&list);
// 移除顺序表中最小的元素
removeMin(&list);
printf("Sequence list after removing the minimum element: ");
printSeqList(&list);
return 0;
}
- 初始化一个顺序表
list
。 - 提示用户输入顺序表中元素的数量
n
,并检查是否超过最大容量MAX_SIZE
。 - 提示用户输入顺序表中的每个元素,并调用
insertElement
函数将其插入到顺序表中。 - 打印原始顺序表的内容。
- 调用
removeMin
函数删除顺序表中最小的元素。 - 再次打印顺序表的内容,显示删除最小元素后的结果。
- 返回0,表示程序正常结束。
示例运行
假设用户输入以下数据:
Enter the number of elements in the sequence list: 5
Enter the elements of the sequence list:
3 1 4 1 5
Original sequence list: 3 1 4 1 5
Sequence list after removing the minimum element: 3 4 1 5
在这个示例中:
- 用户输入了5个元素
[3, 1, 4, 1, 5]
。 - 原始顺序表为
[3, 1, 4, 1, 5]
。 - 删除最小元素
1
后,顺序表变为[3, 4, 1, 5]
。
2.编写算法查找单链表中值最大的结点,并将该结点移至链表尾部。
#include <stdio.h>
#include <stdlib.h>
// 单链表节点定义
typedef struct Node {
int data;
struct Node *next;
} Node;
// 单链表定义
typedef struct {
Node *head;
} LinkedList;
// 初始化链表
void initLinkedList(LinkedList *list) {
list->head = NULL;
}
// 在链表尾部插入节点
void insertAtTail(LinkedList *list, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
} else {
Node *temp = list->head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 查找并移动最大值节点到链表尾部
void moveMaxToTail(LinkedList *list) {
if (list->head == NULL || list->head->next == NULL) {
return; // 如果链表为空或只有一个元素,则无需处理
}
Node *maxNode = list->head;
Node *prevMax = NULL;
Node *current = list->head;
Node *prev = NULL;
// 寻找最大值节点及其前一个节点
while (current != NULL) {
if (current->data > maxNode->data) {
maxNode = current;
prevMax = prev;
}
prev = current;
current = current->next;
}
// 如果最大值不是最后一个节点,删除它
if (maxNode != list->head && prevMax != NULL) {
prevMax->next = maxNode->next;
} else if (maxNode == list->head) {
list->head = list->head->next;
}
// 将最大值节点移到链表尾部
Node *tail = list->head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = maxNode;
maxNode->next = NULL;
}
// 打印链表的内容
void printLinkedList(const LinkedList *list) {
Node *temp = list->head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
LinkedList list;
initLinkedList(&list);
// 用户输入链表中的元素
int n;
printf("Enter the number of elements in the linked list: ");
scanf("%d", &n);
printf("Enter the elements of the linked list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertAtTail(&list, value);
}
printf("Original linked list: ");
printLinkedList(&list);
moveMaxToTail(&list);
printf("Linked list after moving the maximum element to the tail: ");
printLinkedList(&list);
// 清理内存
Node *current = list.head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
return 0;
}
详细解释:
数据结构定义
// 单链表节点定义
typedef struct Node {
int data;
struct Node *next;
} Node;
// 单链表定义
typedef struct {
Node *head;
} LinkedList;
Node
结构体定义了一个链表节点,包含两个成员:int data
:存储节点的数据。struct Node *next
:指向下一个节点的指针。
LinkedList
结构体定义了一个链表,包含一个成员:Node *head
:指向链表头节点的指针。
初始化链表
// 初始化链表
void initLinkedList(LinkedList *list) {
list->head = NULL;
}
- 将链表的头节点初始化为
NULL
,表示链表为空。
在链表尾部插入节点
// 在链表尾部插入节点
void insertAtTail(LinkedList *list, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
} else {
Node *temp = list->head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
- 创建一个新的节点
newNode
,并分配内存。 - 将新节点的数据设置为
value
,并将next
指针设置为NULL
。 - 如果链表为空(即
head
为NULL
),将新节点设置为头节点。 - 否则,遍历链表找到最后一个节点,并将新节点链接到最后一个节点的
next
指针上。
查找并移动最大值节点到链表尾部
// 查找并移动最大值节点到链表尾部
void moveMaxToTail(LinkedList *list) {
if (list->head == NULL || list->head->next == NULL) {
return; // 如果链表为空或只有一个元素,则无需处理
}
Node *maxNode = list->head;
Node *prevMax = NULL;
Node *current = list->head;
Node *prev = NULL;
// 寻找最大值节点及其前一个节点
while (current != NULL) {
if (current->data > maxNode->data) {
maxNode = current;
prevMax = prev;
}
prev = current;
current = current->next;
}
// 如果最大值不是最后一个节点,删除它
if (maxNode != list->head && prevMax != NULL) {
prevMax->next = maxNode->next;
} else if (maxNode == list->head) {
list->head = list->head->next;
}
// 将最大值节点移到链表尾部
Node *tail = list->head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = maxNode;
maxNode->next = NULL;
}
- 检查链表是否为空或只有一个节点,如果是,则直接返回。
- 初始化
maxNode
为头节点,prevMax
为NULL
,current
为头节点,prev
为NULL
。 - 遍历链表,找到最大值节点
maxNode
及其前一个节点prevMax
。 - 如果最大值节点不是头节点且有前一个节点,将
prevMax
的next
指针指向maxNode
的下一个节点,从而删除maxNode
。 - 如果最大值节点是头节点,将头节点更新为
head
的下一个节点。 - 遍历链表找到最后一个节点
tail
,将maxNode
链接到tail
的next
指针上,并将maxNode
的next
指针设置为NULL
。
打印链表的内容
// 打印链表的内容
void printLinkedList(const LinkedList *list) {
Node *temp = list->head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
- 遍历链表,逐个打印每个节点的数据。
- 打印完所有节点后,输出一个换行符。
主函数
int main() {
LinkedList list;
initLinkedList(&list);
// 用户输入链表中的元素
int n;
printf("Enter the number of elements in the linked list: ");
scanf("%d", &n);
printf("Enter the elements of the linked list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertAtTail(&list, value);
}
printf("Original linked list: ");
printLinkedList(&list);
moveMaxToTail(&list);
printf("Linked list after moving the maximum element to the tail: ");
printLinkedList(&list);
// 清理内存
Node *current = list.head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
return 0;
}
- 初始化一个链表
list
。 - 提示用户输入链表中元素的数量
n
。 - 提示用户输入链表中的每个元素,并调用
insertAtTail
函数将其插入到链表中。 - 打印原始链表的内容。
- 调用
moveMaxToTail
函数将最大值节点移动到链表尾部。 - 再次打印链表的内容,显示移动最大值节点后的结果。
- 清理链表中所有节点的内存,防止内存泄漏。
示例运行
假设用户输入以下数据:
Enter the number of elements in the linked list: 5
Enter the elements of the linked list:
3 1 4 1 5
Original linked list: 3 -> 1 -> 4 -> 1 -> 5 -> NULL
Linked list after moving the maximum element to the tail: 3 -> 1 -> 4 -> 1 -> 5 -> NULL
在这个示例中:
- 用户输入了5个元素
[3, 1, 4, 1, 5]
。 - 原始链表为
3 -> 1 -> 4 -> 1 -> 5 -> NULL
。 - 移动最大值节点
5
到链表尾部后,链表变为3 -> 1 -> 4 -> 1 -> 5 -> NULL
。、
3.编写算法实现顺序表的就地逆置,即利用原表的存储空间将线性表(a1,a2,...an) 逆置为(an,an-1...1) ,并分析设计的算法时间复杂度。
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// 顺序表类型定义
typedef struct {
int data[MAX_SIZE];
int length;
} SeqList;
// 初始化顺序表
void initSeqList(SeqList *list) {
list->length = 0;
}
// 插入元素到顺序表
void insertElement(SeqList *list, int value) {
if (list->length >= MAX_SIZE) {
printf("Sequence list is full.\n");
return;
}
list->data[list->length++] = value;
}
// 就地逆置顺序表
void reverseSeqList(SeqList *list) {
if (list->length <= 1) {
return; // 如果链表为空或只有一个元素,则无需处理
}
int left = 0; // 左端索引
int right = list->length - 1; // 右端索引
while (left < right) {
// 交换两端元素
int temp = list->data[left];
list->data[left] = list->data[right];
list->data[right] = temp;
// 向中间靠拢
left++;
right--;
}
}
// 打印顺序表的内容
void printSeqList(const SeqList *list) {
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
SeqList list;
initSeqList(&list);
// 用户输入顺序表中的元素
int n;
printf("Enter the number of elements in the sequence list: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
printf("Too many elements!\n");
return 1;
}
printf("Enter the elements of the sequence list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertElement(&list, value);
}
printf("Original sequence list: ");
printSeqList(&list);
reverseSeqList(&list);
printf("Reversed sequence list: ");
printSeqList(&list);
return 0;
}
详细解释:
数据结构定义
// 顺序表类型定义
typedef struct {
int data[MAX_SIZE]; // 存储数据的数组
int length; // 当前长度
} SeqList;
int data[MAX_SIZE];
:一个固定大小的数组,用于存储顺序表中的元素。MAX_SIZE
是一个宏定义,表示数组的最大容量。int length;
:一个整数,用于记录当前顺序表中实际存储的元素数量。
初始化顺序表
int data[MAX_SIZE];:一个固定大小的数组,用于存储顺序表中的元素。MAX_SIZE 是一个宏定义,表示数组的最大容量。
int length;:一个整数,用于记录当前顺序表中实际存储的元素数量。
初始化顺序表
- 将顺序表的长度初始化为0,表示顺序表为空。
插入元素到顺序表
// 插入元素到顺序表
void insertElement(SeqList *list, int value) {
if (list->length >= MAX_SIZE) {
printf("Sequence list is full.\n");
return;
}
list->data[list->length++] = value;
}
- 检查顺序表是否已满(即
length
是否达到MAX_SIZE
)。如果已满,输出提示信息并返回。 - 否则,将新元素插入到顺序表的末尾,并增加
length
。
就地逆置顺序表
// 就地逆置顺序表
void reverseSeqList(SeqList *list) {
if (list->length <= 1) {
return; // 如果链表为空或只有一个元素,则无需处理
}
int left = 0; // 左端索引
int right = list->length - 1; // 右端索引
while (left < right) {
// 交换两端元素
int temp = list->data[left];
list->data[left] = list->data[right];
list->data[right] = temp;
// 向中间靠拢
left++;
right--;
}
}
- 检查顺序表是否为空或只有一个元素,如果是,则直接返回。
- 初始化左端索引
left
为0,右端索引right
为length - 1
。 - 使用
while
循环,当left
小于right
时,交换left
和right
位置的元素,并将left
和right
向中间靠拢。 - 循环结束后,顺序表中的元素已经被逆置。
打印顺序表的内容
// 打印顺序表的内容
void printSeqList(const SeqList *list) {
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
- 遍历顺序表中的所有元素,并逐个打印。
- 打印完所有元素后,输出一个换行符。
主函数
int main() {
SeqList list;
initSeqList(&list);
// 用户输入顺序表中的元素
int n;
printf("Enter the number of elements in the sequence list: ");
scanf("%d", &n);
if (n > MAX_SIZE) {
printf("Too many elements!\n");
return 1;
}
printf("Enter the elements of the sequence list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertElement(&list, value);
}
printf("Original sequence list: ");
printSeqList(&list);
reverseSeqList(&list);
printf("Reversed sequence list: ");
printSeqList(&list);
return 0;
}
- 初始化一个顺序表
list
。 - 提示用户输入顺序表中元素的数量
n
,并检查是否超过最大容量MAX_SIZE
。 - 提示用户输入顺序表中的每个元素,并调用
insertElement
函数将其插入到顺序表中。 - 打印原始顺序表的内容。
- 调用
reverseSeqList
函数逆置顺序表。 - 再次打印顺序表的内容,显示逆置后的结果。
- 返回0,表示程序正常结束。
示例运行
假设用户输入以下数据:
Enter the number of elements in the sequence list: 5
Enter the elements of the sequence list:
3 1 4 1 5
Original sequence list: 3 1 4 1 5
Reversed sequence list: 5 1 4 1 3
在这个示例中:
- 用户输入了5个元素
[3, 1, 4, 1, 5]
。 - 原始顺序表为
3 1 4 1 5
。 - 逆置顺序表后,顺序表变为
5 1 4 1 3
。
4.编写算法实现链表的就地逆置,即利用原表的存储空间将线性表 逆置为 ,并分析设计的算法时间复杂度。
#include <stdio.h>
#include <stdlib.h>
// 链表节点定义
typedef struct Node {
int data;
struct Node *next;
} Node;
// 链表定义
typedef struct {
Node *head;
} LinkedList;
// 初始化链表
void initLinkedList(LinkedList *list) {
list->head = NULL;
}
// 在链表尾部插入节点
void insertAtTail(LinkedList *list, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
} else {
Node *temp = list->head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
// 就地逆置链表
void reverseLinkedList(LinkedList *list) {
if (list->head == NULL || list->head->next == NULL) {
return; // 如果链表为空或只有一个元素,则无需处理
}
Node *prev = NULL;
Node *current = list->head;
Node *next = NULL;
while (current != NULL) {
next = current->next; // 保存下一个节点
current->next = prev; // 反转当前节点的指针
prev = current; // 移动 prev 和 current
current = next;
}
list->head = prev; // 更新头节点
}
// 打印链表的内容
void printLinkedList(const LinkedList *list) {
Node *temp = list->head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
LinkedList list;
initLinkedList(&list);
// 用户输入链表中的元素
int n;
printf("Enter the number of elements in the linked list: ");
scanf("%d", &n);
printf("Enter the elements of the linked list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertAtTail(&list, value);
}
printf("Original linked list: ");
printLinkedList(&list);
reverseLinkedList(&list);
printf("Reversed linked list: ");
printLinkedList(&list);
// 清理内存
Node *current = list.head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
return 0;
}
详细解释:
数据结构定义
// 链表节点定义
typedef struct Node {
int data;
struct Node *next;
} Node;
// 链表定义
typedef struct {
Node *head;
} LinkedList;
Node
结构体定义了一个链表节点,包含两个成员:int data
:存储节点的数据。struct Node *next
:指向下一个节点的指针。
LinkedList
结构体定义了一个链表,包含一个成员:Node *head
:指向链表头节点的指针。
初始化链表
// 初始化链表
void initLinkedList(LinkedList *list) {
list->head = NULL;
}
- 将链表的头节点初始化为
NULL
,表示链表为空。
在链表尾部插入节点
// 在链表尾部插入节点
void insertAtTail(LinkedList *list, int value) {
Node *newNode = (Node *)malloc(sizeof(Node));
if (!newNode) {
printf("Memory error\n");
return;
}
newNode->data = value;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
} else {
Node *temp = list->head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
- 创建一个新的节点
newNode
,并分配内存。 - 将新节点的数据设置为
value
,并将next
指针设置为NULL
。 - 如果链表为空(即
head
为NULL
),将新节点设置为头节点。 - 否则,遍历链表找到最后一个节点,并将新节点链接到最后一个节点的
next
指针上。
就地逆置链表
// 就地逆置链表
void reverseLinkedList(LinkedList *list) {
if (list->head == NULL || list->head->next == NULL) {
return; // 如果链表为空或只有一个元素,则无需处理
}
Node *prev = NULL;
Node *current = list->head;
Node *next = NULL;
while (current != NULL) {
next = current->next; // 保存下一个节点
current->next = prev; // 反转当前节点的指针
prev = current; // 移动 prev 和 current
current = next;
}
list->head = prev; // 更新头节点
}
- 检查链表是否为空或只有一个节点,如果是,则直接返回。
- 初始化三个指针:
prev
:前一个节点,初始为NULL
。current
:当前节点,初始为头节点。next
:下一个节点,初始为NULL
。
- 使用
while
循环遍历链表:- 保存当前节点的下一个节点
next
。 - 将当前节点的
next
指针指向prev
,实现反转。 - 移动
prev
和current
指针。
- 保存当前节点的下一个节点
- 循环结束后,将
prev
设置为新的头节点。
打印链表的内容
// 打印链表的内容
void printLinkedList(const LinkedList *list) {
Node *temp = list->head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
- 历链表,逐个打印每个节点的数据。
- 打印完所有节点后,输出一个换行符。
主函数
int main() {
LinkedList list;
initLinkedList(&list);
// 用户输入链表中的元素
int n;
printf("Enter the number of elements in the linked list: ");
scanf("%d", &n);
printf("Enter the elements of the linked list:\n");
for (int i = 0; i < n; i++) {
int value;
scanf("%d", &value);
insertAtTail(&list, value);
}
printf("Original linked list: ");
printLinkedList(&list);
reverseLinkedList(&list);
printf("Reversed linked list: ");
printLinkedList(&list);
// 清理内存
Node *current = list.head;
while (current != NULL) {
Node *temp = current;
current = current->next;
free(temp);
}
return 0;
}
- 初始化一个链表
list
。 - 提示用户输入链表中元素的数量
n
。 - 提示用户输入链表中的每个元素,并调用
insertAtTail
函数将其插入到链表中。 - 打印原始链表的内容。
- 调用
reverseLinkedList
函数逆置链表。 - 再次打印链表的内容,显示逆置后的结果。
- 清理链表中所有节点的内存,防止内存泄漏。
- 返回0,表示程序正常结束。
示例运行
假设用户输入以下数据:
Enter the number of elements in the linked list: 5
Enter the elements of the linked list:
3 1 4 1 5
Original linked list: 3 -> 1 -> 4 -> 1 -> 5 -> NULL
Reversed linked list: 5 -> 1 -> 4 -> 1 -> 3 -> NULL
- 用户输入了5个元素
[3, 1, 4, 1, 5]
。 - 原始链表为
3 -> 1 -> 4 -> 1 -> 5 -> NULL
。 - 逆置链表后,链表变为
5 -> 1 -> 4 -> 1 -> 3 -> NULL
。