c语言编写在带头结点的动态单链表和静态单链表上实现线性表操作LENGTH(L)的算法,并将长度写入头结点的数据域中的源代码
时间: 2024-05-12 10:18:50 浏览: 106
C语言写的带有头结点的链表
动态单链表:
```c
typedef struct Node {
int data;
struct Node* next;
} Node;
typedef struct List {
Node* head;
int length;
} List;
void initList(List* L) {
L->head = (Node*)malloc(sizeof(Node));
L->head->next = NULL;
L->length = 0;
}
int length(List* L) {
return L->length;
}
```
静态单链表:
```c
#define MAXSIZE 100
typedef struct Node {
int data;
int next;
} Node;
typedef struct List {
Node nodes[MAXSIZE];
int head;
int length;
} List;
void initList(List* L) {
for (int i = 0; i < MAXSIZE; i++) {
L->nodes[i].next = i + 1;
}
L->nodes[MAXSIZE - 1].next = -1;
L->head = -1;
L->length = 0;
}
int length(List* L) {
return L->length;
}
```
阅读全文