Definition, initialization, empty stack judgment, in-stack, and out-stack operation of the "Data Structure Tour" sequence stack

Source: Internet
Author: User
Tags constant definition

Description

to learn the data structure, want to run a complete sequence of the program can not run, because the book is a part of the algorithm, and did not provide a complete operation of the program, listening to the experimental class, I toss a bit, finally can write a relatively complete sequence of the operation of the small program, For the stack also slowly began to have a feeling. I will take the whole program apart to explain, just put the code in a file, the compiler can be directly compiled to run.



First, to achieve


1. Program functions

on the stack operation of the classic program, the first time to mention the conversion of the number of conversions, the use of the stack operation, it can be very fast to complete the number of binary conversion.



2. Pre-defined, header file import and type aliases

The code is as follows:

#include <stdio.h> #include <stdlib.h> #define Overflow-1#define ERROR 0#define FALSE 0#define TRUE 1#define OK 1typedef int elemtype;typedef int Status;

In addition to the import of two header files is required, the following two points are explained:

(1) The rest of the constant definition is optional, in the following code writing process can use English as much as possible to express the meaning of the program, rather than in the implementation of the code in the process of direct use of numbers, according to personal preference, can also directly use the numbers;

(2) using typedef to do the type of alias is only for the code in the program meaning more clear, the actual can not be used;



3. Definition of sequential stacks

The code is as follows:

typedef struct{elemtype *elem;            The base address of the storage space int top;           The next element of the top element of the stack, referred to as the top of the stack int size;      The current allocated storage capacity, the role of looking into the stack operation can be known int increment;                  When expanding, the increased storage capacity, the role of looking into the stack operation can be known} sqstack; Sequential stack Name



4. Initialization of the stack

The code is as follows:

Status initstack_sq (sqstack &s, int size, int inc) {//accepts 3 parameters, &s is a reference to struct S.elem = (elemtype*) malloc (size*sizeof (  Elemtype));     Allocate storage space if (S.elem = = NULL) return OVERFLOW;            Determine if the previous step allocated storage space is successful s.top = 0;        Set S is an empty stack, S.top is 0 means that the stack is empty stack s.size = size;    Stack's space initial capacity Value s.increment = Inc;      The space initial increment value of the stack (if needed) return OK; The above performs normally, returns OK}



5. Evaluation of the empty stack

The code is as follows:

Status stackempty_sq (Sqstack S) {if (S.top = = 0) return True;elsereturn FALSE;} The decision of the empty stack is that if the stack is empty, return 1, or return 0, of course, you can not rule;//As to why short stack judgment, naturally there is a reason, the following look at the code of the program can be known.



6. Into the stack

The code is as follows:

STATUS&NBSP;PUSH_SQ (sqstack &s, elemtype e) {    //pushes element e into the stack, Here, E is just a formal parameter. elemtype *newbase;        //defines the intermediate variable if (s.top>=  S.size) {        //s.top If you point to the last address that does not store the element, that is, S.top is greater than newbase =  ( elemtype*) realloc (s.elem, //equals s.size, it means the stack is full (s.size + s.increment) *sizeof (elemtype));  // Determine if the expansion is successful by realloc dynamic expansion if (null == newbase)  return overflow; //s.elem =  newbase;      //the value of the intermediate variable is only pointed to S.elem when the expansion is successful, to prevent the expansion failure, s.size = s.size +  s.increment;     //s.elem point to a location that is not the original}s.elem[s.top] = e;     //the e element into the stack s.top++;               //makes S.top add 1, indicating that the top position of the stack is pointed return ok;             //return 1} after normal operation



7. The stack

The code is as follows:

Status pop_sq (Sqstack &s, Elemtype &e) {//stack top element out stack, assign to element eif (0 = = s.top) return ERROR;    e = S.elem[--s.top]; e out of the stack, and will s.top minus 1return OK;}



8. Functions of the binary conversion

In fact, the above steps are to create a sequence stack and define the operation of the sequence stack, and to do some of the possible situations to do some corresponding action, after completion, the following will be used to create the above sequence stack and the operation of the Stack interface, That is, in the number conversion function (here is the decimal to octal) using the above operation interface, the code is as follows:

void converstion (int N) {Sqstack S;    Elemtype e;initstack_sq (S, 10, 5);   Stack S initial capacity is set to 10, each capacity is 5while (N! = 0) {push_sq (S, n%8);            Divide n by the remainder of 8 into the stack n/= 8;    n the quotient of which the value is divided by 8}//Theoretical basis for the addition of 8 take the remainder method while (stackempty_sq (s) = = FALSE) {pop_sq (S, e); The remainder in the output stack is sequentially assigned to the element eprintf ("%d", e); Print element}



9.main function

The input conversion function calls the interface function of the stack operation, in order to realize the operation of the stack during the system conversion; The main function calls the system conversion function to achieve the conversion of the number number, the code is as follows:

int main (void) {printf ("Enter a number:"); scanf ("%d", &num); Converstion (num);p rintf ("\ n");}



Second, the implementation

With the above code, it can be compiled in the compiler execution, here I am using c free 5来 program code compilation:


(1) The result when the number entered is 1348:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/73/73/wKiom1X-bHCSWO55AADAAgjs3ew463.jpg "title=" 1.png " alt= "Wkiom1x-bhcswo55aadaagjs3ew463.jpg"/>


(2) The result when the number entered is 2526:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/73/70/wKioL1X-bsDTXhERAADDp63JNCE633.jpg "title=" 2526. PNG "alt=" wkiol1x-bsdtxheraaddp63jnce633.jpg "/>



Three, the complete code

The following code is put together:

#include <stdio.h> #include <stdlib.h> #define  overflow -1#define error 0#define  FALSE 0#define TRUE 1#define OK 1typedef int ElemType;typedef  Int status;typedef struct{elemtype *elem;int top;int size;int increment;}  SqStack; STATUS&NBSP;INITSTACK_SQ (sqstack &s, int size, int inc) {S.elem =  ( elemtype*) malloc (size*sizeof (elemtype)); if (s.elem == null)  return OVERFLOW; s.top = 0; s.size = size; S.increment = inc;return ok;} STATUS&NBSP;STACKEMPTY_SQ (sqstack s) {if (s.top == 0) Return true;elsereturn false;} STATUS&NBSP;PUSH_SQ (sqstack &s, elemtype e) {elemtype *newbase;if (S.top>=  s.size) {newbase =  (elemtype*) realloc (S.elem, (s.size + s.increment) *sizeof (ElemType)); Null == newbase)  return OVERFLOW; s.elem = newbase; S.size = s.size + s.increment;} s.elem[s.top] = e; S.top++;return ok;} STATUS&NBSP;POP_SQ (sqstack &s, elemtype &e) {if (0 == s.top)  return  Error;e = s.elem[--s.top];return ok;} Void converstion (int n) {sqstack s; ELEMTYPE&NBSP;E;INITSTACK_SQ (s, 10, 5); while (n != 0) {push_sq (s, n%8); N /= 8;} while (STACKEMPTY_SQ (S)  == false) {pop_sq (s, e);p rintf ("%d",  e);}} Int main (void) {int num;printf ("Enter a number:"); scanf ("%d", &num); Converstion (num);p rintf ("\ n");}



This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1696524

Definition, initialization, empty stack judgment, in-stack, and out-stack operation of the "Data Structure Tour" sequence stack

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.