DS sequence stack and Chain team to realize retrieval judgment and ds sequence Retrieval

Source: Internet
Author: User

DS sequence stack and Chain team to realize retrieval judgment and ds sequence Retrieval

The basic operations of the sequence stack and the chain team will not be listed one by one. To realize the retrieval judgment, first understand what is the retrieval? A string of "text-back" is the same character sequence as reading and reading. For example, "abcba", "abba" is "text-back", and "abab" is not "text-back ".

The second is how the sequential stack and the chain team can judge the background? Import the input strings to the stack and the queue in sequence, and then output the stack and the queue in sequence. Because the inbound and outbound stacks are in the same sequence, the outbound and outbound stacks are in the opposite sequence, this realizes the judgment of the background.

Finally, what are the basic operations of the sequence stack and the chain team? The basic operation to use the stack is: 1. Construct an empty stack, 2. judge whether the stack is empty, 7 out of the stack, and 8 into the stack. The basic operations that need to be used are: 1. initialize the chain, 7. Exit, and 8.

The code of the function used to judge the input is:

<Span style = "font-size: 18px;"> Status huiwen (SqStack & S, LinkQueue & Q) // The return function {char a, B, c; c = getchar (); // receives the input string while (c! = '@') {Push (S, c); EnQueue (Q, c); c = getchar () ;}while (! StackEmpty (S) {Pop (S, a); DeQueue (Q, B);} if (! = B) {return ERROW;} else {return OK ;}</span>

In the main function, you only need to build a sequence stack and a chain, and then define a string that accepts the input. Basically, the retrieval judgment is achieved, and a string ended with "@" is input, the code for returning to the text is:

<Span style = "font-size: 18px;" >#include <iostream> using namespace std; // header file libraries in two C languages # include <malloc. h> # include <stdlib. h> // macro definition # define OK 1 # define ERROW 0 # define OVERFLOW-2 # define TRUE 1 # define FALSE 0 # define INFEASIBLE-1 # define STACK_INIT_SIZE 100 // Storage initial space allocation # define STACKINCREMENT 10 // storage space allocation increment // The following is a type redefinition of typedef char SElemType; // re-define SElemType as int type typedef int Status; // re-define Status as int type typedef char QE LemType; // redefines QElemType as int type. // The following is the stack definition and basic operation typedef struct {// redefines SqStck as the structure type SElemType * base; // stack bottom pointer SElemType * top; // stack top pointer int stacksize; // The maximum capacity currently available for the stack} SqStack; typedef struct QNode {// redefine a node Structure QElemType data; struct QNode * next;} QNode, * QueuePtr; typedef struct {// defines a chain team QueuePtr front; // queue head pointer QueuePtr rear; // team end pointer} LinkQueue; // a defined structure variable // 1 initialize the queue Status InitQueue (LinkQueue & Q) {Q. front = Q. rear = (QueuePtr) ma Lloc (sizeof (QNode); if (! Q. front) {exit (OVERFLOW);} Q. front-> next = NULL; return OK;} // enter the queue Status EnQueue (LinkQueue & Q, QElemType e) {QueuePtr p = (QueuePtr) malloc (sizeof (QNode )); if (! P) {exit (OVERFLOW);} p-> data = e; p-> next = NULL; Q. rear-> next = p; Q. rear = p; return OK;} // output queue Status DeQueue (LinkQueue & Q, QElemType & e) {if (Q. front = Q. rear) {return ERROW;} QueuePtr p = (QueuePtr) malloc (sizeof (QNode); p = Q. front-> next; e = p-> data; Q. front-> next = p-> next; if (Q. rear = p) {Q. rear = Q. front;} free (p); return OK;} // 1 Construct an empty stack Status InitStack (SqStack & S) {S. base = (SElemType *) malloc (STACK_INIT_SIZE * s Izeof (SElemType); if (! S. base) {exit (OVERFLOW); // storage allocation Failed} S. top = S. base; S. stacksize = STACK_INIT_SIZE; return OK;} // 2. Check whether the stack is empty. Status StackEmpty (SqStack S) {if (S. base = S. top) {return TRUE;} else {return FALSE;} // 7 output stack Status Pop (SqStack & S, SElemType & e) {if (S. top = S. base) {return ERROW;} e = * -- S. top; return OK;} // Push (SqStack & S, SElemType e) {if (S. top-S.base> = S. stacksize) {// stack full append bucket SElemType * newbase = (SElemType *) realloc (S. base, (S. s Tacksize + STACKINCREMENT) * sizeof (SElemType); if (! Newbase) {exit (OVERFLOW);} S. base = newbase; S. top = S. base + S. stacksize; S. stacksize + = STACKINCREMENT;} * S. top ++ = e; return OK;} Status huiwen (SqStack & S, LinkQueue & Q) {char a, B, c; c = getchar (); // accept the input string while (c! = '@') {Push (S, c); EnQueue (Q, c); c = getchar () ;}while (! StackEmpty (S) {Pop (S, a); DeQueue (Q, B);} if (! = B) {return ERROW;} else {return OK;} int main () {SqStack s; // LinkQueue q; InitStack (s); InitQueue (q ); char * array1 = new char [100]; cout <"Enter the string you want to verify:"; if (huiwen (s, q )) {cout <"this is a reply";} else {cout <"This is not a reply";} return 0 ;}</span>

The input string is abcba @

The output result is:

 

The input string is abcde @

The output result is:

 

 

 


 

 


 


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.