复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98#include <iostream> #include <stdio.h> using namespace std; typedef struct LNode{ int data; struct LNode* next; }LNode,*Linklist; // 头插法建立单链表 void HeadInsert(Linklist &L){ int x; L = (Linklist)malloc(sizeof(LNode)); //申请头节点 L->next = NULL; cout<<"请输入链表每一项,当输入为999时完成链表:"; cin>>x; while (x!=999) { LNode *s = new LNode; s->data=x; s->next = L->next; L->next = s; cin>>x; } } //尾插法建立单链表 void Tailinsert(Linklist &L){ LNode *p; int x; L = (Linklist)malloc(sizeof(LNode)); p=L; cout<<"请输入链表每一项,当输入为999时完成链表:"; cin>>x; while (x!=999) { LNode *s =new LNode; s->data =x; p->next = s; p = s; cin>>x; } p->next=NULL; } //按序号查找节点值 LNode* GetElem(Linklist L,int i){ int j= 1; LNode *p = L->next; if (i==0) { return L; } if (i<1) { return NULL; } while (p&&j<i) { p=p->next; j++; } return p; } //按值查找 LNode* GetElemByint(Linklist &L,int x){ LNode *p=L->next; while (p!=NULL&&p->data!=x) { p=p->next; } return p; } //插入节点(前插)先查找前驱再插入 void InsertLink(Linklist &L,int i,int x){ LNode *p = GetElem(L, i-1); LNode *s = new LNode; s->next = p->next; p->next = s; s->data=x; } //删除节点 void DelatePoint(Linklist &L,int i){ LNode *p = GetElem(L, i-1); LNode *s =new LNode; s=p->next; p->next=s->next; free(s); } int GetLength(Linklist L){ LNode *p = L->next; int count = 0; while (p!=NULL) { p=p->next; count++; } return count; } // 打印输出链表 void PrintfLinklist(Linklist L){ LNode *p = L->next; while (p!=NULL) { cout<<p->data<<" "; p = p->next; } cout<<endl; }
最后
以上就是淡然小馒头最近收集整理的关于C++单链表简单操作的全部内容,更多相关C++单链表简单操作内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复