以下是棧的一些基本操作:
#define STACK_INIT_SIZE 10
#define STACKINCREMENT 2
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
} Sqstack;
int InitStack(Sqstack &S)
{
if(!(S.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType))))
exit(OVERFLOW);
S.top=S.base;
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 ClearStack(Sqstack &S)
{
S.top=S.base;
return OK;
}
int StackEmpty(Sqstack S)
{
if(S.top==S.base)
return TRUE;
else
return FALSE;
}
int StackLength(Sqstack S)
{
return S.top-S.base;
}
int GetTop(Sqstack S,SElemType &e)
{
if(S.top>S.base)
{
e=*(S.top-1);
return OK;
}
else
return ERROR;
}
int Push(Sqstack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*(S.top)++=e;
return OK;
}
int Pop(Sqstack &S,SElemType &e)
{
if(S.top==S.base)
return FALSE;
e=*--S.top;
return OK;
}
int StackTraverse(Sqstack &S,SElemType (*visit)(SElemType))
{
while(S.top>S.base)
visit(*S.base++);
return OK;
}
1.數制轉換:
將一個十進制轉換成八,十六進制數。
typedef int SElemType; // 定義棧元素類型為整型
#include"c1.h"
//#include"c3-1.h" // 采用順序棧
#include"c2.cpp" // 利用順序棧的基本操作
/*void conversion()
{ // 對于輸入的任意一個非負10進制整數,打印輸出與其等值的16進制數
SqStack s;
unsigned n; // 非負整數
SElemType e;
InitStack(s); // 初始化棧
printf("n(>=0)=");
scanf("%u",&n); // 輸入非負十進制整數n
while(n) // 當n不等于0
{
Push(s,n%16); // 入棧n除以16的余數(16進制的低位)
n=n/16;
}
while(!StackEmpty(s)) // 當棧不空
{
Pop(s,e); // 彈出棧頂元素且賦值給e
if(e<=9)
printf("%d",e);
else
printf("%c",e+55);
}
printf("\n");
}*/
void conversion()
{
Sqstack S;
unsigned N;
SElemType e;
InitStack(S);
scanf("%u",&N);
while(N)
{
Push(S,N%8);
N=N/8;
}
while(!StackEmpty(S))
{
Pop(S,e);
printf("%d",e);
}
}
void main()
{
conversion();
}
2.括號配對的檢驗:
typedef char SElemType;
#include"c1.h"
#include"c2.cpp"
void matching()
{
Sqstack S;
InitStack(S);
char *p,a[100],e;
int state;
printf("請輸入一組符號(僅只能含有三種{},(),[]):");
gets(a);
p=a;
state=1;
while(*p && state)
{
if(!(*p=='{' || *p=='}' || *p=='[' || *p==']' || *p=='(' || *p==')'))
{
printf("輸入不正確!");
exit(ERROR);
}
switch (*p)
{
case '(':
case '{':
case '[':{
Push(S,*p++);
break;
}
case ')':{
GetTop(S,e);
if(!StackEmpty(S) && e=='(')
{
Pop(S,e);
p++;
}
else
state=0;
break;
}
case ']':{
GetTop(S,e);
if(!StackEmpty(S) && e=='[')
{
Pop(S,e);
p++;
}
else
state=0;
break;
}
case '}':{
GetTop(S,e);
if(!StackEmpty(S) && e=='{')
{
Pop(S,e);
p++;
}
else
state=0;
break;
}
}
}
if(StackEmpty(S) && state)
printf("輸入的括號相符合!");
else
printf("輸入的括號不符合!");
}
/*void check()
{ // 對于輸入的任意一個字符串,檢驗括號是否配對
Sqstack s;
SElemType ch[80],*p,e;
if(InitStack(s)) // 初始化棧成功
{
printf("請輸入表達式\n");
gets(ch);
p=ch;
while(*p) // 沒到串尾
switch(*p)
{
case '(':
case '[':Push(s,*p++);
break; // 左括號入棧,且p++
case ')':
case ']':if(!StackEmpty(s)) // 棧不空
{
Pop(s,e); // 彈出棧頂元素
if(*p==')'&&e!='('||*p==']'&&e!='[') // 彈出的棧頂元素與*p不配對
{
printf("左右括號不配對\n");
exit(ERROR);
}
else
{
p++;
break; // 跳出switch語句
}
}
else // ???/SPAN>
{
printf("缺乏左括號\n");
exit(ERROR);
}
default: p++; // 其它字符不處理,指針向后移
}
if(StackEmpty(s)) // 字符串結束時???/SPAN>
printf("括號匹配\n");
else
printf("缺乏右括號\n");
}
}*/
void main()
{
//char a[]={[(]])]};
matching();
//check();
}