-
Stack
I want to implement satck in a c program ,i get the functions but they can't run properly same case for queue also anyone help?
the function are as
Code:
typedef struct t_stack *stack;
stack ConsStack( int max_items, int item_size );
/* Construct a new stack
Pre-condition: (max_items > 0) && (item_size > 0)
Post-condition: returns a pointer to an empty stack
*/
void Push( stack s, void *item );
/* Push an item onto a stack
Pre-condition: (s is a stack created by a call to ConsStack) &&
(existing item count < max_items) &&
(item != NULL)
Post-condition: item has been added to the top of s
*/
void *Pop( stack s );
/* Pop an item of a stack
Pre-condition: (s is a stack created by a call to
ConsStack) &&
(existing item count >= 1)
Post-condition: top item has been removed from s
*/
-
stack again...
i get the stack function correctly and make a .h file of that now i want to implement them in main program .what should i do?
my program is :
Code:
#include<stdio.h>
#include "mystack.h"
void push(Stack *S, float val)
{
S->v[ S->top ] = val;
(S->top)++;
/* Equivalent to: S->v[ (S->top)++ ] = val; */
}
float pop(Stack *S)
{
(S->top)--;
return (S->v[S->top]);
/* Equivalent to: return (S->v[--(S->top)]); */
}
void init(Stack *S)
{
S->top = 0;
}
int full(Stack *S)
{
return (S->top >= 20);
}
void MyStackPrint(Stack *S)
{
int i;
if (S->top == 0)
printf("Stack is empty.\n");
else
{
printf("Stack contents: ");
for (i=0;i<S->top;i++)
{
printf("%g ",S->v[i]);
}
printf("\n");
}
}
http://cboard.cprogramming.com/misc....bbcode#buttons
Fixed by Salem
-
What do you mean you want to use it in the main program? Ok, so stick it in there and use it. I'm not sure what the question is. You made a stack, assuming it works, where's the problem? Whenever you need a stack, throw it in, create and instance, and call the functions at the needed times.
Quzah.