【问题标题】:Error in adding element to the back of linked list将元素添加到链表后面时出错
【发布时间】:2013-02-02 11:37:44
【问题描述】:

我正在尝试将一个元素添加到链表的后面。

我能够添加元素,并且在第一次尝试时一切正常,但是当我尝试添加另一个元素时,之前添加的元素变成了垃圾值。

当我用函数声明中完全相同的代码替换主菜单中的LinkedList::process_example(int choice,LinkedList &set) 函数时,问题就解决了。谁能给我解释一下为什么????

#include <iostream>
#include <ctime>
using namespace std;

struct Node;
typedef void* VoidPtr;
typedef Node* NodePtr;
typedef char* ZodiacSign;
const int MAX=12;

struct Node
{
NodePtr next;
VoidPtr data;

};

class LinkedList
{
public:
LinkedList();

//~LinkedList();

void Addelement(VoidPtr);

void printSet();

int compareEqual(VoidPtr,VoidPtr);

void swap(int num,int x,ZodiacSign tempSign [MAX]);

void process_example(int choice);

int check_cardinality();

void Addelementfromback(VoidPtr);



private:

NodePtr head;

ZodiacSign getVP(VoidPtr);


};


int choice=1;
LinkedList set;
do {
    cout<<endl
        <<endl;

    cout<<"Wish to try the following operation?"
        <<endl
        <<"1. Add an element to set"// the function to add to back of linked list
        <<endl
        <<"2. Check an element in set"
        <<endl
        <<"3. check carinality"
        <<endl
        <<"9.  Quit"
        <<endl
        <<endl;

    cout<<"Your choice : ";
    cin>>choice;

    cin.clear();
    cin.ignore(200,'\n');

    set.process_example(choice);

} while (choice !=9);


void LinkedList::process_example(int choice)
{
    switch (choice)
    {
    case 1:
        cout<<endl
            <<endl
            <<"Current S = ";

        this->printSet();

        cout<<"Enter an element :";

        char element [30];

        cin>>element;

        cin.clear();
        cin.ignore(200,'\n');

        this->Addelementfromback(element);

        cout<<endl
            <<endl
            <<"Current S = ";

        this->printSet();

        break;

    case 3:
        cout<<endl
            <<endl;

        cout<<"Current Set S = ";
        set.printSet();

        cout<<endl
            <<"S has ";

        int count=this->check_cardinality();

        cout<<count
            <<" elements";
    }
}

void LinkedList::printSet()
{
    NodePtr temp = head;

    cout<<"{ ";

    while (temp != NULL)
    {
        cout << getVP (temp -> data) << " , ";
        temp = temp -> next;
    }
    cout<<" } ";
    cout << endl;
}

void LinkedList::Addelementfromback(VoidPtr horoscope)
{
    NodePtr temp = head;

    while (temp->next != NULL)
    {
        temp=temp->next;
    }

    NodePtr element = new Node;
    element->data=horoscope;
    element->next=NULL;
    temp->next=element;
}

【问题讨论】:

  • 也许这个问题对你有帮助。 stackoverflow.com/questions/4005284/…
  • 查看LinkedList 的定义可能会有所帮助;不仅仅是几个成员函数。但是在顶部,您认为:NodePtr temp = head; while (temp-&gt;next != NULL)... temp-&gt;next = element;head 为 NULL 时会做什么?换句话说,该代码的行为如何,当新元素是 first 元素时,它会将新元素指针写入哪里?
  • 这可能是因为在调用 process_example 时,您传递了 set.process_example(choice, set) 之类的值,但在方法 process_example 中,您获得了 set 对象的地址,原因是“&”(地址)运算符?另外,我不明白使用set.process_example 调用方法/函数process_example 然后将set 变量作为其参数之一再次传递给它的逻辑。您始终可以使用this 运算符在方法process_example 中访问set 变量。
  • 我添加了LinkedList的定义

标签: c++ debugging class linked-list


【解决方案1】:

正如 WhozCraig 已经提到的,您需要将以下行添加到构造函数中

Head = NULL;

然后你可以在 Addelementfromback 函数的开头添加这样的东西

If(Head == NULL)
{
     Head = new Node;
     Head->data = horoscope;
     Head->next = NULL;
     return;
}

您还需要更改 LinkedList::process_example 中的以下行

 char elements[30];

 char* elements = new char[30];

【讨论】:

  • 感谢您的帮助,但问题仍然存在
  • @Computernerd 又发现了一个问题,并将其添加到我的解决方案中。您的程序适用于我的更改
  • 你真是个天才,非常感谢。你能解释一下为什么 char* element= new char [30] 有效吗???
  • @Computernerd 之前,只要您离开 process_example 的范围,内存就会被释放,但是使用 new 时,您会在堆上分配内存,并且只有在您告诉它时才会释放它。所以现在你要找到合适的地方来释放内存(删除),否则你会有一个很好的内存韭菜;-)
  • 人们可以帮我解答这个问题吗,如果可以的话,我会提高 10 次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2017-02-25
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
相关资源
最近更新 更多