微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

单链表的两种写法

单链表的两种写法

视频链接

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;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