数据结构实验二:栈和队列的基本操作和应用

栈——进制的转换

#include <iostream.h>
#include <malloc.h>
#include <stdlib.h>
typedef  struct{
     int *  base
     int *top;
     int stacksize;
}Stack;
int InitStack(Stack &S)
{
    S. base = ( int *) malloc(STACK_INIT_SIZE* sizeof( int));
     if (!S. base) exit(OVERFLOW);
    S.top = S. base;
    S.stacksize = STACK_INIT_SIZE;
     return  1;
}
int Push(Stack &S,  int e )
{
     if (S.top-S. base >= S.stacksize)
    {
        S. base = ( int *) realloc(S. base, (S.stacksize + T) * sizeof( int));
         if (!S. base) exit(OVERFLOW);
        S.top = S. base + S.stacksize;
        S.stacksize += T;
    }
    *S.top++ = e;
     return  1;
}
int Pop(Stack &S,  int &e)
{
     if (S.top == S. basereturn  0;
    e = *--S.top;
     return  1;
}
int StackEmpty(Stack S)
{
     if (S.top == S. base)
     return  1;
     else  return  0;
}
void conversion( int N,  int r)
{
    Stack S;
     int e; 
    InitStack(S);
     while(N)
    {
        Push(S, N%r);
        N = N/r;
    }
}
while(!StackEmpty(S)){  
  Pop(S, e);
    cout << e;
}
void main()
{
     int N, r;
    cin >> N;
    cin >> r;
    cout << N <<  " 由十进制转换为 " << r <<  " 进制后的值为: ";
    conversion(N, r);
    cout << endl;
}

 

 

 

 

 

 

括号匹配检验
#include<iostream.h>
#include<string.h>
typedef struct node{
    elemtype data;
    struct node *next;
}stack;
stack *top;
void push(elemtype n){
    stack *p;
    p=new stack;
    p->data=n;
    p->next=top->next;
    top->next=p;
}
void pop(){
    stack *q;
    q=top->next;
    top->next=top->next->next;
    delete q;
}
elemtype gettop(){
    return top->next->data;
}
void judge(char a[]){
    int i,tag=0,len=strlen(a);//tag==1 
    for(i=0;i<len;i++)
    if(a[i]=='('||a[i]=='['||a[i]=='{')
        {push(a[i]);continue;}
        if(top->next==NULL)
        {
            cout<<"No"<<endl;
            break;
            tag=1;
        }
        if(a[i]!=')'&&a[i]!=']'&&a[i]!='}')
        continue;
    if(a[i]-gettop()==1||a[i]-gettop()==2)
        pop();
        else
        {
            cout<<"No"<<endl;
            break;
            tag=1;
        }
    }
    if(tag==0&&i==len)
    {
        if(top->next==NULL)
        cout<<"Yes"<<endl;
        else
        cout<<"No"<<endl;
    }
    top->next=NULL;
}
int main()
{
    top=new stack;
    top->next=NULL;
    char a[100];
    int n;
    cin>>n; 
    while(n--){
        cin>>a;
        judge(a);
    }
    return 0;

复制代码

博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/

对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com


本文转自ma6174博客园博客,原文链接:http://www.cnblogs.com/ma6174/archive/2012/01/05/2313307.html ,如需转载请自行联系原作者
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值