Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119838
  • 博文数量: 11
  • 博客积分: 272
  • 博客等级: 二等列兵
  • 技术积分: 181
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-19 16:44
文章存档

2012年(2)

2011年(9)

我的朋友

分类: C/C++

2012-06-20 09:32:38

#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;
}
阅读(2234) | 评论(0) | 转发(0) |
0

上一篇:产品专家Marty Cagan:不做只会编码的人

下一篇:没有了

给主人留下些什么吧!~~