Status NextElem(List l, int i, ElemType* e)
{
while(l && i){
l = l->next;
i--;
}
if(i != 0)
return FALSE;
else{
if(l->next){
*e = l->next->data;
return TRUE;
}else{
return FALSE;
}
}
}
Status ListInsert(List l, int i, ElemType e)
{
l = l->next;
Node* tmp_node;
while(l && i){
tmp_node = l;
l = l->next;
i--;
}
if(i != 0)
return FALSE;
else{
Node* p = (Node*)malloc(sizeof(Node));
p->data = e;
p->next = l;
tmp_node->next = p;
return TRUE;
}
}
Status ListDelete(List l, int i)
{
Node* tmp_node;
while(l && i){
tmp_node = l;
l = l->next;
i--;
}
if(i != 0)
return TRUE;
else{
tmp_node->next = l->next;
free(l);
return TRUE;
}
}
void PrintList(List l)
{
l = l->next;
while(l){
printf("%d\n",l->data);
l = l->next;
}
}
int main()
{
List l = InitList(5);
ElemType tmp_elem = 0;
PrintList(l);
int s = GetElem(l,3,&tmp_elem);
printf("the element is %d\n", tmp_elem);
NextElem(l,3,&tmp_elem);
printf("the goal element's next element is %d\n", tmp_elem);
if(PriorElem(l,3,&tmp_elem))
printf("the goal element's prior element is %d\n", tmp_elem);
int location = LocateElem(l,4);
printf("the location id is %d\n", location);
ListInsert(l,3,8);
PrintList(l);
ListDelete(l,4);
PrintList(l);
return 0;
}
C++ Primer Plus 第6版 中文版 清晰有书签PDF+源代码
将C语言梳理一下,分布在以下10个章节中:
Linux-C成长之路(十):其他高级议题