1326 Problem A Algorithms 2-8~2-11: Basic Operations of Linked Lists

Problem A: Algorithms 2-8~2-11: Basic Operations of Linked Lists

Time Limit: 1 Sec Memory Limit: 32 MB
Commits: 136 Resolved: 68
[ Commit ][ Status ][ Discussion Board ][Assert By:ExternalImport]

Topic description

A linked list is one of the most basic data structures in data structures, and it is a linear list implemented with a linked storage structure. It does not have to move the elements behind it when inserting and deleting compared to the sequential list. Now you are given some integers, and then some elements will be inserted and deleted frequently, and some of them will let you find an element or output all the elements in the current list.

Here's a basic algorithm description for you:

Figure 1: Definition of linked list types and description of the algorithm for obtaining linked list elements

Figure 2: Description of the insertion algorithm for linked lists

Figure 3: Description of the deletion algorithm for linked lists

Figure 4: Description of the linked list creation algorithm

enter

There is only one set of input data, the first line has n+1 integers, the first integer is the number of remaining integers n in this line, followed by n integers. This line of integers is used to initialize the list, and the input order is the reverse of the order in the list, that is, if the list is 1, 2, 3 then the input order is 3, 2, 1.

The second line has an integer m, which means that there are m lines below. Each line has a string, the string is one of "get", "insert", "delete", "show". If it is "get" or "delete", it is followed by an integer a, which means to get or delete the a-th element; if it is "insert", it is followed by two integers a and e, which represent the a-th position. Insert e before; no integer after "show".

output

If the acquisition is successful, the element is output; if the deletion is successful, "delete OK" is output; if the acquisition fails or the deletion fails, "get fail" and "delete fail" are output. If the insertion is successful, output "insert OK", otherwise output "insert fail". If it is "show" then output all elements in the list, if the list is empty then output "Link list is empty". Note: All double quotes are not output.

sample input

3 3 2 1
21
show
delete 1
show
delete 2
show
delete 1
show
delete 2
insert 2 5
show
insert 1 5
show
insert 1 7
show
insert 2 5
show
insert 3 6
show
insert 1 8
show
get 2

Sample output

1 2 3
delete OK
2 3
delete OK
2
delete OK
Link list is empty
delete fail
insert fail
Link list is empty
insert OK
5
insert OK
7 5
insert OK
7 5 5
insert OK
7 5 6 5
insert OK
8 7 5 6 5
7

hint

hint:

1. Because the input data contains a large number of insertion and deletion operations (believe it or not, I believe it anyway), it is necessary to use a linked list, otherwise it is likely to time out. This is also the characteristics of the check list.

2. The elements of the initialized linked list are in reverse order. This method can be used to create the list (insert from the head) in the title.

Summarize:

This question examines the properties of linked lists. In the sequence list, how to judge when to use the sequence list and when to use the linked list? It depends on their characteristics. The characteristics of the sequence table are random access and random access, that is to say, it is more appropriate to use the sequence table if the access and query are frequent; the characteristic of the linked list is that it is not necessary to move the subsequent nodes when inserting and deleting. Frequent use of linked lists is more appropriate.

#include<iostream>
#include<string>
using namespace std;
struct node {
	int data;
	node* next;
};

void create(node* &L, int n) {
	L = new node;
	L->next = NULL;
	for (int i = 0; i < n; i++) {
		node *p = new node;
		cin >> p->data;
		p->next = L->next;
		L->next = p;
	}
}

void get(node* head) {
	int x;
	cin >> x;
	node *p = head;
	for (int i = 0; i < x; i++) {
		p = p->next;
	}
	if (p == NULL) cout << "get fail" << endl;
	else cout << p->data << endl;
}

void insert(node* head) {
	int x, y;
	cin >> x >> y;
	node *p = head;
	for (int i = 0; i < x - 1; i++) {
		p = p->next;
		if (p == NULL) {
			cout << "insert fail" << endl;
			return;
		}
	}
	node *q = new node;
	q->data = y;
	q->next = p->next;
	p->next = q;
	cout << "insert OK" << endl;
}

void del(node* head) {
	int x;
	cin >> x;
	node *p = head;
	node *q = head->next;
	if (q == NULL) {
		cout << "delete fail" << endl;
		return;
	}
	for (int i = 0; i < x - 1; i++) {
		p = p->next;
		q = q->next;
	}
	p->next = q->next;
	cout << "delete OK" << endl;
}

void show(node* head) {
	node *p = head->next;
	if (p == NULL) {
		cout << "Link list is empty" << endl;
		return;
	}
	while (p->next != NULL) {
		cout << p->data << " ";
		p = p->next;
	}
	cout << p->data << endl;
}

int main() {
	int n, m;
	while (cin >> n) {
		node *L;
		create(L, n);
		cin >> m;
		while (m--) {
			string s;
			cin >> s;
			if (s == "get") get(L);
			else if (s == "insert") insert(L);
			else if (s == "delete") del(L);
			else show(L);
		}
	}
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732101&siteId=291194637