首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

对于单链表。有点小无解。该怎么处理

2012-02-13 
对于单链表。有点小无解。。我发现插入函数调用了但是不起作用。。。编译又不出错。麻烦各位朋友帮忙找找看。。另外

对于单链表。有点小无解。。
我发现插入函数调用了但是不起作用。。。编译又不出错。麻烦各位朋友帮忙找找看。。

  另外。。析构函数怎么写才对呢???是不是自动调用的??

  还有一个问题就是。。有没头结点的区别在哪??书里是写不用特殊处理。。不太理解。。麻烦大虾解释下哦。。

 源程序:
#include <iostream>
using namespace std;

class Node
{
public:
int data;
Node *next;
};
class Nodedeal
{
friend class Node;
public:
Nodedeal()
{
first = new Node ;
first -> next = NULL;
}

void Headinifial(int n)//头插法
{
int count=0;
int ndata;
cout<<"请输入"<<n<<"个元素:";
while (count < n)
{
Node *p = new Node ;
cin >> ndata;
p -> data = ndata;
p -> next = first -> next;
first -> next = p;
count ++;
}
}
void Delete(int n) 
{
Node *pre = new Node;
Node *p = new Node ;
int count = 0 ;
pre = first ;
while ( count < (n-1) )

pre = pre -> next;
count ++;
}
p = pre ->next ;
pre ->next = p -> next;
delete p;
}
void Nsearch (int n)
{
cout<<"查找第"<<n<<"个元素";
int count = 0 ;
Node *p = new Node ;
p = first;
while (count<n)
{
p = p ->next;
count ++;
}
cout<<p->data;

}
void Print()
{
cout<<"输出所有元素:";
Node *p = new Node ;
p = first ;
p = p ->next;
while (p)
{
cout<<p->data<<'\n';
p = p ->next;

}
}
void Insert(int n,int ndata)
{

int count = 0 ;
Node *pre = new Node ;
Node *p = new Node ;
p->data = ndata ;
pre = first ;
while (count<(n-1))
{
pre = pre -> next ;
count ++;
}
p -> next = pre -> next ;
p = pre -> next ;

}
/*~Nodedeal()
{
Node *p;
p = first ;
while (p)
{
Node *pre = new Node ;
pre = p ;
p = p ->next;
delete p;
}
}
*/
private:
Node *first;
};
void main()
{
Nodedeal List;
List.Headinifial(5);
List.Delete(3);
List.Nsearch(1);
cout<<endl;
List.Print();
List.Insert(3,50);
List.Print();
}

[解决办法]
楼主,你这个链表类真的要重写了
你已经经过了思考,这时候,可以参考一下其它人的东西了
我这边也有一个链表类,可以发给你看看,调试过,可以用的
关于你的问题?
析构函数:是自动调用的,如果有new的,在delete时自动调用,普通栈变量,离开作用域时,自动析构
头结点:目前,C语言中的链表,90%都是有头结点的,当然也有没有头结点的,但是,其实道理还是一样,还是需要一个指针作为柄,通过它来遍历表,
话说,要是没有头结点这个概念,,,这还是LinkedList吗?那用链表就没什么意义了

C/C++ code
#include <iostream>using namespace std;class LinkedList{private:    struct NODE    {        void *Data;        NODE *Next;    };    NODE* mHead;public:    static const int NOT_FOUND = -1;    LinkedList();    ~LinkedList();    void Clear();    int Size();        void Add(int index, void *data);    void AddFirst(void *data);    void AddLast(void *data);        bool Contains(void *data);    int IndexOf(void *data);    int LastIndexOf(void *data);    bool Set(int index,void *data);    void * Get(int index);    void * GetFirst();    void * GetLast();        void * Remove(int index);    void * Remove(void *data);    };LinkedList::LinkedList(){    mHead = NULL;}LinkedList::~LinkedList(){    this->Clear();}void LinkedList::Clear(){    if( mHead == NULL )    {        return;    }    NODE *cursor;    while( mHead != NULL )    {        cursor = mHead->Next;        delete mHead;        mHead = cursor;    }}void LinkedList::AddFirst(void *data){    if(mHead != NULL)    {        return;    }    NODE *node = new NODE;    node->Data = data;    node->Next = NULL;    mHead = node;}void LinkedList::AddLast(void *data){    if(mHead == NULL)    {        this->AddFirst(data);        return;    }    NODE *cursor = mHead;    while(cursor->Next != NULL)    {        cursor = cursor->Next;    }    NODE *node = new NODE;    node->Data = data;    node->Next = NULL;    cursor->Next = node;}int LinkedList::Size(){    if(mHead == NULL)    {        return 0;    }    int size = 0;    NODE *cursor = mHead;    while(cursor != NULL)    {        size++;        cursor = cursor->Next;    }    return size;}bool LinkedList::Contains(void *data){    if( mHead == NULL )    {        return false;    }    NODE *cursor = mHead;    while(cursor != NULL)    {        if(cursor->Data == data)        {            return true;        }        cursor = cursor->Next;    }    return false;}int LinkedList::IndexOf(void *data){    if( mHead == NULL )    {        return NOT_FOUND;    }    int index = NOT_FOUND;    int pos = 0;    NODE *cursor = mHead;    while(cursor != NULL)    {        if(cursor->Data == data)        {            index = pos;            return index;        }        cursor = cursor->Next;        pos++;    }    return index;}int LinkedList::LastIndexOf(void *data){    if( mHead == NULL )    {        return NOT_FOUND;    }    int index = NOT_FOUND;    int pos = 0;    NODE *cursor = mHead;    while(cursor != NULL)    {        if(cursor->Data == data)        {            index = pos;        }        cursor = cursor->Next;        pos++;    }    return index;}bool LinkedList::Set(int index, void *data){    if( mHead == NULL )    {        return false;    }    int pos = 0;    NODE *cursor = mHead;    while(cursor != NULL && pos <= index)    {        if(pos == index)        {            cursor->Data = data;            return true;        }        cursor = cursor->Next;        pos++;    }    return false;}void * LinkedList::Get(int index){    void *data = NULL;    if( mHead == NULL )    {        return data;    }    int pos = 0;    NODE *cursor = mHead;    while(cursor != NULL && pos <= index)    {        if(pos == index)        {            data = cursor->Data;        }        cursor = cursor->Next;        pos++;    }    return data;}void * LinkedList::GetFirst(){    void *data = NULL;    if( mHead == NULL )    {        return data;    }    data = mHead->Data;    return data;}void * LinkedList::GetLast(){    void *data = NULL;    if( mHead == NULL )    {        return data;    }    NODE *cursor = mHead;    while(cursor->Next != NULL)    {        cursor = cursor->Next;    }    data = cursor->Data;    return data;}void * LinkedList::Remove(int index){    void *data = NULL;    if( mHead == NULL )    {        return data;    }    int pos = 0;    NODE *cursor = mHead;    NODE *last = NULL;    while(cursor != NULL && pos <= index)    {        if(pos == index)        {            data = cursor->Data;            if(cursor == mHead)            {                mHead = mHead->Next;            }            else            {                last->Next = cursor->Next;            }                        return data;        }        last = cursor;        cursor = cursor->Next;        pos++;    }    return data;}void * LinkedList::Remove(void *theData){    void *data = NULL;    if( mHead == NULL )    {        return data;    }    NODE *cursor = mHead;    NODE *last = NULL;    while(cursor != NULL)    {        if(cursor->Data == theData)        {            data = cursor->Data;            if(cursor == mHead)            {                mHead = mHead->Next;            }            else            {                last->Next = cursor->Next;            }            return data;        }        last = cursor;        cursor = cursor->Next;    }    return data;}int main(){    LinkedList list;    int noData = 0;    int data = 5;    void *pData = (void *)&data;    int data2 = 6;    void *pData2 = (void *)&data2;    int data3 = 7;    void *pData3 = (void *)&data3;    int data4 = 8;    void *pData4 = (void *)&data4;    list.AddFirst(pData);    list.AddLast(pData2);    list.AddLast(pData3);    list.AddLast(pData4);    //list.Clear();        bool contains = list.Contains(pData);    int findAt = list.LastIndexOf(pData2);    int *findData = (int *)list.Remove(pData3);    int size = list.Size();    if(findData == NULL)    {        findData = &noData;    }    cout<<*findData<<","<<size<<endl;    getchar();    return 0;} 


[解决办法]

探讨
其实 new 对于创建节点的意义 我还是不太理解。。。

动态分配不是不确定的时候用的吗??

那都确定只创建一个的话。那Node*= new Node 和 Node *p 的不同之处在哪


哎··都怪我C++没学好。。

[解决办法]
探讨
引用:
引用:
其实 new 对于创建节点的意义 我还是不太理解。。。

动态分配不是不确定的时候用的吗??

那都确定只创建一个的话。那Node*= new Node 和 Node *p 的不同之处在哪


哎··都怪我C++没学好。。

用NEW分配内存空间,不然你的数据放在哪里呢?
Node* p只是定……

热点排行