15章:链表

链表可以看成将容器分解成若干小的容器(节点)
1:结构体:将结构添加到链表中
struct book
{
    int num;
    float price;
    struct book *next;
};
将对象添加到链表
class book
{
    int num;
    float price;
    book *next;
};
静态链表在运行前就已经确定好
C语言动态链表的操作:malloc申请内存  free释放
void *p = malloc(sizeof(book))
free(p)
c++动态链表的操作:new和delete
2:动态链表的建立
class book
{
public:
    int num;
    float price;
    book *next;
};
book *head = NULL;
book* create()
{
    book *p1,*p2;
    p1 = new book;
    p2 = p1;
    cout<<"请输入图书的编号,0结束"<<endl;
    cin>>p1->num;
    if (p1->num!=0)
    {
        cout<<"请输入图书的价格"<<endl;
        cin>>p1->price;
    }
    else
    {
        deletep1;
        p1=NULL;
        p2=NULL;
        head=NULL;
        return;
    }
    while (p1->num!=0)
    {
        p2 = p1;
        p1 = new book;
        cout<<"请输入图书的编号,0结束"<<endl;
        cin>>p1->num;
        if (p1->num!=0)
        {
            cout<<"请输入图书的价格"<<endl;
            cin>>p1->price;
        }
        p2->next = p1;
    }
    delete p1;
    p2->next = NULL;
}
3:检查用户是不是输入的是字符串
bool check(string str)
{
    for(int i =0; i < str.Length(); i++)
    {
        if ( (str[i]>'9'||str[i]<'0') && str[i]!='.') 
            return false;
    }
    return true
}
4:走迷宫 8*8的墙壁
找迷宫的入口遍历二维数组array[8][8](0空白)(1砖)(2入口) (3出口)
变量p,n保存入口索引
for (int i =0 ;i < 8 ; i++)
{
    for (int j = 0 ; j < 8 ; j++)
    {
        if (array[i][j] == 2){
            n= i;
            p = j;  
            break;
        }
    }
}
键盘处理函数:四个方向dir=0,1,2,3
坐标x,y
动画图片索引:index = 0
键盘向下处理代码:dir = 0
图片的尺寸:93*100
void onKeyDown()
{
    y+=100
    //判断人物走过的是不是墙壁
    int k = (n*93+x)/93
    int p = (p*100+y)/100
    //等于1表示的是墙壁
    if (array[p][k] == 1)
    {
        y -= 100;人物回到原来的位置
    }
    --显示图片
    if (dir == 0)
    {
        index++;
        if (index == 4)
        {
            index = 0;
        }
    }
    else
    {
        dir = 0;
        index = 0;
    }
    //用链表记录向下移动的节点
    ptr->next = (list*)malloc(sizeof(list));
    ptr->next->m =n;
    ptr->next->n =p;
    ptr->next->x = x;
    ptr->next->y = y;
    preptr = ptr;//preptr记录新节点的上一个节点
    ptr->next->next = NULL;
    ptr = ptr->next;
    ptr->back = preptr;ptr的上一个结点




    键盘向上处理代码 dir = 1
    y-=100
    //判断人物走过的是不是墙壁
    int k = (n*93+x)/93
    int p = (p*100+y)/100
    //等于1表示的是墙壁
    if (array[p][k] == 1)
    {
        y += 100;人物回到原来的位置
    }
    if (dir == 1)
    {
        index++;
        if (index == 4)
        {
            index = 0;
        }
    }
    else
    {
        dir = 1;
        index = 0;
    }
    //用链表记录向上移动的节点
    ptr->next = (list*)malloc(sizeof(list));
    ptr->next->m =n;
    ptr->next->n =p;
    ptr->next->x = x;
    ptr->next->y = y;
    preptr = ptr;//preptr记录新节点的上一个节点
    ptr->next->next = NULL;
    ptr = ptr->next;
    ptr->back = preptr


    键盘向左处理代码 dir = 2
    x-=93
    //判断人物走过的是不是墙壁
    int k = (n*93+x)/93
    int p = (p*100+y)/100
    //等于1表示的是墙壁
    if (array[p][k] == 1)
    {
        x += 93;人物回到原来的位置
    }
    if (dir == 2)
    {
        index++;
        if (index == 4)
        {
            index = 0;
        }
    }
    else
    {
        dir = 2;
        index = 0;
    }
    //用链表记录向左移动的节点
    ptr->next = (list*)malloc(sizeof(list));
    ptr->next->m =n;
    ptr->next->n =p;
    ptr->next->x = x;
    ptr->next->y = y;
    preptr = ptr;//preptr记录新节点的上一个节点
    ptr->next->next = NULL;
    ptr = ptr->next;
    ptr->back = preptr




    键盘向右处理代码 dir = 3
    x+=93
    //判断人物走过的是不是墙壁
    int k = (n*93+x)/93
    int p = (p*100+y)/100
    //等于1表示的是墙壁
    if (array[p][k] == 1)
    {
        x -= 100;人物回到原来的位置
    }
    if (dir == 3)
    {
        index++;
        if (index == 4)
        {
            index = 0;
        }
    }
    else
    {
        dir = 3;
        index = 0;
    }
    //用链表记录向右移动的节点
    ptr->next = (list*)malloc(sizeof(list));
    ptr->next->m =n;
    ptr->next->n =p;
    ptr->next->x = x;
    ptr->next->y = y;
    preptr = ptr;//preptr记录新节点的上一个节点
    ptr->next->next = NULL;
    ptr = ptr->next;
    ptr->back = preptr




    //判断人物走出去了
    int k = (n*93+x)/93
    int p = (p*100+y)/100
    //等于1表示的是墙壁
    if (array[p][k] == 3)
    {
        printf("出去了")
        //用链表记录出去的节点
        ptr->next = (list*)malloc(sizeof(list));
        ptr->next->m =n;
        ptr->next->n =p;
        ptr->next->x = x;
        ptr->next->y = y;
        preptr = ptr;//preptr记录新节点的上一个节点
        ptr->next->next = NULL;
        ptr = ptr->next;
        ptr->back = preptr
    }
}
用链表来记录行走的路线
struct list
{
    int n;//行
    int m;//列
    int x;//x
    int y;//y
    struct list *next;//指向下一个节点的指针
    struct list *back;//指向上一个节点的指针
    bitrmap *bit;//走的图片
};
list *first;//保存第一个节点的地址
list *preptr;//保存上一个节点的地址
list *ptr;//保存的是当前节点的地址,在构造函数时创建节点保存在ptr
//遍历链表
void Go()
{
    m = first->m;
    n = first->n;
    x = first->x;
    y = first->y;
    //墙壁 
    mdc->selectObject(wall)
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (array[i][j] == 1)
            {
                //墙壁图
            }
        }
    }
    mdc->selectObject(first->bitmap)
    if (first->next == NULL)
    {
        go = false;//执行GO函数的标志
    }
    else
    {
        first= first->next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值