【问题标题】:new nodes always becomes the first node in my single linkedlist [closed]新节点总是成为我的单链表中的第一个节点[关闭]
【发布时间】:2014-10-12 17:57:09
【问题描述】:

我一直在学习 C++ 中单链表的实现。 问题是我理解了单链表背后的概念,但我无法猜测我在代码中哪里出错了。 每当我插入一个新节点时,它都会占据第一个位置(即头部),并且列表的大小始终为 1。 我试图解决它,但有时显示功能会变成无限循环。 我对此一无所知。 一个星期以来一直困扰着我。

我只能猜测,我没有在代码中正确引用地址。 我搞砸了指针吗?帮助我理解我所犯的错误。谢谢。

SLLCLAS.CPP

#include<iostream>
#include<conio.h>

class node{
    public:
        int data;
        node *next;
};

class sll{
    private:
        node *head;
    public:
        ssl(){
            head=NULL;
        }
        void display();
        void insert(int,int);
};

void sll::display(){
    if(head==NULL){
        cout<<"List is Empty";
    }else{
        cout<<"\n";
        for(node *t=head;t!=NULL;t=t->next){
        cout<<t->data<<"->";
    }
    cout<<"\n";
}

void sll::insert(int position,int data){
    node *temp=new node();
    temp->data=data;
    if(position<0){
        cout<<"\nPosition not valid";
    }else if(head==NULL || position==1){
        temp->next=head;
        head=temp;
    }else{
        node *p,*q;
        q=head;
        int count=1;
        while(count<position && q!=NULL){
        count++;
        cout<<"\nCount:"<<count;
        p=q;
        q=q->next;
    }
    p->next=temp;
    temp->next=q;
    delete(temp);
}

int main(){
    clrscr();
    sll list;
    int ch,val,pos;
    do{
        cout<<"\nSINGLY LINKED LIST\n1: Insert\n2: Delete\n3: Display\n Enter your choice:";
        cin>>ch;
        switch(ch){
            case 1:
                cout<<"\nEnter the position:";
                cin>>pos;
                cout<<"\nEnter the value:";
                cin>>val;
                list.insert(pos,val);
                list.display();
                break;
            case 2:
                break;
            case 3:
                list.display();
                break;
            default:
                cout<<"\nWrong choice";
        }
        cout<<"\nDo you want to continue(1/0):";
        cin>>ch;
    }while(ch!=0);
    getch();
}


EDIT:

我正在 Windows 8.1 64 位上的 Turbo C++ 3.0 版上运行代码 使用dosbox。

【问题讨论】:

  • 缩进你的代码。停止在数据结构函数中使用 IO。
  • 请提供一个没有调试代码的可读示例
  • &lt;iostream.h&gt;?标头应为&lt;iostream&gt;。另外,您是从哪里得知分号遵循#include 指令的?至于你的问题,你是先在纸上画出链表吗?您应该在编写一行代码之前这样做。一旦您了解了链接如何在纸上 工作,您就可以将您在纸上的内容转移到 C++ 程序中。如果 C++ 不能正常工作,你需要知道你的程序在哪里偏离了计划。
  • @PaulMcKenzie,那个(分号)是我,我认为(我使用的在线格式化程序搞砸了,所以我恢复了)
  • 1.将位置作为数字给出对列表不友好,ssl::insert 应该将位置作为指针或迭代器。 2.使用构造函数(ssl::ssl())代替void ssl::initList()

标签: c++ linked-list singly-linked-list


【解决方案1】:

声明

delete(temp);

insert 函数中高度可疑。

基本上temp 引用您创建和设置的新节点。但是您在退出该功能之前将其删除。在第一个insert 之后,您正在操作释放的内存。

声明

head=*temp;

没有意义。 headnode*,您正在分配 node 值。如果它编译过,head 也包含垃圾值。

【讨论】:

  • head=*temp 是菜鸟!!!抱歉。但是当我上次编译时,我使用了 head=temp。
【解决方案2】:

这是函数insert的固定版本:

void sll::insert(int position, int data) {
    node* temp = new node();
    temp->data = data;
    if (position <= 0) {
        cout << "\nPosition not valid";
    } else if (head == NULL || position == 1) {
        temp->next = head;
        head = temp;   // ---------------------- problem here
    } else {
        node* p, *q;
        q = head;
        int count = 1;
        while (count < position && q != NULL) {
            count++;
            cout << "\nCount:" << count;
            p = q;
            q = q->next;
        }
        p->next = temp;
        temp->next = q;
    }
    //delete (temp); //<---------------- problem here
}
  • 您创建了一个要插入到列表中的节点,但在函数末尾被删除(指针引用了该内存地址),访问内存时,任何事情都可能发生。
  • 另一个问题是编译器错误,您将 node 变量分配给 node* 变量,使用您正在测试的编译器。
  • 还有其他问题,如 #include &lt;iostream.h&gt;,iostream 是来自 C++ 的没有扩展名的标头(例如:#include &lt;vector&gt;#include &lt;iostream&gt;)。
  • main函数不是标准的,不使用参数改成int main(),有参数改成int main(int argc, char* argv[])。您需要从 main 函数返回整数。
  • 我没有定义 clrscr(); 函数,可能与平台有关(我正在 Windows 中使用带有 C++11 的 MinGW GCC 4.9.0 进行测试)。

【讨论】:

  • 如果位置 0 是有效位置,则在头部或位置 1 插入是不一样的...您必须决定 a) 使用基于 0 的索引或 b) 更改第一个 if 条件到(位置
【解决方案3】:
    #include <iostream>
    using namespace std;
   struct node
    {
     int info;
     node *next;
    };

 class DS
  {
    private:
    node *temp;
    node *temp1;
    node *head;
    int key;
    int x;
    public:
    DS()
    {
       head=temp=temp1=NULL;
     }

   void insert()
   {
       for (int i=0; i<1; i--)
     {
          cout<<"Enter 0 to stop and 1 to continue:";
          cin>>x;
          cout<<endl;
          if (x==1)
          {
    if (head==NULL)
    {
        head=new node;
        cout<<"Enter the value in head's Info";
        cin>>head->info;
        cout<<endl;
        head->next=NULL;

    }

    cout<<"Enter the number after which you want to add node:";
    cin>>key;
    cout<<endl;
    temp=head;

    while (temp!=NULL)
    {
        if (temp->info==key)
        {
            temp1=new node;
            cout<<"Enter value in new node:";
            cin>>temp1->info;
            cout<<endl;
            temp1->next=temp->next;
            temp->next=temp1;
        }
        temp=temp->next;
    }
        }
        else break;

   }
    }

void Del()
{
    temp=head;
    if(head==NULL)
        return;
    cout<<"Enter the number after which you want to delete the node:";
    cin>>key;
    cout<<endl;
    while( temp!=NULL)
    {
        if (temp->next->info==key)
        {
            temp1=temp->next;
            temp->next=temp->next->next;
            delete temp1;
            break;
        }

            temp=temp->next;
    }
}

   void searching()
   {
    temp=head;
    cout<<"Enter key to found node:";
    cin>>key;
    cout<<endl;
    while (temp!=NULL)
    {
        if (temp->info==key)
        {
            cout<<"Node found"<<endl;
            break;
        }
        temp=temp->next;
    }
}

void printing()
{
    temp=head;
    while (temp!=NULL)
    {
        cout<<endl;
        cout<<temp->info<<endl;
        temp=temp->next;
    }
}

void emptiness()
{
    temp=head;
    if (temp->next==NULL)
        {
            cout<<"Empty list";
            cout<<endl;
    }
    else if (temp!=NULL)
        cout<<"Not empty";
}
};

   int main()
 {
    DS obj;
    obj.insert();
    cout<<endl;
    obj.Del();
    cout<<endl;
    obj.searching();
    cout<<endl;
    obj.printing();
    cout<<endl;
    obj.emptiness();

   return 0;
  }

【讨论】:

  • 您能否添加一些解释性说明来补充您的代码?
猜你喜欢
  • 1970-01-01
  • 2013-01-15
  • 2018-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
相关资源
最近更新 更多