上面为完整元素,下面为删除元素
#include <stdio.h>
#include <stdlib.h>
struct node
{
int hao;
int val;
int data;
struct node *next;
};
void showlist(struct node *head)//遍历
{
printf("========open=======\n");
while(head)
{
printf("编号 %d head->val %d head->data %d\n", head->hao, head->val, head->data);
head = head->next;
}
printf("========over=======\n");
}
struct node *insert_head(struct node *h, struct node *n)//头插有返回值
{
n->next = h;
return n;
}
void insert_Head(struct node **h, struct node *n)//头插
{
n->next = *h;
*h = n;
}
struct node *insert_tail(struct node *head, struct node *new)//尾插有返回值
{
if(head == NULL)
{
return new;
}
struct node *p = head;
while(p->next != NULL)
{
p = p->next;
}
p->next = new;
return head;
}
void insert_Tail(struct node **h, struct node *n)//尾插
{
while(*h)
{
h = &((*h)->next);
}
*h = n;
}
void reverse_list(struct node **h)//转置(倒序)
{
struct node *newhead = NULL;
struct node *p = *h;
struct node *tmp = NULL;
while(p)
{
tmp = p;
p = p->next;
tmp->next = NULL;
tmp->next = newhead;
newhead = tmp;
}
*h = newhead;
}
int cmp_val(struct node *n1, struct node *n2)
{
return n1->val - n2->val;
}
int cmp_data(struct node *n1, struct node *n2)
{
return n1->data - n2->data;
}
void sortlist (struct node **head, int(*fp)(struct node*, struct node*))//排序
{
struct node *newhead = NULL;
struct node *min = NULL;
struct node *minprev = NULL;
struct node *p = NULL; //做循环
while(*head)
{
//find min
minprev = NULL;
p = *head;
min = p;
while(p->next)
{
//if(min->val > p->next->val)
if(fp(min, p->next) > 0)
{
minprev = p;
min = p->next;
}
p = p->next;
}
//cut
if(min == *head)
{
*head = (*head)->next;
}
else
{
minprev->next = min->next;
}
//insert
// insert_Head(&newhead, min);
min->next = newhead;
newhead = min;
}
*head = newhead;
}
struct node goal = {
.val = 7
};
void findnode(struct node **head, struct node *goal, int(*fp1)(struct node *, struct node *))//查找
{
struct node *newhead = NULL;
while(fp1(*head, goal) == 0)
// while((*head)->val == goal)
{
struct node *tmp = *head;
(*head) = (*head)->next;
tmp->next = NULL;
insert_Tail(&newhead, tmp);
}
struct node *cut = (*head);
while(cut->next )
{
if(fp1(cut->next, goal) == 0)
// if(cut->next->val == goal)
{
struct node *tmp = cut->next;
cut->next = cut->next->next;
tmp->next = NULL;
insert_Tail(&newhead, tmp);
}
else
{
cut = cut->next;
}
}
*head = newhead;
}
void destroy(struct node **h)//销毁
{
struct node *p = NULL;
while(*h)
{
p = (*h)->next;
free(*h);
*h = NULL;
*h = p;
}
}
int main()
{
srand(time(NULL));
int arr[] = {7,7,1,2,7,3,7,7,4,7,7};
struct node *head = NULL;
void destroy(struct node **h)
int i = 0;
int n = sizeof(arr)/sizeof(int);
while(i < sizeof(arr)/sizeof(int))
{
newnode = malloc(sizeof(struct node));
newnode->val = arr[i];
newnode->hao = i;
newnode->data = rand()%100;
newnode->next = NULL;
insert_Head(&head, newnode);
i++;
}
showlist(head); //链表遍历
reverse_list(&head); //转置(倒序)
showlist(head); //链表遍历
sortlist(&head, cmp_val);
showlist(head); //链表遍历
findnode(&head, &goal, cmp_val );
showlist(head); //链表删除元素7
destroy(&head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
int hao;
struct node *next;
};
void show_list(struct node *first)
{
printf("======open======\n");
while(first)
{
printf("编号 %d first->val %d\n", first->hao, first->val);
head = head->next;
}
printf("======over======\n");
} //打印
struct node *insert_tail(struct node *first, struct node *n)
{
if(first == NULL)
{
return n;
}
struct node *p = first;
while(p->next != NULL)
{
p = p->next;
}
p->next = n;
return first;
} //尾插
void insert_Tail(struct node **first, struct node *n)
{
while(*first)
{
*h = &((*h)->next);
}
*h = n;
}
void cur_node(struct node **first, int goal)
{
while( (*first)->val == NULL)
{
(*first) = (*first)->next;
}
struct node *p = *first;
while(p->next)
{
if(p->next->val == goal)
{
p->next = p->next->next;
}
else
{
p = p->next;
}
}
}
void destroy(struct node **first)
{
struct node *p = NULL;
while(h)
{
p = first->next;
free(first);
first = NULL;
h = p;
}
}
int main ()
{
int arr[] = {8,8,1,2,8,3,8,8,4,5,8,8};
struct node *first = NULL;
struct node *newnode = NULL;
int i = 0;
while(i < sizeof(arr)/sizeof(int))
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->val = arr[i];
newnode->hao = i;
newnode->next = NULL;
//first = insert_tail(first, newnode);
insert_Tail(&first, newnode);
i++;
}
show_list(head); //(8,8,1,2,8,3,8,8,4,5,8,8)
cur_node(&first, 8); //调用打印(1,2,3,4,5)
destroy(&head);
return 0;
}