北理mooc题——链表合并

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

题目内容:

    实现两个由单项链表存储的有序字母数据的合并,如有重复的则只保留一个。

    例如:给定{a, c ,f}, { b, e, g}合并后结果为{a, b, c , e , f , g}

输入格式:

    两个有序字母数据

输出格式:

    合并后的字母数据


输入样例1:

a b c[回车]

d e f[回车]

输出样例1:

a b c d e f[回车]

输入样例2:

e f g[回车]

e g m[回车]

输出样例2:

e f g m[回车]


方法一:先对比两链表,再合并:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct link{

	char abc;
	struct link *next;
}; 

typedef struct link LK;

LK *start(char *q)
{
	LK *p1,*p2,*head;
	int i=0;
	char x;
	head=NULL;
	while(*(q+i)!='\0')
	{
		i++;
		if(*(q+i-1)<'a'||*(q+i-1)>'z') continue;
		p1=(LK*)malloc(sizeof(LK));
		p1->abc=*(q+i-1);
		if(head==NULL) {
			head=p1;
			p2=p1;
		}
		else {
			p2->next=p1;
			p2=p1;
		}
		p2->next=NULL;
		
	}
	return head;
}



LK *del(LK **head,LK *p,int k)
{
	int i;
	LK *q;
	q=head; 
	if(k==0)
	{
		*head=p->next;
		p=p->next;
		return p;
	}

	for(i=0;i<k-1;i++)
	{
		q=q->next;
	}
	q->next=p->next;
	free(p);
	return q->next;
	
}


void output(LK *head)
{
	LK *p;
	p=head;
	while(p!=NULL)
	{
		printf("%c",p->abc);
		if(p->next!=NULL) printf(" ");
		else printf("\n");
		p=p->next;
		
	}
	
	
	
}

int main(int argc, char *argv[]) {
	LK *p1,*p2,*head1,*head2;
	int i,j=0;
	char a[20],b[20],*q;
	q=a;
	gets(a);
	p1=start(q);
	head1=p1;
	q=b;
	gets(b);
	p2=start(q);
	head2=p2;
	while(p1!=NULL)
	{
		while(p2!=NULL)
		{
			
			if(p1->abc==p2->abc)
			{
				p2=del(&head2,p2,j);
			}
			else
			{
				j++;
				p2=p2->next;
			}
		}
		j=0;
		p2=head2;

		if(p1->next!=NULL)p1=p1->next;
		else break;
	}

	p1->next=head2;
	output(head1);

	return 0;
}

方法二:先合并链表,再进行内部对比删减:

这样做可以排除两链表内部的重复。


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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct link{

	char abc;
	struct link *next;
}; 

typedef struct link LK;

LK *start(char *q)
{
	LK *p1,*p2,*head;
	int i=0;
	char x;
	head=NULL;
	while(*(q+i)!='\0')
	{
		i++;
		if(*(q+i-1)<'a'||*(q+i-1)>'z') continue;
		p1=(LK*)malloc(sizeof(LK));
		p1->abc=*(q+i-1);
		if(head==NULL) {
			head=p1;
			p2=p1;
		}
		else {
			p2->next=p1;
			p2=p1;
		}
		p2->next=NULL;
		
	}
	return head;
}



LK *del(LK *head,LK *p,int k)
{
	int i;
	LK *x,*q;
	q=head; 
//	if(k==0)
//	{
//		*head=p->next;
//		p=p->next;
//		return p;
//	}

	for(i=0;i<k-1;i++)
	{
		q=q->next;
	}
	q->next=p->next;
	free(p);
	return q->next;
	
}




void output(LK *head)
{
	LK *p;
	p=head;
	while(p!=NULL)
	{
		printf("%c",p->abc);
		if(p->next!=NULL) printf(" ");
		else printf("\n");
		p=p->next;
		
	}
	
	
	
}

int main(int argc, char *argv[]) {
	LK *p1,*p2,*head1,*head2,*temp;
	int i,j=0;
	char a[20],b[20],*q;
	q=a;
	gets(a);
	p1=start(q);
	head1=p1;
	q=b;
	gets(b);
	p2=start(q);
	head2=p2;
	while(p1->next!=NULL)
	{
		p1=p1->next;
	}
	p1->next=head2;

	p1=head1;
	p2=head1;
	head2=p1->next;
	while(p1->next!=NULL)
	{
		while(p2!=NULL)
		{
			if(p1->abc==p2->abc)
			{
				if(j==0){
					p1->next=p2->next;
					temp=p2;
					p2=p1->next;
					head2=p1->next;
/*					free(temp);  备注:此处加上free(temp)就会无限循环,可能跟用了野指针有关(即使只进入此循环一/次),只得暂时注释掉,留待解决方案   */
				}
				else p2=del(head2,p2,j);
			}
			else
			{
				j++;
				p2=p2->next;
			}	
		}
		j=0;
		p1=p1->next;
		p2=p1->next;
		head2=p1->next;
	}
	
	output(head1);

	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_38908061/article/details/72853805