Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions 5.Stack-and-Quene/Stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"


OBJ* stack_new(void)
{
return NULL;
}

OBJ* stack_push(OBJ* top, char data)
{
OBJ* ptr = malloc(sizeof(OBJ));
ptr->data = data;
ptr->next = top;

return ptr;
}

// Взять элемент со стека
OBJ* stack_pop(OBJ* top)
{
if(top == NULL)
{
printf("Стек пуст\n");
return top;
}

OBJ* ptr_next = top->next;
free(top);

return ptr_next;
}

OBJ* stack_delete(OBJ* top)
{
OBJ* current = top;
while (current != NULL)
{
OBJ* temp = current;
current = current->next;
free(temp);
}

return 0;
}

// Функция для просмотра элемента сверху
char stack_peek(const OBJ* top)
{
if(top == NULL)
{
printf("Стек пуст\n");
return -1;
}

return top->data;
}

void show(const OBJ* top)
{
const OBJ* current = top;
if(top == NULL)
{
printf("Стек пуст\n");
return;
}

while(current != NULL)
{
printf("%d\n", current->data);
current = current->next;
}
}
28 changes: 28 additions & 0 deletions 5.Stack-and-Quene/Stack/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef STACK_H
#define STACK_H

typedef struct tag_obj
{
char data;
struct tag_obj* next;
} OBJ;

// Создание нового стека
OBJ* stack_new(void);

// Добавление элемента на стек
OBJ* stack_push(OBJ* top, char data);

// Удаление верхнего элемента
OBJ* stack_pop(OBJ* top);

// Удаление стека
OBJ* stack_delete(OBJ* top);

// Просмотр верхнего элемента
char stack_peek(const OBJ* top);

// Вывод содержимого
void stack_show(OBJ* top);

#endif
118 changes: 118 additions & 0 deletions 5.Stack-and-Quene/Task_2/Sorting-yard.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "../Stack/stack.h"


int precedence(char op)
{
switch(op)
{
case '+':
case '-':
return 1;

case '*':
case '/':
return 2;

default:
return 0;
}
}

int is_operator(char c)
{
return c == '+' || c == '-' || c == '*' || c == '/';
}

char* infix_to_postfix(const char* infix)
{
static char output[1000];
output[0] = '\0';

OBJ* stack = stack_new();
int output_index = 0;

for (int i = 0; infix[i] != '\0'; i++)
{
char token = infix[i];

if (isspace(token))
{
continue;
}

if (isdigit(token))
{
while (isdigit(infix[i]))
{
output[output_index++] = infix[i++];
}

output[output_index++] = ' ';
i--;
}

else if (token == '(')
{
stack = stack_push(stack, token);
}

else if (token == ')')
{
while (stack != NULL && stack_peek(stack) != '(')
{
output[output_index++] = stack_peek(stack);
output[output_index++] = ' ';
stack = stack_pop(stack);
}

if (stack != NULL && stack_peek(stack) == '(')
{
stack = stack_pop(stack);
}
}

else if (is_operator(token))
{
while (stack != NULL && stack_peek(stack) != '(' &&
precedence(stack_peek(stack)) >= precedence(token))
{
output[output_index++] = stack_peek(stack);
output[output_index++] = ' ';
stack = stack_pop(stack);
}

stack = stack_push(stack, token);
}
}

while (stack != NULL)
{
output[output_index++] = stack_peek(stack);
output[output_index++] = ' ';
stack = stack_pop(stack);
}

if (output_index > 0 && output[output_index-1] == ' ')
{
output_index--;
}

output[output_index] = '\0';
stack_delete(stack);
return output;
}

int main() {
char infix[1000];

printf("Введите инфиксное выражение: ");
scanf("%999[^\n]", infix);

char* postfix = infix_to_postfix(infix);
printf("Постфиксная запись: %s\n", postfix);

return 0;
}
80 changes: 80 additions & 0 deletions Stack/stack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"


typedef struct tag_obj
{
int data;
struct tag_obj* next;
} OBJ;

OBJ* stack_new(void)
{
return NULL;
}

OBJ* stack_push(OBJ* top, int data)
{
OBJ* ptr = malloc(sizeof(OBJ));
ptr->data = data;
ptr->next = top;

return ptr;
}

// Взять элемент со стека
OBJ* stack_pop(OBJ* top)
{
if(top == NULL)
{
printf("Стек пуст\n");
return top;
}

OBJ* ptr_next = top->next;
free(top);

return ptr_next;
}

OBJ* stack_delete(OBJ* top)
{
OBJ* current = top;
while (current != NULL)
{
OBJ* temp = current;
current = current->next;
free(temp);
}

return 0;
}

// Функция для просмотра элемента сверху
int stack_peek(const OBJ* top)
{
if(top == NULL)
{
printf("Стек пуст\n");
return -1;
}

return top->data;
}

void show(const OBJ* top)
{
const OBJ* current = top;
if(top == NULL)
{
printf("Стек пуст\n");
return;
}

while(current != NULL)
{
printf("%d\n", current->data);
current = current->next;
}
}
28 changes: 28 additions & 0 deletions Stack/stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifdef STACK_H
#define STACK_H

typedef struct tag_obj
{
int data;
struct tag_obj* next;
} OBJ;

// Создание нового стека
OBJ* stack_new(void);

// Добавление элемента на стек
OBJ* stack_push(OBJ* top, int data);

// Удаление верхнего элемента
OBJ* stack_pop(OBJ* top);

// Удаление стека
OBJ* stack_delete(OBJ* top);

// Просмотр верхнего элемента
int stack_peek(OBJ* top);

// Вывод содержимого
void stack_show(OBJ* top);

#endif