Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Выполнено индивидуальное задание 2 #85

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -11,5 +11,9 @@ add_subdirectory(TestStackC)
add_subdirectory(TestVectorC)

add_subdirectory(TestArrayCPP)
add_subdirectory(TestListCPP)
add_subdirectory(TestStackCPP)

add_subdirectory(TestArrayCPPTemplate)

add_subdirectory(Task)
54 changes: 43 additions & 11 deletions LibraryCPP/list.cpp
Expand Up @@ -3,59 +3,91 @@

struct ListItem
{
ListItem* pNext;
Data data;
};

struct List
{
ListItem* pHead;
};

List *list_create()
{
return new List;
List* list = new List;
list->pHead = NULL;
return list;
}

void list_delete(List *list)
{
// TODO: free items
delete list;
ListItem* currect = list->pHead;
while (currect != NULL)
{
ListItem* temp = currect->pNext;
delete currect;
currect = temp;
}
delete list;
}

ListItem *list_first(List *list)
{
return NULL;
return list->pHead;
}

Data list_item_data(const ListItem *item)
{
return (Data)0;
return item->data;
}

ListItem *list_item_next(ListItem *item)
{
return NULL;
return item->pNext;
}

ListItem *list_item_prev(ListItem *item)
{
return NULL;
// Not for single list
return NULL;
}

ListItem *list_insert(List *list, Data data)
{
return NULL;
ListItem* item = new ListItem;
item->data = data;
item->pNext = list->pHead;
list->pHead = item;
return item;
}

ListItem *list_insert_after(List *list, ListItem *item, Data data)
{
return NULL;
ListItem* newItem = new ListItem;
newItem->data = data;
newItem->pNext = item->pNext;
item->pNext = newItem;
return newItem;
}

ListItem *list_erase(List *list, ListItem *item)
{
return NULL;
ListItem* currect = list->pHead;
ListItem* last;
while (currect != item)
{
last = currect;
currect = currect->pNext;
}
last->pNext = currect->pNext;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Где-то надо ещё и память от удаленного элемента освободить.

delete currect;
return NULL;
}

ListItem *list_erase_next(List *list, ListItem *item)
{
return NULL;
ListItem* newItem = item->pNext;
item->pNext = newItem->pNext;
delete newItem;
return item;
}
2 changes: 1 addition & 1 deletion LibraryCPP/list.h
Expand Up @@ -3,7 +3,7 @@

// List
// Stores integer values inside
typedef int Data;
typedef char Data;

struct List;
struct ListItem;
Expand Down
21 changes: 16 additions & 5 deletions LibraryCPP/stack.cpp
@@ -1,35 +1,46 @@
#include <stdlib.h>
#include "stack.h"
#include "list.h"
#include <iostream>

struct Stack
{
List* list;
};

Stack *stack_create()
{
return new Stack;
Stack* stack = new Stack;
stack->list = list_create();
return stack;
}

void stack_delete(Stack *stack)
{
// TODO: free stack elements
delete stack;
list_delete(stack->list);
delete stack;
}

void stack_push(Stack *stack, Data data)
{
list_insert(stack->list, data);
}

Data stack_get(const Stack *stack)
{
return (Data)0;
return list_item_data(list_first(stack->list));
}

void stack_pop(Stack *stack)
{
if (list_first(stack->list) != NULL)
list_erase(stack->list, list_first(stack->list));
}

bool stack_empty(const Stack *stack)
{
return true;
if (list_first(stack->list) == NULL)
return true;
else
return false;
}
2 changes: 1 addition & 1 deletion LibraryCPP/stack.h
Expand Up @@ -3,7 +3,7 @@

// Stack
// Stores integer values inside
typedef int Data;
typedef char Data;

struct Stack;

Expand Down
3 changes: 3 additions & 0 deletions Task/CMakeLists.txt
@@ -0,0 +1,3 @@
add_executable(Task main.cpp)
target_include_directories(Task PUBLIC ../LibraryCPP)
target_link_libraries(Task LibraryCPP)
73 changes: 73 additions & 0 deletions Task/main.cpp
@@ -0,0 +1,73 @@
#include <iostream>
#include "stack.h"

