Devise a c programming interface for a stack, i.e a (stack.h) header file. Your stack should not arbitrarily add constraint(i.e: max depth of stack).
#ifndef CODE_STACK_H
#define CODE_STACK_H
#include <stdio.h>
struct SItem
{
char m;
struct SItem *next;
};
void topStack();
int isFull();
char Pop();
int Push(char m);
int isEmpty();
void deleteStack();
#ifndef FALSE
#define FALSE (0)
#endif
#ifndef TRUE
#define TRUE (!FALSE)
#endif
#endif
#include <stdlib.h>
#include "code_stack.h"
static struct SItem *top;
void topStack()
{
top = NULL;
}
void deleteStack()
{
struct SItem *temp;
if(!isEmpty())
{
temp = top;
while(top != NULL)
{
temp = top;
top = top->next;
free(temp);
}
}
}
int Push(char m)
{
struct SItem *Nnew;
Nnew = (struct SItem *)malloc(sizeof(struct SItem));
if(Nnew == NULL) return FALSE;
Nnew->m = m;
Nnew->next = NULL;
if(isEmpty())
{
top = Nnew;
}
else
{
Nnew->next = top;
top = Nnew;
}
return TRUE;
}
char Pop()
{
char m;
struct SItem *temp;
if(isEmpty()) return '\0';
temp = top;
top = top->next;
m = temp->m;
free(temp);
return m;
}
int isEmpty()
{
return (top == NULL);
}
int isFull()
{
return FALSE;
}
Comments
Leave a comment