c和c++单链表
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++单链表的更多相关文章
- 时间复杂度分别为 O(n)和 O(1)的删除单链表结点的方法
有一个单链表,提供了头指针和一个结点指针,设计一个函数,在 O(1)时间内删除该结点指针指向的结点. 众所周知,链表无法随机存储,只能从头到尾去遍历整个链表,遇到目标节点之后删除之,这是最常规的思路和 ...
- 单链表的C++实现(采用模板类)
采用模板类实现的好处是,不用拘泥于特定的数据类型.就像活字印刷术,制定好模板,就可以批量印刷,比手抄要强多少倍! 此处不具体介绍泛型编程,还是着重叙述链表的定义和相关操作. 链表结构定义 定义单链表 ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- c++单链表基本功能
head_LinkNode.h /*单链表类的头文件*/#include<assert.h>#include"compare.h"typedef int status; ...
- 单链表、循环链表的JS实现
数据结构系列前言: 数据结构作为程序员的基本知识,需要我们每个人牢牢掌握.近期我也展开了对数据结构的二次学习,来弥补当年挖的坑...... 当时上课的时候也就是跟着听课,没有亲自实现任何一种数据结 ...
- C代码实现非循环单链表
C代码实现非循环单链表, 直接上代码. # include <stdio.h> # include <stdlib.h> # include <malloc.h> ...
- 分离的思想结合单链表实现级联组件:CascadeView
本文介绍自己最近做省市级联的类似的级联功能的实现思路,为了尽可能地做到职责分离跟表现与行为分离,这个功能拆分成了2个组件并用到了单链表来实现关键的级联逻辑,下一段有演示效果的gif图.虽然这是个很常见 ...
- 数据结构:单链表结构字符串(python版)添加了三个新功能
#!/urs/bin/env python # -*- coding:utf-8 -*- #异常类 class stringTypeError(TypeError): pass #节点类 class ...
- 数据结构:单链表结构字符串(python版)改进
此篇文章的replace实现了字符串类的多次匹配,但依然有些不足. 因为python字符串对象为不变对象,所以replace方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- 2-1 Sass的控制命令
@if @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还可以配合 @ ...
- base64的编码解码的一些坑
1. //编码 value = base64encode(utf16to8(src)) //解码 value = utf8to16(base64decode(src)) 这里:base64编码之前先转 ...
- (C/C++) 用函数返回一个结构体
方法一: 参数里含有指向指针的指针. 注意:如果函数参数里只有一个指向结构体的指针,是无法正确地返回结构体的值的.原因在于在编译的时候,会对入参p产生一个备份_p. 参考此文:http://www.c ...
- 制作rpm安装包
1.安装rpmbuild软件 sudo apt-get install rpmbuild2.配置工作路径 在制作 rpm 包之前,首先要配置工作路径,也就是制作 rpm 包所在的目录.制作 rpm 包 ...
- Netty入门1之----认识Netty
Netty 什么是Netty? Netty 是一个利用 Java 的高级网络的能力,隐藏其背后的复杂性而提供一个易于使用的 API 的客户端/服务器框架. Netty 是一个广泛使用的 Java ...
- Xwiki安装部署详解
一.Xwiki简介XWiki是一个由Java编写的基于LGPL协议发布的开源wiki和应用平台.XWiki是一款基于java所编写的wiki,它可以运行在如Tomcat,Jetty,JBoss,Web ...
- Centos7开放端口
Centos7开放端口 Centos升级到7之后,发现无法使用iptables控制Linuxs的端口,google之后发现Centos 7使用firewalld代替了原来的iptables.下面记录如 ...
- DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution. ...
- Hibernate、Mybatis与Spring Data JPA
从零开始集成Springboot+MyBatis+JPA https://www.jianshu.com/p/e14c4a6f6871 MyBatis 与Hibernate的区别 http://xhr ...
- 在自己机器上实现apache的多域名
自己机器上有3个目录,分别是/var/www/html/ /var/www/solaris/ ...