#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. cobbler上部署centos系统修改网卡地址成eth0

    编辑cobbler的profile文件:   cobbler profile edit --name=CentOS-7.2-x86_64 --kopts='net.ifnames=0 biosdevn ...

  2. centos7上开启路由转发

    CentOS7 开启路由转发 2018-03-27   09:18:14 1.临时开启,(写入内存,在内存中开启) echo "1" > /proc/sys/net/ipv4 ...

  3. java模块开发关键步骤

    1. 创建数据表 a) 确定表名(如:role) b) 确定表中的业务列(如:role_name.role_desc) c) 添加其它基本列 i. 如:role_id(主键).status(数据状态, ...

  4. 设置PYTHONIOENCODING

    PYTHONIOENCODING=utf8

  5. 1055 - Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'information_schema.PROFILING.SEQ' which is not functionally dependent on columns in GROUP BY clause

    解决方法一: SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); 优点:不用重启mysql 缺点:重启mysql后还会 ...

  6. 简单JS旋转实现转盘抽奖效果

    闲来没事,做了一个模拟转盘抽奖的HTML&JS的效果: 可以在设置的时候,选择几个区域,并且可以填写指针将要停止的区域 比如,我选择了"区域2",结果就是这样 具体可以见下 ...

  7. 剑指Offer——二叉树的下一个结点

    题目描述: 给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回.注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针. 分析: 如果该结点存在右子树,那么返回右子树的最左结 ...

  8. Ansible安装过程中常遇到的错误(FAQ)

    1.安装完成后允许命令报错 Traceback (most recent call last): File , in <module> (runner, results) = cli.ru ...

  9. 原!linux脚本 expect命令 完成 输入密码交互 进行scp远程文件拷贝

    1.安装expect yum install  expect expect相关知识--- https://blog.csdn.net/lufeisan/article/details/53488395 ...

  10. Ubuntu 12.04安装Google Chrome(转)

    下载google chrome deb包,下载地址:https://www.google.com/chrome/browser/desktop/index.html,google的网站被墙了,如果你下 ...