-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-链表线性表.cpp
56 lines (53 loc) · 1.16 KB
/
02-链表线性表.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
#include <cstdio>
using PtrToNode=struct LNode*;
struct LNode{
int Data;
PtrToNode Next;
};
using Position=PtrToNode;
using List=PtrToNode;
//查找
Position Find(List L,int X){
Position p=L;
while(p&&p->Data!=X)
p=p->Next;
return p;
}
//新建空表
List Empty(){
List L=new LNode;
L->Next=NULL;
return L;
}
//带头结点的插入
bool Insert(List L,int X,Position P){
//默认L有头结点
Position tmp, pre;
//查找P的前一个结点
for(pre=L;pre&&pre->Next!=P;pre=pre->Next);//不需要额外操作
if(pre==NULL){
printf("Insert Postion Error\n");
return false;
}
else{
tmp=new LNode;
tmp->Data=X;
tmp->Next=pre->Next;
pre->Next=tmp;
return true;
}
}
//带头结点的删除
bool Delete(List L,Position P){
Position pre;
for(pre=L;pre&&pre->Next!=P;pre=pre->Next);
if(pre==NULL||P==NULL){
printf("Positon Error\n");
return false;
}
else{
pre->Next=P->Next;
delete P;
return true;
}
}