-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkList.cpp
174 lines (154 loc) · 3.22 KB
/
LinkList.cpp
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct LList{
int num;
LList *next;
}LList;
LList * initList(LList *head){
LList *p1 , *p2;
p1 = p2 = (LList *)malloc(sizeof(LList));
cout << "输入初始值,以0结尾" <<endl;
cin >> p1->num;
p1->next = NULL;
while(p1->num != 0){
if(head == NULL){
head = p1;
}else{
p2->next = p1;
}
p2 = p1;
p1 = (LList *)malloc (sizeof(LList));
cin >> p1->num;
}
free(p1);
p2->next = NULL;
return head;
}
void printList(LList *llist){
LList *temp = llist;
cout << " " <<endl;
while(temp != NULL){
cout << temp->num << endl;
temp = temp->next;
}
cout << "打印完成" <<endl;
}
int GetElem(LList *llist , int pos){
int i = 1;
LList *temp = llist;
if(pos <= 0){
return -1;
}
while(temp != NULL){
if(i == pos){
return temp->num;
}
temp = temp->next;
i ++;
}
return -1;
}
int DeleteList(LList *llist , int pos){
LList *temp = llist;
int i = 1;
if(pos <= 0){
return -1;
}
while(temp != NULL){
if(i == pos -1){
temp ->next = temp->next->next;
return 0;
}
temp = temp->next;
i ++;
}
return -1;
}
/**
* 删除链表中所有值为num的数据
*/
void DeleteList2(LList *llist , int num){
while(llist){
if(llist->num == num){
LList *t = llist->next;
llist->num = llist->next->num;
llist->next = llist->next->next;
}
llist = llist->next;
}
}
/**
* 逆转矩阵(**)
*/
void Contrary(LList **llist){
LList *current = *llist;
LList *left = NULL;
LList *right = NULL;
while(current){
right = current->next;
current->next = left;
left = current;
current = right;
}
*llist = left;
}
/**
* 插入数据
*/
int InsertList(LList *llist , int pos , int n){
LList *temp = llist;
int i = 1;
if(pos <= 0){
return -1;
}
while(temp != NULL){
if(i == pos - 1){
LList *t = (LList *)malloc(sizeof(LList));
t->num = n;
t->next = temp->next;
temp->next = t;
return 0;
}
temp = temp ->next;
i ++;
}
return -1;
}
int main(){
int pos , i;
LList *head = NULL;
LList *llist = NULL;
llist = initList(head);
// cout << "查找的位置" << endl;
// cin >> pos;
// if(-1 == (i = GetElem(llist , pos))){
// cout << "错误位置";
// }else{
// cout << i << endl;
// }
// cout << "插入的位置与值" <<endl;
// cin >> pos;
// cin >> i;
//
// if(-1 == InsertList(llist , pos , i)){
// cout << "位置错误" <<endl;
// }else{
// printList(llist);
// }
// cout << "输入删除的位置" <<endl;
// cin >>i;
// if(-1 == DeleteList(llist , i)){
// cout << "错误位置" <<endl;
// }else{
// cout << "删除成功" << endl;
// printList(llist);
// }
//cout << "输入要删除的值:" <<endl;
//cin >> i;
//DeleteList2(llist , i);
//printList(llist);
Contrary(&llist);
printList(llist);
return 0;
}