# C链表一——链表增删改查以及初始化的头插法与尾插法,链表操作二——合并，逆转

```struct Node
{
ElemType data_;
Node *next_;
};```

```//结点声明
typedef struct tag
{
int Nnum_;
struct tag *Nnext_;
}Node, *pNode;

//打印函数
{
{
}
}```

View Code

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

typedef struct tag
{
int Nnum_;
struct tag *Nnext_;
}Node, *pNode;

{
{
}
}

{
pNode pNew = NULL;
pNode pTail = NULL;

while( size > 0)
{
//申请内存
pNew = (pNode)malloc(sizeof(Node)); //注意这里为何不用pNode而用Node，因为sizeof(pNode) = 4
//赋值
pNew->Nnum_ = rand()%1000;
//插入链表
{
pTail = pNew;
}
else//不为空
{
pTail->Nnext_ = pNew ; //连接新的结点
pTail = pNew; //改名字
}

size --;
}
}

int main(int argc, char const *argv[])
{
srand(time(NULL));

return 0;
}```

View Code

``` 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 typedef struct tag
7 {
8     int Nnum_;
9     struct tag *Nnext_;
10 }Node, *pNode;
11
12
14 {
16     {
19     }
20 }
21
23 {
24     pNode pNew = NULL;
25
26     while( size > 0)
27     {
28         //申请内存
29         pNew = (pNode)malloc(sizeof(Node)); //注意这里为何不用pNode而用Node，因为sizeof(pNode) = 4
30         //赋值
31         pNew->Nnum_ = rand()%1000;
32         //插入链表
34         {
36         }
37         else
38         {
41         }
42         size --;
43     }
44 }
45
46 int main(int argc, char const *argv[])
47 {
48     srand(time(NULL));
51
53     return 0;
54 }```

View Code

``` 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <assert.h>
6
7 typedef struct tag
8 {
9     int Nnum_;
10     struct tag *Nnext_;
11 }Node, *pNode;
12
14 {
15     int i= 0;
17     {
20         ++i;
21     }
22     printf("i:%d\n", i);
23 }
24
26 {
27     pNode pNew = NULL;
28     pNode pTail = NULL;
29
30     while( size > 0)
31     {
32         //申请内存
33         pNew = (pNode)malloc(sizeof(Node)); //注意这里为何不用pNode而用Node，因为sizeof(pNode) = 4
34         //赋值
35         pNew->Nnum_ = rand()%50;
36         //插入链表
38         {
40             pTail = pNew;
41         }
42         else//不为空
43         {
44             pTail->Nnext_ = pNew ; //连接新的结点
45              pTail = pNew; //改名字
46         }
47         size --;
48     }
49 }
50
51 //该函数只能删除一个相同的结点，如果想要删除所有与num相等的结点值，则直接在while循环里执行删除操作即可
53 {
54     assert( phead != NULL); //非空
55     pNode pPre = NULL;
57
58     while( pCur != NULL) //遍历链表找到要删除元素对的位置
59     {
60         if( pCur->Nnum_ == num) //找到
61             break;
62         else
63             {
64                 pPre = pCur;
65                 pCur = pCur->Nnext_;
66             }
67     }
68
69     if( pPre == NULL)//删除的是头结点
70     {
73         free(tmp);
74         tmp = NULL;
75     }
76     else if(pCur != NULL)//删除的是其余结点
77     {
78         pNode tmp = pCur;
79         pPre->Nnext_ = pCur->Nnext_;
80         free(tmp);
81         tmp = NULL;
82     }
83     else //没找到
84         printf("find no element\n");
85 }
86
87 int main(int argc, char const *argv[])
88 {
89     srand(time(NULL));
93
94
97
98     return 0;
99 }```

View Code

``` 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5
6 typedef struct tag
7 {
8     int Nnum_;
9     struct tag *Nnext_;
10 }Node, *pNode;
11
12
14 {
16     {
19     }
20     printf("\n");
21 }
22
24 {
25     pNode pNew = NULL;
26     pNode pCur, pPre;
27
28     while( size > 0 )
29     {
30         pNew = (pNode)malloc(sizeof(Node));
31         pNew->Nnum_ = rand()%1000;
32
33         pPre = NULL;
35         while( pCur != NULL)
36         {
37             if( pNew->Nnum_ > pCur->Nnum_) //按照从小到大排序
38             {
39                 pPre = pCur;
40                 pCur = pCur->Nnext_;
41             }
42             else //找到位置
43                 break;
44         }
45         //找到的位置在头部
46         if( pPre == NULL)
47         {
50         }else//位置在中间或者尾部
51         {
52             pPre->Nnext_ = pNew;
53             pNew->Nnext_ = pCur;
54         }
55         size --;
56     }
57 }
58
59 int main(int argc, char const *argv[])
60 {
61     srand(time(NULL));
64
66
67     return 0;
68 }```

View Code

原文作者：Stephen_Hsu
原文地址: https://www.cnblogs.com/xfxu/p/4087707.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。