Fortune left-law basis class3- subject 1 uses an array queue and stack

1. Using arrays implementing stacks

(1) Analysis

Use index points to the location of the array represents a new number should be placed in what position.
1. Drawing the value assigned to the array index position, then increment index, the index! Stack may continue when = length.
2. When the output current value of the stack, and then a reduction index, index! = 0:00 stack can continue. Stack does not need to delete the current number, since the use of index to control the pushing and popping, data can be overwritten.
3. All elements within the stack is not an array element, but the element before the array index.

(2) the core code

① Drawing

void push(int a,int arr[],int &index)
{
	if(index ==length)
	{
		cout<<"栈已满,不可入栈"<<endl;
		return;
	}
	cout<<a<<"入栈"<<endl;
	arr[index++] = a;
}

The formula is directly to the data stack, the stack may also input data

void push(int arr[],int &index)
{
	if(index == length)
	{
		cout<<"栈已满,不可入栈"<<endl;
		return;
	}
		
	int num;
	cout<<"请输入:";
	cin>>num;
	cout<<num<<"入栈"<<endl;
	arr[index++] = num;
}

② Stack

void pop(int arr[],int &index)
{
	if(index == 0)
	{
		cout<<"栈已空,不可出栈"<<endl;
		return;
	}
	cout<<arr[--index]<<"出栈"<<endl;
}

(3) complete code

#include<iostream>
#include<time.h>
#define length  3
using namespace std;

void print(int arr[],int index)
{
	cout<<"栈内的元素: ";
	for(int i = 0;i<index;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;
}

void push(int arr[],int &index)
{
	if(index == length)
	{
		cout<<"栈已满,不可入栈"<<endl;
		return;
	}
		
	int num;
	cout<<"请输入:";
	cin>>num;
	cout<<num<<"入栈"<<endl;
	arr[index++] = num;
}

void push(int a,int arr[],int &index)
{
	if(index == length)
	{
		cout<<"栈已满,不可入栈"<<endl;
		return;
	}
	cout<<a<<"入栈"<<endl;
	arr[index++] = a;
}

void pop(int arr[],int &index)
{
	if(index == 0)
	{
		cout<<"栈已空,不可出栈"<<endl;
		return;
	}
	cout<<arr[--index]<<"出栈"<<endl;
}
int main()
{

	int arr[length]  ;

	int index = 0;
	push(arr,index);
	print(arr,index);
	push(6,arr,index);
	print(arr,index);
	pop(arr,index);
	print(arr,index);
	push(2,arr,index);
	print(arr,index);
	push(2,arr,index);
	print(arr,index);
	push(2,arr,index);
	print(arr,index);
	pop(arr,index);
	pop(arr,index);
	pop(arr,index);
	pop(arr,index);
	system("pause");
	return 0;
}

(4) the output

Here Insert Picture Description

2. queue array

(1) Analysis

1. Define the number of new pointer to the end of the array was added to put the position of the start point 0, the pointer to the desired position defined start pick number in the array, but also a start point to 0, defined for recording queue size length, beginning set to 0;
2. when size <the total length of the array, the array may be added, plus an after end, size plus one, when the size! = 0, can pick up a number, start a plus, size minus one.
3 (prior to the release Guoshu) If the end point end of the array, but the size is less than the total length, is set to 0. end, continue adding number; Similarly, if the start in the end, but the size! = 0, the release can continue, start zero.
Tips ※ : feeling cycle is equivalent to an array, but only if the conditions that must be met in size. start and end are independent and only related to size.

(2) the core code

① add data

Skilled using ternary operator can replace the relatively simple if-else statement

void add_num(int arr[],int &end,int &size,int num)
{
	if(size == length)
	{
		cout<<"队列已满,不可添加!"<<endl;
		return;
	}
	
	arr[end] = num;
	size++;
	end = end == length - 1 ? 0 : end + 1;



	cout<<"入: "<<num<<endl;;

}

② release data

void sub_num(int arr[],int &start,int &size)
{
	if(size == 0)
	{
		cout<<"队列已空,不可出!"<<endl;
		return;
		
	}
	cout<<"出:"<<arr[start]<<endl;
	size--;
	start = start == length - 1 ? 0 : start+1;
}

③ output queue

void print(int arr[],int start,int size)
{
	if(size == 0)
	{
		cout<<"队列无元素"<<endl;
		return;
	}
		
	cout<<"队列为:";
	while(size != 0)
	{
		cout<<arr[start]<<" "; 
		start = start == length - 1 ? 0 : start+1;
		size--;

	}
	cout<<endl;
}

(3) complete code

#include<iostream>
#include<time.h>
#define length  3
using namespace std;

void print(int arr[],int start,int size)
{
	if(size == 0)
	{
		cout<<"队列无元素"<<endl;
		return;
	}
		
	cout<<"队列为:";
	while(size != 0)
	{
		cout<<arr[start]<<" "; 
		start = start == length - 1 ? 0 : start+1;
		size--;

	}
	cout<<endl;
}
void add_num(int arr[],int &end,int &size,int num)
{
	if(size == length)
	{
		cout<<"队列已满,不可添加!"<<endl;
		return;
	}
	
	arr[end] = num;
	size++;
	end = end == length - 1 ? 0 : end + 1;



	cout<<"入: "<<num<<endl;;

}

void sub_num(int arr[],int &start,int &size)
{
	if(size == 0)
	{
		cout<<"队列已空,不可出!"<<endl;
		return;
		
	}
	cout<<"出:"<<arr[start]<<endl;
	size--;
	start = start == length - 1 ? 0 : start+1;
}

int main()
{
	int arr[length];
	int size = 0;
	int end = 0;
	int start = 0;
	add_num(arr,end,size,6);
	print(arr,start,size);
	add_num(arr,end,size,3);
	print(arr,start,size);
	add_num(arr,end,size,4);
	print(arr,start,size);

	sub_num(arr,start,size);
	print(arr,start,size);

	add_num(arr,end,size,9);
	print(arr,start,size);
	add_num(arr,end,size,0);
	print(arr,start,size);

	sub_num(arr,start,size);
	print(arr,start,size);
	sub_num(arr,start,size);
	print(arr,start,size);
	sub_num(arr,start,size);
	print(arr,start,size);
	sub_num(arr,start,size);


	system("pause");
	return 0;
}

(4) Results Output

Here Insert Picture Description

Published 51 original articles · won praise 1 · views 1392

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103567856