include(stdio.h)
include(stdlib.h)
define OVERFLOW -1
define OK 1
define ERROR 0
define STACK_INIT_SIZE 100
define STACKINCREMENT 10
include(string.h)
typedef struct
{
char *top;
char *base;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base=(char *)malloc(STACK_INIT_SIZE*sizeof(char)); //开辟空间
if(!S->base)
exit(OVERFLOW); //存储分配失败
else
{
(*S).top=S->base;//two ways to stand for
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
}
int DestroyStack(SqStack *S) //销毁栈
{
free(S->base);
(*S).base=NULL;
(*S).top=NULL;
(*S).stacksize=0;
return OK;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)
return OK;
else
return ERROR;
}
int Push(SqStack *S,char e) //插入为新的栈顶元素
{
{
if((S).top-(*S).base>=(*S).stacksize) //栈满 追加存储空间{(*S).base=(SElemType )realloc((*S).base,
S->base=(char *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(char));
if(!S->base)
exit(OVERFLOW); //储存分配失败
S->top=S->base+S->stacksize;
S->stacksize += STACKINCREMENT;
}
*S->top++ = e;
return OK;
}
int Pop(SqStack *S,char *e)
{
if(S->top==S->base)
return ERROR;
else
{
S->top–;
e=S->top;;
return OK;
}
}
main()
{
SqStack S;
char elem;
char e,a[20];
int i=0;
int flag=1;
printf(“int 20,end #”);
do{
scanf(“%c”,&e);
a[i]=e;
i++;
}while(e!=’#’);
if( InitStack(&S))
printf(“stack is ready\n”);
else
printf(“stack is not on\n”);
i=0;e=a[i];
printf(“ready\n”); //将括号存入空间
while(e=='#'&&flag)
{
switch(e)
{
case '(':
case '[':
case '{': Push(&S,e);
i++; e=a[i];
break; //左括号入栈
case ')':
if(!StackEmpty(&S))
{
Pop(&S,&e); //右括号 匹配的话 出栈
if(e!='(')
flag=0;
}
else
flag=0;
i++;e=a[i];
break;
case ']':
if(!StackEmpty(&S))
{
Pop(&S,&e);
if(e!='[')
flag=0;
}
else flag=0;
i++;e=a[i];
break;
case '}':if(!StackEmpty(&S))
{
Pop(&S,&e);
if(e!='{')
flag=0;
}
else
flag=0;
i++;e=a[i];
break;
}
}
if(!StackEmpty(&S))
flag=0;
if(flag==1)
printf("MATCH!\n");
getchar();
}
// main.c
// linkedStack
// Created by apple on 15/2/8.
// Copyright (c) 2015年 APPLE. All rights reserved.
include (stdio.h)
include (stdlib.h)
typedef struct
{
int order;
char *name;
}info;
typedef struct stacknode
{
info stu;
struct stacknode *next;
}StackNode;
typedef struct{
StackNode *top;
}LinkStack;
void initStack(LinkStack *S)
{
S->top = NULL;
}
int StackEmpty(LinkStack *S)
{
return S->top == NULL;
}
void push(LinkStack *S,info stu)
{
StackNode p = (StackNode )malloc(sizeof(StackNode));
p->stu.order = stu.order;
p->stu.name = stu.name;
p->next = S->top;
S->top = p;
}
info Pop(LinkStack *S)
{
info stu;
StackNode *p = S->top;
if(StackEmpty(S))
{
printf(“stack underflow\n”);
}
else
{
stu = p->stu;
S->top = p->next;
free(p);
}
return stu;
}
int main(int argc, const char * argv[])
{
int i;
LinkStack S;
info ma;
initStack(&S);
ma = Pop(&S);
if(ma.name)
printf(“%d %s\n”,ma.order,ma.name);
info stu[3] = {{3,”mayonghua”},{4,”guyanan”},{5,”mayue”}};
for(i = 0; i < 3; i++)
push(&S, stu[i]);
for(i = 0; i < 3; i++){
ma = Pop(&S);
printf(“%d %s\n”,ma.order,ma.name);
}
return 0;
}
include
include
define SIZE 100
typedef struct{
int age;
char name[20];
}student;
typedef struct stack
{
student data[SIZE];
int top;
}sqStack;
sqStack *initStack()
{
sqStack *p;
p = (sqStack *)malloc(sizeof(sqStack));
if(p)
{
p->top = 0;
return p;
}
else
return NULL;
}
int isFullStack(sqStack *S)
{
return (S->top + 1 == SIZE);
}
void pushStack(sqStack *S,student data)
{
if(S->top + 1 == SIZE)
{
printf(“stack is full\n”);
exit(-1);
}
S->data[S->top] = data;
S->top++;
}
int isEmptyStack(sqStack *S)
{
return (S->top == 0);
}
student popStack(sqStack *S)
{
student p;
if(S->top == 0)
{
printf(“stack is Empty\n”);
exit(-1);
}
else
{
p = S->data[–S->top];
}
return p;
}
int main()
{
sqStack *S;
int i;
S = initStack();
student ma;
student student[3] = {{22,”马永华”},{23,”马悦”},{33,”李小龙”}};
for(i = 0; i < 3; i++)
pushStack(S,student[i]);
for(i = 0; i < 3; i++)
{
ma = popStack(S);
printf(“%d %s\n”,ma.age,ma.name);
}
ma = popStack(S);
printf(“%d %s\n”,ma.age,ma.name);
return 0;
}