嵌入式学习第二天

学习日志2
姓名:何尧华 日期:2018.9.11
今日学习任务:
1、什么是栈?
线性结构存储方式:
顺序存储(连续)
链式存储(不连续)
3、栈有哪些信息?
4、理解栈的用法
5、如何编写进栈、出栈、读取栈中元素以及清空栈?

栈的进出方式:先进后出
今日任务完成情况:
学会用栈的用法编写程序 ,按步骤分为:初始化栈、进栈、出栈、判断栈是否为空、获取栈顶元素、清空栈的相关函数编写,完成200多代码量
今天新学到的编程相关指令
→ a 插入(误关,ls -a,删除 rm -rf )
→.h按ESC,shift+: w保存 右击打开终端。
gcc *.c -o main
今日任务完成情况:
基本上能跟上老师的步伐完成任务,有一些生疏。
今日开发中出现的问题汇总:
1、对栈的编写运用需要更加了解,有一些生疏。
2、在编程时有一些基本语法错误导致程序不能编译成功。
今日未解决问题:
编写程序时小问题多总是要回头查找问题 ,对结构体还需要学习。
今日开发收获:每天都有新收获,感觉很充实
今日编写的3个程序:
①stack.h

#ifndef STACK_H
#define STACK_H

#define MAXSIZE     10
#define SUCCESS     1000
#define FAILURE     1001

struct stack
{
    int data[MAXSIZE];
    int top;
};
typedef struct stack S;

#endif

②main.c

#include<stdio.h>
#include"stack.h"

int main()
{
    S stack;
    int ret,i;

    ret = InitStack(&stack);
    if(SUCCESS == ret)
    {
        printf("Init Success!\n");
    }
    else
    {
        printf("Init Failure!\n");
    }
    for(i=0;i<5;i++)
    {
        ret = push(&stack,i + 1);
        if(SUCCESS == ret)
        {
            printf("push %d success!\n",i + 1);

        }
        else
        {
            printf("push failure!\n");
        }
    }

    for(i=0;i<3;i++)
    {
        ret = pop(&stack);
        if(ret == FAILURE)
        {
            printf("pop failure!\n");
        }
        else
        {
            printf("pop %d success!\n",ret);
        }
    }

    ret = EmptyStack(stack);
    if(ret == SUCCESS)
    {
        printf("stack is empty!\n");
    }
    else
    {
        printf("stack is not empty!\n");
    }

    ret = GetTop(stack);
    if(ret == FAILURE)
    {
        printf("Get Top Failure!\n");
    }
    else
    {
        printf("Top %d\n",ret);
    }

    ret = ClearStack(&stack);
    if(ret == FAILURE)
    {
        printf("clear failure!\n");
    }
    else
    {
        printf("clear success!\n");
    }

    ret = EmptyStack(stack);
    if(ret == SUCCESS)
    {
        printf("stack is empty!\n");
    }
    else
    {
        printf("stack is not empty!\n");
    }


    return 0;

}

③stack.c

#include"stack.h"
#include<stdio.h>

int InitStack(S *s)
{
    if(NULL == s)
    {
        return FAILURE;
    }
    s->top = -1;

    return SUCCESS;
}

int push(S *s,int e)
{
    if(NULL == s)
    {
        return FAILURE;
    }

    if(s->top == MAXSIZE - 1)
    {
        return FAILURE;
    }
    s->data[s->top + 1]=e;
    s->top++;

    return SUCCESS;
}

int pop(S *s)
{
    if(NULL == s)
    {
        return FAILURE;
    }
    if(-1 == s->top)
    {
        return FAILURE;
    }
    int e = s->data[s->top];
    s->top--;

    return e;
}

int EmptyStack(S s)
{
    return (s.top == -1) ? SUCCESS : FAILURE;
}

int GetTop(S s)
{
    if(s.top == -1)
    {
        return FAILURE;
    }
    return s.data[s.top];
}

int ClearStack(S *s)
{
    if(NULL == s)
    {
        return FAILURE;
    }
    s->top = -1;

    return SUCCESS;
}

猜你喜欢

转载自blog.csdn.net/weixin_43175816/article/details/82633734