- 链表的定义和初始化
typedef struct LinkList
{
int data;
struct LinkList* next;
}LinkList;
bool Init(LinkList** L)
{
*L = (struct LinkList*)malloc(sizeof(LinkList));
(*L)->next = NULL;
return true;
}
- 判断链表是否为空
bool Empty(LinkList* L)
{
if (L->next == NULL)
{
return true;
}
else
{
return false;
}
}
- 链表长度的判定
int Size(LinkList* L)
{
int size = 0;
while (L->next)
{
size++;
L = L->next;
}
return size;
}
- 在链表指定位置插入元素
bool Insert(LinkList* L, int local,int num)
{
if (local == 1)
{
LinkList* K = (struct LinkList*)malloc(sizeof(LinkList));
K->next = L->next;
K->data = num;
L->next = K;
return true;
}
else
{
int i=0;
while (L->next&&i<local-1)
{
L = L->next;
i++;
}
if (i <= local - 1)
{
LinkList* K = (struct LinkList*)malloc(sizeof(LinkList));
K->next = L->next;
L->next = K;
K->data = num;
return true;
}
else
{
return false;
}
}
}
- 删除链表指定位置的元素
bool DesertLocal(LinkList* L, int local)
{
int i = 0;
LinkList* K;
while (L->next&&i<local-1)
{
L = L->next;
i++;
}
if(i == local-1&&L->next!=NULL)
{
K = L->next;
L->next = K->next;
free(K);
return true;
}
printf("数据不存在\n");
return false;
}
- 清空链表
bool Clear(LinkList* L)
{
if (Empty(L))
{
return true;
}
LinkList* K = L;
K->next = NULL;
L = L->next;
while (L->next)
{
K = L;
L = L->next;
free(K);
}
}
- 删除链表中所有等于a的元素
void DesertElement(LinkList* L, int a)
{
if (Empty(L) == true)
{
return;
}
LinkList* K=L;
while (L->next)
{
K = L->next;
if ((L->next)->data == a)
{
L->next = K->next;
free(K);
}
else
{
L = L->next;
}
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
typedef struct LinkList
{
int data;
struct LinkList* next;
}LinkList;
int Size(LinkList* L)
{
int size = 0;
while (L->next)
{
size++;
L = L->next;
}
return size;
}
bool Empty(LinkList* L)
{
if (L->next == NULL)
{
return true;
}
else
{
return false;
}
}
bool Init(LinkList** L)
{
*L = (struct LinkList*)malloc(sizeof(LinkList));
(*L)->next = NULL;
return true;
}
bool Insert(LinkList* L, int local,int num)
{
if (local == 1)
{
LinkList* K = (struct LinkList*)malloc(sizeof(LinkList));
K->next = L->next;
K->data = num;
L->next = K;
return true;
}
else
{
int i=0;
while (L->next&&i<local-1)
{
L = L->next;
i++;
}
if (i <= local - 1)
{
LinkList* K = (struct LinkList*)malloc(sizeof(LinkList));
K->next = L->next;
L->next = K;
K->data = num;
return true;
}
else
{
return false;
}
}
}
bool DesertLocal(LinkList* L, int local)
{
int i = 0;
LinkList* K;
while (L->next&&i<local-1)
{
L = L->next;
i++;
}
if(i == local-1&&L->next!=NULL)
{
K = L->next;
L->next = K->next;
free(K);
return true;
}
printf("数据不存在\n");
return false;
}
void DesertElement(LinkList* L, int a)
{
if (Empty(L) == true)
{
return;
}
LinkList* K=L;
while (L->next)
{
K = L->next;
if ((L->next)->data == a)
{
L->next = K->next;
free(K);
}
else
{
L = L->next;
}
}
}
void Print(LinkList* L)
{
L = L->next;
while (L)
{
printf("%d ", L->data);
L = L->next;
}
}
bool Clear(LinkList* L)
{
if (Empty(L))
{
return true;
}
LinkList* K = L;
K->next = NULL;
L = L->next;
while (L->next)
{
K = L;
L = L->next;
free(K);
}
}
void test1(LinkList* L)
{
Insert(L, 1, 10);
Insert(L, 2, 2);
Insert(L, 2, 6);
Insert(L, 2, 1);
Insert(L, 2, 4);
Insert(L, 2, 2);
Insert(L, 2, 6);
Insert(L, 2, 3);
Insert(L, 2, 8);
}
int main()
{
LinkList* L;
Init(&L);
test1(L);
int x = Size(L);
printf("%d\n", x);
DesertElement(L, 6);
Print(L);
}