#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/*
创建一个节点
*/
list* Init_list(int data)
{
list* piont = (list *)malloc(sizeof(list));
if(piont == NULL)
return NULL;
piont->data = data;
piont->next = NULL;
return piont;
}
/*
插入一个节点
*/
list* Insert_list(list** head, list* point)
{
if(head == NULL)
{
return NULL;
}
else //头插法
{
point->next = *head;
*head = point;
return *head;
}
}
/*
删除一个节点
*/
list* Delete_point_list(list* head, int data)
{
list* up = head ;
list* last = head ;
if(head == NULL)
return NULL;
for( ;last != NULL; up=last,last=last->next)
{
if(last->data == data)
break;
}
if(last == head) //如果是头指针
{
head = head->next;
free(last) ;
last = head;
}
else if(last->next == NULL) //尾
{
up->next = NULL;
free(last);
}
else //中间
{
up->next = last->next;
free(last);
}
return head;
}
/*
创建一个链表
*/
list* Create_list(void)
{
list* head = NULL;
int data = 0;
printf("输入数据,以-1结尾:\n");
scanf("%d",&data);
head = Init_list(data);
while(1)
{
scanf("%d",&data);
if(data == -1)
break;
Insert_list(&head, Init_list(data));
}
return head;
}
/*
删除一个链表
*/
int Delete_list(list* head)
{
list* up = head ;
list* last = head ;
while(up!=NULL)
{
up = up->next;
free(last);
last = up ;
}
return 1;
}
/*
打印链表
*/
void Loop_list(list* head)
{
list* t = head;
if(t == NULL)
return ;
for( ;t!=NULL; t= t->next)
printf("%d ",t->data);
printf("\n");
}
int main(void)
{
list* head = Create_list();
Loop_list(head);
head = Delete_point_list(head, 5);
Loop_list(head);
Delete_list(head) ;
return 0;
}
链表
最新推荐文章于 2024-06-14 09:15:00 发布