数据结构:链表实现(c)

链表为数据存储常用方法,以下是基于C语言实现普通单向链表。其他双向链表循环链表在此基础上做些许变化。关于是否使用表头的问题,本链表不包含表头,表头可记录链表信息,能降低使用复杂性,按需使用。
声明部分:

#ifndef _LIST_H
#define _LIST_H

typedef int ELEMENT;        //define whatever your want type

enum
{
    ERROR = 0,
    SUCSSE
};


typedef struct
{
    ELEMENT element;
    LIST* next; 
}LIST_NOTE;

LIST_NOTE* list = NULL;

int make_list_empty(LIST_NOTE *list);
int is_list_empty(LIST_NOTE *list);
int is_last_note(LIST_NOTE* note);
LIST_NOTE* position_find(ELEMENT *element, LIST_NOTE *list);
int delete_note(ELEMENT *element, LIST_NOTE *list, LIST_NOTE *note);
LIST_NOTE* find_previous_note(LIST_NOTE *note, LIST_NOTE *list);
int insert_note(ELEMENT *element, LIST_NOTE *list, LIST_NOTE *position);
int delete_list(LIST_NOTE *list);
int first_note(LIST_NOTE *list, LIST_NOTE *note);
int last_note(LIST_NOTE *list, LIST_NOTE *note);

#endif /* _LIST_H */



int make_list_empty(LIST_NOTE *list)
{
    if(list == NULL)
    {
        return SUCSSE;
    }

    LIST_NOTE *note, *head;
    head = list;
    while (list != NULL)
    {
        note = list
        free(list);
        list = note->next;
    }
    head = NULL;

    return SUCSSE;
}


int is_list_empty(LIST_NOTE *list)
{
    return ((list == NULL)? SUCSSE : ERROR);
}


int is_last_note(LIST_NOTE* note)
{
    return ((note->next == NULL)? SUCSSE : ERROR);
}


LIST_NOTE* position_find(ELEMENT *element, LIST_NOTE *list)
{
    if(!list)
    {
        return NULL;
    }

    while (list->next != NULL)
    {
        if(list->element == element)
        {
            return list;
        }
        list = list->next;
    }

    return NULL;
}


LIST_NOTE* find_previous_note(LIST_NOTE *note, LIST_NOTE *list)
{
    if(!list || (list == note))
    {
        return NULL;
    }

    while (list->next != note)
    {
        list = list->next;
    }

    return list;
}


int delete_note(ELEMENT *element, LIST_NOTE *list, LIST_NOTE *note)
{
    if(!list || !note)
    {
        return ERROR;
    }

    if(list == note)
    {
        LIST_NOTE *temp;
        temp = list->next;
        *element = list->element
        free(list);
        list = note->next;
    }
    else
    {
        LIST_NOTE *previous = find_previous_note(note, list);
        previous->next = note->next;
        *element = note->element;
        free(note);
    }

    return SUCSSE;
}


int insert_note(ELEMENT *element, LIST_NOTE *list, LIST_NOTE *position)
{
    if(!list || !position)
    {
        return ERROR;
    }

    if(list == position)
    {
        LIST_NOTE *temp = (LIST_NOTE *)malloc(sizeof(LIST_NOTE));
        temp->element = *element;
        temp->next = position;
        list = temp;        
    }
    else
    {
        LIST_NOTE *previous = find_previous_note(position, list);
        LIST_NOTE *temp = (LIST_NOTE *)malloc(sizeof(LIST_NOTE));
        temp->element = *element;
        temp->next = position;
        previous->next = temp;
    }

    return SUCSSE;
}


int first_note(LIST_NOTE *list, LIST_NOTE *note)
{
    if(!list)
    {
        return ERROR;
    }
    note = list;

    return SUCSSE;
}


int last_note(LIST_NOTE *list, LIST_NOTE *note)
{
    if(!list)
    {
        return ERROR;
    }

    note = list;
    while (list->next != NULL)
    {
        list = list->next;
        note = list;
    }

    return SUCSSE;
}

不要表头确实很麻烦,还是带表头好操作一点,以上代码未验证,仅供参考。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值