#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 ;
}

C++ 单链表的实现的更多相关文章

  1. 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法

    有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...

  2. 单链表的C++实现(采用模板类)

    采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作.  链表结构定义 定义单链表 ...

  3. Java实现单链表的各种操作

    Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素   4.实现链表的反转   5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...

  4. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  5. c++单链表基本功能

    head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...

  6. 单链表、循环链表的JS实现

    数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑......   当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...

  7. C代码实现非循环单链表

    C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...

  8. 分离的思想结合单链表实现级联组件:CascadeView

    本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...

  9. 数据结构:单链表结构字符串(python版)添加了三个新功能

    #!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...

  10. 数据结构:单链表结构字符串(python版)改进

    此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...

随机推荐

  1. win7下maven的安装

    1.在安装maven之前,先确保已经安装JDK1.6及以上版本,并且配置好环境变量.2.上Maven官网(https://maven.apache.org/download.cgi)下载Maven的压 ...

  2. 如何访问局域网的Access数据库?

    1]用共享打印机来打开文件共享, 2]把Access文件.mdb所在的文件夹 共享,然后其他的就和本地一样了. 设置如下: Data Source="\\192.168.7.49\user\ ...

  3. js 正则 exec() 和 match() 数据抽取

    js 的正则表达式平常用的不多,但以前抽取数据的时候用到过,主要是有这样的需求: var text='<td class="data">2014-4-4</td& ...

  4. coderfun-boot接私活利器,文档详实,非一般的开发速度

    项目主页:https://gitee.com/klguang/coderfun-boot 演示地址:http://106.15.195.9:8080/admin/项目文档:https://www.ka ...

  5. excel 使用技巧

    计算两个日期的差值 1.计算当前日期与目标日期相差天数,下面例子中当前日期未2017/3/19 2.列增加数据条

  6. JSTL-标准标签库

    JSTL1.2中的标签库可以分成5类区域      核心:uri="http://java.sun.com/jsp/jstl/core"  prefix=“c”      XML: ...

  7. PAT 甲级 1025 PAT Ranking

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  8. Exception in thread "main" java.lang.UnsupportedClassVersionError: * : Unsupported major.minor version 52.0 解决办法

    Exception in thread "main" java.lang.UnsupportedClassVersionError: * : Unsupported major.m ...

  9. Spring Data @Query查询注解的使用(六)

    按照上一篇文章 我们知道  我们定义的方法  都要根据它的规范进行定义  不然就没法实用 这篇我们讲@Query 查询注解   我们就可以不需要遵循它的方法规则去编写  咱们讲@Query定义到方法上 ...

  10. 【keras框架】

    更高级别的封装.更简单的api,以tensorflow.theano为后端,支持更多的平台 读取网络模型后生成网络结构图 读取 from keras.models import load_model ...