C Language Implementation sequence stack experience

Source: Internet
Author: User

Composition and initialization of stacks

For the "advanced out" of the sequential stack, you need to control 3 of the volume: stack elements, stack top pointers, stack capacity.

Stack capacity is the maximum capacity of the stack, and if exceeded, an overflow error is generated. Of course, you can also expand capacity if necessary.

Stack elements can be stored through dynamic arrays (base[]). Initialize with malloc request (Stack capacity * element type) space.

The stack top pointer is used to record the number of stack elements, always pointing to the previous unit of the top element of the stack (such as the top element of the stack is base[3], the stack top pointer is 4), so that the number of elements can be recorded. However, the stack-top pointer is just a visual term for convenience and is generally defined as an int. When initialized, the top pointer of the stack is 0.

Define Structure body

struct STA
{
int top;
int *base;
int size;
};

typedef struct STA ZAN;

Creation of stacks (allocated space)

zan* Create ()
{
ZAN *p;

p= (ZAN *) malloc (sizeof (ZAN));

return p;
}

Initialization of the stack

void Zer (ZAN *s)
{
s->top=0;
s->size=ini; #define INI 10
s->base= (int *) malloc (ini*sizeof (int));
}

Press Stack

The stack is to let the elements into the stack, top pointer top plus one, the elements are stored in the array base[]. If it is already full, you can increase the memory space (in fact, reallocate the space), at this time the stack capacity size is added one. Use the return value to determine if the stack is successful.

int push (ZAN *s,int x)
{

if ((s->top) >= (s->size))
{
s->base= (int *) realloc (s->base, (s->size+1) *sizeof (int));
if (!s->size) return 0;
s->size++;
}
s->top++;
s->base[s->top-1]=x;
return 1;

}

Take the top element of the stack

The top is only used to record the value of the top element of the stack without having to stack it. You can use pointers to take values. Similarly, the return value is used to determine whether the top is successful.

int GetTop (ZAN *s,int *e)
{
if (s->top==0) return 0;
Else
{
*e=s->base[(S->top)-1];
return 1;
}
}

Pop stacks

The stack is to take out its value, and let the stack top element out of the stack, top minus one. Similarly, use the pointer to record its value, and use the return value to determine whether the stack succeeds.

int pop (ZAN *s,int* e)
{
if (!s->top) return 0;
Else
{
*e=s->base[(S->top)-1];
s->top--;
return 1;
}
}

Ask the stack length

Remove the top value to

int Getlen (zan* s)
{
Return s->top;
}

Determine if the stack is empty

See if Top is 0

int IsEmpty (zan* s)
{
if (!s->top) return 0;
else return 1;
}

Output stack elements

Output base[] From top to bottom, no action on the stack.

void list (zan* s)

{

int i;

Puts ("\ n list ZAN:");

for (i=s->top-1;i>=0;i--)

{

if (i!=s->top-1) printf ("\ n"); You can use spaces instead of line breaks here.

printf ("%d", s->base[i]);

}

Puts ("");

}

Test function

TEST FUNCTION

Main ()
{
ZAN *create ();
void Zer (ZAN *s);
int push (ZAN *s,int x);
int GetTop (ZAN *s,int *e);
int Getlen (zan* s);
int IsEmpty (zan* s);
void list (zan* s);
ZAN *s;
int n,*e,i=1;

    i=1;e=&i;                                                             //Protect


    s=create ();                                                           //Create a stack
    zer (s);                                                                //Initialize

if (!isempty (s)) puts ("ZAN is empty\n"); Empty?
Else puts ("ZAN is not empty\n");

Puts ("Push int x:"); Push an element in
scanf ("%d", &n);
if (push (s,n));
else printf ("Push%d error\n", n);

if (GetTop (s,e)) printf ("GetTop%d\n", *e); Get top element
Else puts ("GetTop error");

printf ("\ n push int x:\n");
scanf ("%d", &n);
if (push (s,n)); Push an element in
else printf ("Push%d error\n", n);


if (GetTop (s,e)) printf ("GetTop%d\n", *e); Get top element
Else puts ("GetTop error");

if (pop (s,e)) printf ("Pop%d\n", *e); Pop Top Element
else printf ("Pop%d error\n", n);

if (GetTop (s,e)) printf ("GetTop%d\n", *e); Get top element
Else puts ("GetTop error");

if (!isempty (s)) puts ("ZAN is empty\n"); Empty?
Else puts ("ZAN is not empty\n");
if (IsEmpty (s)) list (s);

printf ("\ n push int x:\n");
scanf ("%d", &n);
if (push (s,n)); Push an element in
else printf ("Push%d error\n", n);

if (IsEmpty (s)) list (s); Output ZAN
}

C Language Implementation sequence stack experience

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.