c++实现链表的基本操作
2017-06-22 23:43
525 查看
头文件:(新加结点类)
基本代码实现:
效果:
#ifndef LIST_H #define LIST_H class Node{ public: int data; Node *next; void printNode(); }; class List{ public: List(); ~List(); void clearList(); bool isEmpty(); int ListLength(); bool getElem(int i,Node *pNode); int locateElem(Node *pNode); bool priorElem(Node *pCurrentNode,Node *pPreNode); bool nextElem(Node *pCurrentNode,Node *pNextNode); void ListTraverse(); bool listInsert(int i,Node *pNode); bool listDelete(int i,Node *pNode); bool listInsertHead(Node *pNode); bool listInsertTail(Node *pNode); private: Node *m_pList; int m_iLength; }; #endif
基本代码实现:
#include "List.h" #include <iostream> using namespace std; void Node::printNode(){ cout<<data<<endl; } List::List(){ m_pList=new Node; m_pList->data=0; m_pList->next=NULL; m_iLength=0; } bool List::isEmpty(){ return m_iLength==0?true:false; } int List::ListLength(){ return m_iLength; } void List::clearList(){ Node *currentNode=m_pList->next; while(currentNode->next!=NULL){ Node *temp=currentNode->next; delete currentNode; currentNode=temp; } m_pList->next=NULL; m_iLength=0; } List::~List(){ clearList(); delete m_pList; m_pList=NULL; } bool List::listInsertHead(Node *pNode){ Node *temp=m_pList->next; Node *newNode=new Node; if (newNode== NULL) {return false; } newNode->data=pNode->data; m_pList->next=newNode; newNode->next=temp; m_iLength++; return true; } bool List::listInsertTail(Node *pNode){ Node *currentNode=m_pList; while(currentNode->next!=NULL){ currentNode=currentNode->next; } Node *newNode=new Node; if (newNode==NULL) { return false; } newNode->data=pNode->data; newNode->next=NULL; currentNode->next=newNode; m_iLength++; return true; } bool List::listInsert(int i,Node *pNode){ if (i<0||i>m_iLength) { return false; } Node *currentNode=m_pList; for (int k=0;k<i;k++) { currentNode=currentNode->next; } Node *newNode=new Node; if (newNode==NULL) { return false; } newNode->data=pNode->data; newNode->next=currentNode->next; currentNode->next=newNode; m_iLength++; return true; } bool List::listDelete(int i,Node *pNode){ if (i<0||i>=m_iLength) { return false; } Node *currentNode=m_pList; Node *currentNodeBef=NULL; for (int k=0;k<=i;k++) { currentNodeBef=currentNode; currentNode=currentNode->next; } currentNodeBef->next=currentNode->next; pNode->data=currentNode->data; delete currentNode; currentNode=NULL; m_iLength--; return true; } int List::locateElem(Node *pNode){ Node *currentNode=m_pList; int loc=0; while(currentNode->next!=NULL){ currentNode=currentNode->next; if (currentNode->data==pNode->data) { return loc; } loc++; } return -1; } bool List::getElem(int i,Node *pNode){ if (i<0||i>=m_iLength) { return false; } Node *currentNode=m_pList; for (int k=0;k<=i;k++) { currentNode=currentNode->next; } pNode->data=currentNode->data; return true; } bool List::priorElem(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; } pPreNode->data=tempNode->data; return true; } } return false; } bool List::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; } pNextNode->data=currentNode->next->data; return true; } } return false; } void List::ListTraverse(){ Node *currentNode=m_pList; while(currentNode->next!=NULL){ currentNode=currentNode->next; currentNode->printNode(); } }
效果:
#include <iostream> #include "List.h" using namespace std; int main(){ Node e1; Node e2; Node e3; Node e4; Node e5; List *p=new List(); e1.data=5; e2.data=9; e3.data=4; e4.data=6; e5.data=8; Node e; e.data=24; p->listInsertHead(&e1); p->listInsertHead(&e2); p->listInsertTail(&e3); p->listInsertTail(&e4); p->listInsertHead(&e5); p->ListTraverse(); cout<<endl; p->listInsert(3,&e); p->ListTraverse(); cout<<endl; p->listDelete(2,&e); cout<<"删除:"<<e.data<<endl; p->ListTraverse(); cout<<"表元素个数:"<<p->ListLength()<<endl; cout<<endl; p->getElem(5,&e); e.printNode(); cout<<endl; cout<<p->locateElem(&e1)<<endl; cout<<p->locateElem(&e2)<<endl; p->nextElem(&e2,&e); cout<<"e2的后继"<<e.data<<endl; p->priorElem(&e2,&e); cout<<"e2的前驱"<<e.data<<endl<<endl; p->clearList(); if(p->isEmpty()) cout<<"Empty"<<endl; return 0; }
相关文章推荐
- 链表的基本操作函数算法(C/C++实现)
- C++实现链表基本操作
- [C++]数据结构:有序链表SortedChain的基本实现与操作
- C++实现链表基本操作
- c++实现链表的基本操作
- C++ 实现链表的基本操作之一:链表插入
- C++实现链表的基本操作及测试用例
- c/c++ 数据结构-线性表(单链表基本操作的实现)
- 详细的单链表基本操作C/C++实现
- c++实现链表的基本操作
- c++实现链表的基本操作
- 使用C++链表来实现二叉树的存储和基本操作
- 【C++数据结构】模版类实现双循环链表的基本操作
- 使用C++实现链表的基本操作
- C++实现链表基本操作
- 双链表基本操作C++实现
- c++链表基本操作
- C++类模板 实现双向循环链表的基本算法 《数据结构》(C++版 北京科海)中摘抄
- 链表的基本操作实现
- C++自定义链表操作实现