好困啊,终于写完了
1、Node.h
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data; //数据域
Node *next; //指针域
void printNode();
};
#endif
2、Node.cpp
#include "Node.h"
#include <iostream>
using namespace std;
void Node::printNode()
{
cout << data << endl;
}
3、 NodeList.h
#ifndef NODELIST_H
#define NODELIST_H
#include "Node.h"
class NodeList
{
public:
NodeList();
~NodeList();
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
4、NodeList.cpp
#include "NodeList.h"
#include <iostream>
using namespace std;
NodeList::NodeList()
{
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
m_iLength = 0;
}
void NodeList::ClearList()
{
Node *currentNode = m_pList->next;
while (currentNode != NULL)
{
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList->next = NULL;
}
NodeList::~NodeList()
{
ClearList();
delete m_pList;
m_pList = NULL;
}
bool NodeList::ListEmpty()
{
if (m_iLength == 0)
{
return true;
}
return false;
}
int NodeList::ListLength()
{
return m_iLength;
}
bool NodeList::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;
return true;
}
bool NodeList::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 NodeList::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;
return true;
}
bool NodeList::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--;
return true;
}
bool NodeList::GetElem(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;
}//找打i节点
pNode->data = currentNode->data;
return true;
}
int NodeList::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 NodeList::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 NodeList::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 NodeList::ListTraverse()
{
Node *currentNode = m_pList;
while(currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
}
}
5、NodeDemo.cpp
#include <iostream>
using namespace std;
#include "Node.h"
#include "NodeList.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv)
{
Node node1, node2, node3, node4, node5, temp;
node1.data = 1;
node2.data = 2;
node3.data = 3;
node4.data = 4;
node5.data = 5;
temp.data = 0;
NodeList *pList = new NodeList();
pList->ListInsertHead(&node1);
pList->ListInsertHead(&node2);
pList->ListInsertHead(&node3);
pList->ListInsertHead(&node4);
pList->ListTraverse();
cout << endl;
pList->ListInsertTail(&node1);
pList->ListInsertTail(&node2);
pList->ListInsertTail(&node3);
pList->ListInsertTail(&node4);
pList->ListTraverse();
cout << endl;
pList->ListInsert(0, &node5);
pList->ListTraverse();
cout << endl;
pList->ListInsert(3, &node5);
pList->ListTraverse();
cout << endl;
pList->ListDelete(3, &temp);//5
pList->ListTraverse();
cout << endl;
cout << "temp: " << temp.data << endl;
pList->GetElem(1, &temp);
cout << "temp: " << temp.data << endl;
pList->PriorElem(&node2, &temp);
cout << "temp: " << temp.data << endl;
pList->NextElem(&node1, &temp);
cout << "temp: " << temp.data << endl;
return 0;
}
运行成功!