[英]Create and Display Linked List
I am a beginner in C++ and need help in many things. 我是C ++的初学者,需要很多帮助。 Well, for the starters, I have been working on Linked List and not really getting why my header(the first pointer which points towards first node) keep on rotating.
好吧,对于初学者来说,我一直在研究链表,但并没有真正理解为什么我的标头(指向第一个节点的第一个指针)继续旋转。 I am just pointing it towards first node plus my display node is just displaying last node, why is it so?.
我只是将其指向第一个节点,而我的显示节点只是显示最后一个节点,为什么呢? Please tell me where I am wrong.
请告诉我我错了。 Thank you in advance
先感谢您
#include <iostream>
#include <conio.h>
using namespace std;
struct Node
{
int data;
Node *link;
};
Node* create_Node()
{
int no_of_nodes;
Node *header = new Node;
Node *ptr = new Node;
header = ptr;
cout << "Enter no of nodes:";
cin >> no_of_nodes;
cout << "Enter data:";
for(int n = 0; n < no_of_nodes; n++)
{
cin >> ptr->data;
Node *temp = new Node;
ptr->link = temp;
temp = ptr;
}
ptr->link = NULL;
return ptr;
}
void display_link_list(Node * list)
{
Node *temp = new Node;
temp = list;
while(temp != NULL)
{
if(temp->link != NULL)
{
cout << "List:" << list->data << endl;
temp = temp->link;
}
}
}
int main()
{
Node *n = new Node;
n = create_Node();
display_link_list(n);
getch();
return 0;
}
Welcome to C++. 欢迎使用C ++。 My advice here is to break the
Linked list
into two. 我的建议是将“
Linked list
分为两部分。 First the Nodes and then a List struct. 首先是节点,然后是列表结构。
struct Node
{
int data;
Node *next;
Node(int data) : data(data), next(NULL) {}
};
struct List {
Node* tail;
Node* head;
List() : head(NULL), tail(NULL) {}
void insert(int data) {
if(head==NULL) {
head = new Node(data);
tail = head;
} else {
tail->next = new Node(data);
tail = tail->next;
}
}
};
Now you can insert one element into the list at a time and use head
to print the list from beginning to end. 现在,您可以一次将一个元素插入列表,然后使用
head
从头到尾打印列表。
Something basic that you need to understand: 您需要了解的一些基本知识:
When you do Node* p = new Node
, you are setting variable p
to point to the start address of a piece of memory, the size of which being equal to sizeof(Node)
. 当您执行
Node* p = new Node
,您正在将变量p
设置为指向一块内存的起始地址,该内存的大小等于sizeof(Node)
。
Now, when you then do p = something else
(which often appears in your code), you are essentially overriding the previous value of p
with some other value. 现在,当您随后执行
p = something else
(通常出现在代码中)时,实际上是在用其他值覆盖p
的先前值。 It is like doing: 这就像在做:
int i = 5;
i = 6;
So your code does not do what you're expecting to begin with. 因此,您的代码无法实现您期望的开始。
In addition to that, what's bad about overriding the first value with a second value in this case, is the fact that the first value is the address of a dynamically-allocated piece of memory, that you will need to delete
at a later point in your program. 除此之外,在这种情况下用第二个值覆盖第一个值的缺点是,第一个值是动态分配的内存的地址,您需要在以后的某个时间
delete
。您的程序。 And once you've used p
to store a different value, you no longer "remember" that address, hence you cannot delete
that piece of memory. 而且,一旦使用
p
存储了一个不同的值,就不再“记住”该地址,因此无法delete
该内存。
So you should start by fixing this problem in each of the following places: 因此,您应该首先在以下每个位置解决此问题:
Node *header = new Node; // Variable 'header' is assigned
header = ptr; // Variable 'header' is reassigned
Node *temp = new Node; // Variable 'temp' is assigned
temp = list; // Variable 'temp' is reassigned
Node *n = new Node; // Variable 'n' is assigned
n = create_Node(); // Variable 'n' is reassigned
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.