/*单链表操作*/
#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++ 单链表添加,删除,输出,逆序操作的更多相关文章

  1. C语言实现单链表的遍历,逆序,插入,删除

    单链表的遍历,逆序,插入,删除 #include<stdio.h> #include<stdlib.h> #include <string.h> #define b ...

  2. C语言整数按照二进制逆序,输出逆序后的整数值

    问题来源,今天早上和一舍友吃早餐的时候谈到的一个问题,将一个整数按照二进制逆序,然后输出逆序后的数值. 我们知道数值在内存中都是以二进制的形式存放的,假如我们是32位机,每8位为一个字节,int型在3 ...

  3. PTA 循环单链表区间删除 (15 分)

    本题要求实现带头结点的循环单链表的创建和单链表的区间删除.L是一个带头结点的循环单链表,函数ListCreate_CL用于创建一个循环单链表,函数ListDelete_CL用于删除取值大于min小于m ...

  4. pta 奇数值结点链表&&单链表结点删除

    本题要求实现两个函数,分别将读入的数据存储为单链表.将链表中奇数值的结点重新组成一个新的链表.链表结点定义如下: struct ListNode { int data; ListNode *next; ...

  5. Angular-表单动态添加删除

    angular本身不允许去操作DOM,在angular的角度来说,所有操作都以数据为核心,剩下的事情由angular来完成.所以说,想清楚问题的根源,解决起来也不是那么困难. 前提 那么,要做的这个添 ...

  6. Linux C 单链表 读取文件 并排序 实例并解释

    C的指针挺头疼的,先看一个例子: 给指针赋值和通过指针进行赋值这两种操作的差别确实让人费解.谨记区分的重要方法是:如果对左操作数进行解引用,则修改的是指针所指对象的值:    如果没有使用解引用操作, ...

  7. Linux内核单链表

    主要说明Linux内核中单链表操作的关键思想,需要注意的地方 1. 假设 为了说明关键思想,对数据结构进行了精简 2. 数据结构定义 struct ListNode { int val; ListNo ...

  8. JavaScript学习 - 基础(八) - DOM 节点 添加/删除/修改/属性值操作

    html代码: <!--添加/删除/修改 --> <div id="a1"> <button id="a2" onclick=&q ...

  9. 循环链表的创建、插入、删除、逆序、显示(C++实现)

    对于单链表,因为每一个结点仅仅存储了向后的指针.到了尾标志就停止了向后链的操作,这样,其中某一结点就无法找到它的前驱结点了. 对于单链表的操作大家能够看我的这篇博客http://blog.csdn.n ...

随机推荐

  1. 上周 GitHub 热点速览 vol.08:系统设计必看 The System Design Primer

    作者:HelloGitHub-小鱼干 摘要:GitHub Trending 上周看点,老项目依旧抢眼,系统设计必看 Repo:The System Design Primer 周获 1k+ star, ...

  2. 1215 - Finding LCM

    1215 - Finding LCM   LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LC ...

  3. 杭电1007-----C语言实现

    这道题花了好久的时间才做出来,刚开始没有思路,最后看了网上的解答,好难得样子,每次都没有看完,但是掌握了大概思想,今天试着做了一下,已ac 主要思想:先将点对按照x排序,再在x排好序的基础上按照y来排 ...

  4. 【转载】Java的Vector,ArrayList,LinkedList

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  5. cesium1.65api版本贴地贴模型标绘工具效果(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  6. 软链接和硬链接——Linux中的文件共享

    硬链接(Hard Link)和软链接也称为符号链接(Symbolic Link)的目的是为了解决文件的共享使用问题.要阐明其原理,必须先理解Linux的文件存储方式. 索引结点 Linux是一个UNI ...

  7. hyper-v安装ubuntu18的全过程+踩过的坑(win10家庭版)

    一.背景介绍(作者的闲言闲语,可跳过) 最近新购入小米笔记本pro15增强版,想在笔记本上装ubuntu18的系统. 最开始尝试了双系统的方法,但是安装完之后不能wifi上网,显卡MX250不能适配, ...

  8. Linux 实时查看日志文件动态内容

    tailf 27.log | grep 'Classcomment/praise'               'Classcomment/praise' 接口名:查看请求固定接口的时间,实时 tai ...

  9. Android中动态改变Listview中字体的颜色

    效果如下: 账目显示用的是Listview,要实现的功能为使其根据所在Item是“收入”还是“支出”来把数字设置成绿色或红色 方法是自定义适配器,并重写其中getView()函数,实现如下: //自定 ...

  10. 02-flink时间语义 与 Window 基础概念与实现原理

    Flink 多种时间语义对比 Flink 在流应用程序中支持不同的 Time 概念,就比如有 Processing Time.Event Time 和 Ingestion Time.下面我们一起来看看 ...