#ifndef LINKEDLIST_H #define LINKEDLIST_H #define MAXLEN 256 template <class T> struct LinkedList { struct Node { Node* next = 0; // necessary!!! T key; }; Node* head = 0; T* listSearch(const T& k); void listInsert(const T& k); void listDelete(const T& k); void reverse(); }; template <class T> T* LinkedList<T>::listSearch(const T& k) { Node* x = head; while (x != 0 && x->key != k) { x = x->next; } if (x == 0) { return 0; } T* temp = &(x->key); return temp; // Returns a pointer to the data } template <class T> void LinkedList<T>::listInsert(const T& k) { Node* x = new Node(); x->key = k; if (head != 0) { x->next = head; } head = x; } template <class T> void LinkedList<T>::listDelete(const T& k) { Node* deleteOne = head; Node* last = 0; while (deleteOne != 0 && deleteOne->key != k) { last = deleteOne; deleteOne = deleteOne->next; } if (deleteOne == 0) { ; } else if (deleteOne == head) { head = deleteOne->next; } else { last->next = deleteOne->next; } } template <class T> void LinkedList<T>::reverse() { Node* temp[MAXLEN]; for (int i = 0; i != MAXLEN; ++i) { temp[i] = 0; } int k = 0; Node* x = head; while (x != 0) { temp[k] = x; x = x->next; ++k; } temp[0]->next = 0; head = temp[k-1]; for (int i = 1; i != k; ++i) { temp[i]->next = temp[i-1]; } } #endif #ifndef USER_H #define USER_H #include <string> struct User { std::string username; std::string password; User(std::string x, std::string y): username(x), password(y) {} User() = default; bool operator==(const User &left) { if (this->username == left.username) return true; return false; } bool operator!=(const User &left) { if (this->username != left.username) return true; return false; } }; #endif #ifndef MYSYSTEM_H #define MYSYSTEM_H struct MySystem { LinkedList<User> onlineList; bool login(User user); bool logout(User user); }; bool MySystem::login(User user) { onlineList.listInsert(user); if (onlineList.listSearch(user) != 0) return true; return false; } bool MySystem::logout(User user) { onlineList.listDelete(user); if (onlineList.listSearch(user) == 0) return true; return false; } #endif #include <iostream> using namespace std; int main() { LinkedList<int> listInt; listInt.listInsert(3); listInt.listInsert(4); listInt.listInsert(5); listInt.listInsert(7); listInt.listInsert(2); listInt.listDelete(3); listInt.reverse(); cout << listInt.listSearch(3) << endl; cout << listInt.listSearch(4) << endl; cout << listInt.listSearch(2) << endl; return 0; }
二叉树相关,留着备用:
#include <stdio.h> #include <iostream> #include <queue> using namespace std; typedef struct bnode { int data; struct bnode *lc, *rc; } bnode_type; // 前序遍历 int prosearch(bnode_type *root) { if (root != NULL) { printf("%c ", root->data); prosearch(root->lc); prosearch(root->rc); } return 0; } // 后序遍历 int postsearch(bnode_type *root) { if (root != NULL) { prosearch(root->lc); prosearch(root->rc); printf("%c ", root->data); } return 0; } // 前序建立二叉树 bnode_type* creat(bnode_type* root) { char c; cout << "Enter:" << endl; cin >> c; if (c != '#') { root = new bnode_type(); root->data = c; root->lc = creat(root->lc); root->rc = creat(root->rc); } else { root = NULL; } return root; } // 计算树的高度 int deepth(bnode_type* root) { if (root != NULL) { int a = deepth(root->lc); int b = deepth(root->rc); return ((a > b) ? a+1 : b+1); } return 0; } // 计算节点数量 int nodes(bnode_type* root) { if (root != NULL) { return nodes(root->lc) + nodes(root->rc) + 1; } return 0; } // 层序遍历 queue<bnode_type*> mark; void levelOrderTraverse(bnode_type* root) { if (root == NULL) return; mark.push(root); while (mark.size() != 0) { bnode_type* temp = mark.front(); printf("%c ", temp->data); if (temp->lc != NULL) mark.push(temp->lc); if (temp->rc != NULL) mark.push(temp->rc); mark.pop(); } } int main() { /* 前序地建立二叉树,输入#表示节点为空 */ bnode_type* p = NULL; p = creat(p); cout << "deepth=" << deepth(p) << endl; // 5 cout << "nodes=" << nodes(p) << endl; // 6 levelOrderTraverse(p); return 0; }