数据结构--链表--c++实现

node.h

#ifndef node_h
#define  node_h

class node
{
public:
    void print();
    int data;
    node *next;

};

#endif

node.cpp

#include "node.h"
#include <iostream>
using namespace std;

void node::print()
{
    cout<<data<<endl;
}

list.h

#ifndef List_h
#define List_h
#include "node.h"
class List
{
public:
    List(int size);
    ~List();
    void clearList();
    bool listEmpty();
    int listLength();
    bool getElem(int i,node *n);
    int locateElem(node *n);
    bool priorElem(node *pCurrentNode,node *preNode);
    bool nextElem(node *pCurrentNode,node *nextNode);
    void listTraverse();
    bool listInsert(int i,node *n);
    bool listDelete(int i,node *n);
    bool listInsertHead(node *n);
    bool listInsertTail(node *n);

private:
    node *_list;
    int _size;
    int _length;
};


#endif

list.cpp

include “lianbiao.h”

include

using namespace std;

List::List(int size)
{
_list = new node;
_list->data = 0;
_list->next = NULL;
_length = 0;
}

List::~List()
{
clearList();
delete _list;
_list = NULL;
}

void List::clearList(){
node *currentNode =_list->next;
while (currentNode !=NULL)
{
node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
_list->next = NULL;
_length = 0;
}

bool List::listEmpty(){
if (_length == 0)
{
return true;
}
return false;
}

int List::listLength(){
return _length;
}

bool List::getElem(int i,node *n){
if (i<0||i>=_length)
{
return false;
}
node *currentNode = _list;
node *currentNodePre = NULL;
for(int j = 0;j<=i;j++){
currentNodePre = currentNode;
currentNode = currentNode->next;
}
n->data = currentNode->data;
return true;
}

int List::locateElem(node *n){
node *currentNode = _list;
int count = 0;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->data == n->data)
{
return count;
}
count++;
}
return -1;
}

bool List::priorElem(node *pCurrentNode,node *preNode){
node *currentNode = _list;
node *tempNode = NULL;
while (currentNode->next != NULL)
{
tempNode = currentNode;
currentNode = currentNode->next;
if (currentNode->data == pCurrentNode->data)
{
if (tempNode == _list)
{
return false;
}
preNode->data = tempNode->data;
return true;
}
}
return false;
}

bool List::nextElem(node *pCurrentNode,node *nextNode){
node *currentNode = _list;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->data == pCurrentNode->data)
{
if (currentNode->next == NULL)
{
return false;
}
nextNode->data = currentNode->next->data;
return true;
}
}
return false;
}

void List::listTraverse(){
node *currentNode = _list;
while (currentNode->next != NULL){
currentNode= currentNode->next;
currentNode->print();
}
}

bool List::listInsert(int i,node *n){
if (i<0||i>_length)
{
return false;
}
node *currentNode = _list;
for(int j = 0;j

demo.cpp

include “lianbiao.h”

include

using namespace std;

int main(){
node node1;
node1.data = 3;
node node2;
node2.data = 4;
node node3;
node3.data = 5;
node node4;
node4.data = 6;
List *p = new List(6);
p->listInsertHead(&node1);
p->listInsert(1,&node2);
p->listInsertTail(&node3);
p->listInsertTail(&node4);
node temp;//p->listDelete(1,&temp);cout<<”temp:”<

猜你喜欢

转载自blog.csdn.net/weixin_43150428/article/details/82531468