C language implementation Stack

Source: Internet
Author: User
Tags define null
// Sorry, there are not many comments. They were used directly by the second-year-old program. I will pay more attention in the future # define overflow-1
# Define error-2
# Define OK 1
# Define null 0
# Define stack_int_size 100
# Define stackincrement 10
# Include <stdio. h>
Typedef struct {
Char * Base ;
Char * top;
Int stacksize;
} Sqstack;
Int creatstack (sqstack * s)
{
(* S). base = (char *) malloc (stack_int_size * sizeof (char ));
If (! (* S). Base) return error;
(* S). Top = (* s). base;
(* S). stacksize = stack_int_size;
Return OK;
}
Int stacklength (sqstack S)
{Sqstack P;
Int length = 0;
P = s;
While (P. Base! = P. Top)
{P. Top --;
Length ++;
}
Return length;
}
Int gettop (sqstack S, char * E)
{If (S. base = S. Top) return error;
* E = * (S. Top-1 );
Return OK;
}
Int push (sqstack * s, char E)
{
If (* s). Top-(* s). Base> = (* s). stacksize ){
(* S). base = (char *) realloc (* s). Base, (* s). stacksize + stackincrement) * sizeof (char ));
If (* s). Base) Exit (overflow );
(* S). Top = (* s). Base + (* s). stacksize;
(* S). stacksize + = stackincrement;
}
* (* S). Top) ++ = E;
Return OK;
}
Int POP (sqstack * s, char * E)
{If (* s). Top = (* s). Base)
Return Error;
* E = * -- (* s). Top;
Return OK;
}
Int destroystack (sqstack * s)
{Char * P;
While (* s). Top! = (* S). Base)
{P = (* s). Top;
-- (* S). Top;
Free (P );
}
Return OK;
}
Int clearstack (sqstack * s)
{Int length, N;
If (S-> base = s-> top) Return OK;
Else
{While (S-> base! = S-> top)
* (-- S-> top) = NULL;
}
Return OK;
}
Int print (sqstack * s)
{
Char * P;
For (P = s-> top-1; P> = s-> base; p --)
Printf ("% C", * P );
Printf ("/N ");
Return OK;
}
Int stackempty (sqstack S)
{If (S. Top = S. Base)
Return OK;
Else
Return NULL;
}
Main ()
{Sqstack * stack, * stack1, * stack2;
Char * C, E;
Stack = (sqstack *) malloc (sizeof (sqstack ));
Stack1 = (sqstack *) malloc (sizeof (sqstack ));
Stack2 = (sqstack *) malloc (sizeof (sqstack ));
C = (char *) malloc (sizeof (char ));
Clrscr ();
Creatstack (stack );
Creatstack (stack1 );
Creatstack (stack2 );
Printf ("have created a stack, the length of it is % d/N", stacklength (* stack ));
If (! Stackempty (* stack ))
Printf ("Stack is not empty! /N ");
Else
Printf ("Stack is empty! /N ");
Printf ("Please input Chars (end '! '):/N ");
While (E = getchar ())! = '! ')
Push (stack, e );
Printf ("print the stack :");
Print (stack );
Printf ("the length of stack is % d/N", stacklength (* stack ));
While (! Stackempty (* stack) {Pop (stack, c); push (stack1, * C );}
While (! Stackempty (* stack1) {Pop (stack1, c); push (stack2, * C );}
While (! Stackempty (* stack2) {Pop (stack2, c); push (stack, * C );}
Printf ("reverse print the stackis :");
Print (stack );
Destroystack (stack );
Destroystack (stack1 );
Destroystack (stack2 );
Getch ();
}

 

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.