Skip to content
Snippets Groups Projects
Commit e5d98251 authored by thib's avatar thib
Browse files

aled

parent 7822da87
Branches
No related tags found
No related merge requests found
......@@ -9,6 +9,12 @@
// } stack;
void stack_init(stack *stack, int size, int typeSize) {
stack->data = malloc(typeSize * size);
stack->top = -1;
stack->capacity = size;
}
bool is_empty(stack stack) { return stack.top == -1; }
bool is_full(stack stack) { return stack.capacity - 1 == stack.top; }
......@@ -22,12 +28,6 @@ void *peek(stack stack) {
int stack_count(stack stack) { return stack.top + 1; }
void stack_init(stack *stack, int size, int typeSize) {
stack->data = malloc(typeSize * size);
stack->top = -1;
stack->capacity = size;
}
void stack_destroy(stack *stack) {
free(stack->data);
stack->top = -1;
......@@ -68,7 +68,7 @@ char *toPostfix(char *infix, int len) {
char *postfix = malloc(sizeof(char) * len);
int j = 0;
stack_init(&s, len, sizeof(char));
stack_init(&s, len, sizeof(char*));
for (int i = 0; i < len; ++i) {
if (infix[i] == ')') {
......
......@@ -3,19 +3,9 @@
#include <string.h>
int main() {
// stack s;
// stack_init(&s, 5, sizeof(int));
// int *a = malloc(sizeof(int));
// *a = 3;
// push(&s, a);
// printf("%d \n", *(int *)peek(s));
// stack_destroy(&s);
// free(a);
char infix1[]="A+(B/C–D^E";
int len=strlen(infix1);
printf("%d\n",len);
//char infix[5]={'a','+','b','x','c'};
char *postfix=toPostfix(infix1, len);
int len=5;
char infix[5]={'a','+','b','x','c'};
char *postfix=toPostfix(infix, len);
for(int i=0;i<len;i++){
printf("%c ",postfix[i]);
......@@ -23,7 +13,7 @@ for(int i=0;i<len;i++){
printf("\n");
free(postfix);
// char *postfix1 = malloc(sizeof(char) * 5);
// // char *postfix1 = malloc(sizeof(char) * 5);
// stack s;
// int j = 0;
......
......@@ -5,6 +5,40 @@
int main(){
stack s;
// stack_init(&s,1,sizeof(int*));
// int x=3;
// push(&s,&x);
// printf("%d",*(int*)pop(&s));
stack s2;
stack_init(&s2,1,sizeof(char*));
char c='a';
push(&s2,&c);
printf("%c",*(char*)pop(&s2));
stack_init(&s,5,sizeof(int*));
//char infix[5]={'a','+','b','x','c'};
char infix[5]={1,2,3,4,5};
for(int i=0;i<5;i++){
// char *c=malloc(sizeof(char));
// *c=infix[i];
push(&s,&infix[5]);
}
printf("%d",*(int*)pop(&s));
//char z=*(char*)pop(&s);
for(int i=0;i<5;i++){
// char *c=malloc(sizeof(char));
// *c=infix[i];
// printf("%c\n\n\n\n ",*(char*)pop(&s));
//printf("%c",*(char*)pop(&s));
}
stack_destroy(&s);
stack_destroy(&s2);
//free(c);
return EXIT_SUCCESS;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment