#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. 使用javascript来访问本地文件夹

    常用的.net 的aspx文件由于运行在服务器端,并不具有访问用户本地文件夹的权限,导致类似于<a href="file:///d:/backup" > open fo ...

  2. jenkins配置邮件报警

    author:headsen chen date: 2018-05-15  13:49:21 在jerkins的主配置页面上: 注意:不用 安装什么报警邮件的插件.直接配置就可以了. 系统管理 --- ...

  3. ios iphone、ipad启动画面尺寸

    以下是iphone.ipad启动画面的尺寸 iphone4(纵):320 x 480 iphone4 Retina(纵):640 x 960   iphone5(纵):320 x 568 iphone ...

  4. js 闭包与垃圾回收-待删

    关于闭包请看戳 串讲-解释篇:作用域,作用域链,执行环境,变量对象,活动对象,闭包,本篇写的不太好: 先摆定义: 函数对象,可以通过作用域链相互关联起来,函数体内部的变量都可以保存在函数作用域内,这种 ...

  5. js 识别汉字和全角字符

    遇到个问题,需要检测输入框中的字符长度,如果是汉字,算作2个字节。。。 然后我就在想,我怎么去识别他是不是汉字呢???首先想到的可能就是 js 的 charCodeAt 方法, ASCII 码,然后就 ...

  6. R语言中的聚类的使用

    这里的聚类主要用到K-Means和K-Medoide聚类.在进行聚类之前,为了避免不同参数之间量纲不同所造成的影响,先将数据进行(0-1)标准化 # 如参数weight data$weight < ...

  7. libxml_disable_entity_loader

    w避免加载外部实体字符. http://php.net/manual/en/function.libxml-disable-entity-loader.php libxml_disable_entit ...

  8. Django源码安装方法及创建启动项目

    一.源码安装方法 下载源码包:https://www.djangoproject.com/download/ 输入以下命令并安装: tar xzvf Django-X.Y.tar.gz # 解压下载包 ...

  9. PCI 设备详解三

    上篇文章已经分析了探测PCI总线的部分代码,碍于篇幅,这里另启一篇.重点分析下pci_scan_root_bus函数 2016-10-24 pci_scan_root_bus函数 struct pci ...

  10. git 的 pull、fetch、merge

    1.pull = fetch + merge In the simplest terms, git pull does a git fetch followed by a git merge. You ...