"Stack" of data structure + decimal to D binary (stack array simulation)

Source: Internet
Author: User

In fact, this article is drawn mainly water article percent percent

Stack--last- in-first-out hooker

Features: special linear tables that can only be inserted and removed at one end

  Action:--push-> inserts an element into the top of the stack

--pop--> the top element of the stack by dropping the stack

Realize:

Defines an array of length n, with a top (equivalent to a pointer) to the top of the stack, if top=0, the stack is empty; top=n, it means the stack is full.

Top+1 in the stack, top-1 when the stack is out. stack pointer (top) always points to the top of the stack in the Operation

    If the Top>n (n is the length of the stack), the overflow, error handling, if top<0, is underflow, the same error handling

"Specifically--in the stack and check if the stack is full (top is equal to N)--not in the stack, full error handling "  

" out of the stack, check if the stack is empty (top is equal to 0)--not empty, and the stack is error-free."

The approximate implementation is as follows:

#dfine n 100void push (int s[],int top,int x) {if (top==n) cout<< "Overflow";    else {top++;  S[top]=x;  }}void pop (int s[],int top,int x) {if (top==0) cout<< "underflow";    else {y=s[top];  top--; }}

  

Decimal to D-Binary

  Brush 666 of all sit down, basic operation

We implement the decimal to D binary using a method similar to the stack array simulation

"algorithm principle: for the decimal under the N into the D-system, there is: n= (n/d) *d+n%d "(well, it's actually short division.)

such as (1348) 10--> (2504) 8

①1348/8==168 1348%8==4 ^

②168/8==21 168%8==0 ^ | ^

③21/8==2 21%8==5 |

④2/8==0 2%8==2 |

  

In the direction of the arrow, inverted output, that is (1348) 10--> (2504) 8 of the process

The program is implemented as follows:

#include <cstdlib> #include <iostream>using namespace std; #dfine size 100int a[size+1],n,d,i=0;int main () {  cout<< "Please enter a number (N) base 10:";  cin>>n;  cout<<endl; cout<< "Please enter a number (d):" cin>>d;  do {a[++i]=n%d;  n=n/d;  } while (n!=0);  for (int j=i;j>=1;j--) {cout<<a[j]; }   return 0;}

  

Application of the stack:

1. Use a pointer to record "stack top";

2, the elements into the stack, and then out of the stack, to achieve the purpose of reverse output;

"Stack" of data structure + decimal to D binary (stack array simulation)

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.