C 语言 单链表操作

#include<stdio.h>
#include<stdlib.h>

#include<memory.h>

typedef struct list_node
{
int data;                     //数据域,用来存放数据; 
struct list_node *next;       //定义一个结构体指针,指向下一次与当前节点数据类型相同的节点  

}list_single;


/***********************************************************************************************************************
* 创建单链表的一个节点
***********************************************************************************************************************/
list_single *creat_single_list_node(int value)
{
list_single *node = NULL;
node = (list_single *)malloc(sizeof(list_single));
if (node == NULL){
printf("malloc fair !\n");
}
else{
node->data = value;
node->next = NULL;
}
return node;

}


/***********************************************************************************************************************
* 获取单链表的长度,遍历链表
***********************************************************************************************************************/
int get_single_list_len(list_single *pHead)
{
    int len = 0;
list_single * node = NULL;
node = pHead->next;
while (node != NULL){
len++;
node = node->next;
}
return len;
}


/***********************************************************************************************************************
* 删除单链表中的某个节点
***********************************************************************************************************************/
bool delete_single_list_node(list_single *pHead, int position, int *val)
{
int i = 0;
list_single * node = NULL;
list_single * del_node = NULL;
node = pHead;
while ((node != NULL) && (i < position - 1)){
node = node->next;
i++;
}
if ((node == NULL) || i > (position - 1)){
printf("the single list is NULL.\n");
return false;
}
del_node = node->next;
*val = del_node->data;
node->next = del_node->next;
free(del_node);
del_node = NULL;
return true;
}


/***********************************************************************************************************************
* 删除整个单链表
***********************************************************************************************************************/
void delete_all_single_list(list_single *pHead)
{
int i = 0;
list_single * temp_node = NULL;
list_single * del_node = NULL;
del_node = pHead->next;
pHead->next = NULL;
while (del_node != NULL)
{
temp_node = del_node->next;
free(del_node);
del_node = temp_node;
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值