c++学习笔记—单链表基本操作的实现
用c++语言实现的单链表基本操作,包括单链表的创建(包括头插法和尾插法建表)、结点的查找、删除、排序、打印输出、逆置、链表销毁等基本操作。
IDE:vs2013

具体实现代码如下:
- #include "stdafx.h"
- #include <malloc.h>
- #include <iostream>
- using namespace std;
- typedef struct Lnode
- {
- int data;
- struct Lnode *next;
- }*node;
- node head_creat() //头插法建立单链表
- {
- node head = (struct Lnode *)malloc(sizeof(struct Lnode));
- head->next = NULL;
- node p;
- int temp;
- while (cin >> temp)
- {
- p = (node)malloc(sizeof(struct Lnode));
- p->data = temp;
- p->next = head->next;
- head->next=p;
- }
- return head;
- }
- bool search(node h, int target) //查找某元素是否在链表中
- {
- int flag = 0;
- node p = h->next;
- if (h->next == NULL)
- return false;
- for (; p != NULL;)
- {
- if (p->data == target)
- {
- flag = 1;
- break;
- }
- else
- p++;
- }
- if (flag)
- return true;
- else
- return false;
- }
- node back_creat() //尾插法建立单链表
- {
- node head = (struct Lnode *)malloc(sizeof(struct Lnode));
- head->next = NULL;
- node p;
- node r = head;
- int temp;
- while (cin >> temp)
- {
- p = (node)malloc(sizeof(struct Lnode));
- p->data = temp;
- r->next=p;
- r = p;
- }
- r->next = NULL;
- return head;
- }
- void print(node h) //打印单链表
- {
- node p = h->next;
- while (p)
- {
- cout << p->data << endl;
- p = p->next;
- }
- }
- node delete_node(node h,int del_val) //删除指定值的节点
- {
- node p = h->next;
- node q = h;
- node r=NULL;
- while (p)
- {
- if (p->data == del_val)
- {
- r = p;
- p = p->next;
- q->next = p;
- free(r);
- }
- else
- {
- q = p;
- p = p->next;
- }
- }
- return h;
- }
- node sort(node h) //对链表进行排序(降序)
- {
- node p=h->next;
- if (p->next == NULL)
- return h;
- for (; p != NULL; p = p->next)
- {
- for (node q = p->next; q != NULL; q = q->next)
- {
- int temp;
- if (p->data > q->data)
- {
- temp = p->data;
- p->data = q->data;
- q->data = temp;
- }
- }
- }
- return h;
- }
- node reverse(node h) //逆置链表
- {
- node p, q;
- p = h->next;
- h->next = NULL;
- while (p)
- {
- q = p;
- p = p->next;
- q->next = h->next;
- h->next = q;
- }
- return h;
- }
- void destroy_List(node head) //销毁链表
- {
- if (NULL == head)
- {
- return;
- }
- if (NULL == head->next)
- {
- free(head);
- head = NULL;
- return;
- }
- node p = head->next;
- while (NULL != p)
- {
- node tmp = p;
- p = p->next;
- free(tmp);
- }
- free(head);
- head = NULL;
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- cout << "---------------构造链表-------------" << endl;
- node h = back_creat();
- cout << "---------------打印链表-------------" << endl;
- print(h);
- //cout << "-----------删除指定值后打印链表-----" << endl;
- //h = delete_node(h, 2);
- //print(h);
- cout << "-----------排序后打印链表------------" << endl;
- h = sort(h);
- print(h);
- cout << "-----------逆置后打印链表------------" << endl;
- h = reverse(h);
- print(h);
- destroy_List(h);
- return 0;
- }
运行结果:

c++学习笔记—单链表基本操作的实现的更多相关文章
- C++ 单链表基本操作
链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...
- [Golang学习笔记] 08 链表
链表(Linked list)是一种常见数据结构,但并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针. 由于不必须按顺序存储,链表在插入的时候可以达到O(1),比顺序表快得多,但是查 ...
- linux C学习笔记03--单链表
单链表一直是程序员的基础,我也来复习下,下面是link.c中的代码,供main.c 调用,代码很简单,单链表的插入,删除,查找和遍历输出, #include <stdio.h> #incl ...
- C++ 数据结构学习二(单链表)
模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespa ...
- C++面试笔记--单链表
1.编程实现单链表删除节点. 解析:如果删除的是头节点,如下图: 则把head指针指向头节点的下一个节点.同时free p1,如下图所示: 如果删除的是中间节点,如下图所示: 则用p2的n ...
- C++学习笔记48:链表的基本操作
//链表的基本操作 //生成链表,插入结点,查找结点,删除结点,遍历链表,清空链表 //链表类模板 //LinkedList.h #ifndef LINKEDLIST_H #define LINKED ...
- 学习笔记之NodeJs基本操作
nodejs安装见文章:windows下安装node.js及less 运行js文件:node xxx.js 调用http模块,并指定端口为3000,向客户端输出<h1>Node.js< ...
- Python学习笔记020——数据库基本操作
本数据库的操作是Linux虚拟机平台下进行的 1 启动和链接MySQL服务 1.1 服务端 (1)查看服务状态 sudo /etc/init.d/mysql stauts (2)启动服务端 sudo ...
- C语言学习016:单链表
#include <stdio.h> //定义一个链表,链表是一种递归结构,在定义的时候必须要给结构起一个名字 typedef struct folder{ int level; char ...
随机推荐
- Numpy 用于数组的文件输入和输出
将数组以二进制格式保存 np.save 和np.load 是读写磁盘数组数据的两个主要函数.默认情况下,数组是以未压缩的原始二进制格式进行保持在扩展名 为.npy的文件中的 如果文件路径末尾没有扩展名 ...
- C#的publisher与subscriber,事件发布者与订阅者
说明:示例借鉴自这里,但原版很不友好,于是修改了下,一目了然. 直接上代码: using System; using System.Collections.Generic; using System. ...
- 用Python中的tkinter模块作图
tkinter 可以用来创建完整的应用程序,比如简单的字处理软件,还有简单的绘图软件. 一.创建一个可以点的按钮 用tkinter创建一个带按钮的简单程序,代码如下: >>> fro ...
- Python——os(一)进程参数
python的os模块提供了一种使用操作系统相关函数的通用手段,如果只是想读或写文件请移步 open(),向操作路径请查阅 os.path 模块,如果想要读取命令行中所有文件里的所有行请查阅 file ...
- java-信息安全(一)-BASE64,MD5,SHA,HMAC,RIPEMD算法
概述 信息安全基本概念: BASE64 编码格式 Base58 编码 MD5(Message Digest algorithm 5,信息摘要算法) SHA(Secure Hash Algorithm, ...
- 使用redis镜像
运行容器 runoob@runoob:~/redis$ docker run -p : -v $PWD/data:/data -d redis:3.2 redis-server --appendonl ...
- 有限状态机(FSM)的Java 演示
本文从简单的样例入手,逐步演变成很复杂的程序. 在简明 状态模式(5.8)中,状态之间的变换由外界控制,或者说.多种状态是切割的.无关的.状态模式最有趣的地方正是讨论其状态的变迁. 1.引子 空调(a ...
- Java高级面试题及答案
List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...
- 8 map的用法
what's map go里面的map和python字典差不多. 类似其他语言中的哈希表或者字典,以key-value的形式存储的数据 key必须是支持==或者!=比较运算的类型,不可以是函数.map ...
- php5.4安装fileinfo扩展
Fileinfo 扩展是libmagic库的一个封装,可以用来获得文件的一些信息,如MIME类型 安装php_fileinfo扩展 1.windows 用phpinfo()查看php版本 下载 选择合 ...