使用需明白,我提供的代码并不是给大家抄的,而是给大家一个思路来做题。如果你一开始就会意错了的话(只会抄的话),你的水平永远不会得到大的飞跃,如果你还对自己有严格的要求的话,就可以略微参考一下。代码有什么不好或不对的地方欢迎提出。有什么不懂的地方可以尽可能的提出来,我会做解答。可以当面叫我解答(提供给我小组的成员)。(天啊!说的好官方啊!快被自己唬住了!)
数据结构实验之栈一:进制转换
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
stack<int> s;
void binary(int n, int m)
{
while ( n != 0 )
{
s.push(n%m);
n = n/m;
}
}
void display()
{
while ( !s.empty() )
{
printf ( "%d", s.top());
s.pop();
}
}
int main()
{
int n, m;
scanf ( "%d", &n );
scanf ( "%d", &m );
binary(n, m);
display();
return 0;
}
数据结构实验之栈二:一般算术表达式转换成后缀式
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <iostream>
using namespace std;
stack<char> s;
char ch[10005];
int main()
{
int i;
scanf ( "%s", ch );
int len = strlen(ch);
for ( i = 0;i < len; i++ )
{
if ( ch[i] == '#' )
break;
if ( ch[i] >= 'a'&&ch[i] <= 'z' )
printf ( "%c", ch[i] );
else if ( ch[i] == '/'||ch[i] == '*' )
{
if ( s.empty() )
s.push(ch[i]);
else
{
while ( !s.empty() && ( s.top() == '*'||s.top() == '/' ) )
{
printf ( "%c", s.top() );
s.pop();
}
s.push(ch[i]);
}
}
else if ( ch[i] == '+'||ch[i] == '-' )
{
if ( s.empty() )
s.push(ch[i]);
else
{
while ( !s.empty() && s.top() != '(' )
{
printf ( "%c", s.top() );
s.pop();
}
s.push(ch[i]);
}
}
else if ( ch[i] == '(' )
s.push(ch[i]);
else
{
while ( s.top() != '(' )
{
printf ( "%c", s.top() );
s.pop();
}
s.pop();
}
}
while ( !s.empty() )
{
printf ( "%c", s.top() );
s.pop();
}
return 0;
}
数据结构实验之栈三:后缀式求值
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
int main()
{
string ch;
int s[100005];
int i, top = -1;
cin >> ch;
int len = ch.length();
for ( i = 0;i < len; i++ )
{
if ( ch[i] == '*' )
s[top-1] = s[top-1]*s[top--];
else if ( ch[i] == '+' )
s[top-1] = s[top-1]+s[top--];
else if ( ch[i] == '-' )
s[top-1] = s[top-1]-s[top--];
else if ( ch[i] == '/' )
s[top-1] = s[top-1]/s[top--];
else if ( ch[i] == '#')
break;
else
s[++top] = ch[i]-'0';
}
printf ( "%d", s[0] ) ;
}
数据结构实验之栈四:括号匹配
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
stack<char> s;
void inti()
{
while ( !s.empty() )
{
s.pop();
}
}
int Store(char *ch, int len)
{
int i;
for ( i = 0;i < len; i++ )
{
if ( ch[i] == '(' || ch[i] == '[' || ch[i] == '{' )
{
s.push(ch[i]);
}
else if ( ch[i] == ')' )
{
if ( s.empty() || s.top() != '(' )
return 0;
else
s.pop();
}
else if ( ch[i] == ']' )
{
if ( s.empty() || s.top() != '[' )
return 0;
else
s.pop();
}
else if ( ch[i] == '}' )
{
if ( s.empty() || s.top() != '{' )
return 0;
else
s.pop();
}
}
if ( !s.empty() )
return 0;
return 1;
}
int main()
{
char ch[100];
while ( gets(ch) )
{
int len = strlen(ch);
inti();
int ok = Store(ch, len);
if ( ok )
printf ( "yes\n" );
else
printf ( "no\n" );
}
return 0;
}
数据结构实验之栈五:下一较大值(一)
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
struct node
{
int id;
int num;
int next;
}a[1005];
stack<node> s;
void inti()
{
while ( !s.empty() )
s.pop();
}
int main()
{
int n, m, i;
int T;
scanf ( "%d", &T );
while ( T-- )
{
inti();
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
{
scanf ( "%d", &a[i].num );
a[i].id = i;
a[i].next = -1;
if ( s.empty() )
s.push(a[i]);
else
{
while ( !s.empty()&&a[i].num > s.top().num )
{
a[s.top().id].next = a[i].num;
s.pop();
}
s.push(a[i]);
}
}
for ( i = 0;i < n; i++ )
{
printf ( "%d-->%d\n", a[i].num, a[i].next );
}
printf ( "\n" );
}
return 0;
}
数据结构实验之栈六:下一较大值(二)
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
struct node
{
int id;
int num;
int next;
}a[100005];
stack<node> s;
void inti()
{
while ( !s.empty() )
s.pop();
}
int main()
{
int n, m, i;
int T;
scanf ( "%d", &T );
while ( T-- )
{
inti();
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
{
scanf ( "%d", &a[i].num );
a[i].id = i;
a[i].next = -1;
if ( s.empty() )
s.push(a[i]);
else
{
while ( !s.empty()&&a[i].num > s.top().num )
{
a[s.top().id].next = a[i].num;
s.pop();
}
s.push(a[i]);
}
}
for ( i = 0;i < n; i++ )
{
printf ( "%d-->%d\n", a[i].num, a[i].next );
}
printf ( "\n" );
}
return 0;
}
数据结构实验之栈七:出栈序列判定
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
stack<int>s;
int a[10005];
int b[10005];
int main()
{
int n, i;
int T;
scanf ( "%d", &n );
for ( i = 0;i < n; i++ )
scanf ( "%d", &a[i] );
scanf ( "%d", &T );
while ( T-- )
{
for ( i = 0;i < n; i++ )
scanf ( "%d", &b[i] );
int A = 0, B = 0;
int ok = 1;
while ( B < n )
{
if ( a[A] == b[B] )
{
A++;
B++;
}
else if ( !s.empty()&&s.top() == b[B] )
{
s.pop();
B++;
}
else if ( A < n )
{
s.push(a[A]);
A++;
}
else
{
ok = 0;
break;
}
}
printf ( ok ? "yes\n" : "no\n" );
}
return 0;
}
数据结构实验之栈八:栈的基本操作
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;
stack<int> s;
void inti()
{
while ( !s.empty() )
{
s.pop();
}
}
int main()
{
int n, m;
int T, i;
char ch[12];
scanf ( "%d", &T );
while ( T-- )
{
inti();
scanf ( "%d %d", &n, &m );
for ( i = 0; i < m; i++ )
{
scanf ( "%s", ch );
if ( strcmp(ch, "A") == 0 )
{
if ( s.empty() )
printf ( "E\n" );
else
printf ( "%d\n", s.top() );
}
else if ( strcmp(ch, "P") == 0 )
{
int t;
scanf ( "%d", &t );
if ( s.size() == n )
printf ( "F\n" );
else
s.push(t);
}
else if ( strcmp(ch, "O") == 0 )
{
if ( s.empty() )
printf ( "E\n" );
else
{
printf ( "%d\n", s.top() );
s.pop();
}
}
}
if ( T != 0 )
printf ( "\n" );
}
return 0;
}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!