Data structure linear table exercise 2

Design an algorithm to decompose the singly linked list Ha with the lead node into two linked lists Hb and Hc with the same structure. The nodes in the Hb table are nodes with a value less than zero, and the nodes in the Hc table are nodes with a value greater than or equal to 0 in the Ha table (the type of the node value in Ha is integer)

#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<stdio.h>
struct Node{
    
    
	int a;
	struct Node *next;
};
//创建函数
void MargeList_L(Node &La, Node &Lb, Node &Lc) {
    
    
	Node *H = (Node*)malloc(sizeof(Node));
	H = La.next;
	Lb.a = 0;
	Lc.a = 0;
	while (H != NULL) {
    
    
		Node *p = (Node*)malloc(sizeof(Node));
		if (H->a < 0) {
    
    
			p->a = H->a;
			p->next = Lb.next;
			Lb.next = p;
			Lb.a++;
		}
		else {
    
    
			p->a = H->a;
			p->next = Lc.next;
			Lc.next = p;
			Lc.a++;
		}
		H = H->next;
	}
}
int main()
{
    
    
	printf("依次输入链表Ha的值(链表长度为10):\n");
	//创建头节点
    Node *Ha = (Node*)malloc(sizeof(Node));
	Ha->next = NULL;
	int i=0;
	Ha->a = 10;
	while (i<10) {
    
    
		//创建插入节点
		Node *H = (Node*)malloc(sizeof(Node));
		scanf("%d", &H->a);
		H->next = Ha->next;
		Ha->next = H;
		i++;
	}
    /*	
    //输出链表Ha
	Node *H = (Node*)malloc(sizeof(Node));
	H = Ha->next;
	while(H!=NULL){
		printf("%d\n", H->a);
		H = H->next;
	}*/
	//算法开始
	//创建Hb,Hc 链表头节点
	Node *Hb= (Node*)malloc(sizeof(Node));
	Node *Hc = (Node*)malloc(sizeof(Node));
	Hb->next = NULL;
	Hc->next = NULL;
	MargeList_L(*Ha, *Hb, *Hc);
	printf("链表Hb的长度:%d\n链表Hc的长度%d\n", Hb->a, Hc->a);
	printf("链表Hb\n");
	Node *hb = (Node*)malloc(sizeof(Node));
	hb = Hb->next;
	while (hb != NULL) {
    
    
		printf("%d\n", hb->a);
		hb = hb->next;
	}
	printf("链表Hc\n");
	Node *hc = (Node*)malloc(sizeof(Node));
	hc = Hc->next;
	while (hc != NULL) {
    
    
		printf("%d\n", hc->a);
		hc = hc->next;
	}
	
}

Summary of test questions

Linked list created by head insertion method

Node *H = (Node*)malloc(sizeof(Node));
		scanf("%d", &H->a);
		H->next = Ha->next;
		Ha->next = H;

The test case is to constantly give a value to return the result, which needs to continuously obtain data from scanf, the method is

while(scanf("%d",&n)!=EOF){
    
    }

Use EOF to determine whether the input is over.

Passed in JAVA

while(scanner.hasNext()){
    
    }

To judge.
Structure

Try to write an efficient algorithm to delete a minimum node from the singly linked list of the head node

//试编写在头节点的单链表中删除一个最小值结点的高效算法
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<stdio.h>
struct Node{
    
    
	int a;
	struct Node *next;
};
/*//创建函数
void MargeList_L(Node &La, Node &Lb, Node &Lc) {
	Node *H = (Node*)malloc(sizeof(Node));
	H = La.next;
	Lb.a = 0;
	Lc.a = 0;
	while (H != NULL) {
		Node *p = (Node*)malloc(sizeof(Node));
		if (H->a < 0) {
			p->a = H->a;
			p->next = Lb.next;
			Lb.next = p;
			Lb.a++;
		}
		else {
			p->a = H->a;
			p->next = Lc.next;
			Lc.next = p;
			Lc.a++;
		}
		H = H->next;
	}
}*/
int main()
{
    
    
	printf("依次输入链表Ha的值(链表长度为10):\n");
	//创建头节点
    Node *Ha = (Node*)malloc(sizeof(Node));
	int i=0;
	Ha->a = 10;
	//创建尾指针
	Node *r = (Node*)malloc(sizeof(Node));
	r = Ha;
	while (i<3) {
    
    
		//创建插入节点
		Node *H = (Node*)malloc(sizeof(Node));
		scanf("%d", &H->a);
		r->next = H;
		r = H;
		i++;
	}
	r->next =NULL;
    	
 
	Node *H = (Node*)malloc(sizeof(Node));
	H = Ha->next;
	int min;
	min = H->a;
	//创建一个最小值结点的前驱结点标靶
	Node *premin = (Node*)malloc(sizeof(Node));
	//创建一个最小值结点的前驱结点
	Node *pmin = (Node*)malloc(sizeof(Node));
	//创建一个节点,使该节点指向值最小的结点
	Node *minJ = (Node*)malloc(sizeof(Node));
	minJ = H;
	premin = Ha;
	pmin = premin;
	while(H!=NULL){
    
    
		//取第一个元素为最小值,对列表进行循环,判断!
		if (min > H->a) {
    
    
			min = H->a;
			minJ = H;
			pmin = premin;
		}
		premin = premin->next;
		H = H->next;
	}
	pmin->next = pmin->next->next;
	free(minJ);
	Node *H3 = (Node*)malloc(sizeof(Node));
	H3 = Ha->next;
	while (H3 != NULL) {
    
    
		printf("%d\n", H3->a);
		H3 = H3->next;
	}
	//算法开始
	/*//创建Hb,Hc 链表头节点
	Node *Hb= (Node*)malloc(sizeof(Node));
	Node *Hc = (Node*)malloc(sizeof(Node));
	Hb->next = NULL;
	Hc->next = NULL;
	MargeList_L(*Ha, *Hb, *Hc);
	printf("链表Hb的长度:%d\n链表Hc的长度%d\n", Hb->a, Hc->a);
	printf("链表Hb\n");
	Node *hb = (Node*)malloc(sizeof(Node));
	hb = Hb->next;
	while (hb != NULL) {
		printf("%d\n", hb->a);
		hb = hb->next;
	}
	printf("链表Hc\n");
	Node *hc = (Node*)malloc(sizeof(Node));
	hc = Hc->next;
	while (hc != NULL) {
		printf("%d\n", hc->a);
		hc = hc->next;
	}*/
	
}

Question analysis

Singly linked list

printf("依次输入链表Ha的值(链表长度为10):\n");
	//创建头节点
    Node *Ha = (Node*)malloc(sizeof(Node));
	int i=0;
	Ha->a = 10;
	//创建尾指针
	Node *r = (Node*)malloc(sizeof(Node));
	r = Ha;
	while (i<3) {
    
    
		//创建插入节点
		Node *H = (Node*)malloc(sizeof(Node));
		scanf("%d", &H->a);
		r->next = H;
		r = H;
		i++;
	}
	r->next =NULL;

operation result

Insert picture description here

Guess you like

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