C ++指针返回struct实现:Linked List

时间:2013-08-21 03:52:28

标签: c++ pointers memory-management struct linked-list

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

 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. 的头节点
  3. 继续为20,34,32,123等其他节点添加值... 节点的随机值
  4. 我不知道如何初始化head的指针并在其上添加值。

    请帮帮我。我将不胜感激。

2 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分: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);



          }            
                      }