代码实现
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{int data;struct LNode* next;
}LNode,*LinkList;
//创建头结点
LNode* InitList(LinkList L)
{L = (LNode*)malloc(sizeof(LNode));if (L == NULL){return NULL;}L->data = 0;L->next = NULL;return L;
}
//插入
int ListInsert(LinkList L, int i, int e)
{if (i < 1||L==NULL){return 1;}LNode* p;int j = 0;p = L;while (p!= NULL && j < i - 1){p = p->next;j++;}if (p == NULL){return 1;}LinkList q =(LNode*)malloc(sizeof(LNode));if (q == NULL){return 1;}q->data = e;q->next = p->next;p->next = q;return 0;
}
//删除
int ListDelete(LinkList L, int i, int* e)
{if (i < 1 || L == NULL){return 1;}LNode* p;int j = 0;p = L;while (p!= NULL || j < i - 1){p = p->next;j++;}if (p == NULL){return 1;}LNode* q =(LNode*) malloc(sizeof(LNode));q = p->next;*e = q->data;p->next = q->next;free(q);return 0;
}
//判断链表为空
int Empty(LinkList L)
{if (L->next == NULL){return 1;}else{return 0;}
}
//计算链表长度
int Length(LinkList L)
{if (L == NULL){return 0;}LNode* p;int len=0;p = L;while (p->next != NULL){p = p->next;len++;}return len;
}
int main()
{LinkList L =(LNode*)malloc(sizeof(LNode));return 0;
}