【问题标题】:Insertion and deletion in a stack using linked list使用链表在堆栈中插入和删除
【发布时间】:2017-06-17 13:06:25
【问题描述】:

该程序是关于使用 ling 列表在堆栈中插入和删除。push 工作正常,但删除有问题 pop() 函数有一些 错误。每次我尝试删除某些内容时,都会出现下溢的无限错误。 IE。顶部指针始终为空。

 #include<iostream>
 #include<stdlib.h>
 #include<process.h>

 using namespace std;

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

 node *create_new_node(int);
 void push(node*);
 void pop();
 void display(node*);

 int main()
  {
     top=NULL;
     int inf;
     char ch='y';
     while(ch=='y'||ch=='Y')
     {
         newptr=new node;
         cout<<"\nEnter the info to be added in the beginning of the stack\n";
         cin>>inf;
          if(newptr==NULL)
             cout<<"\nCannot create new node.ABORTING!!\n";
         else
         {
         newptr=create_new_node(inf);
         cout<<"\nPress enter to continue\n";
         system("pause");
         }
         push(newptr);
        cout<<"\nthe info has been inserted  in the stack\n";
        cout<<"\nThe stack now is\n";
        display(newptr);

        cout<<"\ndo you wish to add more elements to the stack.\nIf yes then 
 press y or else press n\n";
         cin>>ch;
         if(ch=='n'||ch=='N')
         {
             cout<<"\ndo you to delete elements from the stack\n";
             cout<,"\nIf yes then press d else press n\n";

             cin>>ch;
             if(ch=='d'||ch=='D')
             {
                 while(ch=='d'||ch=='D')
                 {
                     pop();
                     cout<<"\npress d to delete more elements y to add more 
  elements and n to exit\n";
                     cin>>ch;
                 }

             }

        }
     }
     delete(ptr);
     delete(newptr);
     delete(top);
     delete(save);
     return 0;
 }

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

  void push(node *np)
  {
     if(top==NULL)
         top=np;
     else
     { 
        save=top;
        top=np;
         np->next=save;
     }
 }

 void pop()
{
     if(top==NULL)
         cout<<"underflow";
     else
     {
        ptr=top;
        top=top->next;
        delete ptr;

     }
}

void display(node *np)
{
    while(np!=NULL)
    {
        cout<<np->info<<"->";
        np=np->next;
    }
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。
  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 edit 您的问题包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 你在无限循环中调用pop()。您提示用户“按 d 删除更多元素”,但实际上并不等待用户输入。

标签: c++ pointers data-structures linked-list


【解决方案1】:

显示的代码中有多个错误。

你的主要错误:

while(ch=='d'||ch=='D')
{
    pop();
    cout<<"\npress d to delete more elements y to add more elements and n to exit\n";
}

此时,当ch'd''D'时,执行当然会进入while循环。调用pop(),从堆栈中删除最顶部的元素,打印一条消息,并重复while 循环。

此时,您的程序将发现ch 仍然是'd''D'。什么都没有改变它的价值。不幸的是,计算机程序总是完全按照您的要求执行,而不是您认为您希望它执行的操作。不管你怎么看这里,你永远不会在这里找到任何改变ch值的代码。它将永远保持其当前值。于是while 循环再次运行。然后再次。然后再次。在这一点上,ch 的值没有任何改变,所以你有一个无限循环。

另外,在你的main:

    newptr=new node;

此指针的值稍后会与NULL 进行比较;如果不是......它会被完全覆盖

     newptr=create_new_node(inf);

除了泄漏内存之外,这完全没有任何作用。此代码似乎是剩余的垃圾,应在修复错误的while 循环逻辑后进行清理。

【讨论】:

  • 对不起,我没有意识到我在 while 循环中犯的错误。现在程序运行正常。
  • 关于你所说的垃圾......我是编程新手,对内存泄漏和所有东西一无所知......如果你能给我一些建议,我将不胜感激继续。
  • 您的main() newed 了一个新指针。然后指针被覆盖。指向newed 内存的指针丢失。程序的其余部分将采取步骤到delete 什么是newed,而不是这个实例。内存泄漏,丢失。这对于这个简单的程序来说无关紧要,但是如果您希望有一天成为一名专业的 C++ 开发人员,那么您越早学习和理解正确的内存管理是如何工作的,对您来说就越容易。这无法在 stackoverflow.com 上的简短评论中完全解释,但应该包含在您的 C++ 书籍中。
猜你喜欢
  • 1970-01-01
  • 2014-11-09
  • 2015-03-08
  • 1970-01-01
  • 2016-10-19
  • 2015-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多