初始化窗口链表C语言完整代码
时间: 2023-07-09 18:33:06 浏览: 48
以下是一个简单的初始化窗口链表的C语言完整代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct window {
int id;
int width;
int height;
struct window* next;
} Window;
int main() {
Window* head = NULL;
Window* current = NULL;
// 创建第一个窗口
Window* w = (Window*)malloc(sizeof(Window));
w->id = 1;
w->width = 640;
w->height = 480;
w->next = NULL;
// 将第一个窗口添加到链表中
head = w;
current = w;
// 创建第二个窗口
w = (Window*)malloc(sizeof(Window));
w->id = 2;
w->width = 800;
w->height = 600;
w->next = NULL;
// 将第二个窗口添加到链表中
current->next = w;
current = w;
// 创建第三个窗口
w = (Window*)malloc(sizeof(Window));
w->id = 3;
w->width = 1024;
w->height = 768;
w->next = NULL;
// 将第三个窗口添加到链表中
current->next = w;
current = w;
// 输出链表中的窗口信息
printf("Window List:\n");
current = head;
while (current != NULL) {
printf("Window %d: %d x %d\n", current->id, current->width, current->height);
current = current->next;
}
// 释放链表中的内存
current = head;
while (current != NULL) {
head = current->next;
free(current);
current = head;
}
return 0;
}
```
在上面的示例中,定义了一个名为Window的结构体,表示窗口的信息,包含id、width、height和next四个成员变量。其中,next表示下一个窗口的指针。在main函数中,通过malloc函数分配内存来创建三个窗口,并将它们添加到链表中。最后,通过循环遍历链表输出每个窗口的信息,并释放链表中的内存。