Stack C array implementation, C Array Implementation
Stack is an advanced post-release data structure.
Stack operations include: inbound, outbound, initialization, clearing, and traversing.
The C code is as follows:
# Include <stdio. h> # define MaxSize 20 typedef int ElemType; typedef struct stack {ElemType Data [MaxSize]; int top ;}stack; // initialize Stack void InitStack (Stack * S) {S-> top =-1 ;}// void PushStackValue (Stack * S) {printf ("Input the Value of stack member: \ n (0-exit) \ n "); int value; printf (" Please input the 1st value of stack: \ n "); scanf (" % d ", & value ); s-> Data [++ S-> top] = value; while (value) {S-> top ++; printf ("Please input the % dst value of stack: \ n ", S-> top + 1); scanf (" % d ", & value); S-> Data [S-> top] = value ;}} // output Stack void PopStackValue (stack * S) {if (S-> top> = 0) {printf ("the Stack % dst value pop out: % d \ n ", s-> top + 1, S-> Data [-- S-> top]);} else {printf ("The Stack is empty \ n ");}} // determine The empty Stack void IsEmpty (Stack * S) {if (S-> top =-1) {printf ("The Stack is empty. \ n ");} else {printf (" The stack is not empty. \ n ") ;}/// clear the Stack void ClearStack (Stack * S) {S-> top =-1;} // traverse the Stack void ScanStack (Stack * S) {int I; int len = S-> top-1; int StackArray [len]; for (I = len; I> 0; I --) {StackArray [I] = S-> Data [I --];} printf ("The all stack member (from top to bottom) is: \ n "); while (len> = 0) {printf ("% d", S-> Data [len --]);} printf ("\ n");} void main () {Stack S; InitStack (& S); PushStackValue (& S); ScanStack (& S); IsEmpty (& S); PopStackValue (& S ); popStackValue (& S );}
The running result is as follows:
Reprinted by Liu
Use arrays to implement the data structure of stacks and queues
# Include <stdlib. h>
# Include <stdio. h>
Class Stack
{
Private:
Int ele [100];
Int top;
Public:
Stack (){
Top = 0;
}
Void push (int v ){
Ele [top ++] = v;
}
Int pop (){
If (isEmpty ()){
Printf ("The stack is Empty. \ n ");
Return-1;
}
Return ele [-- top];
}
Bool isEmpty (){
Return top = 0;
}
~ Stack (){
}
};
# Define SIZE 100
Class Queue
{
Private:
Int ele [SIZE];
Int head;
Int tail;
Public:
Queue (){
Head = 0;
Tail = 0;
}
Bool isFull (){
Return (tail + 1) % SIZE = head;
}
Bool isEmpty (){
Return head = tail;
}
Void enqueue (int val ){
If (isFull ()){
Printf ("The queue is full, can not enqueue for: % d \ n", val );
Return;
}
Ele [tail] = val;
Tail = (tail + 1) % SIZE;
}
Int dequeue (){
If (isEmpty ()){
Printf ("The queue is empty, can not dequeue. \ n ");
Return-1;
}
Int value = ele [head];
Head = (head + 1) % SIZE;
Return value;
}
};
Int main ()
{
Stack s;
S. push (1, 123 );
S. push (12 );
While (! S. isEmpty ()){
Printf ("% d", s. pop ());
}
Printf ("\ n ");
Queue q;
Q. enqueue (88 );
Q. enqueue (13 );
Q. enqueue (123 );
While (! Q. isEmpty () printf ("% d", q. dequeue ());
Printf ("\ n ");
System ("pause ");
}... Remaining full text>
Can I use an array to implement the C language code for Stack?
Stack is really convenient in processing arrays. This is the basic method function of stack.
// Stack. cpp
# Include "stack. h"
Status SqStack: InitStack (SqStack ** S)
{(* S) = (SqStack *) malloc (sizeof (SqStack ));
(* S)-> base = (SElemType *) malloc (STACKSIZE * sizeof (SElemType ));
If (! (* S)-> base) exit (OVERFLOW );
(* S)-> top = (* S)-> base;
(* S)-> stacksize = 0;
Return 1 ;}
Status SqStack: DestroyStack ()
{Free (base); return 1 ;}
Status SqStack: ClearStack ()
{Stacksize = 0; return 1 ;}
Bool SqStack: StackEmpty ()
{If (stacksize = 0) return true;
Else return false;
}
Int SqStack: StackLength ()
{Return stacksize ;}
SElemType SqStack: GetTop ()
{If (top = base)
{Cerr <"Empty stack! \ N "; exit (1 );}
Return * (top-1 );
}
Status SqStack: Push (SElemType e)
{* (Top ++) = e; stacksize ++;
Return 1;
}
SElemType SqStack: Pop (SElemType * e)
{If (top = base)
{Cerr <"Empty stack! \ N "; exit (1 );}
* E = * -- top;
Stacksize --;
Return * e;
}
Void SqStack: StackTraverse (void (* visit) (SElemType *))
{While (top! = Base ){
Stacksize --; visit (-- top );}}