用c语言写一个单链表的初始化 
时间: 2023-05-12 22:02:57 浏览: 30
单链表的初始化可以通过以下代码实现:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
typedef struct List {
Node *head;
int length;
} List;
void initList(List *list) {
list->head = (Node *)malloc(sizeof(Node));
list->head->next = NULL;
list->length = 0;
}
int main() {
List list;
initList(&list);
return 0;
}
```
在这个代码中,我们定义了一个 `Node` 结构体,表示链表中的一个节点,包含一个整型数据和一个指向下一个节点的指针。我们还定义了一个 `List` 结构体,表示整个链表,包含一个指向头节点的指针和链表的长度。在 `initList` 函数中,我们为链表分配一个头节点,并将链表的长度初始化为 0。最后,在 `main` 函数中,我们创建了一个 `List` 类型的变量 `list`,并调用 `initList` 函数对其进行初始化。
相关问题
使用C语言写一个单链表队列
好的,以下是使用 C 语言编写单链表队列的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
// 队列元素结构体
typedef struct node {
int data;
struct node *next;
} Node;
// 队列结构体
typedef struct {
Node *front; // 队头指针
Node *rear; // 队尾指针
} Queue;
// 初始化队列
void initQueue(Queue *q) {
q->front = q->rear = NULL;
}
// 判断队列是否为空
int isEmpty(Queue *q) {
return q->front == NULL;
}
// 入队
void enqueue(Queue *q, int data) {
Node *node = (Node*)malloc(sizeof(Node));
if (node == NULL) {
printf("Failed to allocate memory.\n");
exit(EXIT_FAILURE);
}
node->data = data;
node->next = NULL;
if (q->rear == NULL) {
q->front = q->rear = node;
}
else {
q->rear->next = node;
q->rear = node;
}
}
// 出队
int dequeue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
exit(EXIT_FAILURE);
}
Node *node = q->front;
int data = node->data;
q->front = node->next;
if (q->front == NULL) {
q->rear = NULL;
}
free(node);
return data;
}
// 遍历队列
void traverseQueue(Queue *q) {
if (isEmpty(q)) {
printf("Queue is empty.\n");
return;
}
Node *node = q->front;
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
int main() {
Queue q;
initQueue(&q);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
printf("Queue: ");
traverseQueue(&q);
printf("Dequeue: %d\n", dequeue(&q));
printf("Dequeue: %d\n", dequeue(&q));
printf("Queue: ");
traverseQueue(&q);
return 0;
}
```
以上代码实现了一个简单的单链表队列,可以使用 `initQueue` 初始化队列,使用 `isEmpty` 判断队列是否为空,使用 `enqueue` 入队,使用 `dequeue` 出队,使用 `traverseQueue` 遍历队列。
用C语言写一个单链表,要求用后插法
非常好的问题!以下是一个用C语言实现的单链表,使用后插法:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构体
typedef struct node {
int data;
struct node *next;
} Node;
// 定义链表结构体
typedef struct list {
Node *head;
Node *tail;
} List;
// 初始化链表
void initList(List *list) {
list->head = NULL;
list->tail = NULL;
}
// 后插法插入节点
void insertNode(List *list, int data) {
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
if (list->head == NULL) {
list->head = newNode;
list->tail = newNode;
} else {
list->tail->next = newNode;
list->tail = newNode;
}
}
// 打印链表
void printList(List *list) {
Node *current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
List list;
initList(&list);
insertNode(&list, 1);
insertNode(&list, 2);
insertNode(&list, 3);
printList(&list);
return 0;
}
```
希望这个代码能够帮助你!
相关推荐
















