struct Node
{
ElementType Element
;
Node
* Next
;
};
typedef struct Node
*PtrToNode
;
typedef PtrToNode List
;
typedef PtrToNode Position
;
List
CreatList();
int IsEmpty(List L
);
int IsLast(Position P
,List L
);
int Find(ElementType X
,List L
);
Position
FindPrevious(ElementType X
,List L
);
void Delete(ElementType X
,List L
);
void Insert(List L
,int i
,ElementType X
);
void push_back(List L
,ElementType X
);
int GetLength(List L
);
void PrintList(List L
);
List
CreatList(){
List L
;
L
=(PtrToNode
)malloc(sizeof(struct Node
));
L
->Next
=NULL;
return L
;
}
int IsEmpty(List L
)
{
return L
->Next
==NULL;
}
int IsLast(Position P
,List L
)
{
return P
->Next
==NULL;
}
int Find(ElementType X
,List L
)
{
Position P
;
int i
=1;
P
=L
->Next
;
while(P
!=NULL&&P
->Element
!=X
){
P
=P
->Next
;
i
++;
}
if(P
==NULL){
cout
<<"未找到该元素";
return -1;
}
else
return i
;
}
Position
FindPrevious(ElementType X
,List L
)
{
Position P
;
P
=L
;
while(P
->Next
!=NULL&&P
->Next
->Element
!=X
)
P
=P
->Next
;
return P
;
}
void Delete(List L
,int i
)
{
Position P
=L
;
Position TmpCell
;
int j
=0;
while(P
->Next
&&j
<i
-1){
P
=P
->Next
;
++j
;
}
if(!(P
->Next
||j
>i
-1))
return;
TmpCell
=P
->Next
;
P
->Next
=TmpCell
->Next
;
free(TmpCell
);
}
void Insert(List L
,int i
,ElementType X
)
{
Position P
=L
;
Position TmpCell
;
int j
=0;
while(P
&&j
<i
-1){
P
=P
->Next
;
++j
;
}
if(!P
||j
>i
-1)
return;
TmpCell
=(Position
)malloc(sizeof(struct Node
));
TmpCell
->Element
=X
;
TmpCell
->Next
=P
->Next
;
P
->Next
=TmpCell
;
}
void push_back(List L
,ElementType X
){
PtrToNode L1
=L
;
while(L1
->Next
!=NULL){
L1
=L1
->Next
;
}
Position TmpCell
;
TmpCell
=(Position
)malloc(sizeof(struct Node
));
TmpCell
->Element
=X
;
L1
->Next
=TmpCell
;
TmpCell
->Next
=NULL;
}
int GetLength(List L
){
int res
;
while(L
!=NULL){
res
++;
L
=L
->Next
;
}
return res
;
}
void PrintList(List L
){
List P
=L
;
while(P
!=NULL){
cout
<<P
->Element
<<" ";
P
=P
->Next
;
}
cout
<<"\n";
}
转载请注明原文地址: https://lol.8miu.com/read-34019.html