+ -
当前位置:首页 → 问答吧 → 大侠们帮我看看怎么回事啊

大侠们帮我看看怎么回事啊

时间:2011-11-27

来源:互联网

C/C++ code
#include<iostream>
#include<time.h>
using namespace std;

struct Node//结点
{
    int data;
    struct Node *next;
};

class Line//链表
{
    private:
    int leng;//记录结点个数
    struct Node *head;
    public:
    Line();//构造函数 初始化操作
    ~Line();//构析函数,删除链表
    void CreatNode();//创建一个新结点
    void DestrayNode(unsigned int count);//删除一个结点
    void PutData();//输出链表
    void ClearList();//清空链表
    int GetLeng();//输出结点个数
    int GetNode(unsigned int count);//取一个结点的值
    void LineInsert(unsigned int count);//在count结点后插入一个结点
    void fun(class Line );
};

void Line::fun(class Line p)
{
    struct Node *pNode;

    pNode = head;
    while(pNode->next)
        pNode = pNode->next;
    pNode->next = p.head->next;
}
void Line::LineInsert(unsigned int count)//在count结点后插入一个结点
{
    if(count > leng)
    {
        cout<<"error:最大结点数为"<<leng<<endl;
        return ;
    }
    struct Node *pNewNode,*pNode;
    pNewNode = new (struct Node);
    cout<<"输入新结点的值"<<endl;
    cin>>pNewNode->data;

    pNode = head;
    for(int i = 1; i <= count;i++)
        pNode = pNode->next;
    pNewNode->next = pNode->next;
    pNode->next = pNewNode;
    leng++;
}


int Line::GetNode(unsigned int count)//取一个结点的值
{
    if(count > leng)
    {
        cout<<"error:最大结点数为"<<leng<<endl;
        return -1;
    }
    struct Node *pNode;
    pNode = head;
    for(int i = 1; i <= count;i++)
        pNode = pNode->next;
    return(pNode->data);
}
int Line::GetLeng()
{
    return leng;
}

Line::Line()//构造函数
{
    leng = 0;
    head = new (struct Node);
    head->data = 0;
    head->next = NULL;
}

Line::~Line()//构析函数
{
    struct Node *p;
    while(head)
    {
        p = head;
        head = head->next;
        delete p;
    }
}

void Line::PutData()//输出每个结点的值
{
    struct Node *pNode;

    pNode = head->next;
    while(pNode)
    {
        cout<<pNode->data<<' ';
        pNode = pNode->next;
    }
    cout<<endl;
}


void Line::CreatNode()//创建一个新结点
{
    struct Node *pNode,*NewNode;
    pNode = head;
    
    while(pNode->next)
        pNode = pNode->next;

    NewNode = new (struct Node);//申请空间
    NewNode->data = rand()%100;//利用随机函数创建结点数值(可自己修改)
    NewNode->next = NULL;
    pNode->next = NewNode;
    leng++;
}


void Line::DestrayNode(unsigned int count)//删除一个结点
{
    struct Node *pFront,*pAfter;
    if(count > leng)
    {
        cout<<"error:最大结点数为"<<leng<<endl;
        return;
    }
    if(count == 1)
    {
        pFront = head;
        head = head->next;
        delete pFront;
    }

    pAfter = head;
    for(int i = 1;i < count; i++)
        pAfter = pAfter->next;

        pFront = pAfter->next;
    pAfter->next = pFront->next;
    leng--;
    delete pFront;
}

void Line::ClearList()//清空链表
{
    struct Node *pAfter,*pFront;

    pFront = head->next;
    pAfter = pFront->next;
    while(pAfter)//对链表逐一删除
    {
        delete pFront;
        pFront = pAfter;
        pAfter = pAfter->next;
    }
    
        delete pFront;
        head->next = NULL;
        leng = 0;
}

int main()
{
    class Line p1,p2;
    int i,j;
    srand((unsigned)time(NULL));//随机函数
    cout<<"链表1的长度";
    cin>>i;
    for(j = 0; j < i; j++)//创建一个10个结点的链表
        p1.CreatNode();
    p1.PutData();//输出所有结点
        cout<<"链表2的长度"<<endl;
    cin>>i;
    for(j = 0; j < i; j++)//创建一个10个结点的链表
        p2.CreatNode();
    p2.PutData();//输出所有结点
    p1.fun(p2);
    p1.PutData();

    return 0;
}


可能有些乱 这个是我写的链表数据结构 就一个函数搞不明白 具体情况是从
p1.fun(p2);
p1.PutData();
main函数最后2行开始的
fun函数的功能是将p1,p2链表链接
在fun函数运行一切正常根据调试 p1与p2链接成功
可是为什么退出函数之后就错误了呢? WHY 实在想不明白啊

作者: xuminghui382   发布时间: 2011-11-27

你调试运行一下 看看是否是指针使用有问题

作者: heksn   发布时间: 2011-11-27