其他分享
首页 > 其他分享> > 单链表的两种写法

单链表的两种写法

作者:互联网

单链表的两种写法

视频链接

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

标签:node,head,单链,curr,struct,next,两种,entry,写法
来源: https://www.cnblogs.com/studentWangqy/p/16542158.html