写一个c语言链表输出程序
时间: 2023-05-23 09:01:14 浏览: 53
好的,下面是一个简单的C语言链表输出程序:
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点类型
struct Node {
int data;
struct Node* next;
};
int main() {
// 创建链表节点
struct Node *first = (struct Node*) malloc(sizeof(struct Node));
struct Node *second = (struct Node*) malloc(sizeof(struct Node));
struct Node *third = (struct Node*) malloc(sizeof(struct Node));
// 给每个节点赋值
first->data = 1;
first->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
// 遍历链表输出每个节点的值
struct Node *p = first;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
return 0;
}
相关问题
帮写一个C语言程序实现链表的基本操作
以下是一个基本的链表操作的C语言程序:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
// 插入节点
void insert(int data) {
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = data;
new_node->next = head;
head = new_node;
}
// 删除节点
void delete(int data) {
struct node *current_node = head;
struct node *previous_node = NULL;
while (current_node != NULL && current_node->data != data) {
previous_node = current_node;
current_node = current_node->next;
}
if (current_node == NULL) {
printf("节点不存在\n");
return;
}
if (previous_node == NULL) {
head = current_node->next;
} else {
previous_node->next = current_node->next;
}
free(current_node);
}
// 查找节点
void search(int data) {
struct node *current_node = head;
while (current_node != NULL && current_node->data != data) {
current_node = current_node->next;
}
if (current_node == NULL) {
printf("节点不存在\n");
return;
}
printf("节点 %d 存在\n", data);
}
// 打印链表
void display() {
struct node *current_node = head;
while (current_node != NULL) {
printf("%d ", current_node->data);
current_node = current_node->next;
}
printf("\n");
}
int main() {
insert(1);
insert(2);
insert(3);
insert(4);
printf("初始链表:\n");
display();
delete(3);
printf("删除节点 3 后的链表:\n");
display();
search(2);
return 0;
}
```
程序中定义了一个结构体 `node`,包含了一个整型数据 `data` 和一个指向下一个节点的指针 `next`。定义了头结点 `head`,初始为 `NULL`。
`insert` 函数用于在链表头部插入一个节点。首先使用 `malloc` 分配一块内存,然后将要插入节点的数据存储到 `data` 中,将原头结点的地址存储到新节点的 `next` 中,最后将新节点的地址存储到头结点 `head` 中。
`delete` 函数用于删除一个节点,需要遍历链表查找要删除的节点。当找到要删除的节点时,将该节点的 `next` 存储到前一个节点的 `next` 中,最后释放要删除的节点的内存。
`search` 函数用于查找链表中是否存在某个节点。遍历链表查找该节点,如果找到了,输出节点存在的信息。
`display` 函数用于打印整个链表,遍历链表并输出每个节点的数据。
在 `main` 函数中,定义了一些节点并插入到链表中,然后调用 `delete` 函数删除一个节点,最后调用 `search` 函数查找一个节点,并调用 `display` 函数打印整个链表。
用C语言写一个双向链表
以下是一个双向链表的基本实现,包含节点的插入、删除、遍历等操作:
```
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *prev;
struct node *next;
};
struct node *head = NULL;
void insert(int value) {
struct node *new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = value;
new_node->prev = NULL;
new_node->next = head;
if (head != NULL) {
head->prev = new_node;
}
head = new_node;
}
void delete(int value) {
struct node *current = head;
while (current != NULL && current->data != value) {
current = current->next;
}
if (current == NULL) {
return;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
void traverse() {
struct node *current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
insert(3);
insert(5);
insert(7);
insert(9);
traverse();
delete(5);
traverse();
delete(9);
traverse();
return 0;
}
```
在这个实现中,我们使用了一个结构体 `node` 来表示链表的节点。每个节点包含一个整数值 `data`,以及两个指针 `prev` 和 `next`,分别指向前一个节点和后一个节点。初始时,我们设置 `head` 为 `NULL`,表示链表为空。
在 `insert` 函数中,我们首先创建一个新节点,并将它的 `data` 值设置为传入的参数。然后,我们将新节点的 `prev` 指针设置为 `NULL`,因为新节点是链表的头节点。接着,我们将新节点的 `next` 指针设置为原来的头节点,并将头节点的 `prev` 指针设置为新节点,这样就完成了节点的插入操作。
在 `delete` 函数中,我们首先遍历链表,找到节点的值等于传入参数的节点,如果找到了,我们就将它的前驱节点的 `next` 指针指向它的后继节点,将它的后继节点的 `prev` 指针指向它的前驱节点,这样就完成了节点的删除操作。
在 `traverse` 函数中,我们遍历整个链表,依次输出每个节点的 `data` 值。
在 `main` 函数中,我们插入了四个节点,并调用 `traverse` 函数输出整个链表。然后,我们删除了值为 5 和 9 的节点,并再次调用 `traverse` 函数输出剩余的节点。最后,程序返回 0,表示正常结束。