| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 470 人关注过本帖
标题:新手求教,绝对不是作业贴,我们还没学c++呢。
只看楼主 加入收藏
Ooh_no
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-9-25
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
新手求教,绝对不是作业贴,我们还没学c++呢。
最近刚刚自己学了C++,于是就想练练手,写了一个链表的类,编译的时候都还没问题,但是运行的时候总出错。我用的是visual studio2008,代码如下:
#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;

struct Node{struct Node *next;int data;};
class A
{
public:
    A();
    ~A();
    int push(int e);
    int pop();
    //bool erease();
    struct Node * begin();
    struct Node * end();
private:
    struct Node * V;
};
A::A()
{
    struct Node *H=new struct Node;
    H->next=NULL;
    (this->V)->next=H;
}
A::~A()
{
    delete [](this->V);
}
int A::push(int e)
{
    struct Node *B=new struct Node;
    struct Node *p;
    p=end();
    B->data= e;
    B->next= NULL;
    p->next=B;
    return 0;
}
/*int A::pop()
{
    struct Node *p,*q;
    p=end();
    q=begin();
    while(q->next != p)
        q=q->next;
    q->next=NULL;
    return p->data;
    delete p;
}
/*bool A::erease(struct Node *p)
{
    struct Node *q;
    while(q->next != p)
        q=q->next;
    q->next=p->next;
    delete p->next;
    return true;
}*/
struct Node *A::begin()
{
    return V->next;
}
struct Node *A::end()
{
    struct Node* p;
    p=begin();
    while(p->next != NULL)
        p=p->next;
    return p;
}
int main()
{
    int e,f=0;
    A shiyan;
    cin>>e;
    shiyan.push(e);
    //f=shiyan.pop();
    //cout<<f<<endl;
    system("pause");
    return 0;
}
在运行的时候,出现了错误提示:实验.exe 中的 0x00e71e2d 处未处理的异常: 0xC0000005: 写入位置 0xcccccccc 时发生访问冲突。
这个程序我看了几天了,还是找不出问题。这个到底是什么情况?望高手解答,谢谢了。

搜索更多相关主题的帖子: 作业 
2010-09-25 18:31
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:0 
你写的太乱了!你的命名也很乱,pop,push等词也通常用于堆栈!!
这是我帮你写的一个构造单链表的程序:
程序代码:
#include <iostream>
using namespace std;
class node
{
private:
    int element;
    node* link;
public:
    node(int el=0,node* lk=0):element(el),link(lk)
    {}
    node* Creat();
    void output(node* p);
    node* del(node* head,int num);
};
node* node::Creat()
{
    node *first=new node;
    node *p;
    node* s=new node;
    int x;
    int n=0;
    while(cin>>x)
    {
       
        p=new node;
        p->element=x;
        if(n==0)
        {
            first=new node(x,0);
            s=first;
            n=1;

        }
        else
        {
            s->link=p;
            s=s->link;
        }

    }
    s->link=NULL;
    return first;
}

void node::output(node* p)
{
    while(p!=NULL)
    {
        cout<<p->element<<endl;
        p=p->link;
    }

}

node* node::del(node* head,int num)
{
    node* p=head;
    node* s=new node;
    while((p->element != num)&&(p->link != NULL))
    {
        s=p;
        p=p->link;
    }
    if(p->element==num)
    {
        if(p==head)
        {
            head=p->link;
            delete p;
        }
        else
        {
            s->link=p->link;
            delete p;
        }
    }
    else
        cout<<"there is no element in the link !"<<endl;
    return   head;
}


int main()
{

    node l;
    node* e,*s1;
    e=l.Creat();
    l.output(e);
    if(cin.fail())
    {
    cin.clear();
    cin.ignore();
    }
    cout<<"please input a elememt that you want to delete :";
    int x1;
    cin>>x1;
    s1=l.del(e,x1);
    l.output(s1);
    return 0;
    }


