在形成链表时遇到问题 [英] Having problem in forming linked list

查看:78
本文介绍了在形成链表时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我在以下代码中识别我的错误以形成链接列表

没有编译器错误





我不参加任何关于c ++的课程并自己学习。

任何帮助将不胜感激



我尝试了什么:



Can anyone help me in identifying my mistake in the following code to form a linked list
There is no compiler error


I don't take any classes on c++ and learn by myself.
Any help would be appreciated

What I have tried:

#include <iostream>

using namespace std;

struct node
{
    int data;
    node* link;
};

int main()
{
    node * Head =NULL;
    node* temp= new node;

    temp->data=2;
    temp->link=NULL;


    delete temp;

    node*temp5= new node;
    temp5->data=3;
    temp5->link=temp;
    delete temp5;

    node*temp2=new node;
    temp2->data=4;
    temp2->link=temp5;
    temp2=Head;
    delete temp2;

    node*temp1=Head;
    while(temp1->link != NULL)
    {

        temp1=temp1->link;
    }

    delete temp1;

}

推荐答案

你不应该删除已分配的节点(您基本上是在创建列表的同时销毁列表)。

仅在您不再需要列表时才会释放内存。
You shouldn't delete allocated nodes (you are basically destroying the list at the same time you're creating it).
Deallocation of memory should happen only when you need the list no more.


您正在创建节点并立即删除它们。您也不会将第一个链接到Head节点,因此您没有列表,链接或其他。你的逻辑应该是:

You are creating nodes and immediately deleting them. You also do not link the first one to the Head node, so you do not have a list, linked or otherwise. Your logic should be:
Create a new node
Set forward link to NULL
If Head == NULL Then set Head link to new node
If Head != NULL Then
    temp node pointer tp = Head
    While tp->link != NULL
        tp = tp->link
    tp->link = new node
Repeat for each node



然后,您应该能够从头到尾遍历列表,检查每个节点的内容。



使用调试器逐步执行代码总是有帮助的,这样你就可以看到每一步发生了什么。


You should then be able to traverse your list from head to the end, checking the content of each node.

It always helps to use your debugger to step through the code so you can see what is happening at each step.


只是添加到上面的点,你试图删除一个不正确的空指针



节点* temp2 =新节点;

temp2-> data = 4;

temp2-> link = temp5;

temp2 = Head; //由于你没有分配任何东西,因此头部将为NULL这里。

删除temp2; //这里你试图删除NULL。



添加一些示例代码以供参考:



Just adding to above points, You are trying to delete a NULL pointer which is not correct

node*temp2=new node;
temp2->data=4;
temp2->link=temp5;
temp2=Head;//Head will be NULL here as you have not assigned any thing here.
delete temp2;//Here you are trying to delete NULL.

Adding Some sample code for reference:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

struct Node
{
	int Data;
	Node* Link;
	friend class LinkedList;
};

class LinkedList
{
private:

	Node* _head;

	Node* GetNewNode(int value, Node* next)
	{
		Node*  newNode = new Node();
		if (newNode != NULL)
		{
			newNode->Data = value;
			newNode->Link = next;
		}
		return newNode;
	}

public:

	LinkedList()
	{
		_head = NULL;
	}

	void Insert(int value, Node* next)
	{
		Node* newNode = GetNewNode(value, next);

		if (newNode != NULL)
		{
			if (_head == NULL)
			{
				_head = newNode;
			}
			else
			{
				Node* temp = NULL;
				temp = _head;
				_head = newNode;

				Node* travereList = NULL;
				travereList = newNode;
				while (travereList->Link != NULL)
				{
					travereList = travereList->Link;
				}
				travereList->Link = temp;
			}
		}
	}

	Node* GetHead()
	{
		return _head;
	}
};

void Print(LinkedList* l)
{
	if (l != NULL)
	{
		Node* temp = NULL;
		temp = l->GetHead();
		while (temp != NULL)
		{
			cout << temp->Data << endl;
			temp = temp->Link;
		}
	}
}

int _tmain(int argc, _TCHAR* argv[])
{

	LinkedList l;
	l.Insert(10, NULL);
	l.Insert(20, NULL);
	l.Insert(30, NULL);
	l.Insert(40, NULL);
	l.Insert(50, NULL);

	Print(&l);

	getchar();
	return 0;
}











谢谢和问候

Sravan Vurapalli






Thanks And Regards
Sravan Vurapalli


这篇关于在形成链表时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