"Introduction to Algorithms" 10.1-5 singular group realizes two-terminal queue __ algorithm

Source: Internet
Author: User
Tags int size
Introduction to Algorithms third Edition P131 topic:

10.1-5 stack inserts and deletes elements can only be done at the same end, and the insert and delete operations of the queue are performed at both ends, and, unlike them, there is a two-terminal queue (deque), both of which can be inserted and deleted at both ends. Write a 4 time O (1) process that implements the insert and delete elements in a two-terminal queue, which is implemented using an array.


Note the point:

1. The left and right endpoints point to a tail endpoint similar to the one in the queue, where the next insert operation is located.

2, then pay attention to traverse the time, the left endpoint and the right endpoint position relationship, there are two possible, so the way of traversal is not the same.


Code:

* * Use singular group to achieve dual-end queue/#include <iostream> using namespace std;
	Class Deque {int lefthead;//left node, pointing to the next operational null position int righthead;//right end node int len;
int* Array;
	Public:deque (int size): Lefthead (0), Righthead (0), Len (size) {array = new int[size];
		} ~deque () {delete []array;
	Lefthead = Righthead = 0;
		/* * Get Previous position * * (int pos) {if (pos = 0) {return len-1;
		else {return--pos;
		* * Get Next position */int Next (int pos) {if (pos = = len-1) {return 0;
		else {return ++pos;
	} bool IsEmpty () {return lefthead = = Righthead? true:false;
		* * * insert/void Leftinsert (int k) {int p = Pre (lefthead) for left node;
			if (p = = righthead) {cout << "error:overflow" << Endl;
		Return
		//If empty, the left and right nodes need to move if (IsEmpty ()) {Righthead = next (Righthead);
		} Array[lefthead] = k;
	Lefthead = p;
		} void Rightinsert (int k) {int n = next (righthead); if (n = = lefthead) {cout <<Error:oveflow "<< Endl;
		Return
		//If empty, the left and right nodes need to move the IF (IsEmpty ()) {Lefthead = pre (Lefthead);
		} Array[righthead] = k;
	Righthead = Next (Righthead);
			/* * Left end delete/int leftdelete () {if (IsEmpty ()) {cout << "error:underflow" << Endl;
		return-1;
		} Lefthead = Next (Lefthead);
		If there is only one element, then the right endpoint will also move after the deletion, because the queue is empty if (Lefthead = = Pre (Righthead)) {righthead = Lefthead;
	return Array[lefthead];
			* * * Right END delete/int rightdelete () {if (IsEmpty ()) {cout << "error:underflow" << Endl;
		return-1;
		} Righthead = Pre (Righthead);
		If there is only one element, then the left endpoint is moved after the deletion, because the queue is empty if (Righthead = Next (Lefthead)) {lefthead = Righthead;
	return Array[righthead]; } void Printdeque () {cout << "lefthead=" << lefthead << ", righthead=" << righthead <<

		Endl
		if (IsEmpty ()) {return; //Consider Lefthead and Righthead respectively in both cases if (Lefthead < Righthead) {for (int i = Lefthead + 1; i < Righthead;
			i++) {cout << "array[" << I << "]=" << Array[i] << ";
		} cout << Endl; else {//lefthead > Righthead case for (int i = 0; i < Righthead; i++) {cout << "array[" <<
			I << "]=" << array[i] << '; for (int i = lefthead + 1; i < Len; i++) {cout << "array[" << I << "]=" << Array[i] &
			lt;< ';
		} cout << Endl;

}

	}

};
	int main () {deque* dq = new deque (5);
	Dq->leftdelete ();
	Dq->leftinsert (4);
	Dq->rightinsert (6);
	Dq->printdeque ();
	cout << dq->leftdelete () << Endl;

	Dq->printdeque ();
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.