Stack structure has the characteristics of last in, first out, and is a useful tool in programming
Let's take a look at the process of base conversion as shown in the figure:
It can be seen that the integer part conforms to the characteristics of LIFO, and the stack structure can be applied
Fractional FIFO, linear table can be applied
sqstack.h
1 #pragma once 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define STACK_INIT_SIZE 100//Initial allocation of storage space 5 #define STACKINCREMENT 10//Incremental storage allocation 6 #define OK 1 7 #define ERROR 0 8 typedef int StackType; //Stack element type 9 10 typedef struct { 11 StackType *base; //Before construction and after destruction, base The value is NULL 12 StackType *top; //Top pointer of stack 13 int stacksize; //Currently allocated storage space in elements 14 }SqStack; //Sequential stack 15 16 //Initialization of stack 17 int InitStack(SqStack *p) { 18 19 20 p->base = (StackType*)malloc(STACK_INIT_SIZE * sizeof(StackType)); 21 if (p->base == NULL) return ERROR; //memory allocation failed 22 p->top = p->base; //The top and bottom of the stack are the same, indicating an empty stack 23 p->stacksize = STACK_INIT_SIZE; 24 return OK; 25 26 } 27 //Judge whether the stack is empty 28 int EmptyStack(SqStack *p) { 29 //Return if empty stack OK,Otherwise return ERROR 30 if (p->top == p->base) return OK; 31 else return ERROR; 32 } 33 //Push in of sequence stack 34 int Push(SqStack *p, StackType e) { 35 //Insertion element e For the new stack top element 36 if ((p->top - p->base) >= p->stacksize) //Stack full, additional storage space 37 { 38 p->base = (StackType*)realloc(p->base, (p->stacksize + STACKINCREMENT) * sizeof(StackType)); 39 if (p->base == NULL) return ERROR;// Storage space allocation failed 40 p->top = p->base + p->stacksize; 41 p->stacksize += STACKINCREMENT; 42 } 43 *(p->top) = e; 44 (p->top)++; 45 return OK; 46 } 47 // Pop up of sequence stack 48 int Pop(SqStack *p, StackType *e) { 49 //Delete if stack is not empty p Top of stack element, using e Return its value 50 if (p->top == p->base) return ERROR; 51 --(p->top); 52 *e = *(p->top); 53 return OK; 54 55 56 } 57 //Destruction of sequence stack 58 int DestroyStack(SqStack *p) { 59 //Release the space at the bottom of stack and empty it 60 free(p->base); 61 p->base = NULL; 62 p->top = NULL; 63 p->stacksize = 0; 64 65 return OK; 66 }
The header file sqlist.h of linear table
#pragma once //Sequential linear table #include <stdio.h> #include <stdlib.h> #define LIST_INIT_SIZE 100 //Initial allocation of linear table storage space #define LISTINCREMENT 10 //Allocation increment of linear table storage space #define OK 1 #define ERROR 0 typedef double ElemType; typedef struct { ElemType *elem; //Storage base address int length; //Current length int listsize; //Currently allocated storage capacity(with sizeof(ElemType))As a unit }SqList; //Initialization of sequence table void InitList(SqList *L) { L->elem = (ElemType*)malloc(LIST_INIT_SIZE); if (!L->elem) //Storage allocation failed exit(-1); L->length = 0; //Empty table length is 0 L->listsize = LIST_INIT_SIZE; //Initial storage capacity } //Judge whether the table is empty int EmptyList(SqList *L) { if (L->length == 0) { return OK; } return ERROR; } //Tail insertion int BackInsert(SqList *L,ElemType e) { ElemType *newbase, *p; if (L->length >= L->listsize) //Current storage space is full, increase allocation { newbase = (ElemType*)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType)); if (!newbase) exit(-1); L->elem = newbase; //New base address L->listsize += LISTINCREMENT; //Increase storage capacity } p = L->elem + L->length; //p Point to the next address of the last element *p = e; //insert e L->length++; //Table length plus 1 return OK; } //Print elements in table void PrintList(SqList *L) { int i; if (EmptyList(L)==OK) { printf("The table is empty!\n"); exit(-1); } for (i = 0; i< L->length; i++) { printf("%d ",*( L->elem+i)); } printf("\n"); } //Destruction table int DestoryList(SqList *L) { free(L->elem); return OK; }
Source code:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <math.h> 4 #include "sqstack.h" //Introducing sequential stack storage structure and its basic operation 5 #include "sqlist.h" //Storage structure of import sequence table and its basic operation 6 #define OK 1 7 #define ERROR 0 8 9 //For entering any non negative decimal number, print its equivalent N Hexadecimal number 10 void Conversion(int N) { 11 double x, decimal; 12 int integer; 13 scanf("%lf", &x); 14 integer =(int)floor(x); //Integral part 15 decimal = x - integer; //Fractional part 16 //Process integer part 17 18 StackType *e; 19 SqStack *ps, s; 20 ps = &s; 21 e = (StackType*)malloc(sizeof(StackType)); //For pointer e Allocate memory address 22 InitStack(ps); //Initialization stack 23 while (integer) //When integer Not equal to 0 24 { 25 Push(ps, integer % N); //Press in integer Divide N Remainder 26 integer = integer / N; 27 } 28 while (EmptyStack(ps) != OK) //When the stack is not empty 29 { 30 Pop(ps, e); //Pop up the top element of the stack and let e Point to its address 31 printf("%d ", *e); // output e Each number is separated by a space 32 } 33 //Processing decimal parts 34 if (decimal) { //Decimal part not 0 35 SqList *L; 36 ElemType m; 37 L = (SqList*)malloc(sizeof(SqList)); //For pointer L Allocate memory address 38 InitList(L); //Initialization sequence table 39 while (decimal) //When decimal Not 0 40 { 41 m = (ElemType)floor(decimal*N); 42 BackInsert(L, m); //insert decimal*N Integer 43 decimal = decimal * N - m; 44 } 45 printf(". "); 46 PrintList(L); //Each number is separated by a space 47 } 48 } 49 int main() { 50 int N; 51 printf("Please enter target base:"); 52 scanf("%d", &N); 53 printf("Decimal number n Convert to%d Base number, please enter:n(>=0)=", N); 54 Conversion(N); 55 return 0; 56 57 }
Reasonable use of data structure can make the code easy to read and understand. Introduction of stack
The problem of program design is simplified, different levels of concern are divided, and the logical thinking is clear.