链表中间插加强版

#include <stdio.h>
#include <stdlib.h>

struct node
{
	int num;
	struct node * next;
};

typedef struct node Node;
typedef struct node * Link;

void create_link(Link * head)
{
	*head = NULL;
}

void insert_node_head(Link * head,Link new_node)
{
    new_node->next = *head;
	*head = new_node;
}

void display_node(Link head)
{
	Link tmp;
	tmp = head;

	while(tmp != NULL)
	{
		printf("num = %d\n",tmp->num);
		tmp = tmp->next;
	}
}

void is_malloc_ok(Link new_node)
{
     if(new_node == NULL)
	{
		printf("malloc error!\n");
		exit(-1);
		}
}

void create_newnode(Link * new_node)
{
	*new_node = (Link) malloc(sizeof(Node));
	is_malloc_ok(*new_node);
}

void insert_node_tail(Link *head,Link new_node)
{
    Link tmp;

	tmp = *head;

	if(*head == NULL)
	{
		new_node->next = *head;
		*head = new_node;
	}
	else 
	{
		while(tmp->next != NULL)
		{
			tmp = tmp->next;
		}
		tmp->next = new_node;
		new_node->next = NULL;
	}
}

void insert_node_mid(Link *head,Link * new_node,int num)
{
	Link p1 = NULL;
	Link p2 = NULL;
	p1 = *head;

	if(*head == NULL)//空
	{
		(*new_node)->next = p1;
		*head = *new_node;
	}
	else//不为空
	{
		while((p1->num < num) && (p1->next != NULL))
		{
			p2 = p1;
			p1 = p1->next;
		}
		if((*head)->next == NULL)//因p1->next != NULL退出循环
		{
			if(num > p1->num)
			{
				(*head)->next = *new_node;
				(*new_node)->next = NULL;
			}
			else
			{
				(*new_node)->next = *head;
				*head = *new_node;
			}
		}
		else
		{
			if(p1 == *head)
			{
				(*new_node)->next = *head;
				*head = *new_node;
			}
			else
			{
				if(p1->next != NULL)
				{
					p2->next = *new_node;
					(*new_node)->next = p1;
				}
				else
				{
					if(num > p1->num)
					{
						p2 = p1->next;
						p1->next = *new_node;
						(*new_node)->next = p2;
					}
					else
					{
						p2->next = *new_node;
						(*new_node)->next = p1;
					}
				}
			}
		}
	}
}

/*void insert_node_mid(Link *head,Link new_node,int num)
{
    Link tmp;
	tmp = *head;
    
	if(NULL == *head)
	{
		printf("link is empty!\n");
		return;
	}
	else
	{
    	while(tmp->num != num && tmp->next != NULL)
    	{
            tmp = tmp->next;
    	}
        if(tmp->num == num)
		{
            new_node->next = tmp->next;
			tmp->next = new_node;
		}
		else
		{
			printf("no such node!\n");
		}
	}

}
*/
void delete_node(Link *head,int num)
{
	Link tmp;
	Link p;
	tmp = p = *head;

	if(NULL == *head)
	{
		printf("link is empty!\n");
		return;
	}

	while(tmp->num != num && tmp->next != NULL)
	{
		p = tmp;
		tmp = tmp->next;
	}

	if(tmp->num == num)
	{
        if(tmp == *head)
		{
            *head = tmp->next;
			free(tmp);
		}
		else
		{
			p->next = tmp->next;
			free(tmp);
		}
	}
	else
	{
		printf("no such node!\n");
	}
}

int main()
{
    Link head = NULL;
    Link new_node = NULL;
    int i;
	int num;

	create_link(&head);

	for(i = 0; i < 10;i++)
	{
        create_newnode(&new_node);
		new_node->num = i;
		insert_node_head(&head,new_node);
		//insert_node_tail(&head,new_node);
	}

	printf("please input a num!\n");
	scanf("%d",&num);

//	create_newnode(&new_node);
//	new_node->num =  num;

	insert_node_mid(&head,&new_node,new_node->num);
    
//	delete_node(&head,num);

	display_node(head);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值