Program work 3. Implementation of Deque using array

需求大意:

用数组实现一个双向链表。链表在这里默认大小为10。

有以下功能:

1. 双向添加元素

2. 双向删除元素

3. 输出整个“双向链表”


Program environment:

       Operation System: Ubuntu 14.04,

Ide: Eclipse,

Compiler: g++;

Language: c++

Analysis:

       From the problem description, we know that the followingfunctions are required:

a.    Insertion from either side of the queue.

b.    Deletion from either side of the queue.

c.    Print out the queue.

d.    Judge an empty queue or a full occupied queue.

In order to use an arrayto implement a double queue, the following are the key:

1.    Use two pairs of pointer, each pair of pointeris for recording head and end of each side of the queue.

2.    Trace the length of the queue.

Implementation:

We use the pointers left_end & right_end to record the each end of the queue, pointers left_head and right_head to keep trace of the top of each side. Thus, each time an insertion occurs, one of the head pointer will “move on”, and elements existed will be copied from left to right or right to left according to the insertion occurs at which side. So does the deletion, just change “move on” to“move back”, the element of the side’s end will be covered.

If two head pointers meet at a same index, we can know that the queue is full, however,keep trace of the queue’s length will simply our work, as the size of the queue is fixed.

About printing out, just print out in this order: from left_end to left_head, then from right_head to right_end.

Input & Output:

The parameters of input is as the problem description, and any number except 1 and 2 will terminate the program.

After each operation, the program will immediately print out the queue.


“双向链表”用一个类实现,以下是头文件的声明:

/*
 * deque.h
 *
 *  Created on: Oct 26, 2014
 *      Author: reidchan
 */

#ifndef DEQUE_H_
#define DEQUE_H_

class Deque {
	public:
		Deque();
		~Deque();
		void InsertFromLeft(int num);     // insert number from queue's left
		void InsertFromRight(int num);    // insert number from queue's right
		void DeleteFromLeft();            // delete number from queue's left
		void DeleteFromRight();           // delete number from queue's right
		void PrintOut();                   // print out the queue
		bool Full();                       // judge if the queue is full
		bool Empty();                      // judge if the queue is empty
		int Length();                      // return the current length of the queue
	private:
		int length;                         // record the length of the length
		int left_head, left_end;            // record the pointers of queue's left
		int right_head, right_end;          // record the pointers of queue's right
		int deque[10];                      // use an array to simulate a queue, size is 10
};

#endif /* DEQUE_H_ */

头文件实现:

/*
 * deque.cpp
 *
 *  Created on: Oct 26, 2014
 *      Author: reidchan
 */

#include "deque.h"
#include <iostream>

Deque::Deque()
{
	length = 0;
	left_head = -1, left_end = 0;		// pay attention to the left_head is -1
	right_head = 10, right_end = 9;     // pay attention to the right_head is 10

	/*** initialization of the queue ***/
	for (int i = 0; i < 10; ++i) {
		deque[i] = -1;
	}
}

Deque::~Deque()
{
	std::cout << "Deque is deleted!" << std::endl;
}

bool Deque::Full()
{
	return length == 10;
}

bool Deque::Empty()
{
	return length == 0;
}

int Deque::Length()
{
	return length;
}

void Deque::InsertFromLeft(int num)
{
	left_head++;	// pointer of left's head move right

	/*** case1: queue's left has no element. ***/
	if (left_head == left_end) {
		deque[left_end] = num;
		length++;
		return;
	}

	/*** case2: queue's left has elements. ***/
	for (int i = left_head; i > left_end; i--) {
		deque[i] = deque[i - 1];
	}
	deque[left_end] = num;
	length++;
	return;
}

void Deque::InsertFromRight(int num)
{
	right_head--;	// pointer of right's head move left

	/*** case1: queue's right has no element. ***/
	if (right_head == right_end) {
		deque[right_end] = num;
		length++;
		return;
	}

	/*** case2: queue's right has elements. ***/
	for (int i = right_head; i < right_end; i++) {
		deque[i] = deque[i + 1];
	}
	deque[right_end] = num;
	length++;
	return;
}

void Deque::DeleteFromLeft()
{
	if (Length() != 0) {
		if (left_end == left_head) {	// delete the last element of the queue's left
			left_head--;
			deque[left_end] = -1;
			length--;
			return;
		} else if (left_head == -1) {	// delete the left-most element of the queue's right
			deque[right_head] = -1;
			right_head++;
			length--;
			return;
		}

		/*** queue's left has elements. ***/
		for (int i = left_end; i < left_head; ++i) {
			deque[i] = deque[i + 1];
		}
		left_head--;
		length--;
	} else if (Length() == 0) {
		std::cout << "Array is empty!" << std::endl;
	}
	return;
}

void Deque::DeleteFromRight()
{
	if (Length() != 0) {
		if (right_head == right_end) {	// delete the last element of the queue's right
			right_head++;
			deque[right_end] = -1;
			length--;
			return;
		} else if (right_head == 10) {	// delete the right-most element of the queue's left
			deque[left_head] = -1;
			left_head--;
			length--;
			return;
		}

		/*** queue's right has elements. ***/
		for (int i = right_end; i > right_head; --i) {
			deque[i] = deque[i - 1];
		}
		right_head++;
		length--;
	} else if (Length() == 0) {
		std::cout << "Array is empty!" << std::endl;
	}
	return;
}

void Deque::PrintOut()
{
	if (Length() != 0) {
		std::cout << "Print array: ";
		for (int i = left_end; i <= left_head; ++i) {
			std::cout << deque[i] << ' ';
		}
		for (int j = right_head; j <= right_end; ++j) {
			std::cout << deque[j] << ' ';
		}
		std::cout << std::endl;
	} else {
		std::cout << "Array is empty!" << std::endl;
	}
}

最后是主函数,用来测试整个“双向链表":(可能交互会有点烦)

//============================================================================
// Name        : Sicily.cpp
// Author      : Reid Chan
// Version     :
// Copyright   : Your copyright notice
//============================================================================

#include <iostream>
#include "deque.h"

using namespace std;

int main()
{
	int cmd, num;
	Deque dq;
	while (true) {
		cout << "Input(1) or delete(2): ";
		cin >> cmd;
		if (cmd == 1) {
			if (!dq.Full()) {
				cout << "Input from left(1) or right(2): ";
				cin >> cmd;
				if (cmd == 1) {
					cout << "Input number: ";
					cin >> num;
					dq.InsertFromLeft(num);
				} else {
					cout << "Input number: ";
					cin >> num;
					dq.InsertFromRight(num);
				}
				dq.PrintOut();
			} else {
				cout << "Array is full! Can't insert anymore." << endl;
			}
		} else if (cmd == 2) {
			if (!dq.Empty()) {
				cout << "Delete from left(1) or right(2): ";
				cin >> cmd;
				if (cmd == 1) {
					dq.DeleteFromLeft();
				} else {
					dq.DeleteFromRight();
				}
				dq.PrintOut();
			} else {
				cout << "Array is empty! Delete nothing." << endl;
			}
		} else {
			break;
		}
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值