Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

stack in c

#include <stdio.h>
#include <malloc.h>
#include "ADTStack.h"

struct Stack_t {
void** array;
int top;
int maxCapacity;
void* (*cp_elm)(void*);
void (*fr_elm)(void*);
char* (*lbl_elm)(void*);
};

Stack init(int max_size, void* (*cpy_elm)(void*), void (*free_elm)(void*), char* (*label_elm)(void*)) {
	Stack ps;
	if ((max_size<=0)||(!cpy_elm)||(!free_elm)||(!label_elm)) {
		fprintf(stderr,"Fatal error
");
                return NULL; 
        }        
	ps = (Stack)malloc(sizeof(struct Stack_t));
	if (!ps) {
		fprintf(stderr,"Fatal error
");
		return NULL;
	}	
	ps->array = malloc(max_size*sizeof(void*));
	if (!(ps->array)) {
		free(ps);
		fprintf(stderr,"Fatal error
");
		return NULL;
	}
	ps->top = 0;
	ps->maxCapacity = max_size;
	ps->cp_elm = cpy_elm;
	ps->fr_elm = free_elm;
	ps->lbl_elm = label_elm;
	return ps;
}

int push (Stack s, void* elm) {
	if((!s)||(s->top==s->maxCapacity)) {
		fprintf(stderr,"Fatal error
");
		return 0;
	}
	s->array[s->top] = s->cp_elm(elm);
	s->top++;
	return 1;
}

void* pop (Stack s) {
	if ((!s)||(s->top==0)) {
		fprintf(stderr,"Fatal error
");
		return NULL;
	}
	s->top--;
	return s->array[s->top]; 
}

void clear (Stack s) {
	if (!s) {
		fprintf(stderr,"Fatal error
");
                return;
        } 
	while (s->top > 0)
		s->fr_elm(s->array[--s->top]);
}

int size(Stack s) {
	if (!s) {
                fprintf(stderr,"Fatal error
");
                return -1;
        }
	return s->top;
}

void print(Stack s) {
	int cur;
	char* label = NULL;
	if (!s) {
                fprintf(stderr,"Fatal error
");
                return;
        } 
	cur = s->top;
	while (cur > 0) {
		label = s->lbl_elm(s->array[--cur]);
		if (!label) {
			fprintf(stderr,"Fatal error
");
			return;
		} else {
			printf("%s
",label);
			free(label);
		}
	}
}

void destroy (Stack s) {
	if (!s) {
                fprintf(stderr,"Fatal error
");
                return;
        } 
	clear(s);
	free(s->array);
	free(s);
}
Comment

stack in c

#ifndef _ADTSTACK_H
#define _ADTSTACK_H

typedef struct Stack_t* Stack;

/* initializes the Stack; sets the maximal capacity to max_size
   returns a pointer to a record on the HEAP
 */
Stack init(int max_size, void* (*cpy_elm)(void*), void (*free_elm)(void*), char* (*label_elm)(void*));

/* inserts a copy of the element to the top of the stack. 
returns a non-zero value upon success, zero on fail.
fails if init was not called prior to that, or if the stack is full. */
int push (Stack s, void* elm);

/* removes the element at the top of the stack and returns it.
fails either if init was not called prior to that, or if the stack is empty */
void* pop (Stack s);

/* removes all elements from the stack; all elements are freed.
fails if init was not called prior to that */
void clear (Stack s);

/* returns the number of elements in the stack, or a negative value when fails.
fails either if init was not called prior to that, or if the stack is empty */
int size(Stack s);

/* prints the contents of the stack.
fails if init was not called prior to that */
void print(Stack s);

/* releases all the resources of the stack */
void destroy (Stack s);

#endif
Comment

PREVIOUS NEXT
Code Example
Typescript :: no corners in broder css 
Typescript :: int sum. 
Typescript :: json to ts type 
Typescript :: Error: Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate. React limits the number of nested updates to prevent infinite loops. 
Typescript :: google sheets query multiple or 
Typescript :: What kind of projects is suitable for the Agile methodology 
Typescript :: typescript deep partial 
Typescript :: c# merge two lists different types 
Typescript :: props tsx 
Typescript :: typescript cast string to number 
Typescript :: how to take inputs in one line in c 
Typescript :: convert javascript to typescript 
Typescript :: typescript typeof interface property 
Typescript :: angular minus date 
Typescript :: typescript implement 
Typescript :: typescript equals string 
Typescript :: Display Popular Posts laravel 
Typescript :: what is use hsts in .net core 
Typescript :: import validator adonisjs 5 
Typescript :: Destructuring props in styled-components 
Typescript :: when should you stop testing 
Typescript :: field sets in salesforce 
Typescript :: How to pass multiple route parameters in Ionic-Angular? 
Typescript :: DISTINQUISH BETWEEN THE AVERAGE CASE AND WORSE CASE RUNNING TIME AND THE FACTORS AFFECTING THAT AFFECTS THE RUNNING TIME OF AN ALGORITHM 
Typescript :: how do you check ewhich version of typescript you are using 
Typescript :: serenity framework break form 
Typescript :: language 
Typescript :: .for each typescript 
Typescript :: ionic iosa app not making requests to server 
Typescript :: optional or required depending on param is true react typescript 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =