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方法并不修改原先的字符串,而是返回修改后的字符串. 而此字符串对象时用单链表 ...
随机推荐
- 清楚苹果 iPai端按钮默认样式
input[type="button"], input[type="submit"], input[type="reset"] { -web ...
- 在oracle表中插入空字段和null测试
create table testTable ( id number, name ) ) select * from testTable ,'user1') ,'') ,null) select co ...
- mysql游标的用法及作用
1当前有三张表A.B.C其中A和B是一对多关系,B和C是一对多关系,现在需要将B中A表的主键存到C中:常规思路就是将B中查询出来然后通过一个update语句来更新C表就可以了,但是B表中有2000多条 ...
- android webview 播放 video经验总结
在目前PC浏览器上,对video的支持基本都没什么问题了.但是如果用webview去跑这样的页面就会遇到许多问题. 下面一段html <!DOCTYPE html> <html> ...
- 面对IBM与亚马逊的犄角攻势,微软云如何招架?
亚马逊AWS和微软Azure是全球公有云的焦点.不就前公布的财报不久前公布的财报,这两家公司云计算的收入越来越接近,从数据显示来看,亚马逊的利润比微软稍高,有人称微软云的高增长来自于捆绑销售,背后真正 ...
- 实验 MPLS LDP配置
实验 MPLS LDP配置 一.学习目的 掌握启用和关闭MPLS的方法 掌握启用和关闭MPLS LDP配置方法 掌握使用MPLS LDP配置LSP的方法 二.拓扑图 三.场景 你是公司的网管员,公司的 ...
- wget 模拟 get post请求
wget命令 默认采用GET请求, 如果使用POST请求, wget --post-data '' url // 这样 POST 请求没有请求体.
- vagrant安装centos7
1. 安装VirtualBox 去官网https://www.virtualbox.org/wiki/Downloads下载最新版的Virtualbox,然后双击安装,一直点击确认完成. 2. 安装V ...
- windows生成硬链接
因工作电脑需要同时使用pl/sql和toad工具需要同时配置32位和64位oracle client如此增加了维护tnsnames.ora的复杂程度使用windows硬链接可以减少工作量,每次只修改源 ...
- 使用c++11写个最简跨平台线程池
为什么需要多线程? 最简单的多线程长啥样? 为什么需要线程池,有什么问题? 实现的主要原理是什么? 带着这几个问题,我们依次展开. 1.为什么需要多线程? 大部分程序毕竟都不是计算密集型的,简单的说, ...