If You Want Something, Go Get It, Period.
2010-09-25 20:03
Ooh_no
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-9-25
收藏
得分:0 
呃 ,其实我其实就是想写个整数栈来着··
2010-09-25 21:20
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:20 
这是我以前写的堆栈及应用!希望对你有用!
程序代码:
#include <iostream>
#include <cassert>
#include <stdlib.h>
using namespace std;
template <class T>
class Node
{
private:
    Node<T>* link;
public:
    T element;
    Node(T d=0,Node<T>* lk=0):element(d),link(lk)
    {}
     template<class Tpye> friend class Stack;
};
template <class T>
class Stack
{
private:
    Node<T>* top;
    int size;
public:
    Stack()
    {
        top=NULL;
        size=0;
    }
    ~Stack()
    {
        ClearStack();
        top=NULL;
    }
    bool IsEmpty() const
    {
        return size==0;
    }
    int Stacksize() const
    {
        return size;
    }
    bool Top(T& x) const
    {
        if(IsEmpty())
        {
            cout<<"Empty!!"<<endl;
            return false;
        }
        x=top->element;
        return true;
    }
    void Push(T x)
    {
        Node<T>* p;
        p=new Node<T>(x,0);
        top=p;
        size++;
    }
    T Pop()
    {
        if(size==0)
        {
            cout<<"没有元素为空!!"<<endl;
            exit(0);
        }
        Node<T>* p=top->link;
        T x=top->element;
        delete top;
        top=p;
        size--;
        return x;
    }
    void ClearStack()
    {
        Node<T> *p,*q;
        p=top;
        while(p!=NULL)
        {
            q=p;
            p=p->link;
            delete q;
        }
        size=0;
    }
};
class complex
{
private:
    double Re,Im;
public:
    complex(double i=0,double j=0):Re(i),Im(j)
    {}
    complex operator + (const complex &rhs) const
    {
        complex c;
        c.Re=this->Re+rhs.Re;
        c.Im=this->Im+rhs.Im;
        return c;
    }
    complex operator - (const complex &rhs ) const
    {
        complex c;
        c.Re=this->Re-rhs.Re;
        c.Im=this->Im-rhs.Im;
        return c;
    }
    complex operator * (const complex &rhs) const
    {
        complex c;
        c.Re=this->Re*rhs.Re-this->Im*rhs.Im;
        c.Im=this->Re*rhs.Im+this->Im*rhs.Re;
        return c;
    }
    complex operator / (const complex &rhs) const
    {
        complex c;
        double m=rhs.Re*rhs.Re+rhs.Im*rhs.Im;
        assert(m!=0);
        c.Re=(this->Re*rhs.Re+this->Im*rhs.Im)/m;
        c.Im=(this->Im*rhs.Re-this->Re*rhs.Im)/m;
        return c;
    }
    friend istream& operator >> (istream& in,complex& rhs);
    friend ostream& operator << (ostream& os,complex& rhs);
};

 istream& operator >> (istream& in,complex& rhs)

 {
     char d;
     in>>rhs.Re>>rhs.Im>>d;
     return in;

 }
ostream& operator << (ostream& os,complex& rhs)
{
    os<<rhs.Re<<(rhs.Im>=0 ? "+":"")<<rhs.Im<<"i";
    return os;
}
int main()
{
    Stack<complex> s;
    complex x,y;
    cout<<"该栈为空 is  "<<((s.IsEmpty()==1)?"true":"false")<<"\n\n";
    cout<<"请输入多个个复数:"<<endl;
    while(cin>>x)
    {
        s.Push(x);
    }
    s.Top(y);
    cout<<"栈顶元素为:"<<endl;
    cout<<y<<endl;
    cout<<"输入复数的个数为:"<<endl;
    cout<<s.Stacksize()<<endl;

}




    






If You Want Something, Go Get It, Period.
2010-09-25 21:41
Ooh_no
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2010-9-25
收藏
得分:0 
谢谢了
2010-09-26 16:07
快速回复:新手求教,绝对不是作业贴,我们还没学c++呢。
数据加载中...
 
   



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

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