努力打卡 每天学习 不浪费每一天 Day70

用c++重学数据结构


 

  

 

 

 

 太浪费空间 用链式存储

二叉树 前中后序+层序

#include<iostream>
using namespace std;

#define T int
struct BiNode {
	T data;
	BiNode* leftChild;
	BiNode* rightChild;
};

class BiTree {
private:
	BiNode* Create();
	void Release(BiNode* bt);
	void PreOrder(BiNode* bt);
	void InOrder(BiNode* bt);
	void PostOrder(BiNode* bt);
	BiNode* root;
public:
	BiTree() { root = Create(); };
	~BiTree() { Release(root); };
	void PreOrder() { PreOrder(root); };
	void InOrder() { InOrder(root); };
	void PostOrder() { PostOrder(root); };
	void LevelOrder();//层序
};

BiNode* BiTree::Create() {
	BiNode* bt;
	int x;
	cin >> x;
	if (x == 0) bt = NULL;
	else {
		bt = new BiNode;
		bt->data = x;
		bt->leftChild = Create();
		bt->rightChild = Create();
	}

	return bt;
}

void BiTree::Release(BiNode* bt) {
	if (bt == NULL) return;
	else {
		Release(bt->leftChild);
		Release(bt->rightChild);
		delete bt;
	}
}

void BiTree::PreOrder(BiNode* bt) {
	if (bt == NULL) return;
	else {
		cout << bt->data << '\t';
		PreOrder(bt->leftChild);
		PreOrder(bt->rightChild);
	}
}

void BiTree::InOrder(BiNode* bt) {
	if (bt == NULL) return;
	else {
		InOrder(bt->leftChild);
		cout << bt->data << '\t';
		InOrder(bt->rightChild);
	}
}

void BiTree::PostOrder(BiNode* bt) {
	if (bt == NULL) return;
	else {
		PostOrder(bt->leftChild);
		PostOrder(bt->rightChild);
		cout << bt->data << '\t';
	}
}

void BiTree::LevelOrder() {
	BiNode* Q[100], *q = NULL;
	int front = -1, rear = -1;
	if (root == NULL) return;
	else {
		Q[++rear] = root;
		while (rear!=front){
			q = Q[++front];
			cout << q->data << "\t";
			if (q->leftChild != NULL) {
				Q[++rear] = q->leftChild;
			}
			if (q->rightChild!=NULL){
				Q[++rear] = q->rightChild;
			}
		}
	}
}

int main() {
	BiTree Tree;
	cout << "前序遍历结果:" << endl;
	Tree.PreOrder();
	cout << endl << "中序遍历结果:" << endl;
	Tree.InOrder();
	cout << endl << "后序遍历结果:" << endl;
	Tree.PostOrder();
	cout << endl << "层序遍历结果:" << endl;
	Tree.LevelOrder();


}

线性表 数组实现

#include<iostream>
using namespace std;
#define MAXSIZE 100
class SeqList {
private:
	int array[MAXSIZE];
	int length;
public:
	SeqList();
	SeqList(int a[],int n);
	~SeqList();
	int getLength();//获取线性表长度
	int getElem(int index);//获取索引元素
	int getIndex(int value);//获取索引
	int deleteElem(int index);//删除索引的值,并且返回
	int deleteIndex(int value);//删除元素,返回索引
	void insertElem(int index, int value);
	bool isEmpty();//查询线性表是否为空
	void printAllElem();//遍历输出
	void initList(int length);//给线性表赋值
	void sort();//排序
	
};
SeqList::SeqList() {
	length == 0;
}
SeqList::SeqList(int a[], int n) {
	if (n > MAXSIZE) throw"上溢";
	else {
		for (int i = 0; i < n; i++) {
			array[i] = a[i];
		}
		length = n;
	}
}
SeqList::~SeqList() {

}

int SeqList::getLength() {
	return length;
}

int SeqList::getElem(int index) {
	if (index<1 || index>MAXSIZE) throw"溢出";
	else {
		return array[index - 1];
	}
}

int SeqList::getIndex(int value) {
	for (int i = 0; i < length; i++) {
		if (array[i] == value) {
			return (i + 1);
		}
	}
}

int  SeqList::deleteElem(int index) {
	if (index<1 || index>MAXSIZE) throw"位置不合理";
	else {
		int x = array[index - 1];
		for (int i = index - 1; i < length; i++) {
			array[i] = array[i + 1];
		}
		length--;
		return x;
	}
}

int SeqList::deleteIndex(int value) {
	for (int i = 0; i < length; i++) {
		if (array[i] == value) {
			for (int k = i; k < length; k++) {
				array[k] = array[k + 1];
			}
			length--;
			return (i + 1);
		}
	}
}

void SeqList::insertElem(int index, int value) {
	if(length==MAXSIZE)throw"上溢";
	if (index<1 || index>MAXSIZE) throw"位置不合理";
	else {
		for (int i = length; i >= index; i--) {
			array[i] = array[i - 1];
		}
		array[index - 1] = value;
		length++;
	}
}

bool SeqList::isEmpty() {
	return length == 0;
}

void SeqList::printAllElem() {
	for (int i = 0; i < length; i++) {
		cout << array[i] <<"\t";
	}
	cout << endl;
}

void SeqList::initList(int length) {
	for (int i = 0; i < length; i++) {
		cin >> array[i];
	}
	cout << "输入成功<" << endl;
	this->length = length;
}

void SeqList::sort() {
	for (int i = 0; i < length; i++) {
		for (int k = i + 1; k < length; k++) {
			if (array[i] > array[k]) {
				int temp = array[i];
				array[i] = array[k];
				array[k] = temp;
			}
		}
	}
}

int main() {
	SeqList list;
	char command;
	int i, x;
	try {
		cout << "温馨提示:想完整使用所有功能请先按 W 进行线性表的初始化。" << endl;
		while (cin >> command)
		{
			if (command == 'Q')
				return 0;
			switch (command)
			{
			case 'D':
				cout << "输入要删除的元素位置:";
				cin >> i;
				cout << list.deleteElem(i) << "该元素已删除" << endl;
				break;
			case 'C':
				cout << "输入要删除的元素:";
				cin >> x;
				cout << list.deleteIndex(x) << "该位置的元素已删除" << endl;
				break;
			case 'I':
				cout << "输入你要插入的位置与元素:";
				cin >> i >> x;
				list.insertElem(i, x);
				cout << "插入完成" << endl;
				break;
			case 'S':
				cout << "输入你要查找的元素位置:";
				cin >> i;
				cout << list.getElem(i) << "这就是你要查找的元素" << endl;
				break;
			case 'G':
				cout << "输入你要查找的元素:";
				cin >> x;
				cout << list.getIndex(x) << "这就是你要查找的元素的位置" << endl;
				break;
			case 'L':
				cout << "线性表的长度为:" << list.getLength() << endl;
				break;
			case 'P':
				list.printAllElem();
				break;
			case 'E':
				cout << (list.isEmpty() ? "yes" : "no") << endl;
				break;
			case 'W':
				int n;
				cout << "输入元素个数:";
				cin >> n;
				list.initList(n);
				break;
			case 'R':
				list.sort();
				cout << "排序完成" << endl;
				break;
			default:
				cout << "输入错误" << endl;
				break;
			}
		}
	}
	catch (const char* str)
	{
		cout << str << endl;
	}
	return 0;


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值