LED PANEL

Google It, You'll Get It.

Monday, March 28, 2011

Stacks - Coding Structure in C

-STACKS
(im using dashes to maintain the indentation here)

--creating the structure
Struct stack
{
Char data [MAX];
int top;
};

--declaring the functions
char pop (struct stack *stk);
void push (struct stack *stk, char item);


--initializing in Main Program(Function)
main()
{
stack s;
s.top = -1;

push( &s , "A" ) // Calling the Push Function while inserting letter "A" into a stack
pop( &s ) // Removing the element

}


--PUSH Function 
void push (stack *stk, char item)
{
If (stk ->top < MAX -1)
{
stk->top++;
stk->data[stk->top] = item;
}
Else
printf(“stack is full \n”);
}

--POP Function
char pop (stack *stk)
{
If(stk->top > -1 )
{
char x = srk -> data[stk->top];
stk->top--;
return x;
}
Else
{
printf(“stack is empty \n”);
return 0;
}
}

No comments:

Post a Comment