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

全部评论

相关推荐

昨天 19:16
Java
点赞 评论 收藏
分享
09-13 17:25
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
真tmd的恶心,1.面试开始先说我讲简历讲得不好,要怎样讲怎样讲,先讲背景,再讲技术,然后再讲提升多少多少,一顿说教。2.接着讲项目,我先把背景讲完,开始讲重点,面试官立即打断说讲一下重点,无语。3.接着聊到了项目的对比学习的正样本采样,说我正样本采样是错的,我解释了十几分钟,还是说我错的,我在上一家实习用这个方法能work,并经过市场的检验,并且是顶会论文的复现,再怎么不对也不可能是错的。4.面试官,说都没说面试结束就退出会议,把面试者晾在会议里面,丝毫不尊重面试者难受的点:1.一开始是讲得不好是欣然接受的,毕竟是学习。2.我按照面试官的要求,先讲背景,再讲技术。当我讲完背景再讲技术的时候(甚至已经开始蹦出了几个技术名词),凭什么打断我说讲重点,是不能听出人家重点开始了?这也能理解,每个人都有犯错,我也没放心上。3.我自己做过的项目,我了解得肯定比他多,他这样贬低我做过的项目,说我的工作是错误的,作为一个技术人员,我是完全不能接受的,因此我就和他解释,但无论怎么解释都说我错。凭什么,作为面试官自己不了解相关技术,别人用这个方式work,凭什么还认为这个方法是错的,不接受面试者的解释。4.这个无可厚非,作为面试官,不打招呼就退出会议,把面试者晾着,本身就是有问题。综上所述,我现在不觉得第一第二点也是我的问题,面试官有很大的问题,就是专门恶心人的,总结面试官说教,不尊重面试者,打击面试者,不接受好的面试者,技术一般的守旧固执分子。有这种人部门有这种人怎么发展啊。最后去查了一下,岗位关闭了。也有可能是招到人了来恶心人的,但是也很cs
牛客20646354...:招黑奴啊,算法工程师一天200?
点赞 评论 收藏
分享
10-11 14:44
济南大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务