(一)链表函数定义文件

#include "linklist.h"

int create_linklist(linklistPtr* L){
    if(!((*L) = (linklistPtr)malloc(sizeof(linklist))))
        return 1;
    (*L)->data = 234;
    (*L)->next =NULL;
    return 0;
    
}

int insert_linklist(linklistPtr* L){
    int choose, value;
    linklistPtr node, mark;
    do {
        printf("Choose head or rear, 1 to head, 2 to rear, 0 to exit:\n");
        scanf("%d", &choose);
        fflush(stdin);
        
        switch (choose) {
            case 1:
                printf("Enter a value:\n");
                scanf("%d", &value);
                if (!(node = (linklistPtr)malloc(sizeof(linklist))))
                    return 1;
                node->data = value;
                node->next = (*L)->next;
                (*L)->next = node;
                break;
            case 2:
                printf("Enter a value:\n");
                scanf("%d", &value);
                if (!(node = (linklistPtr)malloc(sizeof(linklist))))
                    return 1;
                mark = (*L);
                while (mark->next)
                    mark = mark->next;
                node->data = value;
                node->next = NULL;
                mark->next = node;
                break;
            default:
                break;
        }
    } while (choose != 0);
    return 0;
}

int del_linklist(linklistPtr* L, int value){
    linklistPtr prePtr, curPtr;
    prePtr = (*L);
    curPtr = (*L);
    while (curPtr->data != value) {
        if(!curPtr)
            return 1;
        prePtr = curPtr;
        curPtr = curPtr->next;
    }
    prePtr->next = curPtr->next;
    curPtr->next = NULL;
    free(curPtr);
    return 0;
}

int clear_linklist(linklistPtr* L){
    linklistPtr delnode;
    while ((*L)->next) {
        delnode = (*L)->next;
        (*L)->next = delnode->next;
        free(delnode);
    }
    return 0;
}

int get_linklist(linklistPtr* L, int position){
    linklistPtr curPtr;
    curPtr = (*L);
    for (int i = 0; i < position; i++){
        if (!curPtr)
            return 1;
        curPtr = curPtr->next;
    }
        return curPtr->data;
}

int locate_linklist(linklistPtr L, int value){
    linklistPtr locatePtr;
    locatePtr = L;
    int count = 0;
    while (locatePtr->data != value) {
        if (!locatePtr)
            return 1;
        locatePtr = locatePtr->next;
        count++;
    }
    return count;
}

int traversal_linklist(linklistPtr L){
    //do not print the head node
    L = L->next;
    while (L) {
        printf("%d", L->data);
        L = L->next;
    }
    printf("\n");
    return 0;
}

int isEmpty_linklist(linklistPtr L){
    return L->next == NULL ? 0 : 1;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值