[Data structure] The stack converts decimal to octal, and the data structure is decimal.

Source: Internet
Author: User

[Data structure] The stack converts decimal to octal, and the data structure is decimal.



1. Basic stack operations

The code implementation is as follows:



#ifndef _SEQSTACK_H#define _SEQSTACK_H#include<iostream>#include<assert.h>using namespace std;typedef int ElemType;#define STACK_INIT_SIZE 20typedef struct Stack{     ElemType *base;     int       top;     int       capacity;}Stack;void ConverSeq(int n);bool IsFull(Stack *st);bool IsEmpty(Stack *st);void InitStack(Stack *st);bool Push(Stack *st, ElemType x);bool Pop(Stack *st, ElemType *v);bool Pop(Stack *st);#endif

Bool IsFull (Stack * st) {return st-> top> = st-> capacity;} bool IsEmpty (Stack * st) {return st-> top = 0 ;} void InitStack (Stack * st) {st-> base = (ElemType *) malloc (sizeof (ElemType) * STACK_INIT_SIZE); assert (st-> base! = NULL); st-> capacity = STACK_INIT_SIZE; st-> top = 0;} void ConverSeq (int n) {Stack s; // define a Stack int x = 0; // x is used to save the remainder. InitStack (& s); while (n> 0) // tossing and division {x = n % 8; Push (& s, x ); n/= 8;} while (! IsEmpty (& s) // output {Pop (& s, & x); cout <x ;}} bool Push (Stack * st, ElemType x) {if (IsFull (st) {cout <"stack full," <x <"cannot be added to stack! "<Endl; return false;} st-> base [st-> top ++] = x; return true;} bool Pop (Stack * st) {if (IsEmpty (st) {cout <"Stack is empty and cannot be output from Stack! "<Endl; return false;} st-> top --; return true;} bool Pop (Stack * st, ElemType * v) {if (IsEmpty (st )) {cout <"Stack is empty and cannot be output! "<Endl; return false;} * v = st-> base [-- st-> top]; return true ;}




2 Array Implementation

#include <stdio.h>  #include <stdlib.h>  #include <malloc.h>  #define STACKSIZE 100  typedef int ElemType;   typedef struct   {      ElemType stack[STACKSIZE];      int top;  }SeqStack;  void Conversion(int N);  



Void Conversion (int N) {int stack [STACKSIZE], top; top = 0; do {stack [top] = N % 8; top ++; N/= 8 ;} while (N! = 0); cout <"the converted octal values are:"; while (top> 0) {top --; cout <stack [top];} cout <endl ;}


3. Linked List Implementation

#include <stdio.h>  #include <stdlib.h>  #include <malloc.h>  #include <string.h>  typedef char ElemType;  typedef struct node  {      ElemType data;      struct node *next;  }LStackNode,*LinkStack;    void Conversion(int N);




Void Conversion (int N) {LStackNode * p, * top = NULL; do {p = (LinkStack) malloc (sizeof (LStackNode); p-> data = N % 8; p-> next = top; top = p; N/= 8;} while (N! = 0); cout <"number to eight:"; while (top! = NULL) {p = top; cout <p-> data; top = top-> next; free (p) ;}cout <endl ;}
 
 

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.