Data structure C # programming example (2)

Source: Internet
Author: User

Take the example of stack Operation implemented by JavaScript program code in "Data Structure" Study Notes (2) and rewrite it with C # programming:

Stack S = new stack (); S. push (1); S. push (2); S. push (2); S. push (2); S. push (null); object B = S. pop (); console. writeline ("s pop method:" + B); foreach (Int J in S) {console. writeline ("s:" + J);} s. push (3); foreach (int K in S) {console. writeline ("s:" + k );}

Queue operation instance:

Queue S = new Queue (); S. enqueue (1); S. enqueue (2); S. enqueue (2); S. enqueue (2); object B = S. dequeue (); console. writeline ("s dequeue method:" + B); foreach (Int J in S) {console. writeline ("s:" + J);} s. enqueue (3); foreach (int K in S) {console. writeline ("s:" + k) after the enqueue method );}

Note: The stack and queue classes are all located in system. Collections.

The above rewriting can indicate that the push () method and POP () method are two important operations of the stack. The two operations corresponding to the queue are enqueue () method and dequeue () method. Next, use C # Programming to Implement the application example of the stack in section 3.2 of data structure.

3.2.1 digital conversion

Void conversion () {// construct an empty stack S = new stack (); int N; int result; int32.tryparse (console. readline (), out result); n = result; while (n! = 0) {S. Push (N % 8); n = N/8;} while (S. Count! = 0) {Object E = S. Pop (); console. Write (e );}}

The complete program code for generic programming is as follows:

Using system; using system. collections. generic; Class stackdemo {static void conversion () {// construct an empty stack <int> S = new stack <int> (); int N; int result; int32.tryparse (console. readline (), out result); n = result; while (n! = 0) {S. Push (N % 8); n = N/8;} while (S. Count! = 0) {int e = S. Pop (); console. Write (e) ;}} static void main (string [] ARGs) {conversion ();}}

3.2.3 The row editing program book uses stack implementation. Here I use JavaScript to program the implementation as follows:

for(i=0;i<ch.length;i++){    if(tb1.value.length > 0 && ch[i] != '\n')    {        switch(ch[i])        {            case '#':                 list.pop();                break;            case '@':                list = [];                break;            default:                list.push(ch[i]);                break;        }    }}alert(list.join(''));

The above code is to enter multiple lines of text in the text box on the webpage, click the submit button to display the content after processing the program. The following code uses the C # programming example.

Stack<char> s = new Stack<char>();char[] ch = new char[tb1.Text.Length];ch = tb1.Text.ToCharArray();for (int i = 0; i < ch.Length; i++){    if (tb1.Text.Length > 0 && ch[i] != '\n')    {        switch (ch[i])        {            case '#':                s.Pop();                break;            case '@':                s.Clear();                break;            default:                s.Push(ch[i]);                break;        }    }}List<char> ch2 = new List<char>();IEnumerator enu = s.GetEnumerator();while(enu.MoveNext()){    ch2.Add(char.Parse(enu.Current.ToString()));}string sb = "";foreach (char c in ch2){    sb = sb.Insert(0, c.ToString());}Response.Write(sb);s.Clear();

In the code above, we can see that the brute force value is accessed by getting an ienumerator object. In this way, the access benefits do not need to pop them out of the stack (that is, the POP () operation method ).

Let's take a look at Hanoi Tower game: http://chemeng.p.lodz.pl/zylla/games/hanoi3e.html

The following is the program code for C # Programming to Implement the 3.3-section Jakarta tower problem:

Using system; Class program {// count static int COUNT = 0; static void move (byte n, char X, char y) {console. writeline ("moving {0} from {1} to {2}", n, x, y);} // Hanoi method static void Hanoi (byte n, char X, char y, char Z) {If (n = 1) {++ count; // move the disc numbered 1 from X to Z move (n, x, z);} else {++ count; // move the disc numbered 1 to n-1 on X to Y, and Z acts as the middle tower Hanoi (byte) (n-1 ), x, Z, Y); // move the disc numbered N from X to Z move (n, x, z ); // move the disc with numbers 1 to n-1 on y to Z, and use X as the center tower Hanoi (byte) (n-1), y, x, z );}} static void main (string [] ARGs) {// if n is 3, Hanoi (3, 'x', 'y', 'z'); console. writeline (count );}}
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.