Data structure linear table exercise 3

table of Contents

Ha and Hb are the head pointers of the two leading node linked lists, which respectively represent two sets A and B, and the element values ​​of the two linked lists are increasing in order. Now it is required to construct a new linked list of leading nodes with zero development space, whose head pointer is Hc, and the elements of the linked list are the intersection of AB, and are in increasing order. Try to write an algorithm for establishing a new linked list.

Try to write an algorithm to delete all elements in the leading node linked list L1 from the leading node linked list L2

Ha and Hb are the head pointers of the two leading node linked lists, which respectively represent two sets A and B, and the element values ​​of the two linked lists are increasing in order. Now it is required to construct a new linked list of leading nodes with zero development space, whose head pointer is Hc, and the elements of the linked list are the intersection of AB, and are in increasing order. Try to write an algorithm for establishing a new linked list.

// ConsoleApplication8.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
using namespace std;
struct Node {
    
    
	int a;
	struct Node *next;
};
//输出链表元素
void show(Node &head,string name) {
    
    
	cout << "链表"<< name.c_str()<<":"<<endl;
	Node * first;
	first = head.next;
	while (first != NULL) {
    
    
		printf("%d ", first->a);
		first = first->next;
	}
	cout << endl;
}
//抽取两个链表中的共同元素!
Node join(Node &Ha, Node &Hb) {
    
    
	Node *pa;
	pa = (Node*)malloc(sizeof(Node));
	pa = Ha.next;
	Node *pb;
	pb = (Node*)malloc(sizeof(Node));
	pb = Hb.next;
	Node *pc;
	pc = (Node*)malloc(sizeof(Node));
	Node *r;
	r = pc;
	while(pa!=NULL&&pb!=NULL){
    
    //扫描单链表HA,Hb
		if (pa->a < pb->a) {
    
    
			pa = pa->next;//当Ha中元素小于Hb中元素 Ha指针下移
		}
		else if (pa->a > pb->a) {
    
    
			pb = pb->next;
		}
		else {
    
    
			Node *H;
			H = (Node*)malloc(sizeof(Node));
			H->a=pa->a;
			r->next = H;
			r = H;
			pa = pa->next;
			pb = pb->next;
		}

	}
	r->next = NULL;
	return *pc;
}

int main()
{
    
    
  //尾插法建立链表Ha,元素个数为5 元素为递增输入
	Node *Ha;
	Node *r;
	Ha = (Node*)malloc(sizeof(Node));
	r = Ha;
	for (int i = 0; i < 5; i++) {
    
    
		Node *H;
		H = (Node*)malloc(sizeof(Node));
		cin >> H->a;
		r->next = H;
		r = H;

	}
	r->next = NULL;
  //尾插法建立链表Hb 元素个数为4  元素为递增输入
	Node *Hb;
	Hb = (Node*)malloc(sizeof(Node));
	r = Hb;
	for (int i = 0; i < 4; i++) {
    
    
		Node *H;
		H = (Node*)malloc(sizeof(Node));
		cin >> H->a;
		r->next = H;
		r = H;

	}
	r->next = NULL;
	//输出Ha链表中的元素
	show(*Ha, "Ha");
	//输出链表Hb中的元素
	show(*Hb, "Hb");
	//建立新链表的头结点
	Node *Hc;
	Hc = (Node*)malloc(sizeof(Node));
	*Hc=join( *Ha, *Hb);
	//输出Hc链表中的元素
	show(*Hc, "Hc");
}


Insert picture description here

Try to write an algorithm to delete all elements in the leading node linked list L1 from the leading node linked list L2

// ConsoleApplication8.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
using namespace std;
struct Node {
    
    
	int a;
	struct Node *next;
};
//输出链表元素
void show(Node &head, string name) {
    
    
	cout << "链表" << name.c_str() << ":" << endl;
	Node * first;
	first = head.next;
	while (first != NULL) {
    
    
		printf("%d ", first->a);
		first = first->next;
	}
	cout << endl;
}
//在带头结点的链表L1中删除出现在L2中的元素
void deleteNode(Node &L1,Node &L2) {
    
    
	//两层循环,第一层循环,对L2进行循环,第二层循环对L1进行循环
	Node *p;
	Node *q;
	Node *r;     //删除结点
	Node *h;	//删除结点的钱指针
	p = L2.next;
	//外层循环
	int i = 0;
	while (p != NULL) {
    
    
		h = &L1;
		q = L1.next;
		while (q!=NULL) {
    
    
			if (q->a == p->a) {
    
    			
				r = q;
				h->next = q->next;
				q = q->next;
				free(r);
			}
			else {
    
    
				h = h->next;
				q = q->next;
			}
		}
		p = p->next;
	}
}
int main()
{
    
    
	//尾插法建立链表L1,元素个数为5 元素为递增输入
	Node *Ha;
	Node *r;
	printf("请输入链表L1的5个元素:");
	Ha = (Node*)malloc(sizeof(Node));
	r = Ha;
	for (int i = 0; i < 5; i++) {
    
    
		Node *H;
		H = (Node*)malloc(sizeof(Node));
		cin >> H->a;
		r->next = H;
		r = H;

	}
	r->next = NULL;
	//尾插法建立链表L2 元素个数为4  元素为递增输入
	printf("请输入链表L2的4个元素:");
	Node *Hb;
	Hb = (Node*)malloc(sizeof(Node));
	r = Hb;
	for (int i = 0; i < 4; i++) {
    
    
		Node *H;
		H = (Node*)malloc(sizeof(Node));
		cin >> H->a;
		r->next = H;
		r = H;

	}
	r->next = NULL;

	//在带头结点的链表L1中删除出现在L2中的元素
	deleteNode(*Ha, *Hb);
	//输出Ha链表中的元素
	show(*Ha, "L1");
}



Insert picture description here

Guess you like

Origin blog.csdn.net/qq_41827511/article/details/106182927