Re: 串行

楼主: wwndbk (黑人问号)   2022-12-08 22:02:05
※ 引述《Sugarwithkis (sugarwithkis)》之铭言:
: 附合结构
: 指标
: 到底是三小
: 我是不是理解力有障碍
: 明明都有在听课
: 要自己写都不知道怎么下手
: 呜哇哇阿阿
:
:帮我写一段用结构实作链结的c语言程式码
#include <stdio.h>
#include <stdlib.h>
// 定义节点的结构体
struct Node {
int data; // 节点所保存的资料
struct Node *next; // 指向下一个节点的指标
};
// 定义链结串行的结构体
struct LinkedList {
struct Node *head; // 指向第一个节点的指标
struct Node *tail; // 指向最后一个节点的指标
};
// 初始化链结串行
void init(struct LinkedList *list) {
list->head = NULL;
list->tail = NULL;
}
// 在链结串行的最后添加一个新节点
void append(struct LinkedList *list, int data) {
// 建立新节点
struct Node *node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
// 如果链结串行为空,设定head和tail指向新节点
if (list->head == NULL) {
list->head = node;
list->tail = node;
}
// 否则,将tail的next指标指向新节点,并将tail指向新节点
else {
list->tail->next = node;
list->tail = node;
}
}
// 打印链结串行的所有元素
void printList(struct LinkedList *list) {
struct Node *current = list->head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
// 建立链结串行
struct LinkedList list;
init(&list);
// 在链结串行末尾添加几个节点
append(&list, 1);
append(&list, 2);
append(&list, 3);
append(&list, 4);
// 打印链结串行
printList(&list);
return 0;
}
虽然我看不懂在写什么 对阿

Links booklink

Contact Us: admin [ a t ] ucptt.com