單鏈表的實現
Node.h文件
#pragma once
#ifndef NODE_H
#define NODE_H
class Node
{
public:
int data;
Node *next;
void printNode();
};
#endif // !NODE_H
Node.cpp文件
#include "stdafx.h"
#include "Node.h"
#include <iostream>
using namespace std;
void Node::printNode()
{
cout << data << endl;
}
List.h文件
#pragma once
#include "stdafx.h"
#ifndef LIST_H
#define LIST_H
#include "Node.h"
//class Node;
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 *preNode);
bool NextElem(Node *pcurrentNode, Node *pnextNode);
void ListTraval();
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 // !LIST_H
List.cpp文件
#include "stdafx.h"
#include "list.h"
#include <iostream>
using namespace std;
List::List()
{
m_pList = new Node;
m_pList->data = 0;
m_pList->next = NULL;
}
List::~List()
{
ClearList();
delete m_pList;
m_pList = NULL;
}
void List::ClearList()
{
Node *currentNode = m_pList->next;
while (currentNode != NULL)
{
Node *temp = currentNode->next;
delete currentNode;
currentNode = temp;
}
m_pList = NULL;
}
bool List::ListEmpty()
{
if (m_iLength == 0)
return true;
else
return false;
}
int List::ListLength()
{
return m_iLength;
}
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;
int cnt;
Node* currentNode = m_pList;
for (cnt = 0; cnt < i; cnt++)
{
currentNode = currentNode->next;
}
Node* newNode = new Node;
if (newNode == NULL)
return false;
newNode->next = currentNode->next;
newNode->data = pNode->data;
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 cnt = 0; cnt <= i; cnt++)
{
currentNodeBefore = currentNode;
currentNode = currentNode->next;
}
currentNodeBefore->next = currentNode->next;
pNode->data = currentNode->data;
delete currentNode;
currentNode = NULL;
m_iLength--;
return true;
}
bool List::GetElem(int i, Node* pNode)
{
if (i < 0 || i >= m_iLength)
return false;
Node* currentNode = m_pList;
for (int cnt = 0; cnt <= i; cnt++)
{
currentNode = currentNode->next;
}
pNode->data = currentNode->data;
return true;
}
int List::LocateElem(Node* pNode)
{
Node* currentNode = m_pList;
int i=0;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (pNode->data == currentNode->data)
return i;
i++;
return -1;
}
}
bool List::PriorElem(Node*pcurrentNode, Node *preNode)//取前驅
{
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;
preNode->data = tempNode->data;
}
}
return false;
}
bool List::NextElem(Node *pcurrentNode, Node *pnextNode)
{
Node* currentNode = m_pList;
Node* tempNode = NULL;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
if (currentNode->next == NULL)
return false;
tempNode->next = currentNode->next;
if (currentNode->data == pcurrentNode->data)
{
pnextNode->data = tempNode->data;
return true;
}
}
return false;
}
void List::ListTraval()
{
Node* currentNode = m_pList;
while (currentNode->next != NULL)
{
currentNode = currentNode->next;
currentNode->printNode();
cout << " ";
}
cout << endl;
}
單鏈表的實現