CareerCup Stacks and Queues-Q3.2

Source: Internet
Author: User

Reprinted please indicate the source:Http://blog.csdn.net/ns_code/article/details/22312565


Question:

Describe how you cocould use a single array to implement three stacks.

Translation:

Implement three stacks with only one Array

Ideas:

It is easy to use an array to implement a stack. We also use arrays in Q3.1 to simulate the stack. If you want to use an array to implement three stacks, when you press the stack, no matter which stack push operation, the elements are pushed to the array in sequence, at the same time, the stack top index pointer of the stack refers to the elements just pushed in, as shown in (three colors represent three stacks respectively, and the arrow points to the top of each stack ):


But in pop, we need to move the top pointer of the stack down and point it to the next element in the stack, therefore, an index pointing to the next element in the stack needs to be added to each element. As shown in, if the position of the bottommost element in the array needs to be 0, the position number of the next element referred to in element 12 on the top of the stack is 9, that is, element 10, the position number of the next element pointed to by element 11 on the top of the stack is 6, that is, element 7, and the position number of the next element pointed to by element 9 on the top of the stack is 7, that is, element 8. In this way, we need to save the nodes in the array as the following structure:

Typedef struct node {ElemType data; int next; // point to the next element of the stack. If there is no next element, set next to-1} node;
Implementation Code:
/*************************************** * ************* Title Description: use an array to implement three stack dates: *************************************** * *************/typedef int ElemType; # define MAX 100 // the total depth of the three stacks # include <stdio. h> typedef struct node {ElemType data; int next; // point to the next element of the stack. If there is no next element, set next to-1} node; /* when the index pointer at the top of the stack is top, the data datacount is input to stack A to record the top position of the stack where the top element is located */bool push (node *, int & count, int & top, ElemType data) {if (count> = MAX-1 | count <-1 | top> count | top <-1) return false; count ++; A [count]. data = data; A [count]. next = top; top = count; return true;}/* When the top index pointer of the stack is top, the out-stack count is used to record the top position of the stack where the top element is located */bool pop (node * A, int & count, int & top) {if (top <0 | top> count) return false; if (top = count) count --; A [top]. data =-10; // here, to facilitate the observation of the pop situation, set the element at the position after the exit stack to-10top = A [top]. next; return true;} int main () {int top1 =-1; int top2 =-1; int top3 =-1; int count =-1; node A [MAX]; push (A, count, top1, 1); push (A, count, top2, 2); push (A, count, top3, 3 ); push (A, count, top1, 4); push (A, count, top3, 5); push (A, count, top3, 6); push (A, count, top2, 7); push (A, count, top1, 8); int I; printf ("After pushed: \ n"); for (I = 0; I <8; I ++) printf ("A [% d]. data = % d, A [% d]. next = % d \ n ", I, A [I]. data, I, A [I]. next); pop (A, count, top3); pop (A, count, top2); pop (A, count, top1); printf ("After popd: \ n "); for (I = 0; I <8; I ++) printf (" A [% d]. data = % d, A [% d]. next = % d \ n ", I, A [I]. data, I, A [I]. next); return 0 ;}
Test results:

Note: The code is open-source to my Github:Https://github.com/mmc-maodun/CareerCup


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.