【C语言】单链表的所有操作的实现(包括PopBack、PushBack、PopFront、PushFront、Insert)

http://blog.csdn.net/hanjing_1995/article/details/51539563

[cpp] view plain copy
  1. #define _CRT_SECURE_NO_WARNINGS 1  
  2. #include<iostream>  
  3. using namespace std;  
  4.   
  5.   
  6. //单链表的实现  
  7. #include<assert.h>  
  8.   
  9.   
  10. typedef int DataType;  
  11.   
  12. typedef struct SListNode  
  13. {  
  14.     DataType _data;  
  15.     struct SListNode* _next;  
  16. }SListNode;  
  17.   
  18.   
  19. SListNode* _CreateNode(DataType x)  
  20. {  
  21.     SListNode* head = (SListNode*)malloc(sizeof(SListNode));  
  22.     head->_data = x;  
  23.     head->_next = NULL;  
  24.     return head;  
  25. }  
  26.   
  27.   
  28. void PushBack(SListNode*& head, DataType x)  
  29. {  
  30.     if (head == NULL)  
  31.     {  
  32.         head = _CreateNode(x);  
  33.         head->_next = NULL;  
  34.     }  
  35.     else  
  36.     {  
  37.         SListNode* cur = head;  
  38.         while (cur->_next != NULL)  
  39.         {  
  40.             cur = cur->_next;  
  41.         }  
  42.         cur->_next = _CreateNode(x);  
  43.     }  
  44.       
  45. }  
  46.   
  47.   
  48. void PopBack(SListNode*& head)  
  49. {  
  50.     if (head == NULL)  
  51.     {  
  52.         return;  
  53.     }   
  54.     else if (head->_next == NULL)  
  55.     {  
  56.         free(head);  
  57.         head = NULL;  
  58.     }  
  59.     else  
  60.     {  
  61.         SListNode* cur = head;  
  62.         SListNode* next = head;  
  63.   
  64.         while (cur)  
  65.         {  
  66.             next = cur->_next;  
  67.             if (next != NULL && next->_next == NULL)  
  68.             {  
  69.                 free(next);  
  70.                 cur->_next = NULL;  
  71.                 return;  
  72.             }  
  73.   
  74.             cur = cur->_next;  
  75.         }  
  76.     }  
  77.       
  78. }  
  79.   
  80.   
  81. void PushFront(SListNode*& head,DataType x)  
  82. {  
  83.     if (head == NULL)  
  84.     {  
  85.         head = _CreateNode(x);  
  86.     }  
  87.     else  
  88.     {  
  89.         SListNode* pcur = _CreateNode(x);  
  90.         pcur->_next = head;  
  91.         head = pcur;  
  92.     }  
  93. }  
  94.   
  95.   
  96. void PopFront(SListNode*& head)  
  97. {  
  98.     if (head == NULL)  
  99.     {  
  100.         return;  
  101.     }  
  102.     else if (head->_next == NULL)  
  103.     {  
  104.         free(head);  
  105.         head = NULL;  
  106.     }  
  107.     else  
  108.     {  
  109.         SListNode* del = head;  
  110.         SListNode* next = head->_next;  
  111.         free(del);  
  112.         del = NULL;  
  113.         head = next;  
  114.     }  
  115. }  
  116.   
  117.   
  118. void Insert(SListNode* head,int pos,DataType x)  
  119. {  
  120.     assert(pos >= 0);  
  121.     SListNode* cur = head;  
  122.     while (--pos && cur)  
  123.     {              
  124.         cur = cur->_next;          
  125.     }  
  126.     if (pos > 0)  
  127.     {  
  128.         printf("pos位置大于链表长度!\n");  
  129.         return;  
  130.     }  
  131.     SListNode* newcur = _CreateNode(x);  
  132.     if (cur->_next)  
  133.     {  
  134.         SListNode* next = cur->_next;  
  135.       
  136.         cur->_next = newcur;  
  137.         newcur->_next = next;  
  138.     }  
  139.     else if (cur->_next == NULL)  
  140.     {  
  141.         cur->_next = newcur;  
  142.     }  
  143. }  
  144.   
  145.   
  146. size_t Length(SListNode*& head)  
  147. {  
  148.     size_t count = 0;  
  149.     SListNode* cur = head;  
  150.     while (cur)  
  151.     {  
  152.         count++;  
  153.         cur = cur->_next;  
  154.     }  
  155.     return count;  
  156. }  
  157.   
  158.   
  159. void PrintSList(SListNode*& head)  
  160. {  
  161.     if (head == NULL)  
  162.     {  
  163.         return;  
  164.     }  
  165.     SListNode* cur = head;  
  166.     while (cur)  
  167.     {  
  168.         printf("%d->", cur->_data);  
  169.         cur = cur->_next;  
  170.     }  
  171.     printf("\n");  
  172. }  
  173.   
  174.   
  175. void Test()  
  176. {  
  177.     SListNode* sList =NULL;  
  178.     PushBack(sList, 1);  
  179.     PushBack(sList, 2);  
  180.     PushBack(sList, 3);  
  181.     PushBack(sList, 4);  
  182.     PushBack(sList, 5);  
  183.     PrintSList(sList);  
  184.   
  185.     PopBack(sList);  
  186.     PrintSList(sList);  
  187.   
  188.     PushFront(sList, 0);  
  189.     PrintSList(sList);  
  190.   
  191.     PopFront(sList);  
  192.     PrintSList(sList);  
  193.   
  194.     Insert(sList, 3, 10);  
  195.     PrintSList(sList);  
  196.   
  197.     int ret = Length(sList);  
  198.     printf("单链表长度为:%d\n", ret);  
  199. }  
  200.   
  201.   
  202. int main()  
  203. {  
  204.     Test();  
  205.     system("pause");  
  206.     return 0;  
  207. }  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384154.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Shuffle'm Up——简单模拟

