Linux C++ 单链表添加,删除,输出,逆序操作
/*单链表操作*/
#include <iostream>
using namespace std; class Node{
public:
Node(){
next=0;
}
Node(int el, Node *ptr=0)
{
info=el;
next=ptr;
}
int info;
Node *next;
}; class LList{
public:
LList(){head=tail=0;}
~LList();
int isEmpty(){return head==0;}
void addToHead(int);
void addToTail(int);
int deleteHead();
int deleteTail();
void deleteNode(int);
bool isInList(int)const;
void displayall() const;
void inverted();
private:
Node *head, *tail;
}; LList::~LList(){
for(Node *p;!isEmpty();){
p=p->next;
delete head;
head=p;
}
} void LList::addToHead(int el){
head=new Node(el,head);
if(tail==0)
tail=head;
} void LList::addToTail(int el){
if(tail!=0){
tail->next=new Node(el);
tail=tail->next;
}
else head=tail=new Node(el);
} int LList::deleteHead(){
int el=head->info;
Node *tmp=head;
if(head==tail)
head=tail=0;
else head=head->next;
delete tmp;
return el;
} int LList::deleteTail(){
int el=tail->info;
if(head==tail) {
delete tail;
head=tail=0;
}
else{
Node *tmp=NULL;
for(tmp=head;tmp->next!=tail;tmp=tmp->next);
delete tail;
tail=tmp;
tail->next=0;
}
return el;
}
void LList::deleteNode(int el){
if(head!=0){
if(head==tail && el == head->info)
{ delete head;
head=tail=0;
}
else if(el ==head->info)
{ Node *tmp=head;
head=head->next;
delete tmp;
}
else{
Node *pred,*tmp;
for(pred=head,tmp=head->next;tmp!=0 && (tmp->info==el);pred=pred->next,tmp=tmp->next);
if(tmp!=0){
pred->next=tmp->next;
if(tmp==tail)
tail=pred;
delete tmp;
}
}
}
} bool LList::isInList(int el) const{
Node *tmp;
for(tmp=head;tmp!=0 && !(tmp->info==el);tmp=tmp->next);
return tmp!=0;
}
void LList::displayall() const{
Node *tmp;
for(tmp=head;tmp!=0;tmp=tmp->next)
{cout<<tmp->info<<"-";}
cout<<endl;
} void LList::inverted(){
Node *pre=NULL;
Node *next=NULL;
tail=head; while(head!=NULL)
{
next=head->next;
head->next=pre;
pre=head;
head=next; }
head=pre;
} int main()
{
LList *list= new LList();
list->addToTail(1);
list->addToTail(2);
list->addToTail(3);
list->addToTail(4);
list->addToHead(0);
list->addToTail(4);
list->addToTail(5);
list->addToTail(6);
list->addToTail(7);
list->addToTail(8);
list->displayall(); list->inverted();
list->displayall(); return 0;
}
输出结果:
0-1-2-3-4-4-5-6-7-8-
8-7-6-5-4-4-3-2-1-0-
Linux C++ 单链表添加,删除,输出,逆序操作的更多相关文章
- C语言实现单链表的遍历,逆序,插入,删除
单链表的遍历,逆序,插入,删除 #include<stdio.h> #include<stdlib.h> #include <string.h> #define b ...
- C语言整数按照二进制逆序,输出逆序后的整数值
问题来源,今天早上和一舍友吃早餐的时候谈到的一个问题,将一个整数按照二进制逆序,然后输出逆序后的数值. 我们知道数值在内存中都是以二进制的形式存放的,假如我们是32位机,每8位为一个字节,int型在3 ...
- PTA 循环单链表区间删除 (15 分)
本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...
- pta 奇数值结点链表&&单链表结点删除
本题要求实现两个函数,分别将读入的数据存储为单链表.将链表中奇数值的结点重新组成一个新的链表.链表结点定义如下: struct ListNode { int data; ListNode *next; ...
- Angular-表单动态添加删除
angular本身不允许去操作DOM,在angular的角度来说,所有操作都以数据为核心,剩下的事情由angular来完成.所以说,想清楚问题的根源,解决起来也不是那么困难. 前提 那么,要做的这个添 ...
- Linux C 单链表 读取文件 并排序 实例并解释
C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值: 如果没有使用解引用操作, ...
- Linux内核单链表
主要说明Linux内核中单链表操作的关键思想,需要注意的地方 1. 假设 为了说明关键思想,对数据结构进行了精简 2. 数据结构定义 struct ListNode { int val; ListNo ...
- JavaScript学习 - 基础(八) - DOM 节点 添加/删除/修改/属性值操作
html代码: <!--添加/删除/修改 --> <div id="a1"> <button id="a2" onclick=&q ...
- 循环链表的创建、插入、删除、逆序、显示(C++实现)
对于单链表,因为每一个结点仅仅存储了向后的指针.到了尾标志就停止了向后链的操作,这样,其中某一结点就无法找到它的前驱结点了. 对于单链表的操作大家能够看我的这篇博客http://blog.csdn.n ...
随机推荐
- RF-ui自动化
1.关于时间等待 Wait Until Keyword Succeeds 3x 300ms Wait Until Element Is Visible ${x ...
- mac 经常使用的快捷键操作
##### touch bar 作用1: 打开项目的一些快捷操作键. 作用2: 右侧的< 打开有一些以前的常规操作. ##### 手势 ##### mac自带的一些操作 cmd + opt + ...
- 论文翻译:2015_DNN-Based Speech Bandwidth Expansion and Its Application to Adding High-Frequency Missing Features for Automatic Speech Recognition of Narrowband Speech
论文地址:基于DNN的语音带宽扩展及其在窄带语音自动识别中加入高频缺失特征的应用 论文代码:github 博客作者:凌逆战 博客地址:https://www.cnblogs.com/LXP-Never ...
- .NET Core MVC下的TagHelper
.NET web开发者在开发过程中,一定都踩过的坑,明明修改了js文件,可是部署到生产环境,客户反馈说:“还是报错啊”..然后一脸懵逼的去服务器上看文件,确实已经更新了.有经验的coder可能就想到了 ...
- StarUML之四、StarUML的Diagrams(图)与Elements(元素)及相关属性
Diagrams(图)可以理解为画布 1:创建图 在右侧的Model Explorer管理界面的第一个节点右键,或者选择菜单中Model | Add Diagram | [DiagramType]都 ...
- Git 工作流程和Git分支管理策略
git-flow 阮一峰大佬写的文章真不错 git-flow, github-flow, gitlab-flow 阮一峰大佬写的文章真不错
- 苹果Mac电脑永久路由的添加 & Mac 校园网连接教程
学校校园网面向全校师生开放,无奈Windows用户基数大,学校只为Windows平台制作了内网连接工具,Mac平台资源较少,本人查阅相关资料后,总结整理出以下步骤,方便本校学生连接校园网.有永久路由添 ...
- GPU体系架构(二):GPU存储体系
GPU是一个外围设备,本来是专门作为图形渲染使用的,但是随着其功能的越来越强大,GPU也逐渐成为继CPU之后的又一计算核心.但不同于CPU的架构设计,GPU的架构从一开始就更倾向于图形渲染和大规模数据 ...
- .net平台 通过Windows installer 打包和部署实例操作
Windows installer 打包和部署 1.新建项目. 打开VS,新建项目->其他项目类型->安装和部署(Visual Studio Installer). 注:若VS2013 或 ...
- Docker 安装 ELK
安装 首先安装 Docker 与 Docker-Compose 相关的组件,我们这里直接使用准备好的 ELK 镜像,执行以下命令从 Dockerhub 上拉取指定版本的镜像,在本例当中我使用的是 7. ...