#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node *next;
void printNode();
};
#endif
#include "Node.h"
#include <iostream>
using namespace std;
void Node::printNode(){
cout<<data<<endl;
}
#ifndef LIST_H
#define LIST_H
#include "Node.h"
class List
{
public:
List();
~List();
void ClearList();
bool ListEmpty();
int ListLength();
bool GetElem(int i, Node *pNode);
int locateElem(Node *pNode);
bool PriorElem(Node *pcurrentNode, Node *pPreNode);
bool NextElem(Node *pcurrentNode, Node *pNextNode);
void ListTraverse();
bool ListInsert(int i, Node *pNode);
bool ListDelete(int i, Node *pNode);
bool ListInsertHead(Node *pNode);
bool ListInsertTail(Node *pNode);
private:
Node *m_pList;
int m_iLength;
};
#endif
#include "List.h"
#include "Node.h"
#include <iostream>
using namespace std;
List::List(){
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
bool List::ListEmpty(){
if(m_iLength == 0)
return true;
else
return false;
}
int List::ListLength(){
return m_iLength;
}
void List::ClearList(){
Node *currentNode = m_pList->next;
while(currentNode != NULL){
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList->next = NULL;
}
List::~List(){
ClearList();
delete m_pList;
m_pList = NULL;
}
bool List::ListInsertHead(Node *pNode){
Node *temp = m_pList->next;
Node *newNode = new Node;
if(newNode == NULL)
return false;
newNode->data = pNode->data;
m_pList->next = newNode;
newNode->next = temp;
m_iLength++;
return true;
}
bool List::ListInsertTail(Node *pNode){
Node *currentNode = m_pList;
while(currentNode->next!=NULL){
currentNode = currentNode->next;
}
Node *newNode = new Node;
if(newNode == NULL)
return false;
newNode->data = pNode->data;
newNode->next = NULL;
currentNode->next = newNode;
m_iLength++;
return true;
}
bool List::ListInsert(int i, Node *pNode){
if(i < 0 || i > m_iLength)
return false;
Node *currentNode = m_pList;
for(int k = 0; k < i; k++){
currentNode = currentNode->next;
}
Node *newNode = new Node;
if(newNode == NULL)
return false;
newNode->data = pNode->data;
newNode->next = currentNode->next;
currentNode->next = newNode;
m_iLength++;
return true;
}
bool List::ListDelete(int i, Node *pNode){
if(i < 0 || i >= m_iLength)
return false;
Node *currentNode = m_pList;
Node *currentNodeBefore = NULL;
for(int k = 0; k <= i; k++){
currentNodeBefore = currentNode;
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;
delete currentNode;
currentNode = NULL;
m_iLength--;
}
int List::locateElem(Node *pNode){
Node *currentNode = m_pList;
int count = 0;
while(currentNode->next != NULL){
currentNode = currentNode->next;
if(currentNode->data == pNode->data){
return count;
}
count++;
}
return -1;
}
bool List::PriorElem(Node *pcurrentNode, Node *pPreNode){
Node *currentNode = m_pList;
Node *tempNode = NULL;
while(currentNode->next != NULL){
tempNode = currentNode;
currentNode = currentNode->next;
if(currentNode->data == pcurrentNode->data){
if(tempNode == m_pList)
return false;
pPreNode->data = tempNode->data;
return true;
}
}
return false;
}
bool List::NextElem(Node *pcurrentNode, Node *pNextNode){
Node *currentNode = m_pList;
while(currentNode->next != NULL){
currentNode = currentNode->next;
if(currentNode->data == pcurrentNode->data){
if(currentNode->next == NULL)
return false;
pNextNode->data = currentNode->next->data;
return true;
}
}
return false;
}
void List::ListTraverse(){
Node *currentNode = m_pList;
while(currentNode->next != NULL){
currentNode = currentNode->next;
currentNode->printNode();
}
}
#include "List.h"
#include "Node.h"
#include <iostream>
using namespace std;
int main(){
List *pList = new List();
Node node1;
Node node2;
node1.data = 3;
pList->ListInsertHead(&node1);
pList->ListTraverse();
cout<<pList->ListEmpty()<<endl;
cout<<pList->ListLength()<<endl;
delete pList;
pList = NULL;
return 0;
}