双向链表尾删
实现函数:
//尾删
linklist delete_rear(linklist L)
{
if(NULL == L)
{
return NULL;
}
else if(NULL == L->next)
{
free(L);
L = NULL;
}
else
{
linklist p = L;
while(p->next!= NULL)
{
p = p->next;
}
free(p);
p->priv->next = NULL;
}
return L;
}
双向链表释放
实现函数:
//释放
linklist free_space(linklist L)
{
if(NULL == L)
{
return NULL;
}
else
{
while(L != NULL)
{
L = delete_rear(L);
}
}
return L;
}
主函数:
#include"head.h"
int main()
{
linklist L=NULL;
int n = 0;
printf("please enter n:");
scanf("%d",&n);
char element;
//尾插
for(int i = 0 ; i < n ; i++)
{
printf("please enter %d element:",i+1);
scanf(" %c",&element);
L = insert_rear(L,element);
}
printf("尾插后:\n");
output(L);
//尾删
printf("尾删后:\n");
L=delete_rear(L);
output(L);
L = free_space(L);
output(L);
return 0;
}
运行结果:
双向循环链表尾删
实现函数:
//尾删
linklist delete_rear(linklist L)
{
if(NULL == L)
{
return NULL;
}
else if(L == L->next)
{
free(L);
L = NULL;
}
else
{
linklist p = L->priv;
p->priv->next = L;
L->priv = p->priv;
free(p);
p = NULL;
}
return L;
}
双向循环链表释放
实现函数:
//释放
linklist free_space(linklist L)
{
if(NULL == L)
{
return NULL;
}
else
{
while(L != NULL)
{
L = delete_rear(L);
}
}
return L;
}
主函数:
#include"head.h"
int main()
{
linklist L=NULL;
int n = 0;
printf("please enter n:");
scanf("%d",&n);
char element;
//尾插
for(int i = 0 ; i < n ; i++)
{
printf("please enter %d element:",i+1);
scanf(" %c",&element);
L = insert_rear(L,element);
}
printf("尾插后:\n");
output(L);
//尾删
printf("尾删后:\n");
L=delete_rear(L);
output(L);
L = free_space(L);
output(L);
return 0;
}
运行结果:
Xmind