c++学习笔记—单链表基本操作的实现

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

IDE:vs2013

具体实现代码如下:

[cpp] view plaincopy

  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. }

运行结果:

时间: 2024-08-05 06:56:36

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

数据结构学习之单链表基本操作

数据结构学习之单链表基本操作 0x1 前言 今天实验课,学习了下单链表的写法,这里记录下. 0x2 正文 题目要求如下: 本实验的单链表元素的类型为char,完成如下实验要求: (1)初始化单链表h (2)采用尾插法依次插入a.b.c.d.e (3)输出单链表h (4)输出单链表h的长度 (5)判断单链表h是否为空 (6)输出单链表h的第3个元素 (7)输出元素a的逻辑位置 (8)在第4个元素位置上插入元素f (9)输出单链表h (10)删除单链表h的第3个元素 (11)输出单链表h (12)释

数据结构学习之双链表基本操作

数据结构学习之双链表基本操作 0x1 前言 今天实验课,学习了下双链表的写法,这里记录下. 0x2 正文 题目要求如下: 本实验的双链链表元素的类型为char,完成如下实验要求: (1)初始化单链表h (2)采用尾插法依次插入a.b.c.d.e (3)输出单链表h (4)输出单链表h的长度 (5)判断单链表h是否为空 (6)输出单链表h的第3个元素 (7)输出元素a的逻辑位置 (8)在第4个元素位置上插入元素f (9)输出单链表h (10)删除单链表h的第3个元素 (11)输出单链表h (12)

MySQL学习笔记-安装和基本操作

MySQL学习笔记-安装和基本操作   1.安装MySQL 1 # yum -y install mysql mysql-server mysql-devel 添加/etc/my.conf配置: 1 vi /etc/my.conf2 [mysqld]3 default-character-set=utf84 character_set_server=utf85 [client]6 default-character-set=utf8 2.启动和停止MySQL服务: # service mysql

数据结构实验报告-实验一 顺序表、单链表基本操作的实现

实验一    顺序表.单链表基本操作的实现   l  实验目的 1.顺序表 (1)掌握线性表的基本运算. (2)掌握顺序存储的概念,学会对顺序存储数据结构进行操作. (3)加深对顺序存储数据结构的理解,逐步培养解决实际问题的编程能力. l  实验内容 1. 顺序表 1.编写线性表基本操作函数: (1)InitList(LIST *L,int ms)初始化线性表: (2)InsertList(LIST *L,int item,int rc)向线性表的指定位置插入元素: (3)DeleteList1

[MongoDB]学习笔记--基本操作

读取 db.collection.find() db.users.find( { age: {$gt: 18}}, {name: 1, address: 1} ).limit(5).sort({age:1}) users是collection名字,从users中查找; age是query criteria,筛选结果,代表查找name字段的值比18大的; name是projection,筛选列(1代表存在, 0代表不存在),代表返回结果中包含name,address,_id(默认包含字段)字段的值

数据结构复习--java实现单链表基本操作

单链表的基本操作包括建立单链表.查找运算(按序查找和按值查找).插入运算(前插和后插)和删除运算.下面给出具体的java实现程序: package com.zpp.test; //首先创建一个节点类 public class Node { private Node next; //指针域 private int data;//数据域 public Node( int data) { this. data = data; } } package com.zpp.test; public class

单链表基本操作

//头文件 #pragma once #include <stdio.h> #include <assert.h> #include <malloc.h> #include <stdlib.h> typedef int DateType; typedef struct LinkNode {  DateType _data;  struct  LinkNode* _next; } LinkNode; void PrintList(LinkNode* pHead

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

1.编程实现单链表删除节点.       解析:如果删除的是头节点,如下图: 则把head指针指向头节点的下一个节点.同时free p1,如下图所示: 如果删除的是中间节点,如下图所示: 则用p2的next指向p1的next同时,free p1 ,如下图所示: 2.编写程序实现单链表的插入.       解析:单链表的插入,如下图所示: 如果插入在头结点以前,则p0的next指向p1,头节点指向p0,如下图所示: 如果插入中间节点,如下图所示: 则先让p2的next指向p0,再让p0指向p1,如

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

模板类 //LinkList.h 单链表#ifndef LINK_LIST_HXX#define LINK_LIST_HXX#include <iostream>using namespace std; template<class T>struct Node{ T data; Node * next;}; template<class T>class LinkList{ public: LinkList(); //无参构造函数,建立只有头结点的空链表 LinkList