| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 921 人关注过本帖, 1 人收藏
标题:数据结构试验——栈与队列
只看楼主 加入收藏
晓婷长月
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2013-6-4
收藏(1)
 问题点数:0 回复次数:1 
数据结构试验——栈与队列
数据结构试验——栈与队列
程序代码:
#include<iostream>
using namespace std;
typedef int Elemtype;
typedef struct snode{
    Elemtype data;
    struct snode *next;
}Linkstack;
void Initstack(Linkstack **top)
{*top=(Linkstack *)malloc(sizeof(Linkstack));

 (*top)->next=NULL;

 cout<<"栈初始化成功!\n";
}
int push(Linkstack **top,Elemtype x)
{
    Linkstack *s;
    s=(Linkstack*)malloc(sizeof(Linkstack));
    s->data=x;
    s->next=(*top)->next;
    (*top)->next=s;
    return 1;

}
int Empty(Linkstack **top)
{return ((*top)->next==NULL?1:0);}
int pop(Linkstack **top,Elemtype *x)
{
    Linkstack *s;
    if(Empty(top))
    {
        cout<<"\n 栈为空!";
        return 0;
    }
        s=(*top)->next;
        *x=s->data;
        (*top)->next=s->next;
        free(s);
        return 1;
}
//队列功能的实现
typedef struct qnode{
    Elemtype data;
    struct qnode *next;
}qtype;
typedef struct sqtr{
    qtype *front,*rear;
}squeue;
void Initqueue(squeue *LQ)
{
    qtype *p;
    p=(qtype *)malloc(sizeof(qtype));
    p->next=NULL;
    LQ->front=LQ->rear=p;
    cout<<"队列初始化成功!\n";
}
int Enqueue(squeue *LQ,Elemtype x)
{
    qtype *s;
    s=(qtype *)malloc(sizeof(qtype));
    s->data=x;
    s->next=LQ->rear->next;
    LQ->rear->next=s;
    LQ->rear=s;
    return 1;
}
int Empty(squeue *LQ)
{return(LQ->front==LQ->rear?1:0);}
int Outqueue(squeue *LQ,Elemtype *x)
{
    qtype *p;
    if(Empty(LQ))
    {
        cout<<"队列为空!";
        return 0;
    }
    p=LQ->front->next;
    *x=p->data;
    LQ->front->next=p->next;
    if(LQ->front->next==NULL)
        LQ->rear=LQ->front;
    free(p);
    return 1;
}

void Dectoothers(int n,int b)
{
    char B[]="0123456789ABCDEF";
    Elemtype a,c;
    Linkstack *top;
    squeue LQ;
    Initstack (&top);
    Initqueue(&LQ);
    cout<<n<<"转化为"<<b<<"进制数为:";
    while(n)
    {
        push(&top,n%b);
        n=n/b;
    }
    while(!Empty(&top))
    {
        pop(&top,&a);
        Enqueue(&LQ,a);
        Outqueue(&LQ,&c);
        cout<<B[c]<<' ';
    }

    cout<<endl;
}
int main()
{ 
    char s;
    int n,b;
    do
    {
      cout<<"请输入要转化成其他进制数的非负数:";
      cin>>n;
      cout<<"请输入需要转化成的进制:";
      cin>>b;
      Dectoothers(n,b);
      cout<<"需要结束请输入N/n,继续输入Y/y:";
      cin>>s;
    }while((s!='n') && (s!='N'));
      return 0;
}


2013-06-16 03:00
晓婷长月
Rank: 1
等 级:新手上路
帖 子:61
专家分:0
注 册:2013-6-4
收藏
得分:0 
全部资料文件

栈与队列.rar (1.36 KB)
2013-06-16 03:00
快速回复:数据结构试验——栈与队列
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018948 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved