Recursion: Print a sequence first into the stack and then out of the stack all the possible __delete

Source: Internet
Author: User
Tags int size

Title: A sequence, such as 1,2,3. 1 after the stack, there are two cases: one, 2 into the stack, two, 1 out of the stack to the target array. These two types of energy are present at every point in the execution of the program, which results in a different print result. Requirements: Print out all possible output sequences

Train of thought: recursively iterate through all the solution space, pay attention to the time of recursion to restore each step of the environment. My program uses three arrays, source sequence src[], analog stack st[], target array dest[] (that is, the last array to print). where st[0] and dest[0 respectively represent the subscript of the last element of the current array, with an initial state of 0, indicating that there are no elements in the array.

C + + Implementation of the program:

StackInOrOut.cpp

#include <iostream> using namespace std;
void init (int *arr, int start, int end);
void initsrc (int *arr, int start, int end);
void print (int *arr, int start, int end);
void stackinorout (int *src, int *st, int *dest, int index, int size, BOOL flag);
	int main () {int size;
	cout << "Please input size of src:" << Endl;
	CIN >> size;
	int *src = new Int[size];
	int *st = new Int[size + 1], *dest = new Int[size + 1];
	INITSRC (src, 0, size-1);
	Init (ST, 0, size);
	Init (dest, 0, size);   Stackinorout (SRC, St, dest, 0, size, true);
	Just start into the stack, can not be out of the stack delete []src;
	delete []st;
delete []dest;  } void init (int *arr, int start, int end) {for (int i = start; I <= end; ++i) arr[i] = 0;} void initsrc (int *arr, 
int start, int end) {for (int i = start; I <= end; ++i) Arr[i] = i + 1;} void print (int *arr, int start, int end)
	{static int count = 1;
	cout << "Count =" << count << ", arr[0] =" << arr[0] << ' \ t '; for (int i = StarT I <= end;
	++i) cout << arr[i] << ";
	cout << Endl;
++count; } void Stackinorout (int *src, int *st, int *dest, int index, int size, BOOL flag) {if (index = = size) {if (flag = t
			Rue)//If the stack, print results, out of the stack directly exit {int j = dest[0];
			for (int i = st[0]; I!= 0-I.) DEST[++J] = St[i];
		Print (dest, 1, size);
	} return;		} if (true = = Flag) {St[++st[0]] = Src[index];
		Into the stack stackinorout (SRC, st, dest, Index + 1, size, true);
		Stackinorout (SRC, st, dest, Index + 1, size, false);						--ST[0];					Restore Top pointer} else {if (st[0]!= 0) {dest[++dest[0]] = st[st[0]--];
			Out stack top element to target array stackinorout (SRC, st, dest, Index, size, true);
			Stackinorout (SRC, st, dest, Index, size, false);										++ST[0];						Restore top pointer st[st[0]] = dest[dest[0]];										The problem here is that the original program did not restore the value of the elements in the stack, but restored the top pointer--dest[0]; Recover target array Top pointer}}}


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.