2

我知道下面的代码非常简单,但我一直坚持如何实现它以获得正确的输出。感到很沮丧。

 struct node
 {
    node* p_next;
    int p_data;

    node(node* head, int data)
    {
        p_next = head;
        p_data = data;
    }

    explicit node(int data)
    {
        node(nullptr, data);
    }
 };

所以我在 C++ 中有这个结构来构造一些链表。

然后我有插入功能来插入一些节点到该链表

 node* insert_node(node* head, int data)
 {
    return new node(head, data);
 }

在这里,我开始变得愚蠢。我如何实际制作一些带有实际值的链表?我很困惑如何首先构建一个列表并添加一些值。

我一直在尝试以下但得到错误。

struct node node_01(1);
node* node_ptr_01 = new node(1);

我想做的事。

  1. 创建一个值为 10 的头节点
  2. 继续为其他节点添加 20、34、32、123 等的值...节点的随机值

我不知道如何初始化 head 指针并在它们上添加值。

请帮我。我将不胜感激。

4

2 回答 2

1

在您的explicit构造函数中,调用node(nullptr, data);是在本地空间中,结果在关闭后超出范围}

为什么你不能只保存这个构造函数中的值(而不是调用另一个?):

explicit node(int data)
{
    p_next = nullptr;
    p_data = data;
}

顺便说一句,您的insert_node功能运行良好:

int main() {

    // this testing code produces the correct result 
    // (assuming you fixed your explicit constructor):

    node node_01(9);
    node* node_ptr_01 = new node(1);

    cout << node_01.p_data << endl;
    cout << node_ptr_01->p_data << endl;

    node* n = insert_node(node_ptr_01, 5);
    cout << n->p_data;

    cin.get();
}
于 2013-08-21T04:06:49.637 回答
1

请看一下我是如何在 c++ 中实现链表的。我相信它会很容易理解

这是Node.h

#ifndef _NODE_
#define _NODE_

class Node{


      private:

      char data;
      Node* next;
      void allocate();

      public:
      Node();
      char getData(void);
      Node*getNext();
      void setData(char data);
      void setNext(Node * next);
      ~Node();

      };

#endif

这是Node.cpp

#include<iostream>
#include"Node.h"

Node::Node(): data('0'), next(NULL){

             std::cout<<"Node created"<<std::endl;
             }

void Node::setData(char data){

     this->data=data;
     }

void Node::setNext(Node *next){

     this->next=next;
     }    

char Node::getData(void){

     return data;
     }   

Node* Node:: getNext(void){

      return next;
      }

Node::~Node(){
              std::cout<<"Node deleted"<<std::endl;

              }

这是linkedList.h

#include"Node.h"

#ifndef Linked_
#define Linked_


class LinkedList{

      private:
           Node* head; 
           Node* createNode();  
           Node* getToLastNode();  
           int getLength();
           char getUserData();



           public:
                  LinkedList();
                  void addNodeAtTheBeginning();
                  void addNodeAtAGivenLocation(int);
                  void addNodeAtTheEnd();

                  void deleteFirstNode();
                  void deleteNodeAtAGivenLocation(int);
                  void deleteLastNode(); 
                  void display();
                  void deleteLinkedList();
                  ~LinkedList();


      };



#endif

这是LinkedList.cpp

#include"Node.h"
#include"LinkedList.h"
#include<iostream>


Node* LinkedList::createNode(){

      Node *tempNode;
      tempNode = new Node();
      tempNode->setNext(NULL);
      tempNode->setData(getUserData());
      return tempNode;   

      }

int LinkedList::getLength(){
    Node *tempNode=head;
    int count=1;
    if(NULL==head){
                   return 0;
                   }else{

     while(NULL!=tempNode->getNext()){
                   tempNode=tempNode->getNext();
                   count++; 

    }

    return count;
}


}     


LinkedList::LinkedList(){
                         head=NULL;
                        // if(NULL==head){

                          //              head=createNode();
                            //            }

                               }
void LinkedList::addNodeAtTheBeginning(){


             Node *tempNode;
             tempNode=createNode();

             if(NULL!=head){
             tempNode->setNext(head);
             head=tempNode;      
             }else{
                   head=tempNode;                      

              }

             }

void LinkedList::addNodeAtAGivenLocation(int position){

             Node *tempNode;

             Node *tempNode2;

             if(getLength()<position){
                  std::cout<<"No node can be inserted at this poition "<<std::endl;
             }else{
                   tempNode=createNode();   
                     tempNode2=head;             
             for(int i=1;i<position;i++){    

               tempNode2=tempNode2->getNext();  
               }        
                  tempNode->setNext(tempNode2->getNext());
                  tempNode2->setNext(tempNode);                                     
              }



}
void LinkedList::addNodeAtTheEnd(){

                if(NULL==head){
                               head=createNode();
                               }else{
                                 Node *tempNode=head;
                                 while(NULL!=tempNode->getNext()){    
                                     tempNode=tempNode->getNext();
                                     }    
                                     tempNode->setNext(createNode());                
                                    }

}

