单链表的两种写法

单链表的两种写法

视频链接

https://www.youtube.com/watch?v=o8NPllzkFhE

第一种写法(not good tast)


#include <stdio.h>

struct node
{
	int data;
	struct node* next;
}

struct node* head;

//remove a node from single list,entry must in list
void remove(struct node* entry)
{
	struct node* pre=NULL;
	struct node* curr=head;
	//walk the list
	while(curr && curr!=entry)
	{
		pre = curr;
		curr=curr->next;
	}
	//entry is head node, remove head node and maintain head
	if(!pre)
	{
		head = curr->next;
	}
	//entry is other node
	else
	{
		pre->next = entry->next;
		delete curr;	
	}
}

第二种写法(good tast)


#include <stdio.h>

struct node
{
	int data;
	struct node* next;
}

struct node *head;

void remove(struct node* entry)
{
	//indirect points to the *address* of head
	struct node** indirect = &head;

	while(*indirect != entry)
	{
		indirect = &((*indirect)->next);
	}

	*indirect = entry->next;
}

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...