#include <string.h>
#include <iostream>
#include <iomanip>
using namespace std;
typedef char elementType;
typedef struct snode
{
elementType data[MAXLEN];
int top;
}Stack;
void initstack(Stack & s)
{
s.top = -1;
}
bool stackFull(Stack &s)
{
if(s.top==MAXLEN-1)
return true;
else
return false;
}
void push(Stack &s, elementType &x)
{
if(stackFull(s))
return;
else
{
s.top++;
s.data[s.top] = x;
return;
}
}
bool stackEmpty(Stack &s)
{
if(s.top==-1)
return true;
else
return false;
}
bool pop(Stack & s, elementType &x)
{
if(stackEmpty(s))
return false;
else
{
x = s.data[s.top];
s.top--;
return true;
}
}
char top(Stack &s)
{
elementType x;
if(stackEmpty(s))
return ' ';
else
{
x = s.data[s.top];
return x;
}
}
void DeConversion(int n, int mod)
{
Stack S;
char rem, x;
initstack(S);
while(n!=0)
{
rem = n % mod + '0';
push(S, rem);
n = n / mod;
}
while(!stackEmpty(S))
{
pop(S, x);
if(x-'0'>=10)
{
char c = 'A'+x-'9'-1;
cout<<c;
}
else cout<<x;
}
}
bool BracketMatch(string str)
{
Stack s;
initstack(s);
int len;
int i = 0;
char x;
bool tag = true, result = false;
len = str.length();
while(i<len && tag == true)
{
switch(str[i])
{
case '(':
case '[':
case '{':
push(s,str[i]);
break;
case ')':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
top(s);
if(x == '(')
{
pop(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
case ']':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
if(top(s) == '[')
{
pop(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
case '}':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
if(top(s) == '{')
{
pop(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
default:
break;
}
i++;
}
if(stackEmpty(s) && tag == true)
result= true;
else
result = false;
return result;
}
bool IsPopOrder( string &s, string &d)
{
if (s.size()==0 || d.size() == 0||s.size()!=d.size())
return false;
int i = 0;
int j = 0;
char x;
Stack sta;
initstack(sta);
int len = d.size();
while(j<len)
{
while ( stackEmpty(sta) || top(sta) != d[j])
{
if (i == len)
return false;
push(sta, s[i]);
i++;
}
pop(sta, x);
j++;
}
return true;
}
void print(Stack &s, const int N)
{
for(int i=0; i<N; i++)
cout<<setw(3)<<s.data[i];
cout<<endl;
}
void GetAllSequence(char* input, int i, const int N, Stack &stk, Stack &seq)
{
char x;
if (i == N)
{
if (!stackEmpty(stk))
{
char Top = top(stk);
push(seq, Top);
pop(stk,x);
GetAllSequence(input, i, N, stk, seq);
push(stk, Top);
pop(seq, x);
}
else
print(seq, N);
}
else if(i<N)
{
push(stk, input[i]);
GetAllSequence(input, i+1, N, stk, seq);
pop(stk, x);
if (!stackEmpty(stk))
{
char Top = top(stk);
pop(stk, x);
push(seq, Top);
GetAllSequence(input, i, N, stk, seq);
pop(seq,x);
push(stk,Top);
}
}
}
链栈
#include <iostream>
using namespace std;
typedef char elementType;
typedef struct node
{
elementType data;
struct node * next;
}seqstack, *linkedstack;
void initstack(seqstack *& top)
{
top->next = NULL;
}
void pushstack(seqstack *& top, elementType &x)
{
seqstack *s = new seqstack;
s->data = x;
s->next = top->next;
top->next = s;
}
bool stackEmpty(seqstack *top)
{
if(top->next == NULL)
return true;
else
return false;
}
bool popstack(seqstack *& top, elementType &x)
{
seqstack *u;
if(top->next == NULL)
return false;
else
{
x = top->next->data;
u = top->next;
top->next = u->next;
delete u;
return true;
}
}
elementType stackTop(seqstack * top, elementType &x)
{
if(stackEmpty(top))
return -1;
else
{
x = top->next->data;
return x;
}
}
void deConversion(int n, int mod)
{
seqstack *S;
S = new seqstack;
elementType rem, x;
initstack(S);
while(n!=0)
{
rem = n % mod + '0';
pushstack(S, rem);
n = n / mod;
}
while(!stackEmpty(S))
{
popstack(S, x);
if(x-'0'>=10)
{
char c = 'A'+x-'9'-1;
cout<<c;
}
else cout<<x;
}
}
bool bracketMatch(string str)
{
seqstack *s = new seqstack;
initstack(s);
int len;
int i = 0;
char x;
bool tag = true, result = false;
len = str.length();
while(i<len && tag == true)
{
switch(str[i])
{
case '(':
case '[':
case '{':
pushstack(s,str[i]);
break;
case ')':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
stackTop(s, x);
if(x == '(')
{
popstack(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
case ']':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
stackTop(s, x);
if(x == '[')
{
popstack(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
case '}':
if(stackEmpty(s))
{
tag = false;
result = false;
break;
}
stackTop(s, x);
if(x == '{')
{
popstack(s, x);
break;
}
else
{
tag = false;
result = false;
break;
}
default:
break;
}
i++;
}
if(stackEmpty(s) && tag == true)
result= true;
else
result = false;
return result;
}