C++ C+中的简单链表+;
我将创建一个可以插入并显示到现在的链接:C++ C+中的简单链表+;,c++,linked-list,C++,Linked List,我将创建一个可以插入并显示到现在的链接: struct Node { int x; Node *next; }; 这是我的初始化函数,只会为第一个节点调用该函数: void initNode(struct Node *head, int n){ head->x = n; head->next = NULL; } 要添加节点,我认为我的链表无法正常工作的原因在于此函数: void addNode(struct Node *head, int n){
struct Node {
int x;
Node *next;
};
这是我的初始化函数,只会为第一个节点调用该函数:
void initNode(struct Node *head, int n){
head->x = n;
head->next = NULL;
}
要添加节点
,我认为我的链表无法正常工作的原因在于此函数:
void addNode(struct Node *head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct Node *head = new Node;
initNode(head, 5);
addNode(head, 10);
addNode(head, 20);
return 0;
}
Node * addNode(Node *head, int n)
{
Node *NewNode = new Node {n, head};
head = NewNode;
return head;
}
我的main
功能:
void addNode(struct Node *head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct Node *head = new Node;
initNode(head, 5);
addNode(head, 10);
addNode(head, 20);
return 0;
}
Node * addNode(Node *head, int n)
{
Node *NewNode = new Node {n, head};
head = NewNode;
return head;
}
让我按我认为有效的方式运行程序。首先,我将头部节点
初始化为节点
,如下所示:
head = [ 5 | NULL ]
void addNode(struct node *head, int n) {
if (head->Next == NULL) {
struct node *NewNode = new node;
NewNode->value = n;
NewNode->Next = NULL;
head->Next = NewNode;
}
else
addNode(head->Next, n);
}
然后我添加一个n=10的新节点,并传递head作为我的参数
NewNode=[x | next],其中next指向头部。然后我改变了head指向NewNode的位置,因为NewNode是LinkedList中的第一个节点
为什么这不起作用?如果有任何提示能让我朝着正确的方向前进,我将不胜感激。我觉得LinkedList有点难理解
当我打印这个时,它只返回5:您应该引用一个头指针。否则,指针修改在函数外部不可见
void addNode(struct Node *&head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
addNode
功能需要能够更改head
。正如现在编写的那样,只需更改局部变量head
(一个参数)
将代码更改为
void addNode(struct Node *& head, int n){
...
}
将解决此问题,因为现在通过引用传递head
参数,被调用的函数可以对其进行变异。head
在main中定义如下
struct Node *head = new Node;
void initNode(int n){
head->x = n;
head->next = NULL;
}
void addNode(int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode->next = head;
head = NewNode;
}
但是您仅在addNode()
和initNode()
函数中更改头部。这些变化并没有反映在主体上
将head声明为全局,不要将其传递给函数
功能应如下所示
struct Node *head = new Node;
void initNode(int n){
head->x = n;
head->next = NULL;
}
void addNode(int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode->next = head;
head = NewNode;
}
这两种功能都是错误的。首先,函数initNode
的名称令人困惑。它应该命名为例如initList
,不应该执行addNode的任务。也就是说,它不应该向列表中添加值
实际上,函数initNode没有任何意义,因为可以在定义head时初始化列表:
Node *head = nullptr;
或
因此,您可以从列表的设计中排除函数initNode
此外,在代码中,不需要为结构节点
指定详细的类型名称,即在名称节点
之前指定关键字struct
函数addNode
应更改头部的原始值。在函数实现中,只更改作为参数传递给函数的head的副本
void addNode(struct Node *&head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
该功能可以如下所示:
void addNode(Node **head, int n)
{
Node *NewNode = new Node {n, *head};
*head = NewNode;
}
或者,如果编译器不支持新的初始化语法,则可以编写
void addNode(Node **head, int n)
{
Node *NewNode = new Node;
NewNode->x = n;
NewNode->next = *head;
*head = NewNode;
}
或者,您可以使用指向节点的指针的引用,而不是使用指向指针的指针。比如说,
void addNode(Node * &head, int n)
{
Node *NewNode = new Node {n, head};
head = NewNode;
}
或者,您可以从函数返回更新的头部:
void addNode(struct Node *head, int n){
struct Node *NewNode = new Node;
NewNode-> x = n;
NewNode -> next = head;
head = NewNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
struct Node *head = new Node;
initNode(head, 5);
addNode(head, 10);
addNode(head, 20);
return 0;
}
Node * addNode(Node *head, int n)
{
Node *NewNode = new Node {n, head};
head = NewNode;
return head;
}
并在main
中写入:
head = addNode(head, 5);
我要加入战斗。我已经很久没有写C了。再说,这里也没有完整的例子。OP的代码基本上是C,所以我继续使用GCC
这些问题以前都有解决过;next
指针未被提升。这是问题的关键
我还借此机会提出了一个编辑建议;我没有为malloc
设置两个函数,而是将它放在initNode()
中,然后将initNode()
用于malloc
这两个函数(malloc
是“新的C”(如果您愿意的话)。我更改了initNode()
以返回指针
#include <stdlib.h>
#include <stdio.h>
// required to be declared before self-referential definition
struct Node;
struct Node {
int x;
struct Node *next;
};
struct Node* initNode( int n){
struct Node *head = malloc(sizeof(struct Node));
head->x = n;
head->next = NULL;
return head;
}
void addNode(struct Node **head, int n){
struct Node *NewNode = initNode( n );
NewNode -> next = *head;
*head = NewNode;
}
int main(int argc, char* argv[])
{
struct Node* head = initNode(5);
addNode(&head,10);
addNode(&head,20);
struct Node* cur = head;
do {
printf("Node @ %p : %i\n",(void*)cur, cur->x );
} while ( ( cur = cur->next ) != NULL );
}
在本例中,这是我能想到的最简单的示例,没有经过测试。请考虑使用一些不好的做法,而不是像通常那样使用C++(初始化列表、声明和定义的分离等等)。但这些都是我不能在这里讨论的话题
#include <iostream>
using namespace std;
class LinkedList{
// Struct inside the class LinkedList
// This is one node which is not needed by the caller. It is just
// for internal work.
struct Node {
int x;
Node *next;
};
// public member
public:
// constructor
LinkedList(){
head = NULL; // set head to NULL
}
// destructor
~LinkedList(){
Node *next = head;
while(next) { // iterate over all elements
Node *deleteMe = next;
next = next->next; // save pointer to the next element
delete deleteMe; // delete the current entry
}
}
// This prepends a new value at the beginning of the list
void addValue(int val){
Node *n = new Node(); // create new Node
n->x = val; // set value
n->next = head; // make the node point to the next node.
// If the list is empty, this is NULL, so the end of the list --> OK
head = n; // last but not least, make the head point at the new node.
}
// returns the first element in the list and deletes the Node.
// caution, no error-checking here!
int popValue(){
Node *n = head;
int ret = n->x;
head = head->next;
delete n;
return ret;
}
// private member
private:
Node *head; // this is the private member variable. It is just a pointer to the first Node
};
int main() {
LinkedList list;
list.addValue(5);
list.addValue(10);
list.addValue(20);
cout << list.popValue() << endl;
cout << list.popValue() << endl;
cout << list.popValue() << endl;
// because there is no error checking in popValue(), the following
// is undefined behavior. Probably the program will crash, because
// there are no more values in the list.
// cout << list.popValue() << endl;
return 0;
}
#包括
使用名称空间std;
类链接列表{
//类LinkedList中的结构
//这是调用者不需要的一个节点。它只是
//内部工作。
结构节点{
int x;
节点*下一步;
};
//公职人员
公众:
//建造师
LinkedList(){
head=NULL;//将head设置为NULL
}
//析构函数
~LinkedList(){
节点*下一个=头部;
while(next){//遍历所有元素
节点*deleteMe=next;
next=next->next;//保存指向下一个元素的指针
delete deleteMe;//删除当前条目
}
}
//这将在列表的开头添加一个新值
void addValue(int val){
Node*n=new Node();//创建新节点
n->x=val;//设置值
n->next=head;//使节点指向下一个节点。
//如果列表为空,则为空,因此列表的结尾-->确定
head=n;//最后但并非最不重要的一点是,将head指向新节点。
}
//返回列表中的第一个元素并删除节点。
//注意,此处检查时没有错误!
int popValue(){
节点*n=头部;
int-ret=n->x;
头部=头部->下一步;
删除n;
返回ret;
}
//私人会员
私人:
Node*head;//这是私有成员变量。它只是指向第一个节点的指针
};
int main(){
链接列表;
增加值(5);
增加值(10);
增加值(20);
下面是一个示例链接列表
#include <string>
#include <iostream>
using namespace std;
template<class T>
class Node
{
public:
Node();
Node(const T& item, Node<T>* ptrnext = NULL);
T value;
Node<T> * next;
};
template<class T>
Node<T>::Node()
{
value = NULL;
next = NULL;
}
template<class T>
Node<T>::Node(const T& item, Node<T>* ptrnext = NULL)
{
this->value = item;
this->next = ptrnext;
}
template<class T>
class LinkedListClass
{
private:
Node<T> * Front;
Node<T> * Rear;
int Count;
public:
LinkedListClass();
~LinkedListClass();
void InsertFront(const T Item);
void InsertRear(const T Item);
void PrintList();
};
template<class T>
LinkedListClass<T>::LinkedListClass()
{
Front = NULL;
Rear = NULL;
}
template<class T>
void LinkedListClass<T>::InsertFront(const T Item)
{
if (Front == NULL)
{
Front = new Node<T>();
Front->value = Item;
Front->next = NULL;
Rear = new Node<T>();
Rear = Front;
}
else
{
Node<T> * newNode = new Node<T>();
newNode->value = Item;
newNode->next = Front;
Front = newNode;
}
}
template<class T>
void LinkedListClass<T>::InsertRear(const T Item)
{
if (Rear == NULL)
{
Rear = new Node<T>();
Rear->value = Item;
Rear->next = NULL;
Front = new Node<T>();
Front = Rear;
}
else
{
Node<T> * newNode = new Node<T>();
newNode->value = Item;
Rear->next = newNode;
Rear = newNode;
}
}
template<class T>
void LinkedListClass<T>::PrintList()
{
Node<T> * temp = Front;
while (temp->next != NULL)
{
cout << " " << temp->value << "";
if (temp != NULL)
{
temp = (temp->next);
}
else
{
break;
}
}
}
int main()
{
LinkedListClass<int> * LList = new LinkedListClass<int>();
LList->InsertFront(40);
LList->InsertFront(30);
LList->InsertFront(20);
LList->InsertFront(10);
LList->InsertRear(50);
LList->InsertRear(60);
LList->InsertRear(70);
LList->PrintList();
}
#包括
#包括
使用名称空间std;
模板
类节点
{
公众:
Node();
节点(const T&item,Node*ptrnext=NULL);
T值;
节点*下一步;
};
模板
Node::Node()
{
值=空;
next=NULL;
}
模板
节点::节点(常数T和项,节点*ptrnext=NULL)
{
此->值=项目;
此->下一步=ptrnext;
}
T
int main()
{
cout<<"hello this is an example of link list in cpp using classes"<<endl;
ll list1(new Node(1));
list1.append(2);
list1.append(3);
list1.print_list();
}
thanks ❤❤❤