Linked Storage List
#define FindSize 10
typedef struct LNode *List;
struct LNode{
ElementType Data;
List next;
}
List creat(int n){
int i;
List PtrL,Pf,Pb;
for(i=1;i<=n;i++){
Pb = (List)malloc(sizeof(*List));
printf("input Data
");
scanf("%d",Pb->Data);
if(i==1)
PtrL = Pf = Pb;
else
Pf -> next = Pb;
Pb -> next = NULL;
Pf = Pb;
}
return PtrL;
}
int Length(List PtrL){
List p = PtrL;
int i=0;
while(p){
p = p -> next;
i++;
}
return i;
}
List FindKth(int k,List PtrL){
List p = PtrL;
int i=1;
if(k<1||k>Length(PtrL))
return NULL;
while(i<k&&p!=NULL){
p = p -> next;
i++;
}
if(i==k)
return p;
else return NULL;
}
List Find(ElementType X,List PtrL){
List p = PtrL;
while(p->Data!=X&&p!=NULL){
p = p -> next;
}
return p;
}
List Find(ElementType X,List PtrL){
List p = PtrL;
int k=0;
List LL[FindSize];
while(p!=NULL){
if(p->Data=X)
LL[k++] = p;
else p = p -> next;
}
return LL;
}
List Insert(int i,ElementType X,List PtrL){
List s,p;
p = PtrL;
if(i<1||i>Length(PtrL)+1){
printf("wrongPosition!");
return NULL;
}
if(i==1){
s = (List)malloc(sizeof(*List));
s -> next = PtrL;
s -> Data = X;
PtrL = s;
return PtrL;
}
else if(p=FindKth(i-1,PtrL))
s = (List)malloc(sizeof(*List));
s -> next = p -> next;
p -> next = s;
return PtrL;
}else return NULL;
}
List Delete(int i,List PtrL){
List p,s;
if(i==1){
s = PtrL;
PtrL = PtrL -> next;
free(s);
return PtrL;
}
p = FindKth(i-1,PtrL);
if(p==NULL){
printf("wrongPosition!");
return NULL;
}else if(p->next==NULL){
printf("wrongPosition!");
return NULL;
}else{
s = p -> next;
p -> next = s -> next;
free(s);
return PtrL;
}
}
推薦閱讀:
※學習數據挖掘 1 : 線段樹
※殊途同歸(二)[BJOI2018] 鏈上二次求和
※數據結構
※LeetCode-43.字元串相乘(考察點:不知道...)
※九章演算法 | Facebook面試題:有效正方形
TAG:數據結構 |