数据结构--链表--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:”<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值