【题目描述】 A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several diff…

C++ explicit关键字详解

http://www.cnblogs.com/ymy124/p/3632634.html 首先, C中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意思是隐藏的,类构造函数默认情况下即声明为implicit(隐式). 那么显示声…

Fire!——两个BFS

【题目描述】 【题目分析】 看到题目后很清楚是两个BFS&#xff0c;可是我觉得对于火的BFS可以转换成判断&#xff0c;我的做法是将火的位置全部记录下来&#xff0c;然后判断某个位置距离每个火的步数是否小于当前步数&#xff0c;可是错了&#xff0c;还不清楚为什么&#x…

函数调用过程(栈桢)

栈桢 首先来看一段代码 #include<stdio.h> int add(int x, int y) {int z x y;return z; } int main() {int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0; } 此处是为了给a,b分别开辟空间,这时栈桢如图所示 两条push命令将a,b变…

C++智能指针简单剖析

http://blog.csdn.net/lanxuezaipiao/article/details/41603883 导读 最近在补看《C Primer Plus》第六版&#xff0c;这的确是本好书&#xff0c;其中关于智能指针的章节解析的非常清晰&#xff0c;一解我以前的多处困惑。C面试过程中&#xff0c;很多面试官都喜欢问智能指针相…

非常可乐——BFS

【题目描述】 大家一定觉的运动以后喝可乐是一件很惬意的事情&#xff0c;但是seeyou却不这么认为。因为每次当seeyou买了可乐以后&#xff0c;阿牛就要求和seeyou一起分享这一瓶可乐&#xff0c;而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子&#xff0c;它们的容…

整型数据存储

//代码1 #include<stdio.h> int main() {char a -1;signed char b -1;unsigned char c -1;printf("a %d, b %d, c %d", a, b, c);return 0; } 1000 0000 0000 0001 -> -1源码 1111 1111 1111 1110 -> -1反码 1111 1111 1111 1111 -> -1补码 对于…

聊聊gcc参数中的-I, -L和-l

