C++實現單鏈表

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

#ifndef LINKLIST_H_
#define LINKLIST_H_

typedef int ElemType;

typedef struct SLNode {
	ElemType data;
	SLNode * next;
}*SLList;

bool SLListCreate(SLList& SLL);
void SLListDestroy(SLList& SLL);

void SLListClear(SLList SLL);
int  SLListLength(SLList SLL);
bool SLListEmpty(SLList SLL);

bool SLListInsert(SLList SLL, int pos, ElemType e);
bool SLListDelete(SLList SLL, int pos, ElemType& e);
bool SLListAppend(SLList SLL, ElemType e);

int  SLListFindElem(SLList SLL, ElemType e);
bool SLListElemAt(SLList SLL, int pos, ElemType& e);

void SLListSort(SLList SLL, bool desc = false);

void SLListDisplay(SLList SLL);

#endif /* LINKLIST_H_ */



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

#include "LinkList.h"
#include <iostream>

using namespace std;

bool SLListCreate(SLList& SLL)
{
	SLL = new SLNode;
	if(!SLL) return false;
	SLL->next = NULL;
	return true;
}

void SLListDestroy(SLList& SLL)
{
	SLNode* pcur;
	while(SLL) {
		pcur = SLL;
		SLL = SLL->next;
		delete pcur;
	}
}

void SLListClear(SLList SLL)
{
	SLList sub = SLL->next;
	SLL->next = NULL;
	SLNode* pcur;
	while(sub) {
		pcur = sub;
		sub = sub->next;
		delete pcur;
	}
}

int SLListLength(SLList SLL)
{
	SLNode *pcur = SLL->next;
	int cnt = 0;
	while(pcur) {
		++cnt;
		pcur = pcur->next;
	}
	return cnt;
}

bool SLListEmpty(SLList SLL)
{
	return SLL->next == NULL;
}

bool SLListInsert(SLList SLL, int pos, ElemType e)
{
	if(pos < 0)
		return false;
	SLNode* prior = SLL;
	int i = 0;
	while(prior && i != pos) {
		prior = prior->next;
		++i;
	}
	if(!prior) return false;
	SLNode* pcur = new SLNode;
	if(!pcur) return false;

	pcur->data = e;
	pcur->next = prior->next;
	prior->next = pcur;
	return true;
}

bool SLListDelete(SLList SLL, int pos, ElemType& e)
{
	if(pos < 0)
		return false;
	SLNode* prior = SLL;
	int i = 0;
	while(prior && i != pos) {
		prior = prior->next;
		++i;
	}
	if(!prior || !prior->next)
		return false;
	delete prior->next;
	prior->next = NULL;
	return true;
}

bool SLListAppend(SLList SLL, ElemType e)
{
	SLNode * pcur = new SLNode;
	if(!pcur) return false;
	pcur->data = e;
	pcur->next = NULL;

	SLNode * prior = SLL;
	while(prior->next)
		prior = prior->next;
	prior->next = pcur;
	return true;
}

int SLListFindElem(SLList SLL, ElemType e)
{
	SLNode* pcur = SLL->next;
	int pos = 0;
	while(pcur) {
		if(pcur->data == e)
			return pos;
		++pos;
		pcur = pcur->next;
	}
	return -1;
}

bool SLListElemAt(SLList SLL, int pos, ElemType& e)
{
	if(pos < 0)
		return false;
	SLNode *pcur = SLL->next;
	int i = 0;
	while(pcur && i != pos) {
		++i;
		pcur = pcur->next;
	}
	if(!pcur) return false;
	e = pcur->data;
	return true;
}

void SLListSort(SLList SLL, bool desc)
{
	SLNode *sub = SLL->next;
	SLL->next = NULL;
	while(sub) {
		SLNode *pcur = sub;
		sub = sub->next;

		SLNode *prior = SLL;
		while(prior->next &&
				(!desc && pcur->data > prior->next->data ||
				  desc && pcur->data < prior->next->data) )
			prior = prior->next;

		pcur->next = prior->next;
		prior->next = pcur;
	}
}

void SLListDisplay(SLList SLL)
{
	SLNode *pcur = SLL->next;
	while(pcur) {
		cout << pcur->data << " ";
		pcur = pcur->next;
	}
	cout << endl;
}





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

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

using namespace std;

int main(void)
{
	SLList sll;
	if(!SLListCreate(sll))
		exit(EXIT_FAILURE);

	SLListAppend(sll, 5);
	SLListAppend(sll, 2);
	SLListAppend(sll, 3);
	SLListAppend(sll, 1);
	SLListAppend(sll, 6);

	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':
			SLListDisplay(sll);
			break;
		case '2':
			{
				int pos;
				ElemType e;
				cout << "pos: "; cin >> pos;
				cout << "elem: "; cin >> e;
				if(SLListInsert(sll, pos, e))
					cout << "Insert success." << endl;
				else
					cout << "Insert failure." << endl;
			}
			break;
		case '3':
			{
				int pos;
				ElemType e;
				cout << "pos: "; cin >> pos;
				if(SLListDelete(sll, pos, e))
					cout << "elem " << e << " deleted." << endl;
				else
					cout << "delete failure." << endl;
			}
			break;
		case '4':
			{
				ElemType e;
				cout << "elem: "; cin >> e;
				if(SLListAppend(sll, e))
					cout << "Append success." << endl;
				else
					cout << "Append failure." << endl;
			}
			break;
		case '5':
			{
				ElemType e;
				cout << "elem: "; cin >> e;
				int pos = SLListFindElem(sll, 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(SLListElemAt(sll, 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;
				SLListSort(sll, desc);
				cout << "sorting done." << endl;
			}
			break;
		case '8':
			cout << "length: " << SLListLength(sll) << endl;
			break;
		case '9':
			SLListClear(sll);
			cout << "cleared." << endl;
			break;
		case '0':
			running = false;
			cout << "exit program." << endl;
			break;
		}//switch
	}//while

	SLListDestroy(sll);
	system("pause");
	return 0;
}













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