#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}Node;
Node* Create()
{
Node* L = (Node*)malloc(sizeof(Node));
L->next = NULL;
L->data = 0;
return L;
}
void HeadInser(Node* L, int data)
{
Node* Y = (Node*)malloc(sizeof(Node));
Y->data = data;
Y->next = L->next;
L->next = Y;
}
void TailInser(Node* L, int data)
{
Node* Y = (Node*)malloc(sizeof(Node));
Y->data = data;
if (!L)
{
Y->next = L->next;
L->next = Y;
}
else
{
while (L->next)
{
L = L->next;
}
Y->next = L->next;
L->next = Y;
}
}
void PrintList(Node* L)
{
Node* Y = L;
Y = Y->next;
while (Y)
{
printf("%d -> ", Y->data);
Y = Y->next;
}
printf("NULL\n");
}
void List_Find(Node* L,int data)
{
Node* Y = L->next;
while (Y)
{
if (Y->data == data)
{
printf("找到了:%d\n", Y->data);
return;
}
Y = Y->next;
}
printf("抱歉,链表中没有查询值\n");
}
void List_Delet(Node* L, int data)
{
Node* Y = L;
if (Y->next == NULL)
{
free(Y);
Y = NULL;
return;
}
else
{
while (Y->next)
{
if (Y->next->data == data)
{
Node* P = Y->next;
Y->next = Y->next->next;
printf("删除值为%d后的链表: ",P->data);
free(P);
P = NULL;
return;
}
Y = Y->next;
}
}
printf("没有找到需要删除的结点\n");
}
void List_Destroy(Node* L)
{
Node* Y = L;
while (L)
{
L = L->next;
free(Y);
Y = NULL;
Y = L;
}
if (L == NULL)
{
printf("删除链表成功");
}
}
int main(void)
{
Node* LY = Create();
HeadInser(LY, 1);
HeadInser(LY, 2);
HeadInser(LY, 3);
TailInser(LY, 4);
TailInser(LY, 5);
TailInser(LY, 6);
PrintList(LY);
List_Find(LY, 0);
List_Delet(LY, 6);
PrintList(LY);
List_Destroy(LY);
return 0;
}
|