void LinkedList::deleteFirstNode(){

     Node *tempNode;

     if(NULL==head){
                    std::cout<<"No node available for deletion"<<std::endl;
     }else{

            tempNode=head;
            head=head->getNext();
            delete tempNode;              

                          }

                                    }
void LinkedList::deleteNodeAtAGivenLocation(int position){

             Node *tempNode;

             if(getLength()<=position){
                  std::cout<<"No node can be deleted as no node exist at this poition "<<std::endl;
             }else{

             for(int i=1;i<position;i++){    
               tempNode=head;
               tempNode=tempNode->getNext();  
               }        

                  tempNode->setNext(tempNode->getNext()->getNext()); 
                  delete tempNode->getNext();                                    
              }



                                               }
void LinkedList::deleteLastNode(){

     Node *tempNode;
     Node *tempNode2;

     if(NULL==head){
                    std::cout<<"No node available for deletion"<<std::endl;
     }else{

            tempNode=head;

            while(NULL!=tempNode->getNext()){

            tempNode=tempNode->getNext();
            tempNode2=tempNode;

            }
            tempNode=tempNode->getNext();
            tempNode2->setNext(NULL);
            delete tempNode;              

                          }

                                   }

LinkedList::~LinkedList(){

                         Node *tempNode=NULL;

                         if(NULL==head){
                               std::cout<<"No nodes in the Linked List available for Deletion"<<std::endl;          
                                        }else{

                                              tempNode =head;

                                              while(NULL!=head->getNext()){
                                                                              tempNode=head;
                                                                               head=head->getNext();
                                                                                delete tempNode;
                                                                               }
                                                                               delete head;
                                              }



                         std::cout<<"Linked List Deleted"<<std::endl; 
                         head=NULL;      
                                }

void LinkedList::display(void){

  Node *tempNode;
  tempNode=head;

  if(NULL==head){

        std::cout<<"head-->X";

       }else{

        std::cout<<"head-->";
        while(NULL!=tempNode->getNext()){

                std::cout<<"["<<tempNode->getData()<<"]-->"; 
                tempNode=tempNode->getNext();         

        }
        std::cout<<"["<<tempNode->getData()<<"]-->X"<<std::endl; 

        }

      }

void LinkedList::deleteLinkedList(){

     delete this;
     head=NULL;

     }


char LinkedList::getUserData(){
     char data;
     std::cout<<"Enter Data"<<std::endl;
     std::cin>>data;
     return data;
     }

最后是main.cpp

#include <cstdlib>
#include <iostream>
#include"LinkedList.h"
#include"Node.h"
#include<stdlib.h>

void printMenu();
int getUserSelection();
void performOperation(int);
LinkedList lk;


int main(){
    int option=0;
    while(9!=option){
    printMenu();
    option=getUserSelection();
    performOperation(option);

   }


}


void printMenu(void){

                  std::cout<< ""<<std::endl;
                  std::cout<< "1) Add Node At The Beginning"<<std::endl;
                  std::cout<< "2) Add Node At A Given Location"<<std::endl;
                  std::cout<< "3) Add Node At The End"<<std::endl; 
                  std::cout<< "4) Delete First Node"<<std::endl;
                  std::cout<< "5) Delete Node At A Given Location"<<std::endl;
                  std::cout<< "6) Delete Last Node"<<std::endl; 
                  std::cout<< "7) Display "<<std::endl;
                  std::cout<< "8) Delete LinkedList"<<std::endl;
                  std::cout<< "9) Exit"<<std::endl;

            }

int getUserSelection(){
    int option=0;
    std::cout<<"Select an option: "<<std::endl;
    std::cin>>option;
    return option;

}
void performOperation(int option){

          switch (option){
                 case 1:
                      lk.addNodeAtTheBeginning();
                      break;
                 case 2:{
                      int location=0;
                      std::cout<<"Enter a location:"<<std::endl;
                      std::cin>>location;
                       lk.addNodeAtAGivenLocation(location);
                       }
                      break;
                 case 3:
                      lk.addNodeAtTheEnd();
                      break;
                 case 4:
                      lk. deleteFirstNode();
                      break;
                 case 5:{
                      int location=0;
                      std::cout<<"Enter a location:"<<std::endl;
                      std::cin>>location;
                      lk.deleteNodeAtAGivenLocation(location);
                      }
                      break;
                 case 6:
                      lk.deleteLastNode();
                      break;
                 case 7:
                      lk.display();
                      break;

                 case 8:
                      lk.deleteLinkedList();
                      break;                            

                 case 9:
                      exit(0);



          }            
                      }        
于 2013-08-21T04:51:04.977 回答