Question z: C language programming tutorial (third edition) after-school exercises 11.8

Topic description:
There are two linked lists, a and b, and the nodes in each linked list include good learning and achievement. It is required to merge the two linked lists and arrange them in ascending order of student number.

Input: the
first line, the number N and M of the two linked list elements a and b, separated by spaces. The next N rows are the data of a and the M rows are the data of b. Each row of data consists of two parts: student number and grade.

Output:
data sorted in ascending order of student number

Sample input:
2 3
5 100
6 89
3 82
4 95
2 10

Sample output:
2 10
3 82
4 95
5 100
6 89

Problem-solving idea:
After the two linked lists of a and b are established by the tail interpolation method, the two linked lists are sorted in ascending order according to the member ids in the structure by bubble sorting; then the merge operation is performed.

c reference code (lead node):

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

int n,m;

typedef struct Node{
    
    
	int id;
	int grade;
	struct Node *next;
}N,*node;

node create(int n)
{
    
    
    node head;
    head=(node)malloc(sizeof(N));
    head->next=NULL;
    
	node rear;
	node s;
	rear=head;
	
	int i,t1,t2;
	for(i=0;i<n;i++)
	{
    
    
		s=(node)malloc(sizeof(N));
		scanf("%d%d",&s->id,&s->grade);
		s->next=NULL;
		
	    rear->next=s;
		rear=s;	

	}
	
	return head;
}

//void Bubblesort(node p1,node p2)
//{
    
    
//	int i,j,temp1,temp2;
//	node p;
//	
//	for(i=0;i<n-1;i++)
//	{
    
    
//		for(p=p1->next;p->next!=NULL;p=p->next)
//		{
    
    
//			if(p->id>p->next->id)
//			{
    
    
//				temp1=p->id;
//				p->id=p->next->id;
//				p->next->id=temp1; 
//			
//			    temp2=p->grade;
//				p->grade=p->next->grade;
//				p->next->grade=temp2; 
//			}
//		} 
//	}
//	
//	for(j=0;j<m-1;j++)
//	{
    
    
//		for(p=p2->next;p->next!=NULL;p=p->next)
//		{
    
    
//			if(p->id>p->next->id)
//			{
    
    
//				temp1=p->id;
//				p->id=p->next->id;
//				p->next->id=temp1;
//				
//				temp2=p->grade;
//				p->grade=p->next->grade;
//				p->next->grade=temp2; 
//			}
//		}
//	}
//}

void sort(node p1,node p2)
{
    
    
	int temp1,temp2;
	node A;
	node B;
	for(A=p1->next;A!=NULL;A=A->next)
	{
    
    
		for(B=A->next;B!=NULL;B=B->next)
		{
    
    
			if(A->id>B->id)
			{
    
    
				temp1=A->id;
				A->id=B->id;
				B->id=temp1;
				
				temp2=A->grade;
				A->grade=B->grade;
				B->grade=temp2;
			}
		}
	}
	
	for(A=p2->next;A!=NULL;A=A->next)
	{
    
    
		for(B=A->next;B!=NULL;B=B->next)
		{
    
    
			if(A->id>B->id)
			{
    
    
				temp1=A->id;
				A->id=B->id;
				B->id=temp1;
				
				temp2=A->grade;
				A->grade=B->grade;
				B->grade=temp2;
			}
		}
	}
}

node link(node P1,node P2)
{
    
    
	node head=(node)malloc(sizeof(N));
	node p=head;
	node p1=P1->next;
	node p2=P2->next;
	
	while(p1!=NULL&&p2!=NULL)
	{
    
    
		if(p1->id<p2->id)
		{
    
    
			p->next=p1;
			p=p1;
			p1=p1->next;
		}
		else
		{
    
    
			p->next=p2;
			p=p2;
		    p2=p2->next;
		}
	}
	if(p1!=NULL)
	 p->next=p1;
	else
	 p->next=p2;
	 
	return head;
}

void print(node p)
{
    
    
   while(p->next!=NULL)
   {
    
    
   	printf("%d %d\n",p->next->id,p->next->grade);
   	p=p->next;
   }
}

int main()
{
    
    
	scanf("%d%d",&n,&m);
	node a,b,c;

	a=create(n);
	b=create(m);
	
	sort(a,b);
	c=link(a,b);
	print(c);
	return 0;
}

Reference:
Portal

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/114694099