c语言 单链表 排序 删除 查找 (无头链表)2020-12-07

 上面为完整元素,下面为删除元素

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值