| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 465 人关注过本帖
标题:请兄长看看我的这个程序咋回事
只看楼主 加入收藏
爱上对方法国
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2010-10-28
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:4 
请兄长看看我的这个程序咋回事
#include<iostream>
#include<cstdlib>
using namespace std;
struct stacknode{
    int data;
    stacknode *next;
};
struct Linkstack{
    stacknode *top;
};
void initstack(stacknode *p);
void push(stacknode *p,int x);
void display(stacknode *p);
int main()
{
    stacknode *p;
    int i,x,n;
    p=new node;
    initstack(p);
    display(p);
    cout<<"输入你要压入栈中的数据元素个数:"<<endl;
    cin>>n;
    cout<<"依次压入的数据为:"<<endl;
    for(i=0;i<n;i++)
    {
        cin>>x;
        push(p,x);
    }
    display(p);
    return 0;
}
void initstack(stacknode *p)
{
    p->next=NULL;
    cout<<"链栈已经初始化!";
}
void push(stacknode *p,int x)
{
    stacknode *s;
    s=new node;
    s->data=x;
    s->next=p->top;
    p->top=s;
}
void display(stacknode *p)
{
    cout<<"链栈中的数据为:"<<endl;
    stacknode *s;
    s=new node;
    s=p->top;
    while(s!=NULL)
    {
        cout<<s->data;
        s=s->next;
    }
    cout<<endl;
}
   
搜索更多相关主题的帖子: 兄长 
2010-10-28 11:34
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:14 
程序代码:
#include<iostream>
#include<cstdlib>
using namespace std;
struct stacknode{
    int data;
    stacknode *next;
    stacknode *top;
};
/*
struct Linkstack{
    stacknode *top;   // 没有必要定义 这个结构
};
*/
void initstack(stacknode *p);
void push(stacknode *p,int x);
void display(stacknode *p);
int main()
{
    stacknode *p;
    int i,x,n;
    p=new stacknode;                 // 没呀node 这个类型,下同
    initstack(p);
    display(p);
    cout<<"输入你要压入栈中的数据元素个数:"<<endl;
    cin>>n;
    cout<<"依次压入的数据为:"<<endl;
    for(i=0;i<n;i++)
    {
        cin>>x;
        push(p,x);
    }
    display(p);
    return 0;
}
void initstack(stacknode *p)
{
    p->top =NULL;                  // 应该把 top 令为 NULL
    cout<<"链栈已经初始化!";
}
void push(stacknode *p,int x)
{
    stacknode *s;
    s=new stacknode;
    s->data=x;
    s->next=p->top;
    p->top=s;

}
void display(stacknode *p)
{
    cout<<"链栈中的数据为:"<<endl;
    stacknode *s;
    s=new stacknode;
    s=p->top;
    while(s!=NULL)
    {
        cout<<s->data<<"\t";
        s=s->next;
    }
    cout<<endl;
}
    

If You Want Something, Go Get It, Period.
2010-10-28 13:58
爱上对方法国
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2010-10-28
收藏
得分:0 
回复 2楼 m21wo
谢啦
2010-10-28 16:57
爱上对方法国
Rank: 2
等 级:论坛游民
帖 子:12
专家分:10
注 册:2010-10-28
收藏
得分:0 
谢谢拉;
2010-10-28 17:03
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
结贴加分啦呵呵

If You Want Something, Go Get It, Period.
2010-10-28 17:21
快速回复:请兄长看看我的这个程序咋回事
数据加载中...
 
   



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

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