#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#define MAXLEN 256
template <class T>
struct LinkedList {
struct Node {
Node* next = ; // necessary!!!
T key;
};
Node* head = ; 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 != && x->key != k) {
x = x->next;
} if (x == ) {
return ;
}
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 != ) {
x->next = head;
}
head = x;
} template <class T>
void LinkedList<T>::listDelete(const T& k) {
Node* deleteOne = head;
Node* last = ;
while (deleteOne != && deleteOne->key != k) {
last = deleteOne;
deleteOne = deleteOne->next;
}
if (deleteOne == ) {
;
} 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 = ; i != MAXLEN; ++i) {
temp[i] = ;
} int k = ;
Node* x = head;
while (x != ) {
temp[k] = x;
x = x->next;
++k;
} temp[]->next = ;
head = temp[k-];
for (int i = ; i != k; ++i) {
temp[i]->next = temp[i-];
}
}
#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) != ) return true;
return false;
} bool MySystem::logout(User user) {
onlineList.listDelete(user);
if (onlineList.listSearch(user) == ) return true;
return false;
}
#endif #include <iostream>
using namespace std; int main()
{
LinkedList<int> listInt; listInt.listInsert();
listInt.listInsert();
listInt.listInsert();
listInt.listInsert();
listInt.listInsert();
listInt.listDelete();
listInt.reverse(); cout << listInt.listSearch() << endl;
cout << listInt.listSearch() << endl;
cout << listInt.listSearch() << endl;
return ;
}

二叉树相关,留着备用:

#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\n", root->data);
prosearch(root->lc);
prosearch(root->rc);
}
return ;
} // 后序遍历
int postsearch(bnode_type *root) {
if (root != NULL) {
prosearch(root->lc);
prosearch(root->rc);
printf("%c\n", root->data);
}
return ;
} // 前序建立二叉树
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+ : b+);
}
return ;
} // 计算节点数量
int nodes(bnode_type* root) {
if (root != NULL) {
return nodes(root->lc) + nodes(root->rc) + ;
}
return ;
} // 层序遍历
queue<bnode_type*> mark;
void levelOrderTraverse(bnode_type* root) {
if (root == NULL) return;
mark.push(root);
while (mark.size() != ) {
bnode_type* temp = mark.front();
printf("%c\n", 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; //
cout << "nodes=" << nodes(p) << endl; //
levelOrderTraverse(p);
return ;
}

最新文章

  1. php php-5.6.4.tar.bz2 apache 兼容问题 child pid 27858 exit signal Segmentation fault
  2. STM32F105解密STM32F105VB芯片解密STM32F105R8单片机破解多少钱?
  3. jQuery MiniUI开发系列之:UI和数据分离
  4. JSP开发模式2(计算器)
  5. Node.js-提供了四种形式的定时器
  6. Slip.js – 在触摸屏上实现列表的滑动排序功能
  7. zxing实现二维码生成和解析
  8. 微信清理H5真的太早了?这会是应用号发布的前兆吗
  9. C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
  10. 采访ServiceStack的项目领导Demis Bellot——第2部分(转)
  11. CSS定位(CSS定位概述、相对定位、绝对定位、浮动)
  12. poj 3783 Balls 动态规划 100层楼投鸡蛋问题
  13. asp.net core 五 SignalR 负载均衡
  14. 【RL-TCPnet网络教程】第20章 RL-TCPnet之BSD Socket客户端
  15. Web安全之跨站脚本攻击(XSS)
  16. 合并Dev BPL教程
  17. PHP在win7安装Phalcon框架
  18. PyQt5教程——对话框(6)
  19. 十二:video 视频
  20. 第一个Windows窗口应用程序

热门文章

  1. Json数组基础知识
  2. 从远程(包括ftp,http等协议)地址获取文件流信息
  3. 【BZOJ1009】[HNOI2008]GT考试 next数组+矩阵乘法
  4. 160229-02、Sublime Text 3 快捷键总结
  5. 在Ubuntu上搭建hive环境
  6. PHP之冒号、endif、endwhile、endfor 是什么鬼?f
  7. Netty in action—Netty中的ByteBuf
  8. hook Extending the Framework Core
  9. ctf百度杯十二月场what_the_fuck(一口盐汽水提供的答案)
  10. 该死的Kafka,远程连接Kafka超时以及解决办法