"Broken Keyboard" list

topic

You have a broken keyboard. All keys on the keyboard work fine, but sometimes the Home or End key is automatically pressed. You don't know the problem with the keyboard, and you focus on typing without even turning on the monitor. When you turn on the monitor, you are presented with a tragic text. Your task is to work out this tragic text before turning on the display. For each set of data, output one line, the text of the tragedy on the screen.

enter

The input contains multiple sets of data. Each set of data occupies one line and contains no more than 100,000 letters, underscores, characters "[" or "]". The character "[" represents the Home key, and "]" represents the End key. The end-of-input marker is the end-of-file character (EOF). The input file should not exceed 5MB.

input sample

This_is_a_[Beiju]_text [[]][][]
Happy_Birthday_to_Tsinghua_University

Sample output

BeijuThis_is_a__text
Happy_Birthday_to_Tsinghua_University

This question mainly involves the insertion operation of the linked list, the key is that the insertion position needs to be changed.

The idea is to set the cursor. If it is inserted at the end, it is the normal tail insertion method. If it needs to be inserted from the head, set the previous node of the inserted position as the cursor.

Insert after that until the closing bracket is encountered, returning to the end of the paragraph.

Code:

#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct LDataNode{
	char x;
	struct LDataNode *next;
} LDataNode;
typedef struct LHeadNode{
	int count;
	LDataNode *next; //header
	LDataNode * rear; // tail
}LHeadNode,*LinkList;

int InitList(LinkList *head);
int TailInsert(LinkList *head,char ch);
int LTraverse(LinkList head);
int HeadInsert(LinkList *head,char ch);
int Lflag(LDataNode *Pointer,char ch);
int InitList(LinkList *head)//Initialization
{
	*head =(LinkList)malloc(sizeof(LHeadNode));
	LDataNode *vhNode =(LDataNode *)malloc(sizeof(LDataNode));
	if(*head==NULL||vhNode==NULL)
	{
		return -1;//The application failed
	}
	vhNode->next=NULL;
	(*head)->count=0;
	(*head)->next=vhNode;
	(*head)->rear=vhNode;
	return 0;
}
int TailInsert(LinkList *head,char ch)//Segment end insertion
{
	LDataNode* p=(LDataNode *)malloc(sizeof(LDataNode));
	p->x=ch;
	(*head)->rear->next=p;
	(*head)->rear=p;
	(*head)->rear->next=NULL;
}

int Lflag(LDataNode **Pointer, char ch)//The cursor returns to the beginning of the segment
{
	LDataNode* p=(LDataNode *)malloc(sizeof(LDataNode));
	p->x=ch;
	p->next=(*Pointer)->next;
	(*Pointer)->next=p;
	(*Pointer)=(*Pointer)->next;
	
}
int LTraverse(LinkList head)//traverse output
{
	LDataNode *p=head->next->next;
	while(p)
	{
		if(p->x!='['&&p->x!=']')
		printf("%c",p->x);
		p=p->next;
	}
}
int main()
{
	char a[100];
	while(gets(a))
	{
		LinkList head;
	    InitList(&head);
	    LDataNode *Pointer;//Cursor
		Pointer=head->rear;
	    int l=strlen(a);
	    int count=1;
		for(int i=0;i<l;i++)
		{
			if(a[i]=='[')
			{
				//The cursor returns to the beginning of the paragraph
				Pointer=head->next;
				count=0;
			}
			if(a[i]==']')
			{
				//The cursor returns to the end of the paragraph
				Pointer=head->rear;
				count=1;
			}
			//insert data
			if(count)
			{
				TailInsert(&head,a[i]);
			}
			else
			{
				Lflag(&Pointer,a[i]);
			}
		}
		LTraverse(head);//Output
		printf("\n");
	}
	
}


Guess you like

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