C++ C++;指针返回结构实现:链表

C++ C++;指针返回结构实现:链表,c++,pointers,memory-management,struct,linked-list,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); }

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

 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);
我想做的事

  • 创建一个值为10的头部节点
  • 继续为具有20、34、32、123等的其他节点添加值。。。 节点的随机值
  • 我不知道如何初始化head的指针并在其上添加值。


    请帮帮我。非常感谢。

    在您的
    显式
    构造函数中,调用
    节点(nullptr,data)
    位于本地空间中,关闭
    }
    后结果超出范围

    为什么不能将值保存在此构造函数中(而不是调用另一个构造函数)

    顺便说一下,您的
    insert\u节点
    功能运行良好:

    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();
    }
    
    intmain(){
    //此测试代码生成正确的结果
    //(假设您修复了显式构造函数):
    节点_01(9);
    node*node_ptr_01=新节点(1);
    
    CUT< P>请访问C++中如何实现链表。我相信理解< /P>会很简单。 这是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
    
    #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
    
    这是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;
    
                  }
    
    #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;
         }
    
    #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);
    
    
    
              }            
                          }        
    
    这是LinkedList.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;
    
                  }
    
    #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;
         }
    
    #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);
    
    
    
              }            
                          }        
    
    #包括“Node.h”
    #包括“LinkedList.h”
    #包括
    Node*LinkedList::createNode(){
    节点*临时节点;
    tempNode=新节点();
    tempNode->setNext(空);
    tempNode->setData(getUserData());
    返回临时节点;
    }
    int LinkedList::getLength(){
    节点*临时节点=头部;
    整数计数=1;
    if(NULL==头){
    返回0;
    }否则{
    while(NULL!=tempNode->getNext()){
    tempNode=tempNode->getNext();
    计数++;
    }
    返回计数;
    }
    }     
    LinkedList::LinkedList(){
    head=NULL;
    //if(NULL==头){
    //head=createNode();
    //            }
    }
    void LinkedList::addNodeAtheStart(){
    节点*临时节点;
    tempNode=createNode();
    如果(空!=头){
    tempNode->setNext(头部);
    head=tempNode;
    }否则{
    head=tempNode;
    }
    }
    void LinkedList::addNodeAtAGivenLocation(int位置){
    节点*临时节点;
    节点*tempNode2;
    if(getLength()getNext();
    }    
    tempNode->setNext(createNode());
    }
    }
    void LinkedList::deleteFirstNode(){
    节点*临时节点;
    if(NULL==头){
    
    std::coutus通常,您创建一个
    列表
    类,该类使用您的
    节点
    结构来处理从列表中插入和删除节点的操作。使用链接列表的客户端代码将使用您的
    列表
    类,而不是直接操作
    节点
    。您可以从斯坦福大学获得更多信息。请阅读有关li的信息链接列表的概念,如添加、删除和搜索。您需要将Head作为开始节点,它应该是全局的,frm然后您必须使用它。