2017-06-15 92 views
0

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

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

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"; 
} 
+0

函数显示如何与插入节点相关? –

+0

链表中的每个节点都包含下一个节点的地址,所以'np-> next'指向下一个节点,该地址再次被分配给'np'np = np-> next',这就是'np'移动到下一个节点。 –

回答

0

短切长的故事 - 按我的理解,你的基本问题是: -

显示器使用while循环来完成。下一个指针在每个循环中如何改变其值? ??

这恰恰发生在这一行: -

np=np->next;

你基本上是推进指针node结构到另一个node结构,它的地址是在第一个节点结构的next成员。这是教科书的东西,任何基本的算法书应该包括这个彻底

HTH!

+0

接下来保存前一个节点的地址。 np从该地址获取值。那么下一步如何指向另一个节点。 –

+0

接下来保存下一个节点的地址,而不是前一个节点的地址 – Zakir

+0

它如何指向每个循环中的下一个信息? –

0

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

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++中,你会nev呃使用原始指针,你永远不会写自己的列表。使用智能指针而不是原始指针。使用标准容器而不是自己写。

+0

实际上程序运行时没有建议的编辑。我确实想过从create_new_node()返回指针,但编译和输出很好。顺便说一句...你有没有详细说明智能指针? –

+0

@SohitGore 1)如果没有'return ptr',程序就不能正确运行2)智能指针......好了,原始指针的问题在于许多程序员无法处理它们。他们忘记释放/删除导致很多问题的内存(又称内存泄漏)。因此,智能指针是将责任委托给特殊类的一种方式,即唯一指针或共享指针 - 智能指针将自动处理内存释放 - 就像一种垃圾收集。如果你想做现代的C++,你应该阅读它们。 – 4386427

+0

@SohitGore - 顺便说一句:记得总是把警告转化为错误(即编译器选项)。你的编译器尖叫着你没有在该函数返回:-) – 4386427