简体   繁体   English

链表在开始C ++时打印0值

[英]Linked List Printing 0 value at beginning c++

I am refreshing my c++ by creating a simple linked list class. 我通过创建一个简单的链表类来刷新我的c ++。 What I am having problems is when I try to print the list, there is a zero printing at the beginning of the list. 我遇到的问题是,当我尝试打印列表时,列表开头的打印为零。 How can I get rid of this? 我该如何摆脱呢? Also, I am having trouble with my second constructor. 另外,我在第二个构造函数上遇到了麻烦。 How would I go about this?` 我将如何处理?

Here is the code List.h 这是代码List.h

#ifndef NODE_H
#define NODE_H


class List{
    private:
        typedef struct Node{
            int data;
            struct Node* next;
        }* node;

        node head;
        int listLength;

    public:
        List();
        List(int data, node nextLink);
        void printList();
        void push(int data);
        void Delete(int d);
        int listSize(void);
};

my List.cpp 我的List.cpp

#endif

#include "node.h"
#include <iostream>
using namespace std;

List::List(){
    head->data=0;
    head->next= NULL;
    listLength=0;
}

List::List(int data, node nextLink){
    head=NULL;
    listLength++;
}

void List::push(int data){



    if(head==NULL){
        head->data=data; 
        head->next= NULL;
    }
    else{
        node cursor = head;
        while(cursor->next != NULL)
            cursor = cursor -> next;

        node newNode= new Node;
        newNode->data=data;
        newNode->next=NULL;
        cursor->next= newNode;
    }
    listLength++;
}

void List::printList(){
    node cursor=head;
    while(cursor!=NULL){
        //if(cursor->data==0){cursor=cursor->next;}
        if(cursor->next==NULL){
            cout<<cursor->data<<endl;
            return;
        }
        else{
            cout<<cursor->data<<" -> ";
            cursor=cursor->next;
        }

    }
    cout<<endl;
}
int main(){ 
    List li;
    li.push(2);
    li.push(3);
    li.push(0);
    li.push(4);
    li.printList();
    return 0;
}

You never initialize your head node, so you're writing to unallocated memory in the code below. 您永远不会初始化头节点,因此您将在下面的代码中写入未分配的内存。

if(head==NULL){
    head->data=data; 
    head->next= NULL;
}

It should be: 它应该是:

if(head==NULL){
    head = new Node; // added this line
    head->data=data; 
    head->next= NULL;
}

You also probably want the first constructor 您可能还想要第一个构造函数

List::List(){
    head->data=0;
    head->next= NULL;
    listLength=0;
}

to instead be 代替成为

List::List(){
    head = NULL;
    listLength=0;
}

As for the second constructor, I assume you want something like this? 至于第二个构造函数,我假设您想要这样的东西?

List::List(int data, node nextLink){
    head = new Node;
    head->data = data;
    head->next = nextLink;
    listLength = 1;
}

If not, could you better explain what you want? 如果没有,您能否更好地解释您想要什么?

I would also note that it would be generally considered good programming practice to create a constructor for the Node struct that initializes next to NULL , and then you wouldn't have to set it explicitly every time you create a new Node throughout your code. 我还要指出,通常会为在NULL next初始化的Node结构创建一个构造函数,这被认为是良好的编程习惯,因此,您不必在每次在整个代码中创建new Node都对其进行显式设置。

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

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