Judge whether a string is a text-back-to-chain (new, into, out), chain stack (new, into Stack, out stack)

Source: Internet
Author: User

Back-to-text: A symmetric string, such as ABCBA

Train of Thought: based on the principles of Team: first-in-first-out and stack: first-in-first-out, compare whether the characters of team-out and stack-out are equal. If they are equal, they are input.

Create a console application.

1 # region character node class 2 class CharNode 3 {4 public char Char // character 5 {6 get; 7 set; 8} 9 public CharNode Next // next node 10 {11 get; 12 set; 13} 14 public CharNode (char Char, CharNode Next) 15 {16 this. char = Char; 17 this. next = next; 18} 19} 20 21 # endregion 22 23 # region Team Category 24 // <summary> 25 // team 26 /// </summary> 27 class CharQueue 28 {29 CharNode front; // The first 30 CharNode rear; // The last 31 // <su Mmary> 32 // enter 33 // </summary> 34 // <param name = "Char"> node character </param> 35 public void In (char Char) 36 {37 if (rear = null) 38 {39 rear = new CharNode (Char, null); // create a team head node 40 front = rear; 41} 42 else 43 {44 rear. next = new CharNode (Char, null); // create team 45 rear = rear. next; 46} 47} 48 49 // <summary> 50 // team out 51 // </summary> 52 // <returns> </returns> 53 public char? Out () 54 {55 if (front = null) 56 {57 return null; 58} 59 60 char Char char = front. char; 61 front = front. next; 62 if (front = null) 63 rear = null; 64 65 return Char; 66 67} 68 69} 70 # endregion 71 72 # region chain stack 73 public class CharStack 74 {75 CharNode top; 76 /// <summary> 77 // 78 // </summary> 79 // <param name = "Char"> node character </param> 80 public void Push (char Char) 81 {82 83 if (top = null) 84 {85 top = new CharNode (Char, null); 86} 87 else 88 {89 top = new CharNode (Char, top ); 90} 91} 92 // <summary> 93 // output stack 94 // </summary> 95 // <returns> </returns> 96 public char? Pop ()//? Returns null 97 {98 if (this. top = null) 99 return null; 100 else101 {102 char Char = top. char; 103 top = top. next; 104 return Char; 105} 106} 107} 108 109 # endregion110 static void Main (string [] args) 111 {112 Console. writeLine ("pls input one string:"); 113 string str = Console. readLine (); 114 CharStack stack = new CharStack (); // instantiate stack 115 CharQueue queue = new CharQueue (); // instantiate team 116 117 char? CharStack, charQueue; 118 119 foreach (char Char in str) 120 {121 queue. in (Char); // enter the 122 stack. push (Char); // stack 123} 124 125 do126 {127 charQueue = queue. out (); // outputted 128 charStack = stack. pop (); // output stack 129 130 if (charQueue! = CharStack) 131 break; 132} 133 while (charQueue! = Null & charStack! = Null); 134 135 if (charQueue! = Null | charStack! = Null) 136 {137 Console. writeLine ("{0} is not 文"", str); 138} 139 else140 {141 Console. writeLine ("{0} is 文"", str); 142} 143 144 Console. readLine (); 145 146}View Code

 

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.