Answer to Question #265143 in C for Samzzy

Question #265143

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).


1
Expert's answer
2021-11-13T01:18:18-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS