LinkedList 类
Coddy 链表:数据结构系列第 5 课 课程的第 4 课,共 14 课。
挑战
简单编写一个类 LinkedList,并定义一个不接收任何输入的构造函数。
初始化两个字段:
- 将
head设置为null(列表一开始为空)。 - 将
count设置为 0。
保留你在上一课中编写的 Node 类。
亲手试试
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linkedlist.h"
int main() {
LinkedList ll;
LinkedList_init(&ll);
char line[256];
while (fgets(line, sizeof(line), stdin)) {
line[strcspn(line, "\r\n")] = '\0';
char* cmd = strtok(line, " \t");
if (!cmd) continue;
if (strcmp(cmd, "state") == 0) printf("%s %d\n", ll.head == NULL ? "true" : "false", ll.count);
}
return 0;
}
链表:数据结构系列第 5 课中的所有课
自己练习: C 在线编译器