带头节点单链表的所有操作(目前我所想到的),linux纯C实现
来源:互联网 发布:悉知还是知悉 编辑:程序博客网 时间:2024/06/05 15:16
首先是链表的头文件,所有的函数和函数的功能解释都包含在这里: list.h
代码可在anycodes在线编译测试
#ifndef _LIST_H_#define _LIST_H_#include <stdio.h>#include <stdbool.h>#include <stdlib.h>struct node;typedef struct node* pNode;typedef int dataType;typedef pNode list;typedef pNode position;list InitList();//create list by a arraylist CreateList(dataType* a, int L);int ListLength(list L);//the header is still remain.list MakeEmpty(list L);bool IsEmpty(list L);//check whether the node x is the last item of the list.bool IsLastByPos(list L, position x);//check whether the node is the last item of the list by index.bool IsLastByIndex(list L, int index);//find the item by dataposition Find(list L, dataType x);//find the item by indexposition FindByIndex(list L, int index);//find the previous item by dataposition FindPrevious(list L, dataType x);//delete the first item which it's data is equal with xvoid Delete(list L, dataType x);//delete the item by the indexvoid DeleteByIndex(list L, int index);//insert by indexvoid InsertByIndex(list L, int i, dataType x);//insert the new item after the item pvoid InsertByPos(list L, position p, dataType x);//delete all the item of the list, but remain the headervoid DeleteList(list L);position Header(list L);position First(list L);position Advance(position p);//print all the data of the listvoid PrintList(list L);#endif
函数实现:list.c
#include "list.h" typedef struct node { dataType data;pNode next;}node;list InitList(){list head = malloc(sizeof(node));head->next = NULL;return head;}list CreateList(dataType* a, int L){list l = InitList();int i = 0;if(L < 1) {//printf("The length is too short!\n");return l;}for(;i<L;i++)InsertByIndex(l, i, *(a+i));return l;}int ListLength(list L){if(!(L->next)) return 1;int l = 1;position p = L->next;while(p->next){l++;p = p->next;}return l;}list MakeEmpty(list L){if(L != NULL)DeleteList(L);L = malloc(sizeof(node));if(L == NULL){printf("Out of Space!\n");exit(1);}L->next = NULL;return L;}bool IsEmpty(list L){ return L->next == NULL; }bool IsLastByPos(list L, position p){return p->next == NULL;}bool IsLastByIndex(list L, int index){position p = FindByIndex(L, index);return p->next == NULL;}//if there is no specified item, then return null pointer.//if there is serval specified item, then return the first item which the same data of x.position Find(list L, dataType x){position p;p = L->next;while(p != NULL && p->data != x)p = p->next;return p;}position FindByIndex(list L, int index){position p = L;int i = index;if(IsEmpty(L) || index <= 0){printf("The input is error!\n");exit(1);}for(;i>0;i--){if(p->next != NULL)p = p->next;else{printf("The index is bigger than the length of the list!\n");exit(1);}}return p;}//if there is no specified item, then return the last item of the list.//or if there are several specified items, then return the previous item of the first itemposition FindPrevious(list L, dataType x){position p;p = L;while(p->next != NULL && p->next->data != x)p = p->next;return p;}void Delete(list L, dataType x){position p = FindPrevious(L, x);if(!IsLastByPos(L, p)){p->next = p->next->next;free(p->next);}}void DeleteByIndex(list L, int index){position p = FindByIndex(L, index-1);if(p->next == NULL){printf("The index is too big!\n");exit(1);}p->next = p->next->next;free(p->next);}void InsertByIndex(list L, int i, dataType x){position p = L;position tmp;bool isEmpty = IsEmpty(L);tmp = malloc(sizeof(node));if(tmp == NULL){printf("Out of Space!\n");exit(1);}tmp->data = x;if(!isEmpty)p = FindByIndex(L, i);if(isEmpty || (p->next == NULL))tmp->next = NULL;elsetmp->next = p->next;p->next = tmp;}void InsertByPos(list L, position p, dataType x){position tmp;tmp = malloc(sizeof(node));if(tmp == NULL){printf("Out of Space!\n");exit(1);}tmp->data = x;tmp->next = p->next;p->next = tmp;}//delete the list, but the header is still remain.void DeleteList(list L){position p, tmp;p = L->next;L->next = NULL;while(p != NULL){tmp = p->next;free(p);p = tmp;}}position Header(list L){return L;}position First(list L){return L->next;}position Advance(position p){return p->next;}void PrintList(list L) {if(IsEmpty(L)){printf("The list is empty!\n");return;}position p = L;while(p->next){printf("%d ", p->next->data);p = p->next;}}int main(){int a[10] = {12,4354,657,876,2433,23,18,943,54,8};int L = sizeof(a) / sizeof(int);int i = 1;list l = CreateList(a, L);Delete(l, 80);PrintList(l);printf("\n");return 0;}
- 带头节点单链表的所有操作(目前我所想到的),linux纯C实现
- 带头节点单链表的实现
- 带头节点的单链表及其基本操作(Java实现)
- C语言实现单链表节点的删除(带头结点)
- C语言实现单链表的节点插入(带头结点)
- 带头节点的单链表的常用操作
- 带头节点的单链表的基本操作
- 带头节点的单链表的实现
- 带头节点的单链表
- 带头节点的单链表
- 带头节点的单链表
- 带头节点的单链表
- 单链表的实现(不带头节点)
- C语言实现单链表(带头结点)的基本操作
- 带头节点的双链表及其基本操作(Java实现)
- C 带头节点的单链表查找中间节点
- 【C++】模板实现带头节点的双向循环链表
- 在带头节点的单链表中,删除所有值为x的节点(Java实现)
- CV牛人牛事简介
- 杭电OJ_A+B
- preloader打印调试语句
- 电量统计之一:PowerProfile.java与power_profile.xml
- HDU 2066 一个人的旅行 最短路问题
- 带头节点单链表的所有操作(目前我所想到的),linux纯C实现
- color picker
- Mplayer/mencoder as application bundle for Mac OS X?
- KMP算法中的next函数的证明
- Lsnrctl命令参数
- 尝试写博客
- WIN8重见开始菜单(续)
- 3.11 pread函数-文件原子读
- 2013年11月1日