一、基本概念
栈是限定仅在表尾进行插入和删除操作的线性表。
先进后出、后进先出
栈顶:允许操作的一端
栈底:不允许操作的一端
重要操作:入栈,出栈。(相当于增加删除元素)
顺序栈 链式栈
1.创建 CreateSeqStack
2.销毁 DestroySeqStack
3.判断是否为空栈 IsEmptySeqStack
4.判断是否为满栈 IsFullSeqStack
5.压栈 PushSeqStack
6.出栈 PopSeqStack
seqstack.h
typedef struct{
char name[32];
char sex;
int age;
int score;
}DATATYPE;
typedef struct
{
DATATYPE* head;
int tlen;
int top;// clen
}SeqStack;
1.SeqStack*CreateSeqStack(int size);
2.int DestroySeqStack(SeqStack*ss);
3.int PushSeqStack(SeqStack*ss,DATATYPE*data);
4.int PopSeqStack(SeqStack*ss);
5.int IsEmptySeqStack(SeqStack*ss);
6.int IsFullSeqStack(SeqStack*ss);
7.int GetSizeSeqStack(SeqStack*ss);
8.DATATYPE*GetTopSeqStack(SeqStack*ss);
1. CreateSeqStack 创建顺序栈
SeqStack *CreateSeqStack(int size) { SeqStack* ss = ( SeqStack*)malloc(sizeof(SeqStack)); if(NULL ==ss) { perror("CreateSeqStack error malloc1"); return NULL; } ss->head = ( DATATYPE*)malloc(sizeof(DATATYPE)*size); if(NULL ==ss->head) { perror("CreateSeqStack error malloc2"); return NULL; } ss->tlen = size; ss->top = 0; return ss; }
2. DestroySeqStack 销毁顺序栈
int DestroySeqStack(SeqStack *ss) { free(ss->head); free(ss); return 0; }
3. PushSeqStack 入栈
int PushSeqStack(SeqStack *ss, DATATYPE *data) { if(NULL == ss ||NULL ==data) { fprintf(stderr,"SeqStack or data is null \n"); return 1; } if(IsFullSeqStack(ss)) { fprintf(stderr,"PushSeqStack full\n"); return 1; } memcpy(&ss->head[ss->top],data,sizeof(DATATYPE)); ss->top++; return 0; }
4. PopSeqStack 出栈
int PopSeqStack(SeqStack *ss) { if(NULL == ss ) { fprintf(stderr,"SeqStack is null \n"); return 1; } if(IsEmptySeqStack(ss)) { fprintf(stderr,"PopSeqStack is empty \n"); return 1; } ss->top--; return 0; }
5. IsEmptySeqStack 判断顺序栈空
int IsEmptySeqStack(SeqStack *ss) { return 0 == ss->top; }
6. IsFullSeqStack 判断顺序栈满
int IsFullSeqStack(SeqStack *ss) { return ss->top == ss->tlen; }
7. GetSizeSeqStack 获得顺序栈大小//元素个数
int GetSizeSeqStack(SeqStack *ss) { return ss->top; }
8. GetTopSeqStack 获得栈顶元素
DATATYPE *GetTopSeqStack(SeqStack *ss) { if(IsEmptySeqStack(ss)) { return NULL; } return &ss->head[ss->top-1]; }
二、练习
使用顺序栈,完成,c语言源文件,{},(),[ ],符号匹配问题。
seqstack.h
#ifndef SEQSTACK_H
#define SEQSTACK_H
typedef struct{
char sym;
int col;
int row;
}DATATYPE;
typedef struct
{
DATATYPE* head;
int tlen;
int top;// clen
}SeqStack;
SeqStack*CreateSeqStack(int size);
int DestroySeqStack(SeqStack*ss);
int PushSeqStack(SeqStack*ss,DATATYPE*data);
int PopSeqStack(SeqStack*ss);
int IsEmptySeqStack(SeqStack*ss);
int IsFullSeqStack(SeqStack*ss);
int GetSizeSeqStack(SeqStack*ss);
DATATYPE*GetTopSeqStack(SeqStack*ss);
#endif // SEQSTACK_H
seqstack.c
#include "seqstack.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
SeqStack *CreateSeqStack(int size)
{
SeqStack* ss = ( SeqStack*)malloc(sizeof(SeqStack));
if(NULL ==ss)
{
perror("CreateSeqStack error malloc1");
return NULL;
}
ss->head = ( DATATYPE*)malloc(sizeof(DATATYPE)*size);
if(NULL ==ss->head)
{
perror("CreateSeqStack error malloc2");
return NULL;
}
ss->tlen = size;
ss->top = 0;
return ss;
}
int PushSeqStack(SeqStack *ss, DATATYPE *data)
{
if(NULL == ss ||NULL ==data)
{
fprintf(stderr,"SeqStack or data is null \n");
return 1;
}
if(IsFullSeqStack(ss))
{
fprintf(stderr,"PushSeqStack full\n");
return 1;
}
memcpy(&ss->head[ss->top],data,sizeof(DATATYPE));
ss->top++;
return 0;
}
int PopSeqStack(SeqStack *ss)
{
if(NULL == ss )
{
fprintf(stderr,"SeqStack is null \n");
return 1;
}
if(IsEmptySeqStack(ss))
{
fprintf(stderr,"PopSeqStack is empty \n");
return 1;
}
ss->top--;
return 0;
}
int IsEmptySeqStack(SeqStack *ss)
{
return 0 == ss->top;
}
int IsFullSeqStack(SeqStack *ss)
{
return ss->top == ss->tlen;
}
DATATYPE *GetTopSeqStack(SeqStack *ss)
{
if(IsEmptySeqStack(ss))
{
return NULL;
}
return &ss->head[ss->top-1];
}
int DestroySeqStack(SeqStack *ss)
{
free(ss->head);
free(ss);
return 0;
}
int GetSizeSeqStack(SeqStack *ss)
{
return ss->top;
}
main.c
#include <stdio.h>
#include "seqstack.h"
#include <string.h>
#include <stdlib.h>
int do_handle(char *linebuf ,SeqStack* ss,int row)
{
DATATYPE* tmp =NULL;
DATATYPE data;
bzero(&data,sizeof(data));
int col = 1;
while(*linebuf)
{
char c = *linebuf;
switch (c)
{
case '(':
case '[':
case '{':
data.sym = c;
data.col = col;
data.row = row;
PushSeqStack(ss,&data);
break;
case ')':
tmp = GetTopSeqStack(ss);
if(NULL != tmp &&'(' == tmp->sym)
{
PopSeqStack(ss);
}
else
{
if(NULL == tmp)
{
fprintf(stderr," ')' error row:%d col:%d \n",row,col);
}
else
{
fprintf(stderr," ')' error row:%d col:%d or stack top sym:%c row:%d col:%d\n",row,col
,tmp->sym,tmp->row,tmp->col);
}
return 1;
}
break;
case ']':
tmp = GetTopSeqStack(ss);
if(NULL != tmp &&'[' == tmp->sym)
{
PopSeqStack(ss);
}
else
{
if(NULL == tmp)
{
fprintf(stderr," ']' error row:%d col:%d\n",row,col);
}
else
{
fprintf(stderr," ']' error row:%d col:%d or stack top sym:%c row:%d col:%d\n",row,col
,tmp->sym,tmp->row,tmp->col);
}
return 1;
}
break;
case '}':
tmp = GetTopSeqStack(ss);
if(NULL != tmp &&'{' == tmp->sym)
{
PopSeqStack(ss);
}
else
{
if(NULL == tmp)
{
fprintf(stderr," '}' error row:%d col:%d \n",row,col);
}
else
{
fprintf(stderr," '}' error row:%d col:%d or stack top %c row:%d col:%d\n",row,col
,tmp->sym,tmp->row,tmp->col);
}
return 1;
}
break;
}
linebuf++;
col++;
}
return 0;
}
int main()
{
SeqStack* ss = CreateSeqStack(100);
FILE *fp=fopen("/home/linux/1.c","r");
if(NULL == fp)
{
return 1;
}
int row =1;
int flag = 0;
while(1)
{
char buf[1024]={0};
if(NULL==fgets(buf,sizeof(buf),fp))
{
break;
}
int ret = do_handle(buf,ss,row);
if(1 == ret)
{
flag =1;
break;
}
row++;
}
fclose(fp);
if(1 == flag)
{
DestroySeqStack(ss);
exit(1);
}
if(IsEmptySeqStack(ss))
{
printf("file ok\n");
}
else
{
DATATYPE* tmp = GetTopSeqStack(ss);
fprintf(stderr,"stack top %c row:%d col:%d\n",tmp->sym,tmp->row,tmp->col);
}
DestroySeqStack(ss);
printf("Hello World!\n");
return 0;
}