单链表的基本操作--c++
#include <iostream> //实现单链表的建立,测长和打印
#include <string>
using namespace std;
struct node // 定义节点类型,包含一个数据域和一个指针域
{
int data;
node *next;
};
node * creat()
{
node *head,*p,*s; // p指针为当前的指针,s为新建结点的指针
int temp,cycle = ;
//head = (node*)malloc(sizeof(node));
head = new node;
p = head;
while(cycle)
{
cout << "请输入int型数据" << endl;
cin >> temp;
if (temp != ) // 当temp不为零时,创建新的
{
//s = (node*)malloc(sizeof(node));
s = new node; //创建新的结点
s -> data = temp;
cout << "新节点为" << s -> data;
p -> next = s; // 使头结点的指针指向新生成的结点
p = s; // 使p指向当前新的结点
}
else
{
cout << "输入完成" <<endl;
cycle = ;
}
}
head = head -> next; //头指针
p -> next = nullptr; //置最后一个结点的指针为空
return head;
}
// 单链表测长
int length(node *head)
{
int n = ;
node *p = head;
while (p != nullptr )
{
p = p -> next;
n++;
}
return n;
}
// 单链表打印
void printlinklist(node * head)
{
node *p = head;
while( p != nullptr)
{
cout << "data is" << p -> data << endl;
p = p -> next;
}
}
// 单链表插入
node *insert(node * head, int num)
{
node *p0,*p1,*p2;
p1 = head;
p2 = new node;
p0 = new node;// 插入结点
p0 -> data = num; // 插入数据
// 若插入结点的数据大于原结点的数据,并且原结点的存在下一个元素
while(p0 -> data > p1 -> data && p1 -> next != nullptr)
{
p2 = p1;
p1 = p1 -> next; // 这时 p2->p1->p0 ??
}
if (p0 -> data <= p1 -> data)
{
if ( p1 == head)
{ // 头部前插入 p0和p1的位置是 P0->P1->
head = p0;
p0 -> next =p1;
}
else
{ // 插入中间节点, p2->p0->p1
p2 -> next = p0;
p0 -> next = p1;
}
}
else
{// 尾部插入结点, p2->p1->p0->null
p1 -> next = p0;
p0 -> next = nullptr;
}
return head;
}
//删除单链表
node *del(node *head, int num)
{
node *p1,*p2;
p2 = new node;
p1 = head;
while(num != p1 -> data && p1 -> next != nullptr)
{
p2 = p1;
p1 = p1 -> next; // p2->p1
}
if (num == p1->data)
{
if (p1 == head) // 删除头结点
{
head =p1 -> next;
delete p1;
}
else
{
p2 -> next = p1 -> next;
delete p1;
}
}
else
{
cout << "could not been found in current single linklist"<< endl;
}
return head;
}
int main()
{ cout << "创建单链表" << endl;
node *head = creat();
cout << endl; cout << "计算链表的长度" << endl;
int n = length(head);
cout << "the length of the linklist is " << n << endl;
cout << endl; cout << "print the linklist" << endl;
printlinklist(head);
cout << endl; cout << "insert the node" << endl;
cout << "please insert the data";
int indata;
cin >> indata;
head = insert(head,indata);
printlinklist(head);
cout << endl; cout << "delete the node" << endl;
cout << "input the data for deleting ";
int deldata;
cin >> deldata;
head = del(head,deldata);
printlinklist(head);
cout <<endl;
return ;
}
单链表的基本操作--c++的更多相关文章
- PHP单链表的基本操作
链表的实现 数据结构第一个就是链表了,链表分为两种有直接的数组形式的顺序链,这里不讨论,什么array_push(),array_pop(),函数基本能满足日常的需求,但报告老板,我就是想装个X 上代 ...
- 用Java实现单链表的基本操作
笔试题中经常遇到单链表的考题,下面用java总结一下单链表的基本操作,包括添加删除节点,以及链表转置. package mars; //单链表添加,删除节点 public class ListNode ...
- 单链表及基本操作(C语言)
#include <stdio.h> #include <stdlib.h> /** * 含头节点单链表定义及基本操作 */ //基本操作函数用到的状态码 #define TR ...
- 用java简单的实现单链表的基本操作
package com.tyxh.link; //节点类 public class Node { protected Node next; //指针域 protected int data;//数据域 ...
- 【C++/数据结构】单链表的基本操作
#pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <assert.h> ...
- Java实现单链表的各种操作
Java实现单链表的各种操作 主要内容:1.单链表的基本操作 2.删除重复数据 3.找到倒数第k个元素 4.实现链表的反转 5.从尾到头输出链表 6.找到中间节点 7.检测链表是否有环 8.在 ...
- C++实现单链表
之前一直没怎么在意C++中的链表,但是突然一下子让自己写,就老是出错.没办法,决定好好恶补一下该方面的知识,也为今后的数据结构大下个良好的基础,于是我总结出以下几点,有些地方可能不正确,还望大家不吝赐 ...
- 数据结构——Java实现单链表
一.分析 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素.链表中的数据是以结点来表示的,每个结点由元素和指针构成.在Java中,我们可以将单链表定义成一个类,单链表的基 ...
- PHP数据结构之三 线性表中的单链表的PHP实现
线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...
随机推荐
- linux shell 指令 诸如-d, -f, -e之类的判断表达式简介
一.文件比较运算符 1. e filename 如果 filename存在,则为真 如: [ -e /var/log/syslog ] 2. -d filename 如果 filename为目录,则为 ...
- python函数的用法
python函数的用法 目录: 1.定义.使用函数 1.函数定义:def 2.函数调用:例:myprint() 3.函数可以当作一个值赋值给一个变量 例:a=myprint() a() 4.写r ...
- Node 各个版本支持ES2015特性的网站
如果想了解Node 各个版本支持ES2015到那个程度,可以看下面网站. https://node.green/
- js中split()方法得到的数组长度
js 中split(",")方法通过 ”,“ 分割字符串, 如果字符串中没有 “,” , 返回的是字符串本身 var str = “abc”://分隔符个数为0 var newSt ...
- Java高级教程
目录 1.Java面向对象方法 1.1. 创建类和对象的方法 1.2. this的使用 1.3. 静态域和静态方法 1.3.1. 静态域:属于类的级别 1.3.2.静态常量 1.3.3 静态方法 1. ...
- 谱聚类算法(Spectral Clustering)优化与扩展
谱聚类(Spectral Clustering, SC)在前面的博文中已经详述,是一种基于图论的聚类方法,简单形象且理论基础充分,在社交网络中广泛应用.本文将讲述进一步扩展其应用场景:首先是User- ...
- 【字符串】ZSC-勤奋的计算机系学生
Description 计算机系的同学从大一就开始学习程序设计语言了.初学者总是容易写出括号不匹配的程序.至今你仍然清楚地记得,那天上机的时候你的程序编译出错,虽然你使尽了吃奶的力气也没有把错误逮着. ...
- 基于python的Selenium使用小结
之前介绍过基于Unittest和TestNG自动化测试框架,然而基于Web端的测试的基础框架是需要Selenium做主要支撑的,这里边给大家介绍下Web测试核心之基于Python的Selenium 一 ...
- Winform知识汇总之多次绑定DataGridView的DataSource会报错 NullReferenceExcepti
最近做了一个winform的项目,数据绑定在datagridview中,datagridview中的数据需要删除,分页,更新等之类的操作,所以就涉及到了datagridview的重新绑定问题,而且这些 ...
- linux学习笔记整理(六)
第七章 Centos7-文件权限管理本节所讲内容:7.1文件的基本权限:r w x (UGO)7.2文件的特殊权限:suid sgid sticky和文件扩展权限ACL7.3实战:创建一个让root都 ...