The data structure breaks down a single chain of leading nodes into two single linked list __ data structure.

Source: Internet
Author: User

The single linked list of a leading node is decomposed into two single linked lists.

Topic Description:

/*
An algorithm is designed to decompose a single linked list of a leading node into two identically structured
List B and C, where B-white node is less than 0 of the node in a, C table nodes for a node greater than 0,
Requirements B and C still utilize the nodes of table A. (The Elements of Table A are all 0 elements)
*/


Algorithm thought: '

Let's say the original losing streak Oh is LA, will la, decomposed into LB and LC, first you need to generate two head nodes, LB and LC; Set three pointer *pa,*pb,*pc, initially, the PA points to the first node of LA, Pb points to the head node of LB, the PC points to the head node of the LC; then traverse LA, When pa->data>0, the node that the PA points to is inserted at the back of the LB, that is, PB->NEXT=PA, then the PB points to the node, that is Pb=pb->next, the pointer pa is moved pa=pa->next, The pointer field of the last node of the table lb is empty. When pa->data<0, the node that the PA points to is inserted behind the LC, that is, PC->NEXT=PA, and then the PC points to the node, that is Pc=pc->next, the pointer pa is moved pa=pa->next, The pointer field of the last node of the table LC is empty.

The description is as follows:

void Resolve (linklist &la,linklist &lb,linklist &lc) {struct Lnode
	;
	pa=la->next;
	Lb=new Lnode;
	Lc=new lnode;//to generate two new header nodes ... 
	//lb=lc=la;//can't write like this, so write the final output of the LB and lchi the same table, incredibly will be the same. Why.. 
	pb=lb;
	PC=LC;
	struct Lnode *p;
	P=PA;
	while (PA) {
		if (pa->data>0) {
			pb->next=pa;
			pa=pa->next;
			pb=pb->next;
			pb->next=null;
		} else if (pa->data<0) {
			pc->next=pa;
			pa=pa->next;
			pc=pc->next;
			pc->next=null;}}}



Realize:

/* Design an algorithm that decomposes a single list of leading nodes into two linked lists B and C with the same structure. where B-white node for a node of less than 0 in a, C-table node for a node greater than 0 in a, requiring B and C still use a table node.
	(a table elements are not 0 elements)/#include <stdio.h> #define MAX typedef struct lnode{int data;
struct Lnode *next;

}lnode,*linklist;
	int initlist (linklist &l) {l=new lnode;
	l->next=null;
return 1;
	int Listlength (linklist L) {int length=0;
	struct Lnode *p;
	p=l->next;
		while (p) {++length;
	p=p->next;
return length;
	} void Travelist (linklist L) {struct Lnode *p;
	p=l->next;
		while (p) {printf ("%d", p->data);
	p=p->next;
printf ("\ n");
	} void CreateList (linklist &l,int N) {l=new lnode;
	l->next=null;
	struct Lnode *p;
	P=l;
		for (int i=0;i<n;i++) {struct Lnode *s;
		S=new Lnode;
		printf ("Please enter the value of%d nodes:", i+1);
		scanf ("%d", &s->data);
		s->next=null;
		p->next=s;
	P=s;
	} void Resolve (linklist &la,linklist &lb,linklist &lc) {struct lnode;
	pa=la->next;
	Lb=new Lnode; 
	Lc=new lnode;//to generate two new header nodes ... lb=lc=la;//can't write like this, so write the final output of the LB and lchi the same table, incredibly will be the same. 
	Why..
	pb=lb;
	PC=LC;
	struct Lnode *p;
	P=PA;
			while (PA) {if (pa->data>0) {pb->next=pa;
			pa=pa->next;
			pb=pb->next;
		pb->next=null;
			}else if (pa->data<0) {pc->next=pa;
			pa=pa->next;
			pc=pc->next;
		pc->next=null;
	
	[}} int main () {linklist la,lb,lc;
	if (linklist (LA)) {printf ("La initialization succeeds!\n");
	}else{printf ("La initialization failed!\n");
	} if (linklist (lb)) {printf ("lb initialization succeeded!\n");
	}else{printf ("LB initialization failure!\n");
	} if (Linklist (LC)) {printf ("Successful LC initialization!\n");
	}else{printf ("LC initialization failed!\n");
	printf ("Please enter the length of LA:");
	int N1;
	scanf ("%d", &n1);
	CreateList (LA,N1);
	
	Travelist (LA);
	Resolve (LA,LB,LC);
	Travelist (LB);
	
	Travelist (LC);
return 0; }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.