简体   繁体   English

循环单行插入,添加新节点并替换它

[英]Circular SIngly LInked LIst , ADDing a new node and displaing it

Neither of the compilers I've used are able to debug it. 我使用过的两个编译器都无法对其进行调试。 I am trying to add a new node at the end of the list and then displaying it,,, they are not showing any sort of errors , both give a send dont send error by windows , I think this may be a memory leak..Please help me 我试图在列表的末尾添加一个新节点,然后显示它,但它们没有显示任何类型的错误,都给出了Windows的send not don send错误,我认为这可能是内存泄漏。请帮我

#include <iostream>
#include <conio.h>
using namespace std;

struct Node
{
    int data;
    Node *nextptr;
};


class CLLIST{

private:
    Node*firstptr;
    Node*lastptr;

public:
     CLLIST(){

     cout << "Constructor Called !";
      firstptr=lastptr=NULL;
}

 void insert_at_back(int val){
     cout << " \n \n I am in the insert at back function: ";
     Node*newptr;
     newptr = new Node;
     newptr->data=val;

     if(firstptr=NULL)//means list is empty
     {
         firstptr=newptr;

     }else{
         lastptr->nextptr=newptr;
     }

     lastptr=newptr;
     lastptr->nextptr=firstptr;
 }

 void display(){

     Node *temptr,*endptr;
     temptr = new Node;
     endptr = new Node;

     temptr=firstptr;
     endptr = NULL;
     while(temptr!=endptr){

         cout << "I am in the display Function: ";
         cout << firstptr->data << " ";
         firstptr=firstptr->nextptr;
         endptr=firstptr;}

         delete temptr;
         delete endptr;
     }


 };





 int main()
 {
 CLLIST obj1;




  obj1.insert_at_back(26);

  obj1.display();

 cout << " \n \n Done !";

getch();
 }

temptr=firstptr; temptr = firstptr; endptr = NULL; endptr = NULL; while(temptr!=endptr){ while(temptr!= endptr){

     cout << "I am in the display Function: ";
     cout << firstptr->data << " ";
     firstptr=firstptr->nextptr;
     endptr=firstptr;}

     delete temptr;
     delete endptr;
 }

you are comparing tempptr with endptr in the while condition, but you never reassign the tempptr because in the beggining tempptr = firstptr, and then firstptr cycles through list and then you assign endptr = firstptr, this means that tempptr will always be equal to firstptr and endptr and condition temptr!=endptr will never fail 您在while条件下将tempptr与endptr进行比较,但是您永远不要重新分配tempptr,因为在开始时tempptr = firstptr,然后firstptr在列表中循环,然后分配endptr = firstptr,这意味着tempptr将始终等于firstptr和endptr和条件temptr!= endptr将永远不会失败

instead of 代替

firstptr=firstptr->nextptr; firstptr = firstptr-> nextptr;

use 采用

tempptr = tempptr->nextptr; tempptr = tempptr-> nextptr;

also you can use the do { } while() statement and initilize endptr in the begginng 您也可以使用do {} while()语句并在开始时初始化endptr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM