第一、单链表的定义和操作

#include <iostream>
using namespace std; template <typename T>
struct Node {
T data;
Node* next;
}; template <typename T> class SingleLinkList {
public:
SingleLinkList() {
head = new Node<T>();
head->next = NULL;
} ~SingleLinkList() {
Node<T> *p;
while (head) {
p = head;
head = head->next;
delete p;
}
head = NULL;
} void createLinkList(int n) { std::cout << "you create single linklist with "<<n<<" node" << std::endl;
Node<T>* s, *p;
p = head; for (int i = ; i < n; i++)
{
s = new Node<T>();
cout << "please enter the " << i << " number" << endl;
std::cin >> s->data;
s->next = NULL; p->next = s;
p = s;
}
} bool insertNode(T data) {
Node<T>* p = head;
while (p->next!=NULL)
{
p = p->next;
} Node<T> *n = new Node<T>();
n->data = data;
n->next = NULL; p->next = n;
return true;
} bool insertNode(int i, T data) {
Node<T>* p = head;
int j;
for (j = ; j <= i-; j++)
{
p = p->next;
if (p==NULL)
{
break;
}
}
if (p==NULL&&j<(i-))
{
std::cout << "The index:"<<i<<" is not found" << std::endl;
return false;
} Node<T>* node = new Node<T>();
node->data = data; node->next = p->next;
p->next = node;
return true;
} bool deleteNode(int i) {
Node<T>*f = head;
Node<T>*s = head;
int j;
for (j=; j <= i; j++)
{
s = f;
f = f->next;
if (f==NULL)
{
break;
}
}
if (j<i+)
{
std::cout << "The index:" << i << " is not found" << std::endl;
return false;
} s->next = f->next;
delete f;
return true;
} T getElement(int i) {
Node<T> *p = head;
int j;
for (j = ; j <= i; j++)
{
p = p->next;
if (p==NULL)
{
break;
}
}
if (j<i && p==NULL)
{
std::cout << "The index:" << i << " is not found" << std::endl;
} return p->data;
} int findNode(T value) {
Node<T> *p = head;
int i = ;
while (p!=NULL)
{
if (p->data==value)
{
break;
}
p = p->next;
i++;
}
if (p==NULL)
{
return -;
}
return i;
} void printAll() {
Node<T>*p = head->next;
while (p!=NULL)
{
std::cout << p->data << " ";
p = p->next;
}
cout << endl;
} int lenght() {
Node<T>*p = head->next;
int j = ;
while (p!=NULL)
{
p = p->next;
j++;
}
return j;
} bool clearLink() {
Node<T>* current = head->next;
while (head->next!=NULL)
{
current = head->next;
head->next = current->next;
delete current;
}
return true;
} bool isEmpty() {
return head->next == NULL;
}
private:
Node<T>* head;
};

第二、控制台演示

#include "pch.h"
#include <iostream>
#include "SingleLinkList.h"
using namespace std; int main()
{
SingleLinkList<int> sLinkList;
cout << "1:createLinkList(int n);" << endl;
cout << "2:insertNode(T data);" << endl;
cout << "3:insertNode(int i, T data);" << endl;
cout << "4:int findNode(T value)" << endl;
cout << "5:int printAll()" << endl;
cout << "6:deleteNode(int i)" << endl;
cout << "7: T getElement(int i)" << endl;
cout << "8: clearLink()" << endl; cout << "10:exit;" << endl;
int cmd=;
do {
cout << "please tap cmd" << endl;
cin >> cmd;
switch (cmd)
{
case :
{
cout << "please enter the number of nodes you want to create" << endl;
int nNum = ;
cin >> nNum;
sLinkList.createLinkList(nNum);
}
break;
case :
cout << "please enter data " << endl;
int data;
cin >> data;
sLinkList.insertNode(data);
break;
case :
cout << "please enter index and data,example 4 10" << endl;
int d, i;
cin >> i >> d;
sLinkList.insertNode(i, d);
break;
case :
{ cout << "please enter data you want to search" << endl;
int searchData;
cin >> searchData;
int index = sLinkList.findNode(searchData);
cout << "search index:" << index << endl; }
break;
case :
sLinkList.printAll();
break;
case :
cout << "please enter index you want to delete" << endl;
int delIndex;
cin >> delIndex;
sLinkList.deleteNode(delIndex);
break;
case :
cout << "please enter index" << endl;
int getIndex;
cin >> getIndex;
{ int dataElement = sLinkList.getElement(getIndex);
cout << "value:" << dataElement << endl; }
break;
case :
sLinkList.clearLink();
break;
default:
break;
}
} while (cmd != ); return ;
}

C++ 单链表操作总结的更多相关文章

  1. 单链表操作B 分类: 链表 2015-06-07 12:42 15人阅读 评论(0) 收藏

    数据结构上机测试2-2:单链表操作B TimeLimit: 1000ms Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删除 ...

  2. 数据结构之 线性表---单链表操作A (删除链表中的指定元素)

    数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据 ...

  3. c语言实现--带头结点单链表操作

    可能是顺序表研究的细致了一点,单链表操作一下子就实现了.这里先实现带头结点的单链表操作. 大概有以下知识点. 1;结点:结点就是单链表中研究的数据元素,结点中存储数据的部分称为数据域,存储直接后继地址 ...

  4. C单链表操作

    #include <stdio.h> #include <stdlib.h> #define ElemType int #define Status int #define O ...

  5. c语言实现--不带头结点的单链表操作

    1,不带头结点的单链表操作中,除了InitList(),GetElem(),ListInsert(),ListDelete()操作与带头结点的单链表有差别外,其它的操作基本上一样. 2,不带头结点单链 ...

  6. C语言,单链表操作(增删改查)(version 0.1)

    这天要面试,提前把链表操作重新写了一遍.备份一下,以备不时之需. 希望有人能看到这篇代码,并指正. // File Name : list.h #include "stdafx.h" ...

  7. 【数据结构与算法】单链表操作(C++)

    #include <stdio.h> #include <malloc.h> /*单链表节点定义*/ typedef struct LNode { int data; //da ...

  8. C++单链表操作

    #include <stdio.h> typedef struct _Node{   int value;   _Node *next;}Node; void AddNodeTail(No ...

  9. 数据结构之 线性表---单链表的操作B(先逆序+再删除重复元素)

    数据结构上机测试2-2:单链表操作B Time Limit: 1000MS Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删 ...

随机推荐

  1. CSS 清除浮动 clear 属性

    CSS 清除浮动 clear 属性用于设定元素哪一侧不允许有其他浮动元素(而并非取消元素的浮动). 可能的取值如下: 取值 说明 none 默认值,允许两侧都有浮动元素 left 左侧不允许有其他浮动 ...

  2. 【Spring实战】Spring容器初始化完成后执行初始化数据方法

    一.背景知识及需求 在做WEB项目时,经常在项目第一次启动时利用WEB容器的监听.Servlet加载初始化等切入点为数据库准备数据,这些初始化数据是系统开始运行前必须的数据,例如权限组.系统选项.默认 ...

  3. JMter参数化

    参数化是干嘛的呢,咱们在调用接口的时候,有入参,那参数里面的值如果经常变化的话,就得每次去改了,很麻烦,这时候咱们就把需要经常变的值,改成可以变化的或者是咱们提前设置好的一些值,这样的话,调用的时候就 ...

  4. python 多维list声明时的小问题

    a=[[]]*3 a Out[18]: [[], [], []] a[0].append(1) a Out[20]: [[1], [1], [1]] b=[[] for _ in range(3)] ...

  5. pdf 转图片,提取图片研究心得

    1.pdf 中的数据是有多种编码的,详情请看:http://www.cnblogs.com/zendu/p/7644465.html 2.我的工作场景比较特殊,pdf中全部是图片,所以pdf转图片就有 ...

  6. IOS开发 ARC和非ARC下使用Block属性的问题

    1. Block的声明和线程安全 Block属性的声明,首先需要用copy修饰符,因为只有copy后的Block才会在堆中,栈中的Block的生命周期是和栈绑定的,可以参考之前的文章(iOS: 非AR ...

  7. postfix邮件服务器搭建01-准备篇

    本系列文章主要介绍linux下主流的开源邮件系统postfix的搭建过程,构建一个通过postfix虚拟用户管理的完整的邮件系统, 该系统包括以下组件: 邮件收发端postfix,dovecot, 邮 ...

  8. python数据取整

    第三方包:numpy 安装 $ sudo dnf install numpy 4舍6入5取偶 def getInteger(): a = np.float(5.5) # 4舍6入 5取偶 int_a ...

  9. matlab下kmeans及pam算法对球型数据分类练习

    clear all; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %数据初始化 D ...

  10. C# 速编神器LinqPad(新版5.25)

    点此下载5.25 (支持.net4.6,有调试器)(页面有广告,一直点免费下载即可)(可用)密码  lp123456  批处理如下. @echo off start /b LINQPad.exe -n ...