Analysis of stack _c language in sequential structure storage

Source: Internet
Author: User
Tags assert

Stack definition: Linear table for inserts and deletions only at the end of the table

The characteristics of the stack:

1 in general can be in the end of the stack and the stack of data

2 Advanced and Out

3 stack will have stack top, stack bottom, usually the bottom of the stack is high address, stack top for high address, as shown in the following figure

The operating system typically draws a piece of memory, specifically for stack operations, which, of course, is somewhat different from ordinary operations: for example, an array is stored, the address is increased, but when the data is stored on the stack, the address is continuously reduced.

Storage structure of Stacks:

Copy Code code as follows:

typedef struct _SQSTACK
{
selemtype* Base;
selemtype* top;
int stacksize;
}
Sqstack;

Data definition:

Copy Code code as follows:

Size and space growth of the default storage space
#define STACK_INIT_SIZE 100
#define Stack_increment 10

Type definition for storing data
#ifndef Int_type
#define Int_type
#endif//Int_type

#ifdef Int_type
typedef int SELEMTYPE;
#elif defined Float_type
typedef float SELEMTYPE;
#elif defined String_type
typedef char* SELEMTYPE;
#elif defined Struct_type
typedef void* SELEMTYPE;
#endif

Stack operations, will involve initializing the stack, destroying the stack, into the stack (into the stack), out of the stack, but also to determine the stack empty, stack size, as well as the empty stack, as follows:
The initialization of the stack:

Copy Code code as follows:

Initialize stack
int Initstack (Sqstack *s)
{
S->base = (selemtype*) malloc (stack_init_size*sizeof (Selemtype));
if (! S->base)
{
return-1;
}
S->top = s->base;
S->stacksize = stack_init_size;
return 0;
}

Stack is initialized, there is no data, this time top,base all point to the base address of the allocation space, indicating that the stack empty
Destroy Stack:
Copy Code code as follows:

Destroying stacks
int Destroystack (Sqstack *s)
{
if (s->base)
{
Free (s->base);
S->base = NULL;
S->top = NULL;
s->stacksize = 0;
}
return 0;
}

If the stack exists, destroy the address space and set the stack size to 0
Into the stack:

Copy Code code as follows:

int Push (sqstack *s, const selemtype data)
{
ASSERT (S->base!= NULL);
if (s->top-s->base >= stack_init_size)
{
S->base = (selemtype*) realloc (S->base,
(Stack_init_size + stack_increment) * sizeof (Selemtype));
if (! S->base)
{
return-1;
}
S->top = S->base + s->stacksize;
S->stacksize + = stack_increment;
}
*s->top++ = data;

return 0;
}

If the stack exists, destroy the address space and set the stack size to 0
Into the stack:

Copy Code code as follows:

int Push (sqstack *s, const selemtype data)
{
ASSERT (S->base!= NULL);
if (s->top-s->base >= stack_init_size)
{
S->base = (selemtype*) realloc (S->base,
(Stack_init_size + stack_increment) * sizeof (Selemtype));
if (! S->base)
{
return-1;
}
S->top = S->base + s->stacksize;
S->stacksize + = stack_increment;
}
*s->top++ = data;

return 0;
}

If the size of the stack is greater than the allocated length, reallocate the space, and then point the top back to the new location, then save the data to the current top of the stack, and then stack top +1
Out Stack:

Copy Code code as follows:

Out Stack
int Pop (Sqstack *s, Selemtype *data)
{
ASSERT (S->base!= NULL);
if (s->base = = s->top)
{
return-1;
}
*data = * (--s->top);

return 0;
}

First place the stack top position-1, then get the value of the current position
The following are auxiliary functions, as follows:

Copy Code code as follows:

Whether the stack is empty
int Isstackempty (const sqstack &s)
{
Return ((S.base = = s.top)? true:false);
}

Copy Code code as follows:

Get the length of the stack
int getstacklength (const sqstack &s)
{
ASSERT (S.base!= NULL);

return s.stacksize;
}


Copy Code code as follows:

Empty stack
int Clearstack (Sqstack *s)
{
ASSERT (S->base!= NULL);
if (s->base!= s->top)
{
S->top = s->base;
}
s->stacksize = 0;

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.