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. 利用 Task\Query 实现定位 , FeatureLayer 的属性查询

    放纵了几天,又有了学习的动力.今天实现了利用对 FeatureLayer 进行属性查询在地图上进行跳转. 一.我下载了一幅浙江省的县界面地图,存在NAME字段,利用Name就能进行查询了: var n ...

  2. SSIS 使用OLEDB/ADO NET Source 数据流source控件 连接Oracle失败

    在做数据提取的时候发现一个非常奇怪的问题. Oracle客户端是安装正确并且Toad可以正常运行的,但是在新建OLEDB/ADO NET Source 数据流source控件连接Oracle的时候一直 ...

  3. 【One Day菜鸟到大鸟】MyBatis搭建环境

    一.概述     MyBatis是一个持久化框架和Hiberante差不多.比起JDBC来说MyBatis封装了JDBC让我们能够面向对象开发.比起Hiberante来说卸下了Hiberante那种重 ...

  4. jquery 事件监听方法

    一.事件监听方法 mouseover()   鼠标移入事件方法 mouseout()    鼠标移出事件方法 mouseenter()  鼠标移入事件方法 mouseleave()  鼠标移出事件方法 ...

  5. asp.net ashx导出excel到前台

    最近有一个项目使用以前的ashx,不能使用FileResult,只有通过response返回拼接好的字符串.但是通过查阅资料拼接的字符串总是提示文件格式不匹配,虽然能正常打开,但是体验很不好,在此总结 ...

  6. SSIS ->> Environment Variables

    SQL Server Integration Services(SSIS) 在2012版本引入了Environment Variables这个新特性.它允许我们为一个环境创建出一套变量用于为项目内的包 ...

  7. 配置docker容器上ssh无密登录

    配置docker容器上ssh无密登录 1.修改所有容器中root账户密码 ssh到远程主机时,首次需要密码访问,因此需要修改root账号密码. 密码必须要8位以上字母数字混合. $>passwd ...

  8. 【jQuery】jQuery中的事件捕获与事件冒泡

    在介绍之前,先说一下JavaScript中的事件流概念.事件流描述的是从页面中接受事件的顺序.   一.事件冒泡( Event Bubbling)            IE 的事件流叫做事件冒泡,即 ...

  9. 【转载】Kali-linux安装之后的简单设置

         1.更新软件源:修改sources.list文件:leafpad /etc/apt/sources.list然后选择添加以下适合自己较快的源(可自由选择,不一定要全部): #官方源deb h ...

  10. Java中父类强制转换为子类的可能

    之前徒弟问了一个问题, 在Java中, 父类对象到底能不能转换成对应的子类对象? 到底能不能, 今天就来说说这个问题, 先看下面一段代码: package cn.com.hanbinit.test; ...