Skip to content

Instantly share code, notes, and snippets.

@defrindr
Created March 7, 2021 17:23
Show Gist options
  • Save defrindr/9049f8be6839b6d680f31bf7aae48039 to your computer and use it in GitHub Desktop.
Save defrindr/9049f8be6839b6d680f31bf7aae48039 to your computer and use it in GitHub Desktop.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 100
struct Stack {
char* origin;
int count;
int top;
unsigned capacity;
} Stack;
struct Stack* createStack(unsigned cp){
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = cp;
stack->top = -1;
stack->origin = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->origin[++stack->top] = item;
}
void print(struct Stack* stack){
printf("%s\n", stack->origin);
}
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
stack->origin[stack->top] = '\0';
stack->top--;
}
int main(){
struct Stack* stack = createStack(1000);
char str[max];
char* check;
char* cmp="*";
fgets(str, max, stdin);
int popping = 0;
for ( check=&str[0]; *check != '\0'; check++ )
{
int i1 = *cmp;
int i2 = *check;
if(i1 == i2){
pop(stack);
if(popping == 1){
print(stack);
}
popping = 1;
}else{
push(stack, *check);
}
}
// puts(str);
// printf("%d", len);
}
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 100
struct Stack {
char* origin;
int count;
int top;
unsigned capacity;
} Stack;
struct Stack* createStack(unsigned cp){
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = cp;
stack->top = -1;
stack->origin = (char*)malloc(stack->capacity * sizeof(char));
return stack;
}
int isFull(struct Stack* stack)
{
return stack->top == stack->capacity - 1;
}
int isEmpty(struct Stack* stack)
{
return stack->top == -1;
}
void push(struct Stack* stack, char item)
{
if (isFull(stack))
return;
stack->origin[++stack->top] = item;
}
void print(struct Stack* stack){
printf("%s\n", stack->origin);
}
char pop(struct Stack* stack)
{
if (isEmpty(stack))
return INT_MIN;
return stack->origin[stack->top--];
}
int main(){
struct Stack* stack = createStack(1000);
char str[max];
char res[max];
char* check;
char* cmp="*";
fgets(str, max, stdin);
int popping = 0;
int len = strlen(str), i;
for ( check=&str[0]; *check != '\0'; check++ )
{
push(stack, *check);
}
for ( i = 0; i < len; i++ )
{
res[i] = pop(stack);
}
printf("%s\n", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment