首页 > 技术文章 > 第6周学习进度表

mjs123 2016-04-07 23:38 原文

周记 专业学习目标 专业学习时间 博客发表量 人文方面的学习 知识技能总结
6 数据结构与算法,HTML 平均3小时/天 5

《四级单词》

《世界是数字的》

数据结构我们学习了栈,但是觉得自己还理解得不够

,希望能继续努力。

// 栈.cpp : Defines the entry point for the console application.
//
/*1 实现顺序栈的如下功能函数:初始化、入栈、出栈。
2 在主函数中建立一个顺序栈,进栈顺序为A、 B、 C、 D、 E ,出栈顺序为B, C, D, E, A 或进栈顺序为A、 B、 C、 D、 E ,出栈顺序为D、C, E, B、A
(参考教材P63页)*/

#include<stdio.h>
#include"stdlib.h"
#define MaxStackSize 100
#define ElemType char
typedef struct a
{
ElemType stack[MaxStackSize];
int top;
}stack;

void InitStack(stack *s)
{
//if((s=(stack*)malloc(sizeof(stack)))==NULL)
// exit(1);
s->top=-1;
}
int StackEmpty(stack s)
{
if(s.top==-1)
return 1;
else
return 0;
}
void StackPush(stack *s,ElemType elem)
{
if(s->top==MaxStackSize-1)
{
printf("%d","stack is full");
exit(0);
}
else
{
s->top++;
s->stack[s->top]=elem;

}
}
void StackPop(stack *s,ElemType *elem)
{
if(StackEmpty(*s))
{
printf("%d","stack is full");
exit(0);
}
else
*elem=s->stack[s->top--];
}
//进栈顺序为A、 B、 C、 D、 E ,出栈顺序为B, C, D, E, A

int main(int argc, char* argv[])
{
stack *s,p;
s=&p;
InitStack(s);
StackPush(s,'m');
StackPush(s,'A');
StackPush(s,'B');
ElemType e;
StackPop(s,&e);
printf("%c ",e);

StackPush(s,'C');
StackPop(s,&e);
printf("%c ",e);

StackPush(s,'D');
StackPop(s,&e);
printf("%c ",e);

StackPush(s,'E');
StackPop(s,&e);
printf("%c ",e);

StackPop(s,&e);
printf("%c \n",e);

return 0;
}

 

推荐阅读