自己写的一个list 源代码

 LIST

FILE : list.h list.cpp

COMPILER: VC 6.0

SRC:

class Node{
public:
  int dataItem;
  Node* prevNode;
  Node* nextNode;
};

class List{
private:
    Node *  curNode;//current node
    Node *  head;
    int listCount;
    int curIndex;
public:
    List();
    bool setNodeData(int index = 0, Node * nodeData = NULL);
    Node * getNodeData(int index = 0);
    int getListCount();
    int getCurIndex();
    bool insertToTail(Node* nodeData);
    bool insertToHead(Node * nodeData);
    bool insertToCustomPos(int index = 0, Node * nodeData = NULL);
    bool deleteFromTail();
    bool deleteFromHead();
    bool deleteFromCustomPos(int index);  

  };

list.cpp:

 

// list.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "list.h"

List::List(){
   
    this->curNode = new Node();

    if (this->curNode == NULL) {
        this->listCount = 0;
    }else{
        this->listCount = 1;       
        this->curNode->dataItem = 0;
        this->curNode->nextNode = NULL;
        this->curNode->prevNode = NULL;
    }
    this->curIndex = 0;
    this->head = this->curNode;
    this->curNode->nextNode = this->curNode->prevNode = NULL;
}

int List::getCurIndex(){
    return this->curIndex;
}

int List::getListCount(){
    return this->listCount;
}

Node * List::getNodeData(int index ){

    if(index > this->listCount || index < 0)
        return NULL;
    if(index = 0)
        return this->curNode;
    Node * temp = this->head;
    for(int i = 0; i < index ; i++){
        temp  = temp->nextNode;
    }
    return temp;

}

bool List::setNodeData(int index, Node * nodeData){
    if (index > this->listCount || index < 0)
        return false;
    if (index = 0)
        this->curNode->dataItem = nodeData->dataItem;
    Node * temp = this->head;
    int count = 0;
    do{
        temp = temp->nextNode;
        count ++;
    }while (count < index);
    temp->dataItem = nodeData->dataItem;
    temp->nextNode = nodeData->nextNode;
    temp->prevNode = nodeData->prevNode;
    return true;
}

bool List::insertToTail(Node * nodeData){
    if (nodeData == NULL) {
        return false;
    }
    Node * temp = this->head;
    while (temp->nextNode != NULL) {
        temp = temp->nextNode;
    }
    temp->nextNode = nodeData;
    nodeData->prevNode = temp;
    this->listCount ++;
    return true;
}

bool List::insertToHead(Node * nodeData){

    this->head->prevNode = nodeData;
    nodeData->nextNode = this->head;
    this->head = nodeData;
    this->listCount ++;
    this->curIndex ++;
    return true;
}

bool List::insertToCustomPos(int index, Node * nodeData){
   
    if (index > this->listCount || index < 0) {
        return false;
    }
    Node * temp = this->head;
    for( int i = 0 ; i < index ; i++){
        temp= temp->nextNode;
    }
   
    temp->nextNode->prevNode = nodeData;
    nodeData->nextNode = temp->nextNode;
    nodeData->prevNode = temp;
    temp->nextNode = nodeData;

    this->listCount ++;
    return true;
   
}


bool List::deleteFromTail(){
     Node * temp = this->head;
     while (temp->nextNode != NULL) {
        temp = temp->nextNode;
     }
     temp->prevNode->nextNode = NULL;
    delete(temp);
    this->listCount --;
    return false;
}

bool List::deleteFromHead(){
    Node * temp = this->head;
    temp->nextNode->prevNode = NULL;
    this->head = temp->nextNode;
    delete(temp);
    this->listCount --;
    if (this->curIndex != 0) {
        this->curIndex --;
    }
    return false;
}

bool List::deleteFromCustomPos(int index){
    if (index > this->listCount) {
        return false;
    }
    Node * temp = this->head;
    for(int i = 0; i < index ; i ++){
        temp = temp->nextNode;
    }
    temp->prevNode->nextNode = temp->nextNode;
    temp->nextNode->prevNode = temp->prevNode;
    delete(temp);
    this->listCount --;

    return true;

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值