首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

C兑现顺序栈

2013-03-28 
C实现顺序栈/*** Header file for stack** Written by santos at 2013.3.24*/#ifndef STACK_H#define STAC

C实现顺序栈
/*** Header file for stack** Written by santos at 2013.3.24*/#ifndef STACK_H#define STACK_H #define STACK_SIZE 100typedef int ElementType;typedef struct{ElementType data[STACK_SIZE];int top;}Stack;void InitStack(Stack *s);int IsEmpty(Stack *s);int IsFull(Stack *s);void Push(Stack *s, ElementType element);ElementType Pop(Stack *s);ElementType GetTop(Stack *s); #endif

?stack.c

/*** Definition file for stack** Written by santos at 2013.3.24*/#include "stack.h"#include <stdio.h>void InitStack(Stack *s){s->top = -1;}int IsEmpty(Stack *s){if (s->top == -1)return 1;elsereturn 0;}int IsFull(Stack *s){if (s->top == STACK_SIZE - 1)return 1;else return 0;}void Push(Stack *s, ElementType element){if (!IsFull(s)){s->top++;s->data[s->top] = element;}elseprintf("The stack is full!\n");}ElementType Pop(Stack *s){if (!IsEmpty(s)) return s->data[s->top--];else{printf("The stack is empty!\n");return -1;}}ElementType GetTop(Stack *s){if (!IsEmpty(s))return s->data[s->top];else{return -1;}}

?

热点排行