Data Structure - 2.5 exercise lead to a single node of the linked list into two single chain

1. Problem Description

  • The algorithm is designed to take the lead in a single linked list node L1 divided into two lists having the same structure L2 , L3 , wherein L2 node table is L . 1 table value is less than zero node, L3 and the node table is L1 table node value greater than zero (list L1 elements are non-zero integer requiring L2 of , L3 table using node table L1).

2. Topic analysis

  • The first node L2 header table using the original node table L1, L3 table for the application for a new head node. Starting from the first node L1 table, each node p whichever turn, determines the value of the node p is smaller than 0, using the method of forward runs, the node will be less than 0 L1 insertion table, the junction greater than or equal to 0 L3 insertion point table.
// 链表分解
void DisCompose(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p;
	p = L1->next;      // p为工作指针
	
	L2 = L1;
	L2->next = NULL;   // L2表初始化
			
	L3 = new LNode;   // 为L3申请结点空间
	L3->next = NULL;    // L3初始化为空表
		
	while (p != NULL)
	{
		LNode *r;
		r = p->next;      // 暂存p的后继
		if (p->data < 0)
		{
			p->next = L2->next; 
			L2->next = p;       // 将小于0的结点链入L2表, 前插法
		}     
		else if (p->data > 0)
		{ 
			p->next = L3->next; 
			L3->next = p; 
		}           // 将大于等于0的结点链入C表, 前插法
				
		p = r;   // p指向新的待处理结点
	}
}

3. code implementation

  • main.cpp
#include <iostream>

using namespace std;

typedef struct LNode
{
	int data;
	struct LNode *next;
}LNode, *LinkList;

int InitList(LinkList &L)
{
	L = new LNode;
	L->next = NULL;
	return 1;
}

void TraveList(LinkList L)
{
	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;
	LNode *r;
	r = L;
	for (int i = 0; i < n; i++)
	{
		printf("请输入链表第%d个元素的值:", i + 1);
		LNode *s;
		s = new LNode;
		scanf("%d", &s->data);
		s->next = NULL;
		r->next = s;
		r = s;
	}
}

// 链表分解
void DisCompose(LinkList &L1, LinkList &L2, LinkList &L3)
{
	LNode *p;
	p = L1->next;      // p为工作指针
	
	L2 = L1;
	L2->next = NULL;   // L2表初始化
			
	L3 = new LNode;   // 为L3申请结点空间
	L3->next = NULL;    // L3初始化为空表
		
	while (p != NULL)
	{
		LNode *r;
		r = p->next;      // 暂存p的后继
		if (p->data < 0)
		{
			p->next = L2->next; 
			L2->next = p;       // 将小于0的结点链入L2表, 前插法
		}     
		else if (p->data > 0)
		{ 
			p->next = L3->next; 
			L3->next = p; 
		}           // 将大于等于0的结点链入C表, 前插法
				
		p = r;   // p指向新的待处理结点
	}
}
	
int main()
{
	LinkList L1, L2, L3;

	if (InitList(L1))
	{
		printf("L1初始化成功!\n");
	}
	else
	{
		printf("L1初始化失败!\n");
	}

	printf("请输入L1的长度:");
	int n1;
	scanf("%d", &n1);
	CreateList(L1, n1);
	TraveList(L1);

	DisCompose(L1, L2, L3);
	printf("链表分解为L2、L3:\n");
	printf("L2: ");
	TraveList(L2);
	printf("L3: ");
	TraveList(L3);

	system("pause");

	return 0;
}
  • operation result

Guess you like

Origin blog.csdn.net/qq_22847457/article/details/94170810