【问题标题】:Data Structure in C++. insertion in the beginning of the node in linked listC++中的数据结构。插入链表中节点的开头
【发布时间】:2017-06-15 16:17:25
【问题描述】:

我正在学习 C++ 中的数据结构。这是一个简单的插入程序 使用链接和节点。插入发生在节点的开头。 部分代码看不懂。

在函数display() 中,指针np 指向插入的信息,然后使用下一个节点获取前一个信息的值。下一个指针使用insert_beginning() 函数指向上一个信息。 显示是使用while 循环完成的。 next 指针在每个循环中如何改变它的值?

PS:程序运行良好。

#include<iostream>
#include<process.h>
#include<cstdlib>
using namespace std;

 struct node
 {
     int info;
     node *next;
 }*start,*newptr,*save,*ptr;

 node *create_new_node(int);
 void insert_beg(node*);
 void display(node*);

 /*----------------------------------------------------------------------------------------------------------------------------

 The pointer 'start' points to the beginning of the list.

 Function 'create_new_node()' takes one integer argument , allocates memory to create new node and returns
 the pointer to the new node.(return type: node*)

 Function 'insert_beg()' takes node* type pointer as an argument and inserts this node in the beginning of the list.

 Function display takes node* type pointer as an argument and displays the list from this pointer till the end of the list

 ------------------------------------------------------------------------------------------------------------------------------
 */

 int main()
 {
     start=NULL;
     int inf;
     char ch='y';
     while(ch=='y'||ch=='Y')
     {
         system("cls");

         cout<<"enter information for the new node ";
         cin>>inf;
         cout<<"\ncreating new node. Press enter to continue ";
         system("pause");

         newptr = create_new_node(inf);

         if(newptr!=NULL)
         {
             cout<<"\nnew node created successfully. Press enter to 
continue. ";
             system("pause");

         }

         else
         {

             cout<<"\nCannot create new node. ABORTING!! ";
             exit(1);

         }

         cout<<"\nnow inserting this node in the beginning of the list. 
Press enter to continue ";
         system("pause");
         insert_beg(newptr);
         cout<<"\nNow the list is \n";
         display(start);
         cout<<"\nPress 'Y' to enter more nodes, 'N' to exit\n";
         cin>>ch;

     }

     return 0;
 }

 node *create_new_node(int n)
 {
     ptr=new node;
     ptr->info=n;
     ptr->next=NULL;

 }

 void insert_beg(node *np)
 {

        if(start==NULL)
        start=np;
      else
     {
         save=start;
         start=np;
         np->next=save;
     }

 }

 void display(node *np)
 {

     while(np!=NULL)
     {
         cout<<np->info<<" ->";
         np=np->next;

     }
     cout<<"!!!\n";
 }

【问题讨论】:

  • 功能展示与插入节点有什么关系?!
  • 链表中的每个节点都包含下一个节点的地址,所以np-&gt;next指向下一个地址分配给np的节点再次np = np-&gt;next,这就是np的移动方式到下一个节点。

标签: c++ pointers linked-list


【解决方案1】:

长话短说-根据我的理解,您的基本问题是:-

显示是使用 while 循环完成的。 next 指针如何变化 它在每个循环中的值??

这恰好发生在这一行:-

np=np-&gt;next;

您基本上是将指向node 结构的指针推进到另一个node 结构,其地址在第一个节点结构的next 成员中。这是教科书的内容,任何基本的算法书都应该彻底涵盖这一点

HTH!

【讨论】:

  • next 保存前一个节点的地址。 np 从该地址获取值。那么 next 如何指向另一个节点。
  • next 保存下一个节点的地址而不是前一个节点
  • 这恰好发生在这一行:- np=np-&gt;next
【解决方案2】:

您的问题有些不清楚。尤其是因为您声明:

PS:程序运行良好。

它确实不是。有一个错误意味着这个程序将无法运行。

问题是create_new_node没有返回指针值

 node *create_new_node(int n)
 {
     ptr=new node;
     ptr->info=n;
     ptr->next=NULL;
     return ptr;      // This line is missing
 }

此外,使用全局指针变量是一个非常糟糕的主意!

这里

 struct node
 {
     int info;
     node *next;
 }*start,*newptr,*save,*ptr;

您定义了struct node,但您还定义了 4 个变量,即 4 个指向节点的指针。这些变量将是全局的,即在您的所有代码中都可用。您应该绝不做的事情。

而是根据需要制作局部变量 - 例如:

 node *create_new_node(int n)
 {
     node *ptr;   // Local variable instead of global
     ptr=new node;
     ptr->info=n;
     ptr->next=NULL;
     return ptr;
 }

然后为 insert_beg 更改它,使其返回一个新的开始指针 - 比如:

 node* insert_beg(node* start, node *np)
 {
     np->next=start;
     return np;
 }

并在main 中使用它,例如:

node* start = NULL;
...
...
start = insert_beg(start, newptr);

顺便说一句 - 在现代 C++ 中,您永远不会使用原始指针,也永远不会编写自己的列表。使用智能指针而不是原始指针。使用标准容器而不是自己编写。

【讨论】:

  • 实际上程序在没有建议的编辑的情况下运行。我确实想过从 create_new_node() 返回指针,但编译和输出都很好。顺便说一句...你详细说明一下智能指针吗?
  • @SohitGore 1) 如果没有return ptr,程序就无法正确运行 2) 智能指针...好吧,原始指针的问题是许多程序员无法处理它们。他们忘记释放/删除导致很多问题(又名内存泄漏)的内存。因此,智能指针是将责任委托给特殊类的一种方式,即唯一指针或共享指针 - 智能指针将自动处理内存释放 - 就像一种垃圾收集。如果你想做现代 C++,你应该阅读它们。
  • @SohitGore - 顺便说一句:请记住始终将警告转化为错误(即编译器选项)。你的编译器因为在那个函数中没有返回而对你大喊大叫:-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 2010-12-31
  • 2021-08-23
  • 2018-04-18
相关资源
最近更新 更多