无头单向链表

需要实现无头单向链表,实现初始化、插入、删除三个函数接口。

代码:

#include <stdio.h> 
#include <stdlib.h>

typedef int DataType;

typedef struct Node{
        struct Node *_pNext;
        DataType _data;
}Node, *pNode;

void init_list(pNode *ppHead){
        *ppHead = NULL;
}

int insert_list(pNode *ppHead, DataType data){
        pNode p = (pNode)malloc(sizeof(Node));
        if(p == NULL)   return -1;
        p->_data = data;
        p->_pNext = NULL;
        if(*ppHead == NULL){
                *ppHead = p;
        }else{
                p->_pNext = *ppHead;
                *ppHead = p;
        }
        return 0;
}

int remove_list(pNode *ppHead, DataType data){
        pNode pre = NULL;
        pNode tmp = *ppHead;
        if(tmp == NULL) return -1;
        do{
                if(tmp->_data == data){
                        if(pre == NULL){
                                *ppHead = tmp->_pNext;
                        }else{
                                pre->_pNext = tmp->_pNext;
                        }
                        free(tmp);
                        return 0;
                }
                pre = tmp;
                tmp = tmp->_pNext;
        }while(tmp);
        return -2;
}

void print_list(pNode pHead){
        pNode tmp = pHead;
        if(tmp == NULL) return;
        int count = 0;
        printf("=======================\n");
        do{
                printf("%d\t%d\n", count++, tmp->_data);
                tmp = tmp->_pNext;
        }while(tmp);
        printf("-----------------------\n");
}

pNode g_plist;
int main(){
        init_list(&g_plist);
        DataType d = 123;
        insert_list(&g_plist, d);
        insert_list(&g_plist, 456);
        insert_list(&g_plist, 789);
        print_list(g_plist);

        remove_list(&g_plist, 456);
        print_list(g_plist);

        insert_list(&g_plist, 111);
        insert_list(&g_plist, 222);
        insert_list(&g_plist, 333);
        print_list(g_plist);

        remove_list(&g_plist, 333);
        print_list(g_plist);

        remove_list(&g_plist, 123);
        print_list(g_plist);

        insert_list(&g_plist, 123);
        insert_list(&g_plist, 456);
        print_list(g_plist);

        remove_list(&g_plist, 111);
        remove_list(&g_plist, 222);
        print_list(g_plist);

        return 0;
}

运行结果:

$ ./test 
=======================
0       789
1       456
2       123
-----------------------
=======================
0       789
1       123
-----------------------
=======================
0       333
1       222
2       111
3       789
4       123
-----------------------
=======================
0       222
1       111
2       789
3       123
-----------------------
=======================
0       222
1       111
2       789
-----------------------
=======================
0       456
1       123
2       222
3       111
4       789
-----------------------
=======================
0       456
1       123
2       789
-----------------------

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值