刷B站看到的,做个玩玩。IDE:Visual Studio 2022。依赖EsayX图形库
1-效果
2-程序
/*
链表流星雨单文件版本
依赖EsayX图形库
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <graphics.h>
typedef struct Rain
{
int x;
int y;
char str[20];
}ElemType;
//定义节点
typedef struct LNode
{
struct LNode* next;
ElemType data;
}Node, * LinkList;
//链表函数
LinkList createList();
void push_back(LinkList list, ElemType val);
void pop_back(LinkList list);
void showAll(LinkList list);
bool empty(LinkList);
//数字雨函数
//初始化数字雨
void initRain(LinkList list, int size)
{
for (int i = 0; i < size; i++)
{
struct Rain newRain;
newRain.x = i * 15;
newRain.y = rand() % getheight();
for (int i = 0; i < 19; i++)
{
newRain.str[i] = rand() % 26 + 'A';
}
push_back(list, newRain);
}
}
//绘制数字雨
void drawRain(LinkList list)
{
LinkList curNode = list->next;
while (curNode)
{
for (int i = 0; i < 19; i++)
{
settextcolor(RGB(0, 255 - i * 13, 0));
outtextxy(curNode->data.x, curNode->data.y - i * 15, curNode->data.str[i]);
}
curNode = curNode->next;
}
}
//移动一根数字雨
void moveRain(LinkList node)
{
node->data.y++;
if (node->data.y > getheight())
{
node->data.y = 0;
}
}
typedef void(*FUN)(LinkList node);//函数指针
//移动所有数字雨
void moveAllRain(LinkList list, FUN fun)
{
LinkList curNode = list;
while (curNode)
{
fun(curNode);
curNode = curNode->next;
}
}
int main()
{
//创建一个图形窗口
initgraph(960, 640);
//初始化一个链表
LinkList list = createList();
//初始化数字雨,70根
initRain(list, 70);
BeginBatchDraw();
while (true)
{
cleardevice();
//绘制数字雨
drawRain(list);
//移动所有数字雨
moveAllRain(list, moveRain);
FlushBatchDraw();
}
EndBatchDraw();
}
//创建带头节点的链表
LinkList createList()
{
LinkList headNode = (LinkList)calloc(1, sizeof(Node));
if (!headNode)
return NULL;
return headNode;
}
//往链表中插入元素
void push_back(LinkList list, ElemType val)
{
LinkList newNode = (LinkList)calloc(1, sizeof(Node));
if (!newNode)
return;
newNode->data = val;
LinkList curNode = list;
while (curNode->next != NULL)
{
curNode = curNode->next;
}
curNode->next = newNode;
}
//删除尾部元素
void pop_back(LinkList list)
{
if (empty(list))
return;
LinkList curNode = list;
while (curNode->next->next != NULL)
{
curNode = curNode->next;
}
free(curNode->next);
curNode->next = NULL;
}
//遍历所有元素
void showAll(LinkList list)
{
LinkList curNode = list->next;
while (curNode)
{
//printf("%d ", curNode->data);
curNode = curNode->next;
}
//printf("\n");
}
//判断链表是否为空
bool empty(LinkList list)
{
return list->next == NULL;
}
标签:LinkList,void,list,C语言,链表,next,EsayX,data,curNode From: https://www.cnblogs.com/FishSmallWorld/p/17089938.html