用c语言写一个静态链表遍历函数
时间: 2024-05-09 19:20:12 浏览: 9
下面是一个简单的静态链表遍历函数的示例代码:
```
#include <stdio.h>
#define MAX_SIZE 100
typedef struct {
int data;
int next;
} Node;
void traverseList(Node list[], int head) {
int p = head;
while (p != -1) {
printf("%d ", list[p].data);
p = list[p].next;
}
}
int main() {
Node list[MAX_SIZE] = {
{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, -1}
};
traverseList(list, 0);
return 0;
}
```
在这个示例中,我们定义了一个 `Node` 结构体,其中包含了数据和下一个元素的索引。在 `traverseList` 函数中,我们从链表的头节点开始,遍历整个链表,并输出每个节点的数据。在主函数中,我们定义了一个静态链表,然后调用 `traverseList` 函数来遍历它。
相关问题
c语言中怎么合并两个静态链表
在C语言中,可以通过以下步骤合并两个静态链表:
1. 定义一个新的静态链表,作为合并后的链表。
2. 遍历第一个链表,将其中的元素添加到新的链表中。
3. 遍历第二个链表,将其中的元素添加到新的链表中(注意要修改指针指向)。
4. 返回新的链表。
具体的代码实现如下:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct {
int data;
int next;
} Node;
int MergeList(Node list1[], Node list2[]) {
int head1 = list1[0].next;
int head2 = list2[0].next;
int head = 0; // 新链表的头节点
int tail = 0; // 新链表的尾节点
// 如果有一个链表为空,则直接返回另一个链表
if (head1 == 0) {
return head2;
}
if (head2 == 0) {
return head1;
}
// 处理第一个节点
if (list1[head1].data < list2[head2].data) {
head = head1;
tail = head1;
head1 = list1[head1].next;
} else {
head = head2;
tail = head2;
head2 = list2[head2].next;
}
// 合并两个链表
while (head1 != 0 && head2 != 0) {
if (list1[head1].data < list2[head2].data) {
list1[tail].next = head1;
tail = head1;
head1 = list1[head1].next;
} else {
list2[tail].next = head2;
tail = head2;
head2 = list2[head2].next;
}
}
if (head1 == 0) {
list2[tail].next = head2;
} else {
list1[tail].next = head1;
}
return head;
}
int main() {
// 初始化两个链表
Node list1[MAXSIZE] = {{0, 3}, {14, 5}, {23, 2}, {37, 6}, {65, 0}, {78, 4}, {89, 1}};
Node list2[MAXSIZE] = {{0, 4}, {16, 3}, {27, 1}, {39, 5}, {55, 0}, {84, 2}, {93, 6}};
// 合并链表并输出结果
int head = MergeList(list1, list2);
printf("Merged List: ");
while (head != 0) {
printf("%d ", list1[head].data);
head = list1[head].next;
}
printf("\n");
return 0;
}
```
在上面的代码中,我们定义了一个 `Node` 结构体来表示静态链表中的每个节点,其中 `data` 表示节点的数据,`next` 表示指向下一个节点的指针。我们通过 `MergeList` 函数来合并两个静态链表,其中 `list1` 和 `list2` 分别表示两个待合并的链表。函数运行结束后,返回合并后的链表头节点的位置。在主函数中,我们调用 `MergeList` 函数来合并两个链表,并打印输出结果。
需要注意的是,由于静态链表的节点是通过数组实现的,因此在合并过程中需要修改指针指向。具体来说,我们需要维护一个 `tail` 变量来表示新链表的尾节点,然后在每次添加节点时将 `tail` 指向当前节点,并更新 `tail` 的值。最后,当其中一个链表已经遍历完时,我们需要将另一个链表中剩余的节点直接添加到新链表中。
c语言链表静态分配内存
链表是一种常见的数据结构,它可以用于存储一系列的元素,并且可以动态地添加或删除元素。在C语言中,链表可以使用静态内存分配或动态内存分配来实现。静态内存分配是指在编译时就确定了链表的大小,并在程序运行时分配固定大小的内存空间。下面是一个使用静态内存分配实现链表的例子:
```c
#include <stdio.h>
#define MAX_SIZE 100
struct Node {
int data;
int next;
} nodes[MAX_SIZE];
int head = -1;
int tail = -1;
int count = 0;
void add(int data) {
if (count >= MAX_SIZE) {
printf("List is full\n");
return;
}
int index = tail + 1;
nodes[index].data = data;
nodes[index].next = -1;
if (count == 0) {
head = index;
} else {
nodes[tail].next = index;
}
tail = index;
count++;
}
void print() {
int index = head;
while (index != -1) {
printf("%d ", nodes[index].data);
index = nodes[index].next;
}
printf("\n");
}
int main() {
add(1);
add(2);
add(3);
print(); // 输出:1 2 3
return 0;
}
```
在上面的例子中,我们使用了一个结构体数组来存储链表的节点,每个节点包含一个数据和一个指向下一个节点的指针。我们使用head和tail两个变量来记录链表的头和尾,count变量来记录链表的长度。在add函数中,我们首先检查链表是否已满,然后在尾部添加一个新节点,并更新tail指针和count变量。在print函数中,我们从头开始遍历链表,并输出每个节点的数据。