数据结构day6单链表的(复杂)操作实现

数据结构day6单链表的(复杂)操作实现

链表的反转

代码:

int list_reverse(linklist H)
{
	linklist p;
	linklist q;
	if(H == NULL)
	{
		printf("H is NULL\n");
		return -1;
	}

	if(H->next == NULL || H->next->next == NULL)
	{
		return 0;
	}

	p = H->next->next;
	H->next->next = NULL;//链表一分为二 

	while(p != NULL)
	{
	q = p;
	p = p->next;

	q->next = H->next;
	H->next = q;

	}
	return 0;
}

请添加图片描述

请添加图片描述

链表求相邻两个节点最大值的第一个指针,和其相邻两个节点的最大值

代码:

linklist list_adjmax(linklist H, data_t *value)
{

	linklist p, q, r;
	data_t sum;

	if(H == NULL)
	{
		printf("H is NULL\n");
		return NULL;
	}
	if(H->next == NULL || H->next->next == NULL || H->next->next->next == NULL)//H中的data没有值
	{
		return H;
	}

	q = H->next;
	p = H->next->next;
	r = q;

	sum = q->data + p->data;

	while(p->next != NULL)
	{
		p = p->next;
		q = q->next;
		if(sum < q->data + p->data)
		{
			sum = q->data + p->data;
			r = q;

		}
	}
	*value = sum;

	return r;

}

请添加图片描述

请添加图片描述

两个有序链表的合并

代码:

int list_merge(linklist H1, linklist H2)
{	
	linklist p, q, r;


	if(H1 == NULL || H2 == NULL)
	{
		printf("H1 || H2 is NULL\n");
		return -1;
	}
	
	p = H1->next;
	q = H2->next;

	r = H1;

	H1->next = NULL;
	H2->next = NULL;


	while(p && q)
	{
		if(p->data <= q->data)
		{
			r->next = p;
			p = p->next;
			r = r->next;
			r->next = NULL;
		}
		else
		{
			r->next = q;
			q = q->next;
			r = r->next;
			r->next = NULL;
		}

	}

	if(p == NULL)
	{
		r->next = q;

	}
	else
	{
		r->next = p;
	}	

	
	return 0;
}

请添加图片描述

请添加图片描述

请添加图片描述

所有代码(day4-day6)

linklist.h


typedef int data_t;

typedef struct node{
	data_t data;
	struct node * next;


}listnode, * linklist;


linklist list_create();
int list_tail_insert(linklist H, data_t value);//Head
int list_show(linklist H);
int list_insert(linklist H, data_t value, int pos);//链表在新的位置插入一个节点
linklist list_get(linklist H, int pos);//得到特定位置节点的值
int list_delete(linklist H, int pos);//删除某一位置上的节点
linklist list_free(linklist H);//链表的释放
int list_reverse(linklist H);//链表的反转
linklist list_adjmax(linklist H, data_t *value);//求相邻两个节点data值之和为最大
int list_merge(linklist H1, linklist H2);//两个有序链表的合并,合并到H1中


linklist.c

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


linklist list_create()
{
	linklist H;

	H = (linklist)malloc(sizeof(listnode));
	if(H == NULL)
	{
		printf("malloc failed\n");
		return H;
	}

	H->data = 0;
	H->next = NULL;

	return H;
}
int list_tail_insert(linklist H, data_t value)//Head
{
	linklist p;
	linklist q;

	if(H == NULL)
	{
		printf("H is empty\n");
		return -1;
	}

	//1 new node p
	if((p = (linklist)malloc(sizeof(listnode))) == NULL)
	{
		printf("malloc failed\n");
		return -1;
	}
	p->data = value;
	p->next = NULL;
	
	//2 locate tail node
	q = H;
	while(q->next != NULL)
	{
		q = q->next;
	}
	
	//3 insert
	
	q->next = p;
	

	return 0;
}
int list_show(linklist H)
{
	linklist p;
	if(H == NULL)
	{
		printf("H is NULL\n");
		return -1;
	}
	p = H;
	while(p->next != NULL)
	{
		printf("%d ", p->next->data);
		p = p->next;
	}
	puts(" ");
	return 0;
}

int list_insert(linklist H, data_t value, int pos)
{
	linklist p;
	linklist q;
	

	if(H == NULL)
	{
		printf("H is NULL\n");
	}

	//1 locate node p (pos-1)
	p = list_get(H, pos-1);
	
	if(p == NULL)
	{
		return -1;
	}

	//2 new node q
	if((q = (linklist)malloc(sizeof(listnode))) == NULL)
	{
		printf("malloc failed\n");
		return -1;
	}
	q->data = value;
	q->next = NULL;



	//3 insert
	q->next = p->next;
	p->next = q;

	return 0;
}
linklist list_get(linklist H, int pos)
{
	linklist p;
	int i;
	if(H == NULL)
	{
		printf("H is NULL\n");
		return NULL;
	}
	if(pos == -1)
	{
		return H;
	}
	if(pos < -1)
	{
		printf("pos is invalid\n");
		return NULL;
	}

	p = H;

	i = -1;
	while(i < pos)
	{
		p = p->next;
		if(p == NULL)
		{
			printf("pos is invalid\n");
			return NULL;
		}
		i++;
	}


	return p;

}
int list_delete(linklist H, int pos)
{
	linklist p;
	linklist q;

	//1
	if(H == NULL)
	{
		printf("H is NULL\n");
		return -1;
	}

	//2 locate prior找到上一个节点
	p = list_get(H, pos-1);
	if(p == NULL)
		return -1;

	if(p->next == NULL)
	{
		printf("delete pos is invalid\n");
		return -1;
	}
	
	//3 update list
	q = p->next;
	p->next = q->next;

	//4 free
	printf("free:%d\n",q->data);
	free(q);
	q = NULL;	
	
	

	return 0;
}
linklist list_free(linklist H)
{
	linklist p;
	if(H == NULL)
	{
		printf("H is NULL\n");
		return NULL;
	}
	p = H;

	printf("free:");
	while(H != NULL)
	{
		p = H;
		printf("%d ",p->data);
		H = H->next;
		
		free(p);
	}
	puts("");
	return NULL;
}
int list_reverse(linklist H)
{
	linklist p;
	linklist q;
	if(H == NULL)
	{
		printf("H is NULL\n");
		return -1;
	}

	if(H->next == NULL || H->next->next == NULL)
	{
		return 0;
	}

	p = H->next->next;
	H->next->next = NULL;//链表一分为二 

	while(p != NULL)
	{
	q = p;
	p = p->next;

	q->next = H->next;
	H->next = q;

	}
	return 0;
}
linklist list_adjmax(linklist H, data_t *value)
{

	linklist p, q, r;
	data_t sum;

	if(H == NULL)
	{
		printf("H is NULL\n");
		return NULL;
	}
	if(H->next == NULL || H->next->next == NULL || H->next->next->next == NULL)//H中的data没有值
	{
		return H;
	}

	q = H->next;
	p = H->next->next;
	r = q;

	sum = q->data + p->data;

	while(p->next != NULL)
	{
		p = p->next;
		q = q->next;
		if(sum < q->data + p->data)
		{
			sum = q->data + p->data;
			r = q;

		}
	}
	*value = sum;

	return r;

}
int list_merge(linklist H1, linklist H2)
{	
	linklist p, q, r;


	if(H1 == NULL || H2 == NULL)
	{
		printf("H1 || H2 is NULL\n");
		return -1;
	}
	
	p = H1->next;
	q = H2->next;

	r = H1;

	H1->next = NULL;
	H2->next = NULL;


	while(p && q)
	{
		if(p->data <= q->data)
		{
			r->next = p;
			p = p->next;
			r = r->next;
			r->next = NULL;
		}
		else
		{
			r->next = q;
			q = q->next;
			r = r->next;
			r->next = NULL;
		}

	}

	if(p == NULL)
	{
		r->next = q;

	}
	else
	{
		r->next = p;
	}	

	
	return 0;
}

test.c

#include "linklist.h"
#include <stdio.h>


int test_get(void);
int test_insert(void);
int test_delete(void);
int test_free(void);
int test_reverse(void);
int test_adjmax(void);
int test_merge(void);
int main(int argc, char *argv[])
{
	test_merge();
	return 0;
}

int test_get(void)
{
	linklist H;
	linklist p;
	int value;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);

	p = list_get(H, 0);
	if(p != NULL)
	{
	printf("value:%d\n", p->data);
	}

}
int test_insert(void)
{

	linklist H;
	linklist p;
	int value;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);

	list_insert(H, 100, 4);
	printf("*******************************\n");
	list_show(H);
	if(p != NULL)
	{
	printf("value:%d\n", p->data);
	}
}
int test_delete(void)
{
	linklist H;
	linklist p;
	int value;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);

	list_delete(H, 2);
	printf("*******************************\n");
	list_show(H);

}
int test_free(void)
{
	linklist H;
	linklist p;
	int value;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);

	list_delete(H, 2);
	printf("*******************************\n");
	list_show(H);
	printf("free before:H's position:%p\n",H);
	H = list_free(H);
	printf("free after:H's position:%p\n",H);


}
int test_reverse(void)
{
	linklist H;
	linklist p;
	int value;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);
	
	list_reverse(H);
	printf("*******************************\n");
	list_show(H);
	H = list_free(H);
}

int test_adjmax(void)
{
	linklist H;
	linklist p;
	linklist r;
	int value;
	int sum;
	H = list_create();
	
	if(H == NULL)
		return -1;

	printf("input:\n");
	while(1)
	{
		scanf("%d",&value);
		if(value == -1)
			break;
		list_tail_insert(H, value);
		printf("input:\n");
	}
	list_show(H);
	
	r = list_adjmax(H, &sum);
	if( r != NULL && r != H)
	{
		printf("data is %d\n", r->data);
		printf("max is %d\n", sum);
	}
	list_show(H);
	H = list_free(H);
}
int test_merge(void)
{
	linklist H1;
	linklist H2;
	int i;

	int a[] = {1, 4, 6, 8, 10};
	int b[] = {2, 4, 8, 16, 20};

	H1 = list_create();
	H2 = list_create();
	if(H1 == NULL)
	{
		return -1;
	}
	if(H2 == NULL)
	{
		return -1;
	}
	for(i = 0; i < sizeof(a)/sizeof(int); i++)
	{
		list_tail_insert(H1, a[i]);
	}
	for(i = 0; i < sizeof(b)/sizeof(int); i++)
	{
		list_tail_insert(H2, b[i]);
	}

	list_show(H1);
	list_show(H2);

	list_merge(H1, H2);

	list_show(H1);
	list_show(H2);
	

	list_free(H1);
	list_free(H2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aurora Smith

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值