单链表的两种写法
单链表的两种写法
视频链接
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;
}