Algorithm notes Chapter 7 data structure

Keywords: Algorithm greedy algorithm

1. Stack
Simple calculator, C + + code is as follows:

#include<cstdio>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
struct node {
	bool flag;
	double num;
	char op;
};
stack<node>s;
map<char,int>op;
string str;
queue<node>q;
void Change() {
	int i;
	node temp;
	for (i = 0; i < str.length();) {
		if (str[i] >= '0' && str[i] <= '9') {
			temp.flag = true;
			temp.num = str[i++] - '0';
			while (i < str.length() && str[i] >= '0' && str[i] <= '9') {
				temp.num = temp.num * 10 + (str[i] - '0');
				i++;
			}
			q.push(temp);
		}
		else {
			temp.flag = false;
			while (!s.empty() && op[str[i]] <= op[s.top().op]) {
				q.push(s.top());
				s.pop();
			}
			temp.op = str[i];
			s.push(temp);
			i++;
		}
	}
	while (!s.empty()) {
		q.push(s.top());
		s.pop();
	}
}
double Cal() {
	double temp1, temp2;
	node cur, temp;
	while (!q.empty()) {
		cur = q.front();
		q.pop();
		if (cur.flag == true) s.push(cur);
		else {
			temp2 = s.top().num;
			s.pop();
			temp1 = s.top().num;
			s.pop();
			if (cur.op == '+') temp.num = temp1 + temp2;
			else if (cur.op == '-') temp.num = temp1 - temp2;
			else if (cur.op == '*')temp.num = temp1 * temp2;
			else temp.num = temp1 / temp2;
			s.push(temp);
		}
	}
	return s.top().num;
}		
int main() {
	op['+'] = op['-']=1;
	op['*'] = op['/'] = 2;
	while (getline(cin, str), str != "0") {
		for (string::iterator it = str.end()-1; it != str.begin(); it--) {
			if (*it == ' ') {
				str.erase(it);
			}
		}
		while (!s.empty()) {
			s.pop();
		}
		Change();
		printf("%.2f\n", Cal());
	}
}

First, read in the data and store it in the form of string. Because there are numbers and calculation operators in the data, they should be stored in the form of string. Secondly, detect the data, delete the spaces in the data, and use the iterator of string to detect from back to front. If it is detected as a space, erase the elements corresponding to the iterator with erase function. Because it is a circular input data, the set stack needs to be checked. Because the final result is at the top of the stack, the stack is generally non empty, so it needs to check whether it is empty. If it is empty, pop out all elements. This completes all the preparations. Because the input is a suffix expression, it needs to be converted into a suffix expression that is easy to calculate. The function of Change() is to convert infix expression to suffix expression. Because data has two possibilities: operands and operators. Therefore, it is necessary to create a structure node, which defines three quantities: double operand variable, char operator variable and bool flag variable. Flag variable is used to display whether the read character is an operand or an operator. The operand is set to true and the operator is set to false. Then detect. If it is an operand, the character value of the character should be between '0' and '9', which can be equal to these two numbers. After it is determined that it is an operand, it cannot be directly pushed into the queue (a queue is defined to store suffix expressions) because it may form tens, hundreds and other numbers with subsequent numbers. Therefore, it is necessary to determine whether its next bit is also an operator and assign this number to a temp temporary storage variable. Perform a while loop. If it is an operand, add temp10 and the operand to form a new temp. After the loop is pushed out, press temp into the queue. If it is an operator, it is judged by the operator at the top of the stack. At this time, you need to define the priority of the operator. This problem only implements ± / four operations, so you can define a map < char, int > and a map op describing priority. Assign the priority of ± to 1 and the priority of * / to 2. In this way, the priority assigned by the operation can be distinguished. If the read character is an operator and its priority is higher than that of the top of the stack operator, the top of the stack operator will pop up in the queue until the priority of the top of the stack operator is lower than that of the read operator. Push the read operator onto the stack. Finally, after the judgment is completed, judge whether the stack is empty. Generally, there will be one or more residual operators. Press the remaining operators of the stack into the queue, so that the infix expression will be converted into a suffix expression. Then perform the calculation operation. The Cal() function is the function to calculate the suffix expression. The step to calculate the suffix expression is to read the suffix expression in the queue. If it is an operand, push it into the stack. If it is an operator, calculate according to the definition of the operator, and take two stack top elements. The second is the first operand and the first is the second operand. After the calculation, push the calculation result into the stack until the queue is read.
[] the length of string is the length function, and the others are the size function.

2. Queue
3. Linked list
Dynamic linked list, using pointers.

#include<cstdio>
#include<stdlib.h>
struct node {
	int data;
	node* next;
};
node* Create(int Array[]) {
	node* head, *pre, *p;
	head = new node;
	head->next = NULL;
	pre = head;
	for (int i = 0; i < 5; i++) {
		p = new node;
		p->data = Array[i];
		p->next = NULL;
		pre->next = p;
		pre = p;
	}
	return head;
}
int search(node* head, int x) {
	int count = 0;
	node* p = head->next;
	while (p != NULL) {
		if (p->data == x)count++;
		p = p->next;
	}
	return count;
}
void insert(node* head, int pos, int x) {
	node* q,*temp;
	q = head;
	for (int i = 0; i < pos - 1; i++) {
		q = q->next;
	}
	temp = new node;
	temp->data = x;
	temp->next = q->next;
	q->next = temp;
}
void del(node* head, int x) {
	node* q = head->next;
	node* pre = head;
	while (q != NULL) {
		if (q->data == x) {
			pre->next = q->next;
			delete(q);
			q = pre->next;
		}
		else {
			pre = q;	
			q = q->next;
		}
	}
}
int main() {
	int Array[5] = { 5,4,3,2,1 };
	node* l=Create(Array);
	l = l->next;
	while (l != NULL) {
		printf("%d", l->data);
		l = l->next;
	}
}

Linked list creation, insertion and search.
Static linked list, using hash implementation, the subscript of the array is the address.

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
struct node {
	char data;
	int next;
	bool flag;
}; node p1[99999];
int main() {
	char data;
	int	N;
	int address1,address2;
	int address;
	int next;
	int temp = 0;
	for (int i = 0; i < 99999; i++) {
		p1[i].flag = false;
	}
	scanf("%d %d %d", &address1 ,&address2 ,& N);
	for (int j = 0; j < N; j++) {
		scanf("%d %c %d", &address, &data, &next);
		p1[address].data = data;
		p1[address].next = next;
	}
	p1[address1].flag = true;
	temp = address1;
	while (temp != -1) {
		temp = p1[temp].next;
		p1[temp].flag = true;
	}
	temp = address2;
	while (1) {
		if (p1[temp].flag == 1) {
			if (temp != -1) {
				printf("%05d", temp);
				break;
			}
			else {
				printf("-1");
				break;
			}
		}
		else {
			temp = p1[temp].next;
		}
	}
}

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<algorithm>
using namespace std;
struct node {
	int data;
	int next;
	int address;
	bool flag;
} p[100000];
bool cmp(node a, node b) {
	if (a.flag == false || b.flag == false) {
		return a.flag > b.flag;
	}
	else {
		return a.data < b.data;
	}
}
int main() {
	int begin;
	int N;
	int address;
	int s;
	int count=0;
	int m;
	scanf("%d %d", &N, &begin);
	for (int i = 0; i < N; i++) {
		scanf("%d", &address);
		scanf("%d %d", &p[address].data, &p[address].next);
		p[address].address = address;
	}
	s = begin;
	for (int j = 0; j < 100000; j++) {
		p[j].flag = false;
	}
	while (s != -1) {
		p[s].flag = true;
		s = p[s].next;
		count++;
	}
	sort(p, p + 100000, cmp);
	if (count!=0) {
		printf("%d %05d\n", count, p[0].address);
		for (m = 0; m < count-1; m++) {
			p[m].next = p[m + 1].address;
			printf("%05d %d %05d\n", p[m].address, p[m].data, p[m].next);
		}
		m = 4;
		p[4].next = -1;
		printf("%05d %d %d\n", p[m].address, p[m].data, p[m].next);
	}
	else {
		printf("0 -1");
	}
	return 0;
}

[] when using scanf, if the read value of a variable is to be used in the later read variables, it needs to be written in two scanfs. Read the variable first, and then read the variable to be used (usually the array subscript).

Posted by brittny85 on Tue, 30 Nov 2021 17:12:24 -0800