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

C++ pointer return struct implementation : Linked List

本文关键字:链表 实现 结构 指针 返回 C++      更新时间:2023-10-16

我知道下面的代码很简单,但我一直被困在如何实现它以获得正确的输出上。感觉很沮丧。

 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的指针并在其上添加值

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

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();
}

请介绍一下我是如何在c++中实现链表的。我相信理解会很简单

这是节点.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);

          }            
                      }