【编程】链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012335044/article/details/82981790

链表

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


typedef struct node{
	int a;
	struct node* next;
}Node,*Link_list;

void link_add(Link_list * head,int a)
{
	Link_list p_head;
	if (*head == NULL)
	{
		Link_list p_head = (Link_list)malloc(sizeof(Node));
		p_head->a = a;
		p_head->next = NULL;
		*head = p_head;
		return;
	}

	p_head = *head;
	while(p_head->next != NULL)
	{
		p_head = p_head->next;
	}
	Link_list p_new = (Link_list) malloc(sizeof(Node));
	p_new->a = a;
	p_new->next = NULL;
	p_head->next=p_new;
	return ;
}
int link_show(Link_list p_head)
{

	printf("link status:\n");
	while(p_head != NULL)
	{
		printf("%d ",p_head->a);
		p_head = p_head->next;
	}
	printf("\n");
	return 0;
}
Link_list link_reserve(Link_list* head)
{
	Link_list p_f,p_n,p_p;
	p_n = *head;
	p_p = (*head)->next;
	if ((*head) == NULL)
	{
		return ;
	}
	else if (p_n->next == NULL)
	{
		return (*head);
	}

	(*head)->next = NULL;
	while(p_p != NULL)
	{
		p_f = p_p;
		p_p = p_p->next;
		p_f->next = p_n;
		p_n = p_f;
	}

	(*head) = p_f;
	return (*head);

}
int main(int argc, char *argv[])
{
	Link_list head = NULL;
	link_add(&head, 1);
	link_add(&head, 11);
	link_add(&head, 111);
	link_show(head);

	head = link_reserve(&head);
	link_show(head);
	return 0;
}



猜你喜欢

转载自blog.csdn.net/u012335044/article/details/82981790
今日推荐