void buildBackPolishExpression(const std::string& text)
{
Stack* stack = stack_create();

for (int i = 0; i < text.size(); i++)
{
if (text[i] != '*' && text[i] != '/' && text[i] != '+' && text[i] != '-')
{
std::cout << text[i];
}
else
{
if (stack_empty(stack))
{
stack_push(stack, text[i]);
}
else
{
if (stack_get(stack) == '*' || stack_get(stack) == '/')
{
std::cout << stack_get(stack);
stack_pop(stack);
if (!stack_empty(stack) && (text[i] == '+' || text[i] == '-'))
{
std::cout << stack_get(stack);
stack_pop(stack);
stack_push(stack, text[i]);
}
else
{
stack_push(stack, text[i]);
}
}
else
{
if (text[i] == '*' || text[i] == '/')
{
stack_push(stack, text[i]);
}
else
{
std::cout << stack_get(stack);
stack_pop(stack);
stack_push(stack, text[i]);
}
}
}
}
}

while (!stack_empty(stack))
{
std::cout << stack_get(stack);
stack_pop(stack);
}

stack_delete(stack);
}

int main()
{
std::string text;

std::cout << "Enter an expression: ";
std::cin >> text;

buildBackPolishExpression(text);

return 0;
}
3 changes: 3 additions & 0 deletions TestListCPP/CMakeLists.txt
@@ -0,0 +1,3 @@
add_executable(TestListCPP main.cpp)
target_include_directories(TestListCPP PUBLIC ../LibraryCPP)
target_link_libraries(TestListCPP LibraryCPP)
36 changes: 36 additions & 0 deletions TestListCPP/main.cpp
@@ -0,0 +1,36 @@
#include <iostream>
#include "list.h"

int main()
{
List *list = list_create();

if (!list)
std::cout << "List creation error\n";

list_insert(list, 1);
list_insert(list, 2);
list_insert(list, 3);

if (list_item_data(list_first(list)) != 3)
std::cout << "list_insert error\n";

list_insert_after(list, list_first(list), 4);

if (list_item_data(list_item_next(list_first(list))) != 4)
std::cout << "list_insert_after error\n";

list_erase(list, list_first(list));

if (list_item_data(list_first(list)) != 4)
std::cout << "list_erase error\n";

std::cout << "List: ";
for (ListItem *item = list_first(list) ; item ; item = list_item_next(item))
{
std::cout << list_item_data(item) << " ";
}
std::cout << "\n";

list_delete(list);
}
3 changes: 3 additions & 0 deletions TestStackCPP/CMakeLists.txt
@@ -0,0 +1,3 @@
add_executable(TestStackCPP main.cpp)
target_include_directories(TestStackCPP PUBLIC ../LibraryCPP)
target_link_libraries(TestStackCPP LibraryCPP)
41 changes: 41 additions & 0 deletions TestStackCPP/main.cpp
@@ -0,0 +1,41 @@
#include <iostream>
#include "stack.h"

int main()
{
Stack *stack = stack_create();

stack_push(stack, 1);
stack_push(stack, 2);
stack_push(stack, 3);

if (stack_get(stack) != 3)
std::cout << "Invalid stack top after push\n";

std::cout << "Get: " << stack_get(stack) << "\n";
stack_pop(stack);

if (stack_get(stack) != 2)
std::cout << "Invalid stack top after pop\n";

std::cout << "Get: " << stack_get(stack) << "\n";
stack_pop(stack);

if (stack_get(stack) != 1)
std::cout << "Invalid stack top after pop\n";

std::cout << "Get: " << stack_get(stack) << "\n";
stack_push(stack, 4);
stack_push(stack, 5);

if (stack_get(stack) != 5)
std::cout << "Invalid stack top after push\n";

while (!stack_empty(stack))
{
std::cout << "Get: " << stack_get(stack) << "\n";
stack_pop(stack);
}

stack_delete(stack);
}