循环链表

#include <stdio.h>
#include <stdlib.h>
struct student
{
	int num;
	struct student * next;
};

typedef struct student STU;
typedef struct student * Link;

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

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

void create_newnode(Link *newnode)
{
	*newnode = (Link)malloc(sizeof(STU));
	is_malloc_ok(*newnode);
}

void insert_newnode(Link *head,Link newnode)
{
	if(*head == NULL)
	{
		*head = newnode;
		newnode->next = *head;
	}
	else
	{
		Link tmp;
		tmp = *head;
		while(tmp->next != *head)
		{
			tmp = tmp->next;
		}
			newnode->next = *head;
			tmp->next = newnode;
			*head = newnode;
	}
}

void display(Link head)
{
	Link tmp;
	tmp = head;
	if(head != NULL)
	{
		while(tmp->next != head)
		{
			printf("number = %d \n",tmp->num);
			tmp = tmp->next;
		}

		if(tmp->next == head)
		{
			printf("number = %d \n",tmp->num);
		}
	}
	else
	{
		printf("link is empty!\n");
	}
}

void freelink(Link *head)
{
	Link tmp;
	Link head_old;
	tmp = *head;
	head_old = *head;
	if(*head == NULL)
	{
		printf("Link is empty !\n");
		return;
	}
	while(tmp->next != head_old)
	{
		*head = (*head)->next;
		free(tmp);
		tmp = (*head)->next;
	}
	free(tmp);
	*head = NULL;
	printf("free successfully !\n");

}

void insert_newnode_mid(Link *head,Link *newnode,int num)
{
	Link p1,p2 = NULL;
	p1 = *head;
	if(*head == NULL)
	{
		*head = *newnode;
		(*newnode)->next = *head;
	}
	else
	{
		while(p1->num < num && p1->next != *head)
		{
			p2 = p1;
			p1 = p1->next;
		}
		if((*head)->next == *head)
		{
			if((*head)->num > num)
			{
				(*newnode)->next = *head;
				*head = *newnode;
			}
			else
			{
				(*head)->next = *newnode;
				(*newnode)->next = *head;
			}
		}
		else
		{
			if(p1 == *head)
			{
				(*newnode)->next = *head;
				*head = *newnode;
			}
			else
			{
			    if(p1->next == *head)
			    {
				    if(p1->num < num)
				    {
                        p1->next = *newnode;
					    (*newnode)->next = NULL;
				    }
				    else
				    {
					    p2->next = *newnode;
			    		(*newnode)->next = p1;
				    }
			    }
			    else
			    {
				    p2->next = *newnode;
			        (*newnode)->next = p1;
			    }
			}
			
		}
	}
}

void reverslink(Link *head)
{
	Link p1 = NULL;
	Link p2 = NULL;
	Link p3 = NULL;
	p1 = *head;
	if(p1 == NULL)
	{
		printf("Link is empty !\n");
		return;
	}
	if(p1->next == (*head)->next)
	{
		return;
	}
	else
	{
		p2 = p1->next;
		if(p2->next == (*head))
		{
			*head = p2;
			p2->next = p1;
			p1->next = (*head)->next;
		}
		else
		{
			p3 = p2->next;
			while(p3->next != (*head)->next)
			{
				p2->next = p1;
				p1 = p2;
				p2 = p3;
				p3 = p3->next;
			}
			p2->next = p1;
			p3->next = p2;
			p1 = *head;
			p1->next = (*head)->next;
			*head = p3;

		}
	}


}
int main()
{
    Link head = NULL;
	Link newnode = NULL;
	int i;

	create_link(&head);
	for(i = 0;i < 10;i++)
	{
		create_newnode(&newnode);
		newnode->num = i;
//      scanf("%d",&newnode->num);
//		insert_newnode_mid(&head,&newnode,newnode->num);
		insert_newnode(&head,newnode);
	}

	display(head);
	freelink(&head);
//  reverslink(&head);
	display(head);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值