#include
#include
typedef struct _list{
int elem;
struct _list *next;
}LIST;
LIST list_head={-1,NULL};
void showlist();
LIST *
find(int elem)
{
LIST *temp=list_head.next;
while(temp!=NULL){
if (temp->elem == elem){
return temp;
}else{
temp=temp->next;
continue;
}
}
return NULL;
}
LIST *
findprevious(LIST *p)
{
LIST *temp = &list_head;
if (p == &list_head){
printf("can not delete the head of list\n");
return NULL;
}
while(temp!=NULL){
if (temp->next == p){
return temp;
}
temp=temp->next;
}
return temp;
}
int
delete(LIST *p)
{
LIST *temp = NULL;
temp = findprevious(p);
if (!temp){
printf("not find delete pointer, return\n");
return -1;
}
temp->next=p->next;
free(p);
return 0;
}
int
Insert(int elem, LIST *p)
{
LIST *node=(LIST*)malloc(sizeof(LIST));
if (!node){
return -1;
}
//init node
node->elem=elem;
node->next=NULL;
if (!p->next){
p->next = node;
}else{
node->next=p->next;
p->next=node;
}
return 0;
}
void
showlist()
{
LIST *temp=list_head;
while(temp!=NULL){
printf("--val:%d\n", temp->elem);
temp=temp->next;
}
}
int
main()
{
int i;
LIST *temp=NULL;
for(i=0;i<10;i++){
Insert(i, &list_head);
}
showlist();
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
temp=find(5);
Insert(11, temp);
showlist();
return 0;
}
阅读(2269) | 评论(0) | 转发(0) |