C++实现单链表
之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错。没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不吝赐教,旨在共同进步。
总结:
1、链表List的基本单元是节点Node,因此想要操作方便,就必须为每一步打好基础,Node的基本结构如下:
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
我们可以看出,Node的成员变量一共有两个,都是public,因为我们要对这两个变量进行操作,所以不能是private类型的。然后是一个构造函数,第二个参数默认值为NULL,也就是说如果我们创建新节点时只指定第一个参数,而不写第二个参数,那么它默认的就是NULL,以这种方式可以更灵活的使用Node,个人建议这么使用哦。
2、第二步就是创建我们的链表了,同样我们这里先给出链表的代码,在进行一一的解释。
class List{
private:
Node *head,*tail;
int position;
public:
List(){head=tail=NULL;};
~List(){delete head;delete tail;};
void print();
void Insert(int da=0);
void Delete(int da=0);
void Search(int da=0);
};
我们这里面有两个数据类型,一个是Node。另一个是指代节点位置的成员变量(起不到什么作用,且不去管它吧)。使用head和tail来命名便是为了见名知意,使操作更加准确。然后是重要的六个函数,各自的功能不言而喻咯,其实最重要的是在每一个函数中我们都默认能操作head和tail两个成员变量,这样能简化我们的参数列表,使得函数更加优雅。
下面是我的一个单链表的实现,包含创建链表,插入值,删除特定的值,查找特定值得在链表中的位置。
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int da=0,Node *p=NULL){
this->data=da;
this->next=p;
}
};
class List{
private:
Node *head,*tail;
int position;
public:
List(){head=tail=NULL;};
~List(){delete head;delete tail;};
void print();
void Insert(int da=0);
void Delete(int da=0);
void Search(int da=0);
int getValueAt(int position);
void setValueAt(int position,int da);
};
int List::getValueAt(int position){
Node *p=head;
if(p==NULL){
cout<<"The List is Empty!"<<endl;
}else{
int posi=0;
while(p!=NULL&&posi!=position){
posi++;
p=p->next;
}
if(p==NULL){
cout<<"There is no value of this position in this List!"<<endl;
}else{
cout<<"In this Position,the value is"<<p->data<<endl;
}
}
return p->data;
}
void List::setValueAt(int position,int da){
Node *p=head;
if(p==NULL){
cout<<"The List is Empty!"<<endl;
}else{
int posi=0;
while(p!=NULL&&posi!=position){
posi++;
p=p->next;
}
if(p==NULL){
cout<<"There is No Position in this List!"<<endl;
}else{
p->data=da;
cout<<"The Value in this position has been Updated!"<<endl;
}
}
}
void List::Search(int da){
Node *p=head;
if(p==NULL){
cout<<"Sorry, The List is Empty!"<<endl;
return;
}
int count=0;
while(p!=NULL&&p->data!=da){
p=p->next;
count++;
}
cout<<"the value you want to search is at position %d"<<count<<endl;
}
void List::Delete(int da){
Node *p=head,*q=head;
if(p==NULL){
cout<<"Sorry, The List is Empty!"<<endl;
return;
}
while(p!=NULL&&p->data!=da){
q=p;
p=p->next;
}
q->next=p->next;
cout<<"The Deletion Operation had been finished!"<<endl;
}
void List::Insert(int da){
if(head==NULL){
head=tail=new Node(da);
head->next=NULL;
tail->next=NULL;
}else{
Node *p=new Node(da);
tail->next=p;
tail=p;
tail->next=NULL;
}
}
void List::print(){
Node *p=head;
while(p!=NULL){
cout<<p->data<<" \a";
p=p->next;
}
cout<<endl;
}
int main(){
cout<<"Hello World!"<<endl;
List l1;
l1.Insert(1);
l1.Insert(2);
l1.Insert(3);
l1.Insert(4);
l1.Insert(5);
l1.Insert(6);
l1.Insert(7);
l1.print();
l1.Search(4);
l1.Delete(6);
l1.print();
l1.getValueAt(3);
l1.setValueAt(3,9);
l1.print();
cout<<"The End!"<<endl;
return 0;
}
//在此我想解释的是,之所以数字4在链表中的位置为3,是因为其是从零开始计数的
下面是代码运行后的结果:
好了,单链表的基本操作大致就是这样了,希望我们都能从中有所收获。如果您发现代码中有什么错误,还望不吝赐教,让我们共同进步吧。
C++实现单链表的更多相关文章
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- c++单链表基本功能
head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- 分离的思想结合单链表实现级联组件:CascadeView
本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- Noip2017 普及 T3 Chess
神奇的场上原码 #include<iostream> #include<cstdio> #include<cstring> #include<queue> ...
- all unicode
Unicode Chart Range Decimal Name 0x0000-0x007F 0-127 Basic Latin 0x0080-0x00FF 128-255 Latin-1 Suppl ...
- LintCode题解之统计数字
直接硬搜就可以了,只是需要考虑k为0的情况. public class Solution { /* * @param : An integer * @param : An integer * @ret ...
- [self init]
在字典转模型中遇到了这样的代码: #import "HMAppInfo.h" @implementation HMAppInfo - (instancetype)initWithD ...
- Python3 解释器
Linux/Unix的系统上,Python解释器通常被安装在 /usr/local/bin/python3.4 这样的有效路径(目录)里. 我们可以将路径 /usr/local/bin 添加到您的Li ...
- formData的实现
参考:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest <!doctype ...
- Scala:类,对象和特征(接口)
http://blog.csdn.net/pipisorry/article/details/52902609 Scala类和对象 类是对象的抽象,而对象是类的具体实例.类是抽象的,不占用内存,而对象 ...
- SQLite 语法(http://www.w3cschool.cc/sqlite/sqlite-syntax.html)
SQLite 语法 SQLite 是遵循一套独特的称为语法的规则和准则.本教程列出了所有基本的 SQLite 语法,向您提供了一个 SQLite 快速入门. 大小写敏感性 有个重要的点值得注意,SQL ...
- Java 拓展之调用其他语言
目前而言,编程语言真的是太多了.每一种都是一种智慧的结晶,但是每个都存在其缺点.网上经常能看到一些程序员争论"XX是世界上最好的语言"之类的话题.其实我们大可不必这样.语言本身只是 ...
- Struts 2 之文件上传
如果要获得上传文件的原始名称,需要定义一个String类型的属性,属性名必须为***FileName,其中***为File属性的名称:同理,如果要获取该文件的MIME类型,需要定义一个***Conte ...