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;
}