单链表的基本操作--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实现
线性表的链式存储:用一组任意的存储单元存储线性表中的数据元素.用这种方法存储的线性表简称线性链表. 链式存储线性表的特点:存储链表中结点的一组任意的存储单元可以是连续的,也可以是不连续的,甚至是零散分 ...
随机推荐
- MySQL 5.7忘记root密码如何修改?
一直以来,MySQL的应用和学习环境都是MySQL 5.6和之前的版本,也没有去关注新版本MySQL 5.7的变化和新特性.今天帮人处理忘记root密码的时时候,发现以前的方法不奏效了.具体情况如下所 ...
- chmod命令-权限
---··[转] hmod命令:改变文件权限. 一:符号模式: 命令格式:chmod [who] operator [permission] filename who包含的选项 ...
- spring4笔记----PropertyPlaceholderConfigurer 属性占位符配置器
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/spring username=root password= ...
- Linux环境下执行java -jar xxx.jar命令如何让springboot项目在后台运行
段落引用> 由于springboot内置了tomcat容器,我们通常会把项目打成jar或者war后直接使用java -jar xxx.jar命令去运行程序,但是当前ssh窗口被锁定或者按下ctr ...
- SQL 一列拆分多行
select a.col1,b.col2 from (select col1,col2=convert(xml,' <root> <v>'+replace(col2,',',' ...
- python3+beautifulsoup4爬取汽车信息
import requests from bs4 import BeautifulSoup response = requests.get("https://www.autohome.com ...
- wamp 中安装cakephp Fatal error: You must enable the intl extension to use CakePHP. in XXX
今天在wamp下安装cakephp3.x的时候,报出这么一条错误:Fatal error: You must enable the intl extension to use CakePHP. in ...
- 常见C语言内存错误
前言 C语言强大的原因之一在于几乎能掌控所有的细节,包括对内存的处理,什么时候使用内存,使用了多少内存,什么时候该释放内存,这都在程序员的掌控之中.而不像Java中,程序员是不需要花太多精力去处理垃圾 ...
- centos7下安装docker(24docker swarm 数据管理)
service的容器副本会scal up/down,会failover,会在不同的主机上创建和销毁,这就引出一个问题,如果service有数据,那么这些数据该如何存放呢? 1.打包在容器中: 显然不行 ...
- 写了12年JS也未必全了解的连续赋值运算
引子 var a = {n:1}; var b = a; // 持有a,以回查 a.x = a = {n:2}; alert(a.x);// --> undefined alert(b.x);/ ...