http://blog.csdn.net/stpeace/article/details/49408665 在本文中&#xff0c; 我们来聊聊gcc中三个常见的参数&#xff0c; 也即-I, -L和-l 一. 先说 -I (注意是大写的i) 我们先来看简单的程序&#xff1a; main.c: [cpp] view plaincopy #include <stdio.h> #incl…

Pots——BFS

【题目描述】 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j;…

HDU - 4578Transformation——线段树+区间加法修改+区间乘法修改+区间置数+区间和查询+区间平方和查询+区间立方和查询

【题目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …

[C++基础]034_C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)

http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/18/2728753.html 1. 主版本模板类 首先我们来看一段初学者都能看懂&#xff0c;应用了模板的程序&#xff1a; 1 #include <iostream>2 using namespace std;3 4 template<class T1, class T2>5 clas…

自定义类型: 结构体,枚举,联合

1.结构体 个人认为结构体和数组特别相似&#xff0c;只不过结构体和数组的区别在于结构体的成员可以是不同类型&#xff0c;而数组成员类型是相同的。 &#xff08;1&#xff09;结构体的声明 struct tag {成员列表//至少得有一个成员 }值列表;//值列表可以省略 struct {int a…

详解C++中的函数调用和下标以及成员访问运算符的重载

http://www.jb51.net/article/78436.htm 这篇文章主要介绍了详解C中的函数调用和下标以及成员访问运算符,讲到了这些二元运算符使用的语法及重载,需要的朋友可以参考下函数调用 使用括号调用的函数调用运算符是二元运算符。 语法 ?1primary-expression ( expression-list )备…

A计划——BFS

【题目描述】 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后&#xff0c;而今&#xff0c;不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主&#xff0c;因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚&#xff0c;告招天下勇士…

使用openssl的md5库

http://blog.csdn.net/sinat_35297665/article/details/78244523 在linux机器上&#xff0c;有一个命令可以计算出文件的md5值&#xff0c;那就是md5sum&#xff0c;如果没有的话&#xff0c;就需要安装RPM包&#xff1a;coreutils。 现在我们使用openssl的库也可以方便的计算出…

主席树入门

今天学习了一下主席树&#xff08;名字这么强的嘛&#xff09; 虽然直接理解起来不容易&#xff0c;但是这种解决问题的思想其实并不陌生。 我们可以首先来看维护整个区间第K大的线段树 我们将[l,r]区间内数字的多少用线段树进行维护&#xff0c;这样的话为了求取区间第k大&…

Socket网络编程--小小网盘程序(1)

http://www.cnblogs.com/wunaozai/p/3886588.html 这个系列是准备讲基于Linux Socket进行文件传输。简单的文件传输就是客户端可以上传文件&#xff0c;可以从服务器端下载文件。就这么两个功能如果再加上身份验证&#xff0c;就成了FTP服务器了&#xff0c;如果对用户的操作再…

使用 Verdaccio 构建自己的私有 npm 仓库

前言 无论你是公司的开发者&#xff0c;还是个人开发者&#xff0c;你可能都听说过或者使用过 npm&#xff0c;这是一个使用广泛的 JavaScript 包管理器。但是&#xff0c;你是否遇到过以下的问题&#xff1a;你需要一个私有的包存放地方&#xff0c;或者你需要在离线环境下使…

HDU - 4348To the moon——主席树+区间修改

HDU - 4348To the moon 【题目描述】 【题目分析】 题目中说明每次更新后时间都会加1&#xff0c;而且还会需要查询以前的区间&#xff0c;还会需要返回以前的时间&#xff0c;所以是很裸的主席树。区间查询的话我们同样需要用到lazy标记 通过这道题我发现线段树的操作还是很灵…

进入一个目录需要那些权限

1.文件访问者的分类 文件的访问者具体可分为以下几类&#xff1a; (1)拥有者 (2)组拥有者 (3)其他用户 这些都代表什么意思呢&#xff1f; 其中r表示只读&#xff0c;w表示只写&#xff0c;x表示可执行&#xff0c;第一个字母代表了文件的类型&#xff0c;其中文件类型可以分为…