file:Node.hpp
#ifndef Node_hpp
#define Node_hpp
#include <stdio.h>
class Node{
public:
Node();
~Node();
void printData();
public:
int data;
Node *next;
Node *prev;
};
#endif /* Node_hpp */
file:Node.cpp
#include "Node.hpp"
#include <iostream>
using namespace std;
Node::Node(){
}
Node::~Node(){
}
void Node::printData(){
cout << data;
}
file:LinkedList.hpp
#ifndef LinkedList_hpp
#define LinkedList_hpp
#include <stdio.h>
#include "Node.hpp"
class LinkedList{
public:
LinkedList();
~LinkedList();
void ClearList();
bool ListEmpty();
int ListLength();
bool getElem(int index,Node *pNode);
int locateElem(Node *pNode);
bool preElem(Node *pCurrentNode,Node *pPreNode);
bool nextElem(Node *pCurrentNode,Node *pNextNode);
void ListTraverse();
bool ListInsert(int index,Node *pNode);
bool ListDelete(int index,Node *pNode);
bool ListInsertHead(Node *pNode);
bool ListInsertTail(Node *pNode);
private:
int m_iLength;
Node *m_pList;
};
#endif /* LinkedList_hpp */
file:LinkedList.cpp
#include "LinkedList.hpp"
#include <iostream>
using namespace std;
LinkedList::LinkedList(){
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
bool LinkedList::ListEmpty(){
return m_iLength==0?true:false;
}
int LinkedList::ListLength(){
return m_iLength;
}
void LinkedList::ClearList(){
Node *CurrentNode = m_pList->next;
while(CurrentNode != NULL){
Node *temp = CurrentNode->next;
delete CurrentNode;
CurrentNode = temp;
}
m_pList->next = NULL;
m_iLength = 0;
}
LinkedList::~LinkedList(){
ClearList();
delete m_pList;
m_pList = NULL;
}
bool LinkedList::ListInsertHead(Node *pNode){
Node *temp = m_pList->next;
Node *newNode = new Node;
if(newNode == NULL){
return false;
}
else{
m_pList->next = newNode;
newNode->data = pNode->data;
newNode->next = temp;
m_iLength++;
return true;
}
}
bool LinkedList::ListInsertTail(Node *pNode){
Node *CurrentNode = m_pList;
while(CurrentNode->next != NULL){
CurrentNode = CurrentNode->next;
}
Node *newNode = new Node;
if(newNode == NULL){
return false;
}
else{
CurrentNode->next = newNode;
newNode->data = pNode->data;
newNode->next = NULL;
m_iLength++;
return true;
}
}
bool LinkedList::ListInsert(int index, Node *pNode){
if(index < 0 || index > m_iLength){
return false;
}
else{
Node *CurrentNode = m_pList;
for(int k = 0;k < index;k++){
CurrentNode = CurrentNode->next;
}
Node *newNode = new Node;
if(newNode == NULL){
return false;
}
else{
newNode->data = pNode->data;
newNode->next = CurrentNode->next;
CurrentNode->next = newNode;
m_iLength++;
return true;
}
}
}
bool LinkedList::ListDelete(int index, Node *pNode){
if(index < 0 || index > m_iLength){
return false;
}
else{
Node *CurrentNode = m_pList;
Node *CurrentNodePre = NULL;
for(int k = 0;k < index;k++){
CurrentNodePre = CurrentNode;
CurrentNode = CurrentNode->next;
}
CurrentNodePre->next = CurrentNode->next;
pNode->data = CurrentNode->data;
delete CurrentNode;
CurrentNode = NULL;
m_iLength--;
return true;
}
}
bool LinkedList::getElem(int index,Node *pNode){
if(index < 0 || index > m_iLength){
return false;
}
else{
Node *CurrentNode = m_pList;
Node *CurrentNodePre = NULL;
for(int k = 0;k < index;k++){
CurrentNodePre = CurrentNode;
CurrentNode = CurrentNode->next;
}
pNode->data = CurrentNode->data;
return true;
}
}
int LinkedList::locateElem(Node *pNode){
Node *CurrentNode = new Node;
int count = 0;
while(CurrentNode->next != NULL){
CurrentNode = CurrentNode->next;
if(CurrentNode->data == pNode->data){
return count;
}
else{
count ++;
}
}
return -1;
}
bool LinkedList::preElem(Node *pCurrentNode,Node *pPreNode){
Node *CurrentNode = m_pList;
Node *tempNode = NULL;
while(CurrentNode->next != NULL){
tempNode = CurrentNode;
CurrentNode = CurrentNode->next;
if(CurrentNode->data == pCurrentNode->data){
if(tempNode == m_pList){
return false;
}
else{
pPreNode->data = tempNode->data;
return true;
}
}
}
return false;
}
bool LinkedList::nextElem(Node *pCurrentNode,Node *pNextNode){
Node *CurrentNode = m_pList;
while(CurrentNode->next != NULL){
CurrentNode = CurrentNode->next;
if(CurrentNode->data == pCurrentNode->data){
if(CurrentNode->next == NULL ){
return false;
}
else{
pNextNode->data = CurrentNode->next->data;
return true;
}
}
}
return false;
}
void LinkedList::ListTraverse(){
Node *CurrentNode = m_pList;
while(CurrentNode->next != NULL){
CurrentNode = CurrentNode->next;
CurrentNode->printData();
}
cout << endl;
}
file:demo.cpp
#include <iostream>
#include <string>
#include "LinkedList.hpp"
using namespace std;
int main(void){
Node node1;
node1.data = 3;
Node node2;
node2.data = 4;
Node node3;
node3.data = 5;
Node node4;
node4.data = 6;
LinkedList *pList = new LinkedList();
cout << endl << "测试ListInsertHead:" << endl << endl;
pList->ListInsertHead(&node1);
pList->ListTraverse();
pList->ListInsertHead(&node2);
pList->ListTraverse();
pList->ListInsertHead(&node3);
pList->ListTraverse();
pList->ListInsertHead(&node4);
pList->ListTraverse();
cout << endl << "测试ListInsertTail:" << endl << endl;
pList->ListInsertTail(&node1);
pList->ListTraverse();
pList->ListInsertTail(&node2);
pList->ListTraverse();
pList->ListInsertTail(&node3);
pList->ListTraverse();
pList->ListInsertTail(&node4);
pList->ListTraverse();
cout << endl << "测试ListInsert:" << endl << endl;
Node node5;
node5.data = 0;
pList->ListInsert(1, &node5);
pList->ListTraverse();
pList->ListInsert(2, &node5);
pList->ListTraverse();
pList->ListInsert(4, &node5);
pList->ListTraverse();
pList->ListInsert(11, &node5);
pList->ListTraverse();
cout << endl << "测试ListDelete:" << endl << endl;
Node node;
pList->ListDelete(1, &node);
node.printData();
cout << endl;
pList->ListTraverse();
pList->ListDelete(3, &node);
node.printData();
cout << endl;
pList->ListTraverse();
pList->ListDelete(5, &node);
node.printData();
cout << endl;
pList->ListTraverse();
cout << endl << "测试getElem:" << endl << endl;
pList->getElem(1, &node);
node.printData();
cout << endl;
pList->getElem(4, &node);
node.printData();
cout << endl;
pList->getElem(6, &node);
node.printData();
cout << endl;
pList->getElem(8, &node);
node.printData();
cout << endl;
cout << endl << "测试nextElem:" << endl << endl;
pList->ListTraverse();
pList->nextElem(&node1, &node);
node.printData();
cout << endl;
pList->nextElem(&node3, &node);
node.printData();
cout << endl;
pList->nextElem(&node4, &node);
node.printData();
cout << endl;
cout << endl << "测试preElem:" << endl << endl;
pList->ListTraverse();
pList->preElem(&node1, &node);
node.printData();
cout << endl;
pList->preElem(&node3, &node);
node.printData();
cout << endl;
pList->preElem(&node4, &node);
node.printData();
cout << endl;
cout << endl << "测试locateElem:" << endl << endl;
cout << pList->locateElem(&node1) << endl;
cout << pList->locateElem(&node3) << endl;
cout << pList->locateElem(&node4) << endl;
cout << endl << "测试ListLength:" << endl << endl;
cout << pList->ListLength() << endl;
cout << endl << "测试ClearList:" << endl << endl;
cout << pList->ListEmpty() << endl;
pList->ClearList();
pList->ListTraverse();
cout << pList->ListLength() << endl;
cout << pList->ListEmpty() << endl;
delete pList;
pList = NULL;
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