链表初级
程序员文章站
2022-03-11 21:48:15
...
增删改查
#ifndef _LinkList_H_
#define _LinkList_H_
typedef struct
{
int x;
int y;
int dir;
}DATA;
//节点
struct NODE
{
DATA data;
NODE* next;
};
void CreateLinkList();//创建
bool InsertLinkList(int xb, DATA data);//插入
void PushbackLinkList(DATA data); //添加到末尾
bool EraseLinkList(int xb);//删除指定
int FindDataLinkList(DATA data,bool (*bj)(DATA&, DATA&),
int startxb = 0 );//查找数据
DATA* GetDataLinkList(int xb);//获取
void ClearLinkList();//清空
int GetLenLinkList();//获取长度
void DestroyLinkList();//销毁
void SortLinkList(bool (*bj)(DATA&, DATA&));//排序
void draw();
void run();
#endif
#include <iostream>
#include<windows.h>
#include "LinkList.h"
#define W 20
#define H 20
#define S (W*H)
NODE* head;
int len = 0;
int px = 5;
int py = 5;
int zdir = 0;
void CreateLinkList()
{
head = (NODE*)malloc(sizeof(NODE));
head->next = 0;
}
void PushbackLinkList(DATA data)
{
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = data;
newnode->next = 0;
NODE* p = head;
while (p->next)
{
p = p->next;
len++;
}
p->next = newnode;
}
bool InsertLinkList(int xb, DATA data)
{
if (xb<0 || xb>len - 1)
{
return false;
}
NODE* p = head;
int tlen = 0;
while (p->next)
{
p = p->next;
tlen++;
if (tlen == xb - 1)
{
NODE* np = p;
NODE* newnode = (NODE*)malloc(sizeof(NODE));
newnode->data = data;
newnode->next = np;
p = newnode;
break;
}
}
return true;
}
bool EraseLinkList(int xb)//删除指定
{
if (xb < 0 || xb > GetLenLinkList() - 1)
return false;
NODE* p = head;
for (int i = 0; i < xb; ++i)
p = p->next;
NODE* pnn = p->next->next;
free(p->next);
p->next = pnn;
return true;
}
//int FindDataLinkList(DATA data,bool (*bj)(DATA&, DATA&),
// int startxb);//查找数据
//DATA* GetDataLinkList(int xb);//获取
void ClearLinkList()//清空
{
NODE* p = head->next;
while (p)
{
NODE* n = p->next;
free(p);
p = n;
}
head->next = 0;
}
int GetLenLinkList()//获取长度
{
int len = 0;
NODE* p = head->next;
while (p)
{
p = p->next;
len++;
}
return len;
}
void DestroyLinkList()//销毁
{
NODE* p = head;
while (p)
{
NODE* n = p->next;
free(p);
p = n;
}
}
void draw()
{
int map[S] = {};
const char* ARR[3] = { "□","●","○" };
int len = GetLenLinkList();
NODE*p = head->next;
map[px + py * W] = 1;
for (int i = 0; p; ++i)
{
map[p->data.x + p->data.y*W] = 2;
p = p->next;
}
for (int i = 0; i < S; i++)
{
std::cout << ARR[map[i]];
if (i%W == W - 1)std::cout << std::endl;
}
}
void run()
{
if (GetAsyncKeyState('W') & 1)
{
py--;
zdir = 0;
}
else if (GetAsyncKeyState('S') & 1)
{
py++;
zdir = 1;
}
else if (GetAsyncKeyState('A') & 1)
{
px--;
zdir = 2;
}
else if (GetAsyncKeyState('D') & 1)
{
px++;
zdir = 3;
}
else if (GetAsyncKeyState(' ') & 1)
{
DATA a;
a.x = px;
a.y = py;
a.dir = zdir;
PushbackLinkList(a);
}
//子弹移动
NODE* p = head->next;
int ttlen = GetLenLinkList();
for (int i = 0; i<ttlen; ++i)
{
switch (p->data.dir)
{
case 0:p->data.y--;
if (p->data.y < 0)
{
EraseLinkList(i);
ttlen--;
} break;
case 1:p->data.y++;
if (p->data.y > H - 1)
{
EraseLinkList(i);
ttlen--;
}break;
case 2:p->data.x--;
if (p->data.x < 0)
{
EraseLinkList(i);
ttlen--;
} break;
case 3:p->data.x++;
if (p->data.x > W - 1)
{
EraseLinkList(i);
ttlen--;
}break;
}
p = p->next;
}
}
void SortLinkList(bool (*bj)(DATA&, DATA&));//排序
#include <iostream>
#include "LinkList.h"
void main()
{
//创建
CreateLinkList();
while (1)
{
system("cls");
draw();
run();
}
}