double free detected in tcache 2问题记录

 问题:

给定两个非递减的整数序列A和B,利用链表表示序列A和B,将A和B合并为一个非递增的有序序列C,序列C允许有重复的数据。要求空间复杂度为O(1)。

输入
多组数据,每组数据有三行,第一行为序列A和B的长度n和m,第二行为序列A的n个元素,第三行为序列B的m个元素(元素之间用空格分隔)。n=0且m=0时输入结束。

输出
对于每组数据输出一行,为合并后的序列,每个数据之间用空格分隔。

测试样例:

输入
5 5
1 3 5 7 9
2 4 6 8 10
5 6
1 2 2 3 5
2 4 6 8 10 12
0 0
输出
10 9 8 7 6 5 4 3 2 1
12 10 8 6 5 4 3 2 2 2 1 

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
 
/*
这很明确为一个简单的,链表输入,栈输出问题!
*/
typedef struct Llist *List;
typedef struct Llist *Stack;
struct Llist{
	int data;
	struct Llist *next;
};
 
List readL(int num);
void attach(int num, List* rear);
List crt_pare(List a, List b);
void push_stack(int data, Stack S);
int pop_stack(Stack S);
int compare(int a, int b);
void print_result(List P);
void free_link(List *PtrL);
 
int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m) == 2 && (n || m)){
		
		List PtrL1, PtrL2;
		Stack PtrL3;
		PtrL1 = readL(n);
        PtrL2 = readL(m);
		if(!PtrL1 && !PtrL2){
			printf("error 1 not read !");
			break;
		}
		PtrL3 = crt_pare(PtrL1, PtrL2);
		free_link(&PtrL1);
		free_link(&PtrL2);
		
		print_result(PtrL3);
	}
	return 0;
}
List readL(int num)
{
	List ptrL, rearL, temp;
	int r_n;
	
	ptrL = (List)malloc(sizeof(struct Llist));
	ptrL ->next = NULL;
	rearL = ptrL;
	for(int i = num; i > 0; i--){
		scanf("%d", &r_n);
		attach(r_n, &rearL);
	}
	temp = ptrL;
	ptrL = ptrL->next;
	free(temp);
	return ptrL;
}
List crt_pare(List a, List b)
{
	List c_1, c_2;
	Stack c_3, c_temp;
	int a_flag = 0, b_flag = 0;
	int c1, c2;
	Stack PtrL;
	c_1 = a; c_2 = b;
	if(c_1 == NULL)
	{
		c_1 = (List)malloc(sizeof(struct Llist));
		c_1 ->data = INT_MAX;
		c1 = 1;
	}
	if(c_2 == NULL)
	{
		c_2 = (List)malloc(sizeof(struct Llist));
		c_2 ->data = INT_MAX;
		c2 = 1;
	}
	c_3 = (Stack)malloc(sizeof(struct Llist));
	c_3 ->next = NULL;
	PtrL = c_3;
	while(1){
		if(c_1 ->next == NULL){
			a_flag = 1;
		}
		if(c_2 ->next == NULL){
			b_flag = 1;
		}			
		switch(compare(c_1 ->data, c_2 ->data)){
			case 1: push_stack(c_2 ->data, PtrL);
					if(b_flag == 0){
						c_2 = c_2 ->next;
					}else{
						c_2 ->data = INT_MAX;
					}	
					break;
					
			case 0: push_stack(c_1 ->data, PtrL);
					if(a_flag == 0){
						c_1 = c_1 ->next;
					}else{
						c_1 ->data = INT_MAX;
					}
					break;
		}
		if(c_1 ->data == INT_MAX && c_2 ->data == INT_MAX){
			break;
		}
	}
	if(c1 == 1){
		free(c_1);
	}
	if(c2 == 1){
		free(c_2);
	}
	return c_3;
}
void attach(int num, List* prear){
	List PtrL;
	PtrL = (List)malloc(sizeof(struct Llist));
	PtrL ->next = NULL;
	PtrL ->data = num;
	(*prear) ->next = PtrL;
	*prear = PtrL;
}
int compare(int a, int b)
{
	if(a >= b)
		return 1;
	else
		return 0;
}
void print_result(Stack P)
{
	int n;
	if (!P)
		printf("^v^!!!, error, <..>");
	while(P ->next){
		n = pop_stack(P);
		printf("%d ",n);
	}
	printf("\n");
}
 
void push_stack(int data, Stack S)
{
	Stack new_s;
	new_s = (Stack)malloc(sizeof(struct Llist));
	new_s ->data = data;
	new_s ->next = S ->next;
	S ->next = new_s;
}
int pop_stack(Stack S)
{
	Stack p_s;
	int top;
	if(S ->next == NULL){
		printf("已经全部输出了\n");
		return -1;
	}else{
		p_s = S ->next;
		S ->next = p_s ->next;
		top = p_s ->data;
		free(p_s);
		return top;
	}
}
void free_link(List *PtrL)
{
	List temp;
	if(!(*PtrL)){
		return ;
	}
	while(*PtrL){
		temp = *PtrL;
		(*PtrL) = (*PtrL) ->next;
		free(temp);
	}
}

5 5
1 3 5 7 9
2 4 6 8 10
10 9 8 7 6 5 4 3 2 1
5 6
1 2 2 3 5
2 4 6 8 10 12
12 10 8 6 5 4 3 2 2 2 1
0 0
即正确运行,

free(): double free detected in tcache 2
/1222/sample.in:Aborted
free(): double free detected in tcache 2
/1222/test1.in:Aborted
free(): double free detected in tcache 2
/1222/test2.in:Aborted
于是我展开下面的从新调试

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
 
/*
这很明确为一个简单的,链表输入,栈输出问题!
*/
typedef struct Llist *List;
typedef struct Llist *Stack;
struct Llist{
	int data;
	struct Llist *next;
};
 
List readL(int num);
void attach(int num, List* rear);
List crt_pare(List a, List b);
void push_stack(int data, Stack S);
int pop_stack(Stack S);
int compare(int a, int b);
void print_result(List P);
void free_link(List *PtrL);
 
int main()
{
	int n, m;
	while(scanf("%d%d", &n, &m) == 2 && (n || m)){
		
		List PtrL1, PtrL2;
		Stack PtrL3;
		PtrL1 = readL(n);
		PtrL2 = readL(m);

        if(!PtrL1 && !PtrL2){
			printf("error 1 not read !");
			break;
		}
		PtrL3 = crt_pare(PtrL1, PtrL2);
		free_link(&PtrL1);
		free_link(&PtrL2);
		
		print_result(PtrL3);
		if(!PtrL1){
			printf("\nPtrL1 已销毁!");
		}
		if(!PtrL2){
			printf("\nPtrL2 已销毁!");
		}
		if(!(PtrL3 ->next)){
			printf("\nPtrL3 已销毁!");
		}
	}
	return 0;
}
List readL(int num)
{
	List ptrL, rearL, temp;
	int r_n;
	
	ptrL = (List)malloc(sizeof(struct Llist));
	ptrL ->next = NULL;
	rearL = ptrL;
	for(int i = num; i > 0; i--){
		scanf("%d", &r_n);
		attach(r_n, &rearL);
	}
	temp = ptrL;
	ptrL = ptrL->next;
	free(temp);
	return ptrL;
}
List crt_pare(List a, List b)
{
	List c_1, c_2;
	Stack c_3, c_temp;
	int a_flag = 0, b_flag = 0;
	int c1, c2;
	Stack PtrL;
	c_1 = a; c_2 = b;
	if(c_1 == NULL)
	{
		c_1 = (List)malloc(sizeof(struct Llist));
		c_1 ->data = INT_MAX;
		c1 = 1;
	}
	if(c_2 == NULL)
	{
		c_2 = (List)malloc(sizeof(struct Llist));
		c_2 ->data = INT_MAX;
		c2 = 1;
	}
	c_3 = (Stack)malloc(sizeof(struct Llist));
	c_3 ->next = NULL;
	PtrL = c_3;
	while(1){
		if(c_1 ->next == NULL){
			a_flag = 1;
		}
		if(c_2 ->next == NULL){
			b_flag = 1;
		}			
		switch(compare(c_1 ->data, c_2 ->data)){
			case 1: push_stack(c_2 ->data, PtrL);
					if(b_flag == 0){
						c_2 = c_2 ->next;
					}else{
						c_2 ->data = INT_MAX;
					}	
					break;
					
			case 0: push_stack(c_1 ->data, PtrL);
					if(a_flag == 0){
						c_1 = c_1 ->next;
					}else{
						c_1 ->data = INT_MAX;
					}
					break;
		}
		if(c_1 ->data == INT_MAX && c_2 ->data == INT_MAX){
			break;
		}
	}
	if(c1 == 1){
		free(c_1);
	}
	if(c2 == 1){
		free(c_2);
	}
	return c_3;
}
void attach(int num, List* prear){
	List PtrL;
	PtrL = (List)malloc(sizeof(struct Llist));
	PtrL ->next = NULL;
	PtrL ->data = num;
	(*prear) ->next = PtrL;
	*prear = PtrL;
}
int compare(int a, int b)
{
	if(a >= b)
		return 1;
	else
		return 0;
}
void print_result(Stack P)
{
	int n;
	if (!P)
		printf("^v^!!!, error, <..>");
	while(P ->next){
		n = pop_stack(P);
		printf("%d ",n);
	}
	printf("\n");
}
 
void push_stack(int data, Stack S)
{
	Stack new_s;
	new_s = (Stack)malloc(sizeof(struct Llist));
	new_s ->data = data;
	new_s ->next = S ->next;
	S ->next = new_s;
}
int pop_stack(Stack S)
{
	Stack p_s;
	int top;
	if(S ->next == NULL){
		printf("已经全部输出了\n");
		return -1;
	}else{
		p_s = S ->next;
		S ->next = p_s ->next;
		top = p_s ->data;
		printf("已销毁元素 %d\n",p_s ->data);
		free(p_s);
		return top;
	}
}
void free_link(List *PtrL)
{
	List temp;
	if(!(*PtrL)){
		return ;
	}
	while(*PtrL){
		temp = *PtrL;
		(*PtrL) = (*PtrL) ->next;
		printf("已销毁元素 %d\n",temp ->data);
		free(temp);
	}
}

5 5
1 3 5 7 9
2 4 6 8 10

已销毁元素 1

已销毁元素 3

已销毁元素 5

已销毁元素 7

已销毁元素 2147483647

已销毁元素 2

已销毁元素 4

已销毁元素 6

已销毁元素 8

已销毁元素 2147483647

已销毁元素 10
10
已销毁元素 9
9
已销毁元素 8
8
已销毁元素 7
7
已销毁元素 6
6
已销毁元素 5
5
已销毁元素 4
4
已销毁元素 3
3
已销毁元素 2
2
已销毁元素 1
1

PtrL1 已销毁!
PtrL2 已销毁!
PtrL3 已销毁!5 6
1 2 2 3 5
2 4 6 8 10 12

已销毁元素 1

已销毁元素 2

已销毁元素 2

已销毁元素 3

已销毁元素 2147483647

已销毁元素 2

已销毁元素 4

已销毁元素 6

已销毁元素 8

已销毁元素 10

已销毁元素 2147483647

已销毁元素 12
12
已销毁元素 10
10
已销毁元素 8
8
已销毁元素 6
6
已销毁元素 5
5
已销毁元素 4
4
已销毁元素 3
3
已销毁元素 2
2
已销毁元素 2
2
已销毁元素 2
2
已销毁元素 1
1

PtrL1 已销毁!
PtrL2 已销毁!
PtrL3 已销毁!0 0

  • 15
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。该系统主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者,包含项目源码、数据库脚本、项目说明等,有论文参考,可以直接作为毕设使用。 后台框架采用SpringBoot,数据库使用MySQL,开发环境为JDK、IDEA、Tomcat。项目经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。 该系统的功能主要包括商品管理、订单管理、用户管理等模块。在商品管理模块中,可以添加、修改、删除商品信息;在订单管理模块中,可以查看订单详情、处理订单状态;在用户管理模块中,可以注册、登录、修改个人信息等。此外,系统还提供了数据统计功能,可以对销售数据进行统计和分析。 技术实现方面,前端采用Vue框架进行开发,后端使用SpringBoot框架搭建服务端应用。数据库采用MySQL进行数据存储和管理。整个系统通过前后端分离的方式实现,提高了系统的可维护性和可扩展性。同时,系统还采用了一些流行的技术和工具,如MyBatis、JPA等进行数据访问和操作,以及Maven进行项目管理和构建。 总之,本系统是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。系统经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值