请看一下我是如何在 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);
}
}