栈:
特点:
栈是限定仅在表尾进行操作的结构
先进后出,后进先出
栈顶:
栈顶允许被操作
栈底:
栈底不允许被操作
按照物理机构设计栈分为:顺序栈 链式栈
顺序栈:
seqstack.h
#ifndef _SEQSTACK_H_
#define _SEQSTACK_H_
typedef struct person {
char name[32];
char sex;
int age;
int score;
}DATATYPE;
typedef struct list {
DATATYPE *head;
int tlen;
int top;
}SeqStack;
SeqStack* CreateSeqStack(int len);
int DestroySeqStack(SeqStack* ss);
int PushSeqStack(SeqStack* ss, DATATYPE* data);
int PopSeqStack(SeqStack*ss);
DATATYPE* GetTopSeqStack(SeqStack* ss);
int IsEmptySeqStack(SeqStack* ss);
int IsFullSeqStack(SeqStack* ss);
int GetSizeSeqStack(SeqStack*ss);
#endif
seqstack.c
#include "seqstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
SeqStack* CreateSeqStack(int len)
{
SeqStack* ss = (SeqStack*)malloc(sizeof(SeqStack));
if(NULL == ss)
{
perror("CreateSeqStack malloc 1");
return NULL;
}
ss->head =(DATATYPE*) malloc(sizeof(DATATYPE)*len);
if(NULL == ss->head )
{
perror("CreateSeqStack malloc 1");
return NULL;
}
ss->tlen = len;
ss->top = 0;
return ss;
}
int PushSeqStack(SeqStack* ss, DATATYPE* data)
{
if(IsFullSeqStack(ss))
{
return 1;
}
memcpy(&ss->head[ss->top++],data,sizeof(DATATYPE));
return 0;
}
int PopSeqStack(SeqStack*ss)
{
if(IsEmptySeqStack(ss))
{
return 1;
}
ss->top--;
return 0;
}
int IsFullSeqStack(SeqStack* ss)
{
return ss->top == ss->tlen ;
}
int IsEmptySeqStack(SeqStack* ss)
{
return 0 == ss->top ;
}
DATATYPE* GetTopSeqStack(SeqStack* ss)
{
if(IsEmptySeqStack(ss))
{
return NULL;
}
return &ss->head[ss->top -1];
}
int GetSizeSeqStack(SeqStack*ss)
{
return ss->top;
}
示例:
实现({【括号匹配
#include "seqstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int do_check(char *buf,SeqStack* ss,int row)
{
DATATYPE data;
int col =1;
DATATYPE* tmp=NULL;
while(*buf)
{
//memset(&data,0,sizeof(data));
bzero(&data,sizeof(data));
switch(*buf)
{
case '(':
data.sym = *buf;
data.row = row;
data.col = col;
PushSeqStack(ss,&data);
break;
case '[':
data.sym = *buf;
data.row = row;
data.col = col;
PushSeqStack(ss,&data);
break;
case '{':
data.sym = *buf;
data.row = row;
data.col = col;
PushSeqStack(ss,&data);
break;
case ')':
tmp= GetTopSeqStack(ss);
if(NULL == tmp)
{
printf("buf sym:%c row:%d col:%d\n",*buf,row,col);
return 1;
}
if('('==tmp->sym )
{
PopSeqStack(ss);
}
else
{
printf("1.top error sym:%c row:%d col:%d or 2 buf sym:%c row:%d col%d\n",
tmp->sym ,tmp->row ,tmp->col,*buf,row,col );
return 1;
}
break;
case ']':
tmp= GetTopSeqStack(ss);
if(NULL == tmp)
{
printf("buf sym:%c row:%d col:%d\n",*buf,row,col);
return 1;
}
if('['==tmp->sym )
{
PopSeqStack(ss);
}
else
{
printf("1.top error sym:%c row:%d col:%d or 2 buf sym:%c row:%d col%d\n",
tmp->sym ,tmp->row ,tmp->col,*buf,row,col );
return 1;
}
break;
case '}':
tmp= GetTopSeqStack(ss);
if(NULL == tmp)
{
printf("buf sym:%c row:%d col:%d\n",*buf,row,col);
return 1;
}
if('{'==tmp->sym )
{
PopSeqStack(ss);
}
else
{
printf("1.top error sym:%c row:%d col:%d or 2 buf sym:%c row:%d col%d\n",
tmp->sym ,tmp->row ,tmp->col,*buf,row,col );
return 1;
}
break;
}
buf++;
col++;
}
return 0;
}
int main(int argc, char *argv[])
{
SeqStack* ss = CreateSeqStack(50);
FILE* fp = fopen("/home/linux/1.c","r");
if(NULL == fp)
{
perror("fopen");
exit(1);
}
int flag = 0;
int row =1;
while(1)
{
char buf[512]={0};
if(fgets(buf,sizeof(buf),fp))
{
int ret = do_check(buf,ss,row);
if(ret!= 0 )
{
flag = 1;
break;
}
}
else
{
break;
}
row++;
}
if(1 == flag)
{
return 0;
}
else
{
if(IsEmptySeqStack(ss))
{
printf("ok!");
}
else
{
DATATYPE* tmp = GetTopSeqStack(ss);
printf("%c row:%d col:%d\n",tmp->sym
,tmp->row ,tmp->col);
}
}
return 0;
}
链式栈:
linkstack.h
#ifndef _LINKSTACK_H_
#define _LINKSTACK_H_
typedef struct {
char name[32];
char sex;
int age;
int score;
}DATATYPE;
typedef struct stack_node {
DATATYPE data;
struct stack_node *next;
}LinkStackNode;
typedef struct {
LinkStackNode* top;
int clen;
}LinkStackList;
LinkStackList* CreateLinkStack();
int DestroyLinkStackList(LinkStackList**ls);
int PushLinkStack(LinkStackList* ls, DATATYPE*data);
int PopLinkStack(LinkStackList*ls);
DATATYPE* GetTopLinkStack(LinkStackList*ls);
int IsEmtpyLinkStack(LinkStackList* ls);
int GetSizeLinkStack(LinkStackList*ls);
#endif
linkstack.c
#include "linkstack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
LinkStackList* CreateLinkStack()
{
LinkStackList* ls = (LinkStackList*)malloc(sizeof(LinkStackList));
if(NULL == ls)
{
perror("CreateLinkStack malloc");
return NULL;
}
ls->top = NULL;
ls->clen = 0 ;
return ls;
}
int DestroyLinkStackList(LinkStackList**ls)
{
int len = GetSizeLinkStack(*ls);
int i = 0 ;
for(i = 0 ;i<len;i++)
{
PopLinkStack(*ls);
}
free(*ls);
*ls =NULL;
return 0;
}
int PushLinkStack(LinkStackList* ls, DATATYPE*data)
{
LinkStackNode* newnode = (LinkStackNode*)malloc(sizeof(LinkStackNode));
if(NULL == newnode)
{
perror("PushLinkStack malloc");
return 1;
}
memcpy(&newnode->data ,data,sizeof(DATATYPE));
newnode->next = NULL;
if(IsEmtpyLinkStack(ls))
{
ls->top = newnode;
}
else
{
newnode->next = ls->top ;
ls->top = newnode;
}
ls->clen ++;
return 0;
}
int PopLinkStack(LinkStackList*ls)
{
if(IsEmtpyLinkStack(ls))
{
return 1;
}
LinkStackNode* tmp = ls->top ;
ls->top = ls->top ->next ;
free(tmp);
ls->clen --;
return 0;
}
DATATYPE* GetTopLinkStack(LinkStackList*ls)
{
if(IsEmtpyLinkStack(ls))
{
return NULL;
}
return &ls->top->data ;
}
int IsEmtpyLinkStack(LinkStackList* ls)
{
return 0 == ls->clen ;
}
int GetSizeLinkStack(LinkStackList*ls)
{
return ls->clen ;
}
示例:
实现30*2+5四则运算 简单计算器
#include "linkstack.h"
#include <stdio.h>
#include <string.h>
//30*2+5
//
int num =0;
void get_num(char c)
{
num = num*10 +c-'0' ;
}
int get_pority(char c)
{
switch(c)
{
case '+':
return 1;
case '-':
return 2;
case '*':
return 3;
case '/':
return 4;
}
return -1;
}
int get_result(int num1,int num2,char op)
{
switch(op)
{
case '+':
return num1+num2;
case '-':
return num1-num2;
case '*':
return num1*num2;
case '/':
return num1/num2;
}
return 0;
}
int main(int argc, char *argv[])
{
char buf[]="30*2+5";
LinkStackList* ls_num = CreateLinkStack();
LinkStackList* ls_sym = CreateLinkStack();
char* tmp = buf;
DATATYPE data;
int flag = 0 ;
while(*tmp)
{
bzero(&data,sizeof(data));
if(*tmp>='0' && *tmp<='9')
{
get_num(*tmp);
tmp++;
continue;
}
if(0 == flag)
{
data.num = num;
PushLinkStack(ls_num,&data);
num=0;
}
DATATYPE* top =GetTopLinkStack(ls_sym);
if(IsEmtpyLinkStack(ls_sym) || get_pority(top->sym) < get_pority(*tmp) )
{
bzero(&data,sizeof(data));
data.sym = *tmp;
PushLinkStack(ls_sym,&data);
flag = 0 ;
}
else
{
DATATYPE* top_num = GetTopLinkStack(ls_num);
int num2 = top_num->num;
PopLinkStack(ls_num);
top_num = GetTopLinkStack(ls_num);
int num1 =top_num->num;
PopLinkStack(ls_num);
DATATYPE* top_sym = GetTopLinkStack(ls_sym);
char op=top_sym ->sym;
int result = get_result(num1,num2,op);
PopLinkStack(ls_sym);
bzero(&data,sizeof(data));
data.num = result;
PushLinkStack(ls_num,&data);
flag = 1;
continue;
}
tmp++;
}
bzero(&data,sizeof(data));
data.num = num;
PushLinkStack(ls_num ,&data);
while(!IsEmtpyLinkStack(ls_sym))
{
DATATYPE* top_num = GetTopLinkStack(ls_num);
int num2 = top_num->num;
PopLinkStack(ls_num);
top_num = GetTopLinkStack(ls_num);
int num1 =top_num->num;
PopLinkStack(ls_num);
DATATYPE* top_sym = GetTopLinkStack(ls_sym);
char op=top_sym ->sym;
int result = get_result(num1,num2,op);
PopLinkStack(ls_sym);
bzero(&data,sizeof(data));
data.num = result;
PushLinkStack(ls_num,&data);
}
DATATYPE* top_result = GetTopLinkStack(ls_num);
printf("result is %d\n",top_result->num);
return 0;
}