Data structure (Yan Weimin) sequential Stack

Source: Internet
Author: User
Next lecture! Oh! The basic operations on the sequence Stack are provided today (9 )! The Code is as follows (the code can be run after being pasted)

Some of my points: if the initial stack_init_size is defined as 10, it is best to save only nine. If there are 10 and the stack is full, the top Pointer Points to an invalid physical address, in this case, if the top pointer is incorrect, pointer overflow will occur! Insecure!

# Include "stdafx. H"

# Include <string. h>
# Include <ctype. h>
# Include <malloc. h> // malloc ()
# Include <limits. h> // int_max, etc.
# Include <stdio. h> // EOF (= ^ Z or F6), null
# Include <stdlib. h> // atoi ()
# Include <Io. h> // EOF ()
# Include <math. h> // floor (), Ceil (), ABS ()
# Include <process. h> // exit ()
// Code of the function result status
# Define true 1
# Define false 0
# Define OK 1
# Define error 0
# Define Infeasible-1
// # Define overflow-2 remove this line because the overflow value has been defined as 3 in math. h.
Typedef int status; // status indicates the function type, and its value is the function result status code, such as OK.
Typedef int Boolean; // Boolean is of the boolean type, and its value is true or false.

// Sequential storage representation of the c3-1.h Stack
# Define stack_init_size 10 // initial storage space allocation
# Define stackincrement 2 // increment of storage space allocation

Typedef int selemtype; // defines the stack element type as an integer.
Typedef struct sqstack
{
Selemtype * base; // The base value is null before and after stack construction.
Selemtype * Top; // stack top pointer */
Int stacksize; // The currently allocated storage space, in the unit of Elements
} Sqstack; // sequential Stack

// Basic operations for bo3-1.c sequential stack (storage structure defined by c3-1.h) (9)
Status initstack (sqstack * s)
{// Construct an empty stack s
(* S). base = (selemtype *) malloc (stack_init_size * sizeof (selemtype ));
If (! (* S). Base)
Exit (overflow);/* storage allocation failed */
(* S). Top = (* s). base;
(* S). stacksize = stack_init_size;
Return OK;
}

Status destroystack (sqstack * s)
{/* Destroy stack S, s no longer exists */
Free (* s). Base );
(* S). base = NULL;
(* S). Top = NULL;
(* S). stacksize = 0;
Return OK;
}

Status clearstack (sqstack * s)
{/* Set S to an empty stack */
(* S). Top = (* s). base;
Return OK;
}

Status stackempty (sqstack S)
{/* If Stack S is empty, true is returned; otherwise, false is returned */
If (S. Top = S. Base)
Return true;
Else
Return false;
}

Int stacklength (sqstack S)
{/* Returns the number of S elements, that is, the stack length */
Return S. top-S.base;
}

Status gettop (sqstack S, selemtype * E)
{/* If the stack is not empty, use e to return the top element of S stack and Return OK; otherwise, Return Error */
If (S. Top> S. Base)
{
* E = * (S. Top-1 );
Return OK;
}
Else
Return Error;
}

Status push (sqstack * s, selemtype E)
{/* Insert element e as the new stack top element */
If (* s). Top-(* s). Base> = (* s). stacksize)/* Full stack, append storage space */
{
(* S). base = (selemtype *) realloc (* s). Base, (* s). stacksize + stackincrement) * sizeof (selemtype ));
If (! (* S). Base)
Exit (overflow);/* storage allocation failed */
(* S). Top = (* s). Base + (* s). stacksize;
(* S). stacksize + = stackincrement;
}
* (* S). Top) ++ = E;
Return OK;
}

Status POP (sqstack * s, selemtype * E)
{/* If the stack is not empty, delete the stack top element of S, use e to return its value, and Return OK; otherwise, Return Error */
If (* s). Top = (* s). Base)
Return Error;
* E = * -- (* s). Top;
Return OK;
}

Status stacktraverse (sqstack S, status (* visit) (selemtype ))
{/* Call the visit () function for each element in the stack from the bottom of the stack to the top of the stack (). */
/* Once visit () fails, the operation fails */
While (S. Top> S. Base)
Visit (* S. Base ++ );
Printf ("/N ");
Return OK;
}
Status visit (selemtype C)
{
Printf ("% d", C );
Return OK;
}
Int main (INT argc, char * argv [])
{
Int J;
Sqstack S;
Selemtype E;
If (initstack (& S) = OK)
For (j = 1; j <= 12; j ++)
Push (& S, J );
Printf ("the elements in the stack are :");
Stacktraverse (S, visit );
Pop (& S, & E );
Printf ("pop-up top stack Element E = % d/N", e );
Printf ("Stack null No: % d (1: NULL 0: No)/n", stackempty (s ));
Gettop (S, & E );
Printf ("Stack top Element E = % d stack length: % d/N", E, stacklength (s ));
Clearstack (& S );
Printf ("No: % d (1: NULL 0: No)/n", stackempty (s ));
Destroystack (& S );
Printf ("after the stack is destroyed, S. top = % u s. base = % u s. stacksize = % d/N ", S. top, S. base, S. stacksize );
Return 0;
}

 

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.