C++實現雙向鏈表

/*
 * DLList.h
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#ifndef DLLIST_H_
#define DLLIST_H_

typedef int ElemType;

typedef struct DLNode{
	ElemType data;
	DLNode* prior;
	DLNode* next;
}*DLList;

bool DLListCreate(DLList& DLL);
void DLListDestroy(DLList& DLL);

void DLListClear(DLList DLL);
int  DLListLength(DLList DLL);
bool DLListEmpty(DLList DLL);

bool DLListInsert(DLList DLL, int pos, ElemType e);
bool DLListDelete(DLList DLL, int pos, ElemType& e);
bool DLListAppend(DLList DLL, ElemType e);

int  DLListFindElem(DLList DLL, ElemType e);
bool DLListGetElem(DLList DLL, int pos, ElemType& e);

void DLListDisplay(DLList DLL);

void DLListSort(DLList DLL, bool desc);

#endif /* DLLIST_H_ */



/*
 * DLList.cpp
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#include"DLList.h"
#include<iostream>
#include<cstdlib>

using namespace std;

bool DLListCreate(DLList& DLL)
{
	DLL = new DLNode;
	if(!DLL) return false;
	DLL->prior = DLL->next = NULL;
	return true;
}

void DLListDestroy(DLList& DLL)
{
	DLNode * pcur;
	while(DLL) {
		pcur = DLL;
		DLL = DLL->next;
		delete pcur;
	}
}

void DLListClear(DLList DLL)
{
	DLList sub = DLL->next;
	DLL->next = NULL;
	DLNode *pcur;
	while(sub) {
		pcur = sub;
		sub = sub->next;
		delete pcur;
	}
}

int DLListLength(DLList DLL)
{
	DLNode * pcur = DLL->next;
	int cnt = 0;
	while(pcur) {
		++cnt;
		pcur = pcur->next;
	}
	return cnt;
}

bool DLListEmpty(DLList DLL)
{
	return DLL->next == NULL;
}

bool DLListInsert(DLList DLL, int pos, ElemType e)
{
	if(pos < 0) return false;

	DLNode * pcur = new DLNode;
	if(!pcur) return false;
	pcur->prior = pcur->next = NULL;
	pcur->data = e;

	DLNode * prior = DLL;
	int i = 0;
	while(prior->next && i != pos) {
		prior = prior->next;
		++i;
	}

	pcur->next = prior->next;
	if(pcur->next)
		pcur->next->prior = pcur;
	prior->next = pcur;
	pcur->prior = prior;

	return true;
}

bool DLListDelete(DLList DLL, int pos, ElemType& e)
{
	if(pos < 0) return false;

	DLNode * pcur = DLL->next;
	int i = 0;
	while(pcur && i != pos) {
		pcur = pcur->next;
		++i;
	}
	if(!pcur) return false;

	pcur->prior->next = pcur->next;
	if(pcur->next)
		pcur->next->prior = pcur->prior;

	e = pcur->data;
	delete pcur;
	return true;
}

bool DLListAppend(DLList DLL, ElemType e)
{
	DLNode *pcur = new DLNode;
	if(!pcur) return false;
	pcur->prior = pcur->next = NULL;
	pcur->data = e;

	DLNode *prior = DLL;
	while(prior->next)
		prior = prior->next;
	prior->next = pcur;
	pcur->prior = prior;
	return true;
}

int DLListFindElem(DLList DLL, ElemType e)
{
	DLNode * pcur = DLL->next;
	int pos = 0;
	while(pcur) {
		if(pcur->data == e)
			return pos;
		++pos;
		pcur = pcur->next;
	}
	return -1;
}

bool DLListGetElem(DLList DLL, int pos, ElemType& e)
{
	if(pos < 0) return false;

	DLNode * pcur = DLL->next;
	int i = 0;
	while(pcur && i != pos) {
		pcur = pcur->next;
		++i;
	}
	if(!pcur) return false;
	e = pcur->data;
	return true;
}

void DLListDisplay(DLList DLL)
{
	DLNode * pcur = DLL->next;
	while(pcur) {
		cout << pcur->data << " ";
		pcur = pcur->next;
	}
	cout << endl;
}

void _MergeSort(DLNode *&head, bool desc);
void _Merge(DLNode*& head, DLNode* part1, DLNode* part2, bool desc);

void DLListSort(DLList DLL, bool desc)
{	//mergesort
	if(!DLL->next || !DLL->next->next)
		return;
	_MergeSort(DLL->next, desc);
	return;
}

void _MergeSort(DLNode *&head, bool desc)
{
	if(!head->next) return;

	DLNode *sub1fst, *sub1lst, *sub2fst, *sub2lst;
	sub2lst = sub1fst = sub1lst = head;
	head = NULL;

	//cut list
	int step = 0;
	while(sub2lst->next) {
		sub2lst = sub2lst->next;
		++step;
		if(step%2 == 0)
			sub1lst = sub1lst->next;
	}
	sub2fst = sub1lst->next;
	sub1fst->prior = sub1lst->next = NULL;
	sub2fst->prior = sub2lst->next = NULL;

	_MergeSort(sub1fst, desc);
	_MergeSort(sub2fst, desc);

	_Merge(head, sub1fst, sub2fst, desc);
	return;
}

void _Merge(DLNode*& head, DLNode* part1, DLNode* part2, bool desc)
{
	if(head != NULL)
		exit(EXIT_FAILURE);

	DLNode * prear = NULL, *pcur = NULL;
	while(part1 && part2) {
		if(	(!desc && part1->data <= part2->data) ||
			(desc && part1->data >= part2->data) ) {
			pcur = part1;
			part1 = part1->next;
		}else {
			pcur = part2;
			part2 = part2->next;
		}
		//append.
		if(head == NULL)
			prear = head = pcur;
		else {
			prear->next = pcur;
			pcur->prior = prear;
			prear = pcur;
		}
	}

	if(part1) {
		prear->next = part1;
		part1->prior = prear;
	}else if(part2) {
		prear->next = part2;
		part2->prior = prear;
	}
}




/*
 * Main.cpp
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#include<iostream>
#include<cstdlib>
#include"DLList.h"

using namespace std;

int main(void)
{
	DLList dll;
	if(!DLListCreate(dll))
		exit(EXIT_FAILURE);

	DLListAppend(dll, 5);
	DLListAppend(dll, 2);
	DLListAppend(dll, 3);
	DLListAppend(dll, 1);
	DLListAppend(dll, 6);
	DLListAppend(dll, 5);
	DLListAppend(dll, 2);
	DLListAppend(dll, 3);
	DLListAppend(dll, 1);


	bool running = true;
	while(running) {
		cout << "function: " << endl
				<< "1. display" << endl
				<< "2. insert" << endl
				<< "3. delete" << endl
				<< "4. append" << endl
				<< "5. find elem" << endl
				<< "6. get elem" << endl
				<< "7. sort" << endl
				<< "8. length" << endl
				<< "9. clear" << endl
				<< "0. exit" << endl;

		char ch;
		cin >> ch;
		while(cin.get() != '\n');
		switch(ch) {
		case '1':
			DLListDisplay(dll);
			break;
		case '2':
			{
				int pos;
				ElemType e;
				cout << "pos: "; cin >> pos;
				cout << "elem: "; cin >> e;
				if(DLListInsert(dll, pos, e))
					cout << "Insert success." << endl;
				else
					cout << "Insert failure." << endl;
			}
			break;
		case '3':
			{
				int pos;
				ElemType e;
				cout << "pos: "; cin >> pos;
				if(DLListDelete(dll, pos, e))
					cout << "elem " << e << " deleted." << endl;
				else
					cout << "delete failure." << endl;
			}
			break;
		case '4':
			{
				ElemType e;
				cout << "elem: "; cin >> e;
				if(DLListAppend(dll, e))
					cout << "Append success." << endl;
				else
					cout << "Append failure." << endl;
			}
			break;
		case '5':
			{
				ElemType e;
				cout << "elem: "; cin >> e;
				int pos = DLListFindElem(dll, e);
				if(pos != -1)
					cout << "elem found at: " << pos << endl;
				else
					cout << "elem not found." << endl;
			}
			break;
		case '6':
			{
				int pos;
				ElemType e;
				cout << "pos: "; cin >> pos;
				if(DLListGetElem(dll, pos, e))
					cout << "elem at " << pos << " is " << e << endl;
				else
					cout << "get elem failure" << endl;
			}
			break;
		case '7':
			{
				int desc;
				cout << "asc(0) or desc(1): "; cin >> desc;
				DLListSort(dll, desc);
				cout << "sorting done." << endl;
			}
			break;
		case '8':
			cout << "length: " << DLListLength(dll) << endl;
			break;
		case '9':
			DLListClear(dll);
			cout << "cleared." << endl;
			break;
		case '0':
			running = false;
			cout << "exit program." << endl;
			break;
		}//switch
	}//while

	DLListDestroy(dll);
	system("pause");
	return 0;
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章