单链表的基本操作--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实现
线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...
随机推荐
- Symantec Backup Exec 2010 安装报 bad ELF interpreter: No such file or directory
在64位的Red Hat Enterprise Linux Server release 6.6上安装Symantec Backup Exec 2010时, 遇到下面错误: # ./installra ...
- shell编程—简单的使用(二)
使用shell编辑.sh使其输出hello tynam 1.新建一个.sh文件,然后进行编辑 vi hello_tynam.sh 2.进行编辑,先按i键进行激活,然后输入echo hello tyna ...
- 探索SQL Server元数据(三):索引元数据
背景 在第一篇中我介绍了如何访问元数据,元数据为什么在数据库里面,以及如何使用元数据.介绍了如何查出各种数据库对象的在数据库里面的名字.第二篇,我选择了触发器的主题,因为它是一个能提供很好例子的数据库 ...
- Hive中笔记 :三种去重方法,distinct,group by与ROW_Number()窗口函数
一.distinct,group by与ROW_Number()窗口函数使用方法 1. Distinct用法:对select 后面所有字段去重,并不能只对一列去重. (1)当distinct应用到多个 ...
- ini (ini-parser)配置文件解析 for donet
介绍 此ini解析库适用于mono(unity3d),donet,大小在30kb左右. 开源免费:https://github.com/rickyah/ini-parser 使用示例 engine_c ...
- Eclipse修改Maven仓库配置
修改Apache-Maven\conf下的settings.xml文件 <?xml version="1.0" encoding="UTF-8"?> ...
- LeetCode算法题-Excel Sheet Column Title(Java实现)
这是悦乐书的第180次更新,第182篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第39题(顺位题号是168).给定正整数,返回Excel工作表中显示的相应列标题.例如: ...
- eshint的配置
{ "strict" : "implied", //文件里面使用"use strict" "undef" : true, ...
- nuxt拦截IE浏览器
需求场景 判断浏览器类型,让譬如IE的低版本浏览器跳转到指定提示浏览器升级页面. 难点分析 使用过的都知道,nuxt没有暴露主入口页面也就是index.html啊,我们以前常用的IE条件判断没地方写. ...
- ES5-ES6-ES7_Promise对象详解
Promise对象概述(什么是Promise) Promise 是异步编程的一种解决方案,比传统的异步解决方案——回调函数和事件——更合理和更强大 所谓Promise,简单说就是一个容器,里面保存着某 ...