7-37 Xiaoming typing (10 points) (data structure) The simplest code to write the simplest data structure

Keywords: C vim data structure pta

Xiao Ming is typing a document using Microsoft Word. The document contains only a-z lowercase letters and spaces. During typing, the Home key, End key, _arrow key, _arrow key, Insert key, Backspace key may be pressed one or more times. Write a program that gives Xiao Ming the sequence of keys on the keyboard and outputs the final text displayed on the Xiao Ming screen.Tip: The Home key moves the current cursor to the beginning of the text, the End key moves the current cursor to the end of the text, and the_and_keys move the current cursor one position to the left or right (if the cursor cannot move left at the header and right at the end of the document), and the Insert key switches between inserting and replacing text (by default, the insertion state), the Backspace key deletes a character in front of the current cursor.

Input format:
Enter no more than 50,000 characters for the key sequence of the smart keys. Contains a-z lower case letters, spaces, and characters [,], {,}, -, =. The characters'['denote the Home key,']' denote the End key,'{'denotes the_key,'}' denotes the_key,'-'denotes the Insert key,'=' denotes the Backspace key.

Output format:

Output is the text that will eventually appear on the Smart Screen. There is no carriage return or line break after the last letter.

Input Sample 1:

jilin[i lofe{{-v-} ] universiti=y

Output Sample 1:

i love jilin university

Input Sample 2:

abcd[c-de

Output Sample 2:

cdecd

Input Sample 3:

[[]][][]happy=birthday

Output Sample 3:

happbirthday

Input Sample 4:

efg[bbb}}=}}}}=[{{{{a

Output Sample 4:

abbbe

I use the double-chain table because it is very flexible, so I use the double-chain table. There are many solutions to this problem. I want to practice the double-chain table.
I'm using a double-linked list. Please don't spray me. I'll write a note (escape).

To learn this structure, take a good look at the notes and do this.
The code doesn't have any garbage or anything you can't read.
All five actions are two lines of code (about 4, 8 lines each)

#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>

using namespace std ;

struct Node
{
	char c ;
	struct Node* prev , *next ;
	Node(char _c) : c(_c) {} // Constructor to construct a node that holds c characters
} ;

typedef Node* List ;

List Create_List() // Initialization of double-linked lists, I see a lot of pta questions are initialized with functions (although I don't think it's necessary) 
{
	List L = new Node(0); // Node I pick a node 0 at random, it doesn't matter.
 	L -> next = L -> prev = L ; // Head node of double-linked list points to itself
	return L ;
}

int main()
{
	List dummy = Create_List () , p = dummy ; // p is the cursor
	char c ; 
	int is_insert = 1 ; // Indicates whether the current insert or delete is
	while(~scanf("%c" , &c)) //Enter Characters
	{
		if(c == '\n') break ; // I found that the last input was a line break, so I decided
		if('a' <= c && c <= 'z' || c == ' ') // If only insert or delete
		{
			if(!is_insert) // Replace directly if it is either inserted or deleted
			{
				p -> next -> c = c ; // Replace the next symbol of the cursor
				p = p -> next ; // Move cursor to next position
				continue ;
			}
			List q = new Node(c) ; // This is an insert operation
			// Four sentences of code for insertion
			q -> c = c ;
			q -> next = p -> next ;
			q -> next -> prev = q ;
			p -> next = q ;
			// Update Cursor
			q -> prev = p ;
			p = q ;
		}
		else if(c == '[')
			p = dummy ; // Back to Head
		else if(c == ']')
			p = dummy -> prev ; // Because it's a double-linked list, the last one on the head node is the tail
		else if(c == '{')
		{
			if(p != dummy) // It feels like I didn't try it again (I ran away)
				p = p -> prev ; // Move to the previous place
		}
		else if(c == '}') 
		{
			if(p -> next != dummy) // Move to the next place
				p = p -> next ; 
		}
		else if(c == '-')
			is_insert ^= 1 ; // XOR 1 has magical properties, 1 becomes 0, 0 becomes 1 
		else
		{
			if(p != dummy) // I'm not sure about this, I'm afraid I won't be able to get rid of the head node Gaga
			{
				p = p -> prev ;
				p -> next = p -> next -> next ;
				p -> next -> prev = p ;
			}
		}
	}
	for(List p = dummy -> next ; p != dummy ; p = p -> next) // ergodic
		printf("%c" , p -> c) ;
}

Posted by Otoom on Wed, 06 Oct 2021 15:29:22 -0700