数据结构(线性表--单链表--不带头节点)

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

#define TYPE int
#define PH "%d "

typedef struct ListNode
{
	TYPE data;				//	数据域
	struct ListNode* next;	//	指针域
}ListNode;

//	创建节点
ListNode* create_list_node(TYPE data)
{
	//	给节点分配堆内存
	ListNode* node = malloc(sizeof(ListNode));
	node->data = data;
	node->next = NULL;
	return node;
}

//	头添加 链表不会满
void head_add_list(ListNode** head,TYPE data)
{
	ListNode* node = create_list_node(data);
	
	node->next = *head;
	*head = node;
}

//	尾添加
void tail_add_list(ListNode** head,TYPE data)
{
	ListNode* node = create_list_node(data);

	if(NULL == *head)	//	head是空链表
	{
		*head = node;
		return;
	}

	//	让n遍历到链表最后一个节点停下
	ListNode* n = NULL;
	for(n=*head; NULL != n->next; n=n->next);
	
	n->next = node;
}

//	插入 (要找到待插入位置的前驱节点)
bool insert_list(ListNode** head,int index,TYPE data)
{
	if(0 > index)
		return false;
	
	if(0 == index)
	{
		head_add_list(head,data);
		return true;
	}

	ListNode* prev = *head;
	while(--index && NULL != prev)
	{
		prev = prev->next;
	}

	//	index非法
	if(NULL == prev)
		return false;

	ListNode* node = create_list_node(data);
	node->next = prev->next;
	prev->next = node;
	return true;
}

//	头删除
bool head_del_list(ListNode** head)
{
	if(NULL == *head)
		return false;

	//	记录待删除节点
	ListNode* temp = *head;
	*head = (*head)->next; 	//*head = temp->next;
	free(temp);
	return true;
}

//	尾删除
bool tail_del_list(ListNode** head)
{
	if(NULL == *head)
		return false;

	ListNode* prev = *head;
	if(NULL == prev->next)
	{
		free(prev);
		*head = NULL;
		return true;
	}

	while(NULL != prev->next->next)
		prev = prev->next;

	free(prev->next);
	prev->next = NULL;
	return true;
}

//	按位置删除
bool index_del_list(ListNode** head,int index)
{
	if(0 > index) return false;
	if(0 == index) return head_del_list(head);

	//	找待删除位置的前驱
	ListNode* prev = *head;
	
	while(--index && NULL != prev->next)
	{
		prev = prev->next;
	}

	//	index非法
	if(NULL == prev->next)	return false;

	//	记录待删除节点
	ListNode* temp = prev->next;
	prev->next = temp->next;	//	prev->next = prev->next->next;
	free(temp);
	return true;

}


//	遍历链表
void show_list(ListNode* head)
{
	for(ListNode* n=head; NULL != n; n=n->next)
	{
		printf(PH,n->data);
	}
	printf("\n");
}

int main(int argc,const char* argv[])
{
	/*	理解链表的本质
	ListNode* n1 = create_list_node(10);	
	ListNode* n2 = create_list_node(20);	
	ListNode* n3 = create_list_node(30);	
	ListNode* n4 = create_list_node(40);	

	n1->next = n2;
	n2->next = n3;
	n3->next = n4;

	for(ListNode* n=n1; NULL != n; n = n->next)
	{
		printf(PH,n->data);
	}
	*/

	//	创建头指针 指向第一个节点
	ListNode* head = NULL;
	for(int i=0; i<5; i++)
	{
		tail_add_list(&head,i+10);
	}
	
	insert_list(&head,5,88);
	
	head_del_list(&head);
	head_del_list(&head);

	tail_del_list(&head);
	//tail_del_list(&head);

	index_del_list(&head,0);

	show_list(head);
}



  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值