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

IDE:vs2013

具体实现代码如下:

  1. #include "stdafx.h"
  2. #include <malloc.h>
  3. #include <iostream>
  4. using namespace std;
  5. typedef struct Lnode
  6. {
  7. int data;
  8. struct Lnode *next;
  9. }*node;
  10. node head_creat()   //头插法建立单链表
  11. {
  12. node head = (struct Lnode *)malloc(sizeof(struct Lnode));
  13. head->next = NULL;
  14. node p;
  15. int temp;
  16. while (cin >> temp)
  17. {
  18. p = (node)malloc(sizeof(struct Lnode));
  19. p->data = temp;
  20. p->next = head->next;
  21. head->next=p;
  22. }
  23. return head;
  24. }
  25. bool search(node h, int target)     //查找某元素是否在链表中
  26. {
  27. int flag = 0;
  28. node p = h->next;
  29. if (h->next == NULL)
  30. return false;
  31. for (; p != NULL;)
  32. {
  33. if (p->data == target)
  34. {
  35. flag = 1;
  36. break;
  37. }
  38. else
  39. p++;
  40. }
  41. if (flag)
  42. return true;
  43. else
  44. return false;
  45. }
  46. node back_creat()   //尾插法建立单链表
  47. {
  48. node head = (struct Lnode *)malloc(sizeof(struct Lnode));
  49. head->next = NULL;
  50. node p;
  51. node r = head;
  52. int temp;
  53. while (cin >> temp)
  54. {
  55. p = (node)malloc(sizeof(struct Lnode));
  56. p->data = temp;
  57. r->next=p;
  58. r = p;
  59. }
  60. r->next = NULL;
  61. return head;
  62. }
  63. void print(node h)    //打印单链表
  64. {
  65. node p = h->next;
  66. while (p)
  67. {
  68. cout << p->data << endl;
  69. p = p->next;
  70. }
  71. }
  72. node delete_node(node h,int del_val)     //删除指定值的节点
  73. {
  74. node p = h->next;
  75. node q = h;
  76. node r=NULL;
  77. while (p)
  78. {
  79. if (p->data == del_val)
  80. {
  81. r = p;
  82. p = p->next;
  83. q->next = p;
  84. free(r);
  85. }
  86. else
  87. {
  88. q = p;
  89. p = p->next;
  90. }
  91. }
  92. return h;
  93. }
  94. node sort(node h)     //对链表进行排序(降序)
  95. {
  96. node p=h->next;
  97. if (p->next == NULL)
  98. return h;
  99. for (; p != NULL; p = p->next)
  100. {
  101. for (node q = p->next; q != NULL; q = q->next)
  102. {
  103. int temp;
  104. if (p->data > q->data)
  105. {
  106. temp = p->data;
  107. p->data = q->data;
  108. q->data = temp;
  109. }
  110. }
  111. }
  112. return h;
  113. }
  114. node reverse(node h)  //逆置链表
  115. {
  116. node p, q;
  117. p = h->next;
  118. h->next = NULL;
  119. while (p)
  120. {
  121. q = p;
  122. p = p->next;
  123. q->next = h->next;
  124. h->next = q;
  125. }
  126. return h;
  127. }
  128. void destroy_List(node head)  //销毁链表
  129. {
  130. if (NULL == head)
  131. {
  132. return;
  133. }
  134. if (NULL == head->next)
  135. {
  136. free(head);
  137. head = NULL;
  138. return;
  139. }
  140. node p = head->next;
  141. while (NULL != p)
  142. {
  143. node tmp = p;
  144. p = p->next;
  145. free(tmp);
  146. }
  147. free(head);
  148. head = NULL;
  149. }
  150. int _tmain(int argc, _TCHAR* argv[])
  151. {
  152. cout << "---------------构造链表-------------" << endl;
  153. node h = back_creat();
  154. cout << "---------------打印链表-------------" << endl;
  155. print(h);
  156. //cout << "-----------删除指定值后打印链表-----" << endl;
  157. //h = delete_node(h, 2);
  158. //print(h);
  159. cout << "-----------排序后打印链表------------" << endl;
  160. h = sort(h);
  161. print(h);
  162. cout << "-----------逆置后打印链表------------" << endl;
  163. h = reverse(h);
  164. print(h);
  165. destroy_List(h);
  166. return 0;
  167. }

运行结果:

c++学习笔记—单链表基本操作的实现的更多相关文章

  1. C++ 单链表基本操作

    链表一直是面试的高频题,今天先总结一下单链表的使用,下节再总结双向链表的.本文主要有单链表的创建.插入.删除节点等. 1.概念 单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数 ...

  2. [Golang学习笔记] 08 链表

    链表(Linked list)是一种常见数据结构,但并不会按线性的顺序存储数据,而是在每一个节点里存到下一个节点的指针. 由于不必须按顺序存储,链表在插入的时候可以达到O(1),比顺序表快得多,但是查 ...

  3. linux C学习笔记03--单链表

    单链表一直是程序员的基础,我也来复习下,下面是link.c中的代码,供main.c 调用,代码很简单,单链表的插入,删除,查找和遍历输出, #include <stdio.h> #incl ...

  4. C++ 数据结构学习二(单链表)

    模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespa ...

  5. C++面试笔记--单链表

    1.编程实现单链表删除节点.       解析:如果删除的是头节点,如下图: 则把head指针指向头节点的下一个节点.同时free p1,如下图所示: 如果删除的是中间节点,如下图所示: 则用p2的n ...

  6. C++学习笔记48:链表的基本操作

    //链表的基本操作 //生成链表,插入结点,查找结点,删除结点,遍历链表,清空链表 //链表类模板 //LinkedList.h #ifndef LINKEDLIST_H #define LINKED ...

  7. 学习笔记之NodeJs基本操作

    nodejs安装见文章:windows下安装node.js及less 运行js文件:node xxx.js 调用http模块,并指定端口为3000,向客户端输出<h1>Node.js< ...

  8. Python学习笔记020——数据库基本操作

    本数据库的操作是Linux虚拟机平台下进行的 1 启动和链接MySQL服务 1.1 服务端 (1)查看服务状态 sudo /etc/init.d/mysql stauts (2)启动服务端 sudo ...

  9. C语言学习016:单链表

    #include <stdio.h> //定义一个链表,链表是一种递归结构,在定义的时候必须要给结构起一个名字 typedef struct folder{ int level; char ...

随机推荐

  1. (笔记)Mysql命令update set:修改表中的数据

    update set命令用来修改表中的数据. update set命令格式:update 表名 set 字段=新值,… where 条件; 举例如下:mysql> update MyClass ...

  2. 第三百三十六节,web爬虫讲解2—urllib库中使用xpath表达式—BeautifulSoup基础

    第三百三十六节,web爬虫讲解2—urllib库中使用xpath表达式—BeautifulSoup基础 在urllib中,我们一样可以使用xpath表达式进行信息提取,此时,你需要首先安装lxml模块 ...

  3. 嵌入式开发之makefile---交叉编译静态库和动态库的生成和调用

    c和cpp 混合的动态库生成: $(LIBSO): $(COBJS) $(CPPOBJS) $(CPP) -shared -o $@ $^ $(LIBS) ////////////////////// ...

  4. Oracle EM错误,java.lang.Exception: Exception in sending Request :: null 分类: Oracle 2015-07-08 21:24 44人阅读 评论(0) 收藏

    操作系统:Win7 64bit Oracle: 10.2.0.1.0 很久没有使用EM了,打开一看,居然报错了,出现java.lang.Exception: Exception in sending ...

  5. par函数mgp 参数-控制坐标轴的位置

    mgp 参数的值为长度为3的一个向量,默认值为 c(3, 1, 0); 3个数值控制的元素不同 1) 第一个数值:3, 控制xlab 和  ylab的位置 示例用法: par(mfrow = c(1, ...

  6. Hadoop学习笔记——WordCount

    1.在IDEA下新建工程,选择from Mevan GroupId:WordCount ArtifactId:com.hadoop.1st Project name:WordCount 2.pom.x ...

  7. CocoaPods:说点关于它的

    CocoaPods安装和使用教程 安装及使用方法,这里有现成的,很细致,不再赘述(发音:zhuìshù,敲半天ao'shu,找不到这个词 =.=)   记录一下遇到的问题 1.CocoaPods 版本 ...

  8. 怎样解决Java Web项目更改项目名后报错

    作为企业级开发最流行的工具,用Myeclipse开发java web程序无疑是最合适的,有时候,我们需要web工程的项目名,单方面的改动工程的项目名是会报错的,那么该如何改web工程项目名呢? 简 单 ...

  9. CentOS7 防火墙配置(关闭)

    CentOS7 的防火墙配置跟曾经版本号有非常大差别,经过大量尝试,最终找到解决这个问题的关键 CentOS7这个版本号的防火墙默认使用的是firewall.与之前的版本号使用iptables不一样. ...

  10. gtest运行小析

    Gtest是google推出的C++测试框架,本篇文档,从整体上对Gtest的运行过程中的关键路径进行分析和梳理. 分析入口 新建一个最简单的测试工程,取名为source_analyse_proj,建 ...