c++版
#include<iostream>
#include<malloc.h>
using namespace std;
struct node{
int data;
node *next;
node(){
next=NULL;
}
};
node *head;
void clist(){
head=(node *)malloc(sizeof(node));
head->next=NULL;
}
void create(int i){
node *p,*q;
q=(node *)malloc(sizeof(node));
p=head;
while(i--){
cin>>q->data;
p->next=q;
p=q;
q=(node *)malloc(sizeof(node));
}
}
void display(){
node *p;
p=head->next;
while(p){
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void clist1(){
node *p;
while(head){
p=head->next;
free(head);
head=p;
}
}
int getline(){
int length=;
node *p=head->next;
while(p){
length++;
p=p->next;
}
return length;
}
int find(int e){
node *p=head->next;
while(p){
if(p->data==e) return ;
p=p->next;
}
return ;
}
node *getnode(int i){
if(i<||i>getline()){
cout<<"no"<<endl;
throw i;
}
node *p=head;
while(p&&i){
p=p->next;
i--;
}
return p;
}
void insert(int i,int e){
node *p;
node *q;
q=(node *)malloc(sizeof(node));
q->data=e;
if(i==){
q->next=head->next;
head->next=q;
}
else{
p=getnode(i-);
if(i==getline())
p->next=q;
else{
q->next=p->next;
p->next=q;
}
}
}
void Delete(int e){
if(!find(e))
{
cout<<"没这个数"<<endl;
return;
}
node *p=head;
node *q=head->next;
while(q){
if(q->data==e)
break; p=p->next;
q=q->next;
}
p->next=q->next;
return;
}
bool isempty(){
return head->next==NULL;
}
void reverse(){
if(isempty()){
cout<<"为空"<<endl;
}
node *p,*q;
int len=getline();
int i=;
int j=len;
while(i<j){
p=getnode(i);
q=getnode(j);
int temp=p->data;
p->data=q->data;
q->data=temp;
++i;
--j;
}
}
int main()
{
clist();
int g;
cout<<"输入你链表里有几个数";
cin>>g;
create(g);
display();
cout<<"长度为"<<getline()<<endl;
cout<<"输入你要查找的数"<<endl;
int t;
cin>>t;
if(find(t)) cout<<"有"<<endl;
else cout<<"没有"<<endl;
cout<<"输入你要插入的位置和数"<<endl;
int a,b;
cin>>a>>b;
insert(a,b);
display();
cout<<"输入你要删除的数"<<endl;
cin>>t;
Delete(t);
display();
cout<<"翻转后的情况为"<<endl;
reverse();
display();
return ;
}
#include<iostream>
#include<cstring>
using namespace std; class cnode{
public :
int data;
cnode *next;
cnode()
{
next=NULL;
}
}; class clist{
private:
cnode *head;
public:
clist();
void create();
void display();
~clist();
int getlin() const;
bool isempty()const;
bool find(const int e) const;
cnode *getnode(int i) const;
void insert(int i,const int e);
void Delete(const int e);
void reverse(); };
clist::clist(){
head=new cnode();
head->next=NULL;
} void clist::create()
{
cnode *p,*q;
p=head;
q=new cnode();
cout<<"请输入值ctrl+z停止:"<<endl;
while(cin>>q->data)
{
p->next=q;
p=q;
q=new cnode();
}
}
void clist::display()
{
cnode *p;
p=head->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
clist::~clist()
{
cnode *p;
while(head)
{
p=head->next;
delete head;
head=p; }
}
int clist::getlin() const
{
int length=;
cnode* p=head->next;
while(p)
{
length++;
p=p->next;
}
return length;
} bool clist::isempty()const
{
return (head->next==NULL);
}
bool clist::find(const int e)const
{
cnode* p=head->next;
while(p)
{
if(p->data==e)
return true;
p=p->next;
}
return false;
}
cnode* clist::getnode(int i)const
{
if(i<||i>getlin())
{
throw i;
}
cnode* p=head;
while(p&&i)
{
p=p->next;
i--;
}
return p;
} void clist::insert(int i,const int e)
{
cnode* p;
cnode *node=new cnode();
node->data=e;
if(i==)
{
node->next=head->next;
head->next=node;
}
else{
p=getnode(i-);
if(i==getlin())
p->next=node;
else{
node->next=p->next;
p->next=node;
}
}
} void clist::Delete(const int e)
{
if(!find(e))
{
cout<<"不包含啊"<<endl;
return ;
}
cnode* p=head;
cnode *q=head->next;
while(q)
{
if(q->data==e)
{
break; }
p=p->next;
q=q->next; }
p->next=q->next;
return;
} void clist::reverse(){
if(isempty())
{
cout<<"kongle"<<endl;
}
cnode *p,*q;
int len=getlin();
int i=;
int j=len;
while(i<j)
{
p=getnode(i);
q=getnode(j);
int temp=p->data;
p->data=q->data;
q->data=temp;
++i;
--j;
}
}
int main()
{
clist* link=new clist();
link->create();
link->display();
cout<<link->getlin()<<endl;
cout<<link->isempty()<<endl;
cout<<link->find()<<endl; link->insert(,);
link->insert(,);
link->Delete();
link->display();
link->reverse();
link->display();
system("pause");
return ;
}

c和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. C#中的Sealed和J#中的Final比较(转载)

    Sealed与Final修饰符其实并不是一个语言平台的产物,他们有着各自所属的语言环境,但这两个关键字都是.Net平台中不可或缺的,那么二者用法几何,随本文一探究竟. 一.Sealed sealed ...

  2. .NET开源工作流RoadFlow-流程运行-工作委托

    如果某一个人某一段时间不在单位,则可以将自己的工作委托给他人代为处理. 在 流程处理-->工作委托 中可以管理自己的委托,管理员也可以在 流程管理-->工作委托 中管理所有人的委托: 委托 ...

  3. nyist 20 吝啬的国度(dfs)

    吝啬的国度 题目描述: 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来. 现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市, 必须经过的前一 ...

  4. 139.00.009提高Github Clone速度

    @(139 - Environment Settings | 环境配置) Method 1 :SS+系统内置代理 用 git 内置代理,直接走系统中运行的代理工具中转,比如,你的 SS 本地端口是 1 ...

  5. KNN 与 K - Means 算法比较

    KNN K-Means 1.分类算法 聚类算法 2.监督学习 非监督学习 3.数据类型:喂给它的数据集是带label的数据,已经是完全正确的数据 喂给它的数据集是无label的数据,是杂乱无章的,经过 ...

  6. keras 联合训练

    转自: https://blog.csdn.net/Yan_Joy/article/details/62235704

  7. QML Delegate中访问该持有者的方式 附加属性(转载)

    http://blog.csdn.net/yuxiaohen/article/details/17226971   用法很奇葩记录一下,实测可以,用于弱化delegate与持有者的依赖 delegat ...

  8. Php 性能参数优化 及 Iptables 防火墙限制用户访问平率

    Php-Fpm.Conf 文件配置优化 [global] pid = run/php-fpm.pid process_control_timeout=5 [www] listen.allowed_cl ...

  9. CefSharp开发

    CefSharp是用chromium内核开发的.net版本浏览器工具.目前只支持X86模式.所以在调试的时候要把平台改为X86 CefSharp开发指引:https://ourcodeworld.co ...

  10. LINUX中块设备文件和字符设备文件的本质区别

    在LINUX系统文件类型分类的文章中我们提到了 块设备 和 字符设备文件,那么什么是块设备 字符设备文件呢?他们之间有什么本质上的区别呢? 设备文件分为 Block(块) Device Driver ...