OJ brushing record: storage structure and operation of linear table

The sequential storage structure and operation of the linear tableQuestion number: 454

Topic requirements:
Please define a sequence table, you can perform the following operations on the sequence table:

(1) Insert some elements before a certain element
(2) Delete the element at a certain position
(3) Find a certain element
(4) Get the element at a certain position
(5) Traverse and output all the elements
keyboard input some commands, you can execute the above operating. In this question, the sequence table element is an integer, the first element position of the sequence table is 1, and the maximum length of the sequence table is 20.

Input description
The input format of each command and related data is as follows:

Insert operation command before a certain position: I, the next row is the number of inserted elements n, the
following is n rows of data, each row of data has two values, which represent the insertion position and the inserted element value respectively

Find an element: S x, x is the value of the element to be found

Get the element at a certain position: G i, i is the position of the element that needs to be obtained

Delete an element at a certain position: Di, i is the position of the deleted element

Traverse and output all elements: V

When the input command is E, the program ends.
Output description
When the input command is S, please output the location of the element you want to find, if not found, please output None

When the input command is G, please output the obtained element value. If the input element position is incorrect,
output "position incorrect"

When the input command is D, please output the deleted element value, if the table is empty, output "underflow",
if the input position is incorrect, output "incorrect position"

When the input command is I, if the table is full, the output "overflow", if the input position is incorrect, the
output "position is incorrect".
Note that all elements occupy one row.
Input example
I
2
1 1
2 2
S 2
D 1
I
2
1 3
2 4
G 2
V
E
output example
2
1
4
3
4
2

Problem-solving ideas:
operating arrays, focusing on inserting and deleting linear tables by moving array elements.
Then, pay attention to the details!

Customs clearance code:

#include <iostream>

#define MAXSIZE 20

using namespace std;

class SeqList {
    
    
	public:
		SeqList():len_(0) {
    
    }

	public:
		void Insert(int pos, int val);
		int Delete(int pos);
		int Search(int val);
		int Get(int pos);
		void Print();

	private:
		int arr_[MAXSIZE];
		int len_;
};

void SeqList::Insert(int pos, int val) {
    
    
	if (len_ == MAXSIZE) throw "上溢";
	if (pos < 1 || pos > MAXSIZE) throw "位置不正确";

	for (int i = len_; i > pos - 1; i--) {
    
    
		arr_[i] = arr_[i - 1];
	}
	arr_[pos - 1] = val;

	len_++;
}

void SeqList::Print() {
    
    
	for (int i = 0; i < len_; i++) {
    
    
		cout << arr_[i] << endl;
	}
}

int SeqList::Delete(int pos) {
    
    
	if (len_ == 0) throw "下溢";
	if (pos < 1 || pos > len_ + 1) throw "位置不正确";

	int deletedVal = arr_[pos - 1];

	for (int i = pos; i < len_; i++) {
    
    
		arr_[i - 1] = arr_[i];
	}

	len_--;

	return deletedVal;
}

int SeqList::Get(int pos) {
    
    
	if (pos < 1 || pos > len_ + 1) throw "位置不正确";

	return arr_[pos - 1];
}

int SeqList::Search(int val) {
    
    
	bool isFind = false;
	int pos;

	for (int i = 0; i < len_; i++) {
    
    
		if (arr_[i] == val) {
    
    
			isFind = true;
			pos = i + 1;
			break;
		}
	}

	if (isFind == false) throw "None";

	return pos;
}

int main() {
    
    
	SeqList list;
	char command;
	int n, pos, val;

	while (cin >> command) {
    
    
		if (command == 'E') break;

		try {
    
    
			switch (command) {
    
    
				case 'I':
					cin >> n;
					for (int i = 0; i < n; i++) {
    
    
						cin >> pos >> val;
						list.Insert(pos, val);
					}
					break;

				case 'D':
					cin >> pos;
					cout << list.Delete(pos) << endl;
					break;

				case 'S':
					cin >> val;
					cout << list.Search(val) << endl;
					break;

				case 'G':
					cin >> pos;
					cout << list.Get(pos) << endl;
					break;

				case 'V':
					list.Print();
					break;
			}
		} catch (const char* str) {
    
    
			cout << str << endl;
		}
	}

	return 0;
}

The chain storage structure and operation of linear tablesQuestion number: 455

Subject requirements:
Same as above.

Problem-solving ideas:
water.

Customs clearance code:

#include <iostream>

using namespace std;

struct Node {
    
    
	int _val;
	Node* _next;
	Node(int val):_val(val), _next(NULL) {
    
    }
};

class List {
    
    
	public:
		List();
		~List();

	public:
		void Insert(int pos, int val);
		int Search(int val);
		int Get(int pos);
		int Delete(int pos);
		int getLength();
		void Print();

	private:
		Node* head_;
		int length_;
};

List::List() {
    
    
	head_ = new Node(-1);
	length_ = 0;
}

List::~List() {
    
    
	Node* previousNext = NULL;

	for (Node* p = head_; p != NULL; p = previousNext) {
    
    
		previousNext = p->_next;
		delete p;
	}
}

void List::Insert(int pos, int val) {
    
    
	if (pos < 1) throw "位置不正确";

	int count = 0;
	Node* node = new Node(val);

	for (Node* p = head_; p != NULL; p = p->_next) {
    
    
		if (count + 1 == pos) {
    
    
			node->_next = p->_next;
			p->_next = node;
		}
		count++;
	}

	length_++;
}

int List::Search(int val) {
    
    
	int count = 0;
	int pos;
	bool isFind = false;

	for (Node* p = head_; p != NULL; p = p->_next) {
    
    
		if (p->_val == val) {
    
    
			isFind = true;
			pos = count;
			break;
		}
		count++;
	}

	if (isFind == false) throw "None";

	return pos;
}

int List::Delete(int pos) {
    
    
	if (length_ == 0) throw "下溢";
	if (pos < 1 || pos > length_) throw "位置不正确";

	int count = 0;
	int val;
	Node* DeletedNode = NULL;

	for (Node* p = head_; p != NULL; p = p->_next) {
    
    
		if (count + 1 == pos) {
    
    
			DeletedNode = p->_next;
			val = DeletedNode->_val;
			p->_next = p->_next->_next;
			delete DeletedNode;
			break;
		}
		count++;
	}

	length_--;

	return val;
}

int List::Get(int pos) {
    
    
	if (pos < 1 || pos > length_) throw "位置不正确";

	int count = 0;
	int val;

	for (Node* p = head_; p != NULL; p = p->_next) {
    
    
		if (count == pos) {
    
    
			val = p->_val;
			break;
		}
		count++;
	}

	return val;
}

int List::getLength() {
    
    
	return length_;
}

void List::Print() {
    
    
	for (Node* p = head_->_next; p != NULL; p = p->_next)
		cout << p->_val << endl;
}

int main() {
    
    
	List list;
	char command;
	int n, pos, val;

	while (cin >> command) {
    
    
		if (command == 'E') break;

		try {
    
    
			switch (command) {
    
    
				case 'I':
					cin >> n;
					for (int i = 0; i < n; i++) {
    
    
						cin >> pos >> val;
						list.Insert(pos, val);
					}
					break;

				case 'S':
					cin >> val;
					cout << list.Search(val) << endl;
					break;

				case 'G':
					cin >> pos;
					cout << list.Get(pos) << endl;
					break;

				case 'D':
					cin >> pos;
					cout << list.Delete(pos) << endl;
					break;

				case 'L':
					cout << list.getLength() << endl;
					break;

				case 'V':
					list.Print();
					break;
			}
		} catch (const char* str) {
    
    
			cout << str << endl;
		}
	}

	return 0;
}

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108763577
Recommended