双向循环链表:
数据结构示意图1.1.1如下:
图1.1.1 链表节点结构示意图
让代码来讲话吧:
Structure.h
#ifndef DATA_STRUCTURE_DCNLIST_STRUCTURE_H
#define DATA_STRUCTURE_DCNLIST_STRUCTURE_H
#include <iostream>
using namespace std;
#define Mark 0
#define HeadM pHead->mark = Mark
typedef struct SCNode {
int date;
unsigned int mark;
SCNode *pNext;
} SN,
SCN;
typedef struct DCNode {
int date;
unsigned int mark;
DCNode *pPrev;
DCNode *pNext;
} DN,
DCN;
#endif
Structure.cpp
#include "Structure.h"
#include "Operation.h"
DCN *FindTail(DCN *pHead) {
DCN *pNTemp = pHead;
while (pHead->pNext != pNTemp)pHead = pHead->pNext;
return pHead;
}
DCN *FindNode(DCN *pHead, int pos) {
while (pos-Mark != pHead->mark) {
pHead = pHead->pNext;
}
return pHead;
}
DCN *RegularMark(DCN *pHead) {
DCN *pNTemp = pHead;
int count = Mark;
do {
pHead->mark = count++;
}while (pNTemp!= (pHead = pHead->pNext));
return pNTemp;
}
Operation.h
#ifndef DATA_STRUCTURE_DCNLIST_OPERATION_H
#define DATA_STRUCTURE_DCNLIST_OPERATION_H
#include "Structure.h"
DCN *FindTail(DCN *pHead);
DCN *FindNode(DCN *pHead, int pos);
DCN *RegularMark(DCN *pHead);
DCN *CreateHead();
DCN *InsertHead(DCN *pHead);
DCN *InsertTail(DCN *pHead);
DCN *InsertMiddle(DCN *pHead, int pos);
DCN *DeleteHead(DCN *pHead);
DCN *DeleteTail(DCN *pHead);
DCN *DeleteMiddle(DCN *pHead, int pos);
unsigned int DisplayList(DCN *pHead);
#endif
Operation.cpp
#include "Operation.h"
DCN *CreateHead() {
DCN *pHead = new DCNode();
pHead->pNext = pHead;
pHead->pPrev = pHead;
HeadM;
cout << "Enter a value of the ListHead[" << pHead->mark << "]:" << endl;
cin >> pHead->date;
return pHead;
}
DCN *InsertHead(DCN *pHead) {
DCN *LTail = FindTail(pHead);
int TpMark = pHead->mark;
char ch = 'y';
while ('y' == ch || 'Y' == ch) {
DCN *newNode = new DCNode();
cout << "Enter a value of the node[" << ++TpMark << "]:" << endl;
newNode->mark = TpMark;
cin >> newNode->date;
pHead->pPrev = newNode;
newNode->pNext = pHead;
newNode->pPrev = LTail;
LTail->pNext = newNode;
pHead = newNode;
cout << "Enter choice inserter(y/n):" << endl;
cin >> ch;
}
return pHead;
}
DCN *InsertTail(DCN *pHead) {
DCN *LTail = pHead;
char ch = 'Y';
LTail = FindTail(LTail);
int TpMark = LTail->mark;
while (ch == 'y' || ch == 'Y') {
DCN *newNode = new DCNode();
cout << "Enter a value of the node[" << ++TpMark << "]:" << endl;
newNode->mark = TpMark;
cin >> newNode->date;
LTail->pNext = newNode;
newNode->pPrev = LTail;
newNode->pNext = pHead;
pHead->pPrev = newNode;
LTail = newNode;
cout << "Enter choice inserter(y/n):" << endl;
cin >> ch;
}
return pHead;
}
DCN *InsertMiddle(DCN *pHead, int pos) {
DCN *pNTemp01 = pHead, *pNTemp02 = nullptr;
pos--;
char ch = 'y';
RegularMark(pHead);
pNTemp01 = FindNode(pNTemp01, pos);
pNTemp02 = pNTemp01->pNext;
while (ch == 'y' || ch == 'Y') {
DCN *newNode = new DCNode();
newNode->mark = pNTemp01->mark;
newNode->mark++;
cout << "Enter a value of the inserted node:" << endl;
cin >> newNode->date;
pNTemp01->pNext = newNode;
newNode->pPrev = pNTemp01;
newNode->pNext = pNTemp02;
pNTemp02->pPrev = newNode;
pNTemp01 = pNTemp01->pNext;
cout << "Enter choice of inserter(y/n):" << endl;
cin >> ch;
}
RegularMark(pHead);
return pHead;
}
DCN *DeleteHead(DCN *pHead) {
char ch = 'y';
DCN *pNTemp = pHead, *LTail = pHead->pPrev;
while ('y' == ch || 'Y' == ch) {
pNTemp = pHead;
pHead = pHead->pNext;
pHead->pPrev = LTail;
LTail->pNext = pHead;
cout << "Enter choice of deleting head node (y/n):" << endl;
cin >> ch;
if (pHead != LTail) {
DisplayList(pHead);
delete pNTemp;
} else {
cout << "Can't remove the last node!" << endl;
return pHead;
}
}
return pHead;
}
DCN *DeleteTail(DCN *pHead) {
char ch = 'y';
DCN *pNTemp = nullptr, *LTail = pHead->pPrev, *pNTemp01 = nullptr;
RegularMark(pHead);
while ('y' == ch || 'Y' == ch) {
pNTemp01 = LTail->pPrev;
pNTemp = LTail;
pNTemp01->pNext = pHead;
pHead->pPrev = pNTemp01;
cout << "Enter choice of deleting tail node (y/n):" << endl;
cin >> ch;
if (pHead != LTail) {
DisplayList(pHead);
LTail = LTail->pPrev;
delete pNTemp;
} else {
cout << "Can't remove the last node!" << endl;
return pHead;
}
}
return pHead;
}
DCN *DeleteMiddle(DCN *pHead, int pos) {
DCN *pNTemp01 = pHead, *pNTemp02 = nullptr, *pNTemp03 = nullptr;
pNTemp01 = FindNode(pNTemp01, pos - 2);
pNTemp02 = pNTemp01->pNext;
pNTemp03 = pNTemp02->pNext;
pNTemp01->pNext = pNTemp03;
pNTemp03->pPrev = pNTemp01;
delete pNTemp02;
pNTemp02 = nullptr;
return pHead;
}
unsigned int DisplayList(DCN *pHead) {
DCN *LTail = FindTail(pHead), *pNTemp = pHead;
unsigned int count = 0;
cout << "Forward display list:" << endl;
do {
cout << "NODE[" << pHead->mark << "] = " << pHead->date << endl;\
pHead = pHead->pNext;
count++;
} while (pHead != LTail->pNext);
cout << "\nInverted display list:" << endl;
do {
cout << "NODE[" << LTail->mark << "] = " << LTail->date << endl;
LTail = LTail->pPrev;
} while (LTail != pNTemp->pPrev);
return count;
}
main.cpp
#include "Structure.h"
#include "Operation.h"
int main() {
DCN *pHead = nullptr;
pHead = CreateHead();
pHead = InsertTail(pHead);
pHead = DeleteMiddle(pHead,2);
unsigned int count = DisplayList(pHead);
cout << "\nCount of list = " << count << "\nThe tail date of list = " << FindTail(pHead)->date << endl;
if (pHead!= nullptr)delete pHead;
return 0;
}
测试结果如下图1.1.2: 图1.1.2 测试结果
|