C # stack and queue exercises

Source: Internet
Author: User

3.1 Compare the similarities and differences between the three data structures of linear tables, stacks, and queues.

Stack is a linear table that restricts insertions and deletions to only one end of a table.
A queue is a linear table that qualifies only one end of a table to be inserted and deleted at the other.
From the point of view of "data structure", they are all linear structures, that is, the relationship between data elements is the same. But they are completely different data types. In addition to their respective basic set of operations, the main difference is the "qualification" of the insert and delete operations.

Stack and queue are two kinds of linear data structures widely used in program design, they are characterized by the particularity of basic operation, the stack must be operated by "last in first out" rule, and the queue must be operated by "FIFO" rule. Compared with linear tables, their insertions and deletions are more constrained and limited, so they are also called the finite linear table structure.


3.2 If the sequence of elements in the stack is 1,2,3,4, how many of the possible stack sequences can be obtained? Write out all the possible sequences.

First of all, this is a Cattleya number, with 2n individuals lined up into a theater. Admission fee is 5 yuan. of which only n individuals have a 5-dollar bill, the other N people only 10 yuan banknotes, the theater no other money can change, ask how many methods make as long as 10 yuan people buy tickets, the ticket office has 5 yuan of money change? (will hold 5 yuan to see as the 5 yuan into the stack, holding 10 yuan to see as the stack of some 5 yuan out of the stack).

For this example, the theater to always have change to find, then the number of people entering the theatre, the number of people carrying 10 yuan banknotes must be less than the equivalent of 5 yuan banknotes, or it is certain that there is no change in a person's situation.

Now get back to the point. For a given stack sequence, how to ask for its stack sequence?

We can put into the stack as 1, the stack is recorded as 0. The number of 1 in the prefix subsequence must be greater than or equal to 0, that is, the number of times to be greater than or equal to the number of stacks, such as 1 1 0 1 0 0, its arbitrary prefix sequence of 1 is greater than or equal to the number of 0.

Let's look at an example: for 1 2 3 this into the stack sequence, 1 1 0 1 0 0 is a stack out stack sequence, the first 1 for the element 1 into the stack, then the second 1 for the element 2 into the stack, and then the third is 0, representing the stack, that is, the element 2 out of the stack, then the fourth is 1, the element 3 into the stack Represents the stack, that is, the element 3 out of the stack, and then the sixth is 0, which represents the element 1 out of the stack. The last 1 1 0 1 0 0 represents the stack sequence 2 3 1.

Now the question is converted to how to find all 0 1 sequences that meet the criteria. In fact, this is the same as the following: Given the logarithm of parentheses, output all conforming sequences. such as 2 pairs of parentheses, the output has () () or (()) two. 1 can be seen as ' (', 0 can be seen as ') '.

Number of Cattleya

The first is the definition of the Cattleya number: Make the H (0) =1,h (1) =1,catalan number satisfy the recursive type:

H (N) = h (0) *h (n-1) +h (1) *h (n-2) + ... + h (n-1) H (0) (n>=2).

A recursive calculation scheme can be written according to the recursive formula above.

 Public Static intCatalan (intN) {intNcount =0; if(N <0 )            {                return 0; }            if(0= = N | |1==N) {return 1; }             for(inti =0; I < n; ++i) {ncount+ = Catalan (i) * Catalan (N-i-1); }            returnncount; }


3.3 If the sequence of elements in the stack is a,b,c,d,e,f, can we get the d,c,e,f,a,b and a,c,e,d,b,f of the stack sequence? And explain why you can't get it or how to get it.

D,c,e,f,a,b can not get, the position A and B to change the

A,c,e,d,b,f can. A into the stack B in, C in, c out, D in e-in, e out, d out, b out, F into f out


3.4 Write out the precede function and operate function in example 3-3.
3.5 Write an algorithm that takes the elements of a sequential stack out in turn, and prints the value of the element.

To invert the elements of the stack, we consider popping the stack element into another stack and then outputting

  write an algorithm that takes the elements of a sequential stack out in turn and prints the value of the element. Public        static void Reversestack (int _n)        {            if (_n < 0)            {                return;            }            seqstack<int> stack = new seqstack<int> (_n);            seqstack<int> revstack = new seqstack<int> (_n);            for (int i = 0; i < _n; ++i)            {                stack. Push (i+1);            }            for (int i = 0; i < _n; ++i)            {                Revstack.push (stack. Pop ());                            }            for (int i = 0; i < _n; ++i)            {                Console.WriteLine ("The Elements of the stack are" + revstack.pop ());}        }


3.6 In the sequential queue, what shouting truth overflow? What do you mean false overflow? Why do sequential queues usually use a circular sequential queue structure?

When there are data elements enqueued, the tail indicator rear plus 1, when there are data elements out of the team, the team head indicator front plus 1. When Front=rear, indicates that the queue is empty, the tail indicator rear reaches the upper bound of the array and front is 1, the queue is full.

An overflow occurs if another data element is queued. But in fact the queue is not full, there is free space, this phenomenon is called "false overflow." This is due to the operation Principle of queue "team tail in the first out". The method of solving false overflow is to treat the sequential queue as the loop structure of the end-to-end, the relationship of the tail-end indicator is not changed, and this kind of queue is called circular order queue.

//to find the length of the Loop order queue Public intGetLength () {return(rear-front+maxsize)%maxsize;} //emptying the Loop order queue Public voidClear () {Front= Rear =-1; } //determine if the Loop order queue is empty Public BOOLIsEmpty () {if(Front = =rear) {         return true; }     Else     {         return false; } } //determines whether the loop order queue is full Public BOOLIsfull () {if((Rear +1)% maxsize==front) {         return true; }     Else     {         return false; } } 
View Code


3.7 Write an algorithm to remove the elements of a chain queue sequentially, and print the value of the element.

  Public Static voidPrintlinkqueue (int_n) {Linkqueue<int> queue =Newlinkqueue<int>();  for(inti =0; i < _n; ++i) {queue. Enqueue (i+1); Console.WriteLine ("the elements of the queue are sequentially"+queue.            Dqueue ()); }        }


3.8 Example 3-4 is implemented with chain queues and sequential queues, and can handle input characters that are not case-sensitive
The case of English characters.

Well, I'll write it down here, as for the priority question, there's time to talk later.

C # stack and queue exercises

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.