1.顺序表
#include <iostream>
#include <cstring>
using std::cout;
using std::endl;
template <typename Type> class Vector {
private:
int size, length;
Type *data;
public:
Vector(int input_size) {
size = input_size;
length = 0;
data = new Type[size];
}
~Vector() {
delete[] data;
}
bool insert(int loc, Type value) {
if (loc < 0 || loc > length) {
return false;
}
if (length >= size) {
expand();
}
for (int i = length; i > loc; --i) {
data[i] = data[i - 1];
}
data[loc] = value;
length++;
return true;
}
void expand() {
Type *old_data = data;
size = size * 2;
data = new Type[size];
for (int i = 0; i < length; ++i) {
data[i] = old_data[i];
}
delete[] old_data;
}
int search(const Type &value) {
for (int i = 0; i < length; ++i) {
if (data[i] == value) {
return i;
}
}
return -1;
}
bool remove(int index) {
if (index < 0 || index >= length) {
return false;
}
for (int i = index + 1; i < length; ++i) {
data[i - 1] = data[i];
}
length = length - 1;
return true;
}
void print() {
for(int i = 0; i < length; i++){
if(i > 0){
cout << " ";
}
cout << data[i];
}
cout << endl;
}
};
int main() {
Vector<int> a(100);
cout << a.insert(0, 1) << endl;
cout << a.insert(0, 2) << endl;
a.print();
cout << a.remove(1) << endl;
a.print();
cout << a.search(0) << endl;
cout << a.search(1) << endl;
return 0;
}
2.链表
#include <iostream>
#include<iostream>
using namespace std;
template <typename Type> class Node {
public:
Type data;
Node<Type> *next;
Node(const Type &_data){
data = _data;
next = NULL;
}
};
template <typename Type> class LinkedList {
private:
Node<Type> *head;
public:
LinkedList() {
head = NULL;
}
~LinkedList() {
Node<Type> *current_node = head;
while(current_node != NULL){
Node<Type> *delete_node = current_node;
current_node = current_node->next;
delete delete_node;
}
}
bool insert(Node<Type> *node, int index) {
if(head == NULL){
if(index != 0){
cout << "failed" << endl;
return false;
}
head = node;
cout << "success" << endl;
return true;
}
if(index == 0){
node->next = head;
head = node;
cout << "success" << endl;
return true;
}
Node<Type> *current_node = head;
int count = 0;
while (current_node->next != NULL && count < index - 1){
current_node = current_node->next;
count++;
}
if(count == index - 1){
node->next = current_node->next;
current_node->next = node;
cout << "success" << endl;
return true;
}else{
cout << "failed" << endl;
return false;
}
}
void output() {
Node<Type> *current_node = head;
while(current_node != NULL){
if(current_node != head)cout << " ";
cout << current_node->data;
current_node = current_node->next;
}
cout << endl;
}
};
int main() {
LinkedList<int> linkedlist;
int a, b, c;
cin >> a;
for(int i = 0; i < a; i++){
cin >> b >> c;
Node<int> *node = new Node<int>(c);
linkedlist.insert(node, b);
}
linkedlist.output();
return 0;
}
链表删除
void delete_node(int index){
if(head == NULL){
return ;
}
Node<Type> *current_node = head;
int count = 0;
if(index == 0){
head = head->next;
delete current_node;
return;
}
while(current_node->next != NULL && count < index - 1){
current_node = current_node->next;
count++;
}
if(count == index - 1 && current_node->next != NULL){
Node<Type> *delete_node = current_node->next;
current_node->next = delete_node->next;
delete delete_node;
}
}
链表反转
void reverse(){
if(head == NULL){
return ;
}
Node<Type> *next_node, *current_node;
current_node = head->next;
head->next = NULL;
while(current_node != NULL){
next_node = current_node->next;
current_node->next = head;
head = current_node;
current_node = next_node;
}
}
3.队列
#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
Type *data;
int head, tail, length;
public:
Queue(int length_input) {
data = new Type[length_input];
length = length_input;
head = 0;
tail = -1;
}
~Queue() {
delete[] data;
}
bool push(Type element) {
if (tail + 1 >= length) {
return false;
}
tail++;
data[tail] = element;
return true;
}
void output() {
for (int i = head; i <= tail; i++) {
cout << data[i] << " ";
}
cout << endl;
}
Type front(){
return data[head];
}
void pop(){
head++;
}
bool empty(){
return head > tail;
}
};
int main() {
Queue<int> queue(100);
for (int i = 1; i <= 10; i++) {
queue.push(i);
}
queue.output();
if(!queue.empty()){
cout << queue.front() << endl;
queue.pop();
}
queue.output();
return 0;
}
循环队列
#include <iostream>
using std::cout;
using std::endl;
template <typename Type> class Queue {
private:
Type *data;
int head, tail, length, count;
public:
Queue(int length_input) {
data = new Type[length_input];
length = length_input;
head = 0;
tail = -1;
count = 0;
}
~Queue() {
delete[] data;
}
bool push(Type element) {
if (count >= length) {
return false;
}
tail = (tail + 1) % length;
data[tail] = element;
count++;
return true;
}
void output() {
int i = head;
do {
cout << data[i] << " ";
i = (i + 1) % length;
} while(i != (tail + 1) % length);
cout << endl;
}
Type front() {
return data[head];
}
void pop() {
head = (head + 1) % length;
count--;
}
bool empty() {
return count == 0;
}
};
int main() {
Queue<int> queue(100);
for (int i = 1; i <= 10; i++) {
queue.push(i);
}
queue.output();
if (!queue.empty()) {
cout << queue.front() << endl;
queue.pop();
}
queue.output();
return 0;
}
表达式求值
#include<iostream>
#include<string>
#include<cassert>
using std::cin;
using std::cout;
using std::endl;
using std::string;
template<typename Type> class Stack {
private:
Type *elements;
int top_index, size;
public:
Stack(int input_length) {
elements = new Type[input_length];
top_index = -1;
size = input_length;
}
~Stack() {
delete [] elements;
}
bool push(const Type &value) {
if(top_index >= size - 1){
return false;
}
top_index++;
elements[top_index] = value;
return true;
}
bool pop() {
if(top_index < 0){
return false;
}
top_index--;
return true;
}
Type top() {
assert(top_index >= 0);
return elements[top_index];
}
bool empty() {
return top_index < 0;
}
};
bool precede(char a, char b) {
if((a == '*' || a == '/') && (b == '+' || b == '-')){
return true;
}else{
return false;
}
}
float operate(char a, double b, double c) {
if(a == '+'){
return b + c;
}else if(a == '-'){
return b - c;
}else if(a == '*'){
return b * c;
}else{
return b / c;
}
}
void calc(Stack<double> &numbers, Stack<char> &oper) {
double a = numbers.top();
numbers.pop();
double b = numbers.top();
numbers.pop();
numbers.push(operate(oper.top(), b, a));
oper.pop();
}
int main() {
string s;
int m;
cin >> m;
Stack<double> numbers(m);
Stack<char> oper(m);
cin >> s;
int i = 0;
while(i < m){
if(isdigit(s[i])){
numbers.push(s[i] - '0');
i++;
}else{
if(oper.empty() || precede(s[i], oper.top())){
oper.push(s[i]);
i++;
}else{
calc(numbers, oper);
}
}
}
while(!oper.empty()){
calc(numbers, oper);
}
cout << numbers.top() << endl;
return 0;
}
3.二叉树
三种遍历
#include<iostream>
using std::cout;
using std::endl;
class Node {
public:
int data;
Node *lchild, *rchild;
Node(int _data) {
data = _data;
lchild = NULL;
rchild = NULL;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void preorder() {
cout << data << " ";
if (lchild != NULL) {
lchild->preorder();
}
if (rchild != NULL) {
rchild->preorder();
}
}
void inorder() {
if (lchild != NULL) {
lchild->inorder();
}
cout << data << " ";
if (rchild != NULL) {
rchild->inorder();
}
}
void postorder(){
if(lchild != NULL){
lchild->postorder();
}
if(rchild != NULL){
rchild->postorder();
}
cout << data << " ";
}
};
class BinaryTree {
private:
Node *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
delete root;
}
void build_demo() {
root = new Node(1);
root->lchild = new Node(2);
root->rchild = new Node(3);
root->lchild->lchild = new Node(4);
root->lchild->rchild = new Node(5);
root->rchild->rchild = new Node(6);
}
void preorder() {
root->preorder();
}
void inorder() {
root->inorder();
}
void postorder(){
root->postorder();
}
};
int main() {
BinaryTree binarytree;
binarytree.build_demo();
binarytree.preorder();
cout << endl;
binarytree.inorder();
cout << endl;
binarytree.postorder();
cout << endl;
return 0;
}
二叉排序树
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
template <typename Type> class Node {
public:
Type data;
Node<Type> *lchild, *rchild, *father;
Node(Type _data, Node<Type> *_father = NULL) {
data = _data;
lchild = NULL;
rchild = NULL;
father = _father;
}
~Node() {
if (lchild != NULL) {
delete lchild;
}
if (rchild != NULL) {
delete rchild;
}
}
void insert(Type value) {
if (value == data) {
return;
} else if (value > data) {
if (rchild == NULL) {
rchild = new Node<Type>(value, this);
} else {
rchild->insert(value);
}
} else {
if (lchild == NULL) {
lchild = new Node<Type>(value, this);
} else {
lchild->insert(value);
}
}
}
Node<Type>* search(Type value){
if(data == value){
return this;
}else if(value > data){
if(rchild == NULL){
return NULL;
}else{
return rchild->search(value);
}
}else{
if(lchild == NULL){
return NULL;
}else{
return lchild->search(value);
}
}
}
};
template <typename Type> class BinaryTree {
private:
Node<Type> *root;
public:
BinaryTree() {
root = NULL;
}
~BinaryTree() {
delete root;
}
void insert(Type value) {
if (root == NULL) {
root = new Node<Type>(value);
} else {
root->insert(value);
}
}
bool find(Type value){
if(root == NULL){
return false;
}
if(root->search(value) == NULL){
return false;
}else{
return true;
}
}
};
int main() {
BinaryTree<int> binarytree;
int arr[10] = { 8, 9, 10, 3, 2, 1, 6, 4, 7, 5 };
for (int i = 0; i < 10; i++) {
binarytree.insert(arr[i]);
}
int value;
cin >> value;
if(binarytree.find(value)){
cout << "search success!" << endl;
}else{
cout << "search failed!" << endl;
}
return 0;
}