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. 项目经验:GIS<MapWinGIS>建模第五天

    实现连能性的分析,及分析完成后,针对独立的块区域进行管网的修补工作 实现步骤如下图所示:

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

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

  3. Redhat Linux 7.3 虚拟机通过USB挂载NTFS格式的移动硬盘

    分为如下几个步骤: 一.设置本地yum,安装gcc(如果本机已经安装gcc,则跳过此步) 在虚拟机连接linux iso安装盘 查看光盘挂载情况 mkdir /iso mount /dev/cdrom ...

  4. mysql 更新sql报错:You can't specify target table 'wms_cabinet_form' for update in FROM clause

    数据库里面有两个字段的位置不对,要把他们对调换下.因为没有数据库写的权限,需要用sql语句来实现.原来以为简单的 update table a set a.字段a=(select b字段 from t ...

  5. 给Docker武士们的正式邀请,赶紧收哦!

    亲爱的Docker武士,Docker大师们喊你来参加Docker的定期聚啦~收好时间.地点,快来相见.切磋Docker吧!5月17日,微软上海港汇办公室,我们与你不见不散! 点击阅读原文,或直接进入注 ...

  6. mysql 配置详解

    [client]port = 3306socket = /tmp/mysql.sock [mysqld]port = 3306socket = /tmp/mysql.sock basedir = /u ...

  7. 爬虫入门之Scrapy框架实战(新浪百科豆瓣)(十二)

    一 新浪新闻爬取 1 爬取新浪新闻(全站爬取) 项目搭建与开启 scrapy startproject sina cd sina scrapy genspider mysina http://roll ...

  8. mysql8采用caching-sha2-password加密

    因为搭建docker容器mysql,直接pull mysql latest版本,因为目前mysql的版本已经升级到了8.0. 像我们之前链接mysql的方式,或者说客户端,就不行了. 比如navica ...

  9. windows7 端口查看以及杀死进程释放端口

    1.调出命令窗口:开始---->运行---->cmd,或者是window+R组合键 2.输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是4300 ...

  10. 设计模式:命令(Command)模式

    设计模式:命令(Command)模式 一.前言 命令也是类,将命令作为一个类来保存,当要使用的时候可以直接拿来使用,比如脚本语言写出的脚本,只需要一个命令就能执行得到我们想要的需要操作很长时间才能得到 ...