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

lab3 Николаев Лев #1198

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
10 changes: 6 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ else()
add_compile_options(-Wall -Wextra -Wpedantic -Wno-gnu-empty-struct -Wno-unused-parameter)
endif()

add_subdirectory(LibraryC)
#add_subdirectory(LibraryC)
add_subdirectory(LibraryCPP)
add_subdirectory(LibraryCPPClass)
add_subdirectory(LibraryCPPTemplate)
#add_subdirectory(LibraryCPPClass)
#add_subdirectory(LibraryCPPTemplate)

add_subdirectory(Lab1C)
#add_subdirectory(Lab1C)

add_subdirectory(lab3)
2 changes: 1 addition & 1 deletion LibraryCPP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
add_library(LibraryCPP STATIC array.cpp list.cpp queue.cpp stack.cpp vector.cpp)
add_library(LibraryCPP STATIC list.cpp queue.cpp vector.cpp)

add_subdirectory(Tests)
28 changes: 14 additions & 14 deletions LibraryCPP/Tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
# target_link_libraries(TestArrayCPP LibraryCPP)
# add_test(TestArrayCPP TestArrayCPP)

# add_executable(TestListCPP list.cpp)
# target_include_directories(TestListCPP PUBLIC ..)
# target_link_libraries(TestListCPP LibraryCPP)
# add_test(TestListCPP TestListCPP)
add_executable(TestListCPP list.cpp)
target_include_directories(TestListCPP PUBLIC ..)
target_link_libraries(TestListCPP LibraryCPP)
add_test(TestListCPP TestListCPP)

# add_executable(TestQueueCPP queue.cpp)
# target_include_directories(TestQueueCPP PUBLIC ..)
# target_link_libraries(TestQueueCPP LibraryCPP)
# add_test(TestQueueCPP TestQueueCPP)
# set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)
add_executable(TestQueueCPP queue.cpp)
target_include_directories(TestQueueCPP PUBLIC ..)
target_link_libraries(TestQueueCPP LibraryCPP)
add_test(TestQueueCPP TestQueueCPP)
set_tests_properties(TestQueueCPP PROPERTIES TIMEOUT 10)

# add_executable(TestStackCPP stack.cpp)
# target_include_directories(TestStackCPP PUBLIC ..)
# target_link_libraries(TestStackCPP LibraryCPP)
# add_test(TestStackCPP TestStackCPP)

# add_executable(TestVectorCPP vector.cpp)
# target_include_directories(TestVectorCPP PUBLIC ..)
# target_link_libraries(TestVectorCPP LibraryCPP)
# add_test(TestVectorCPP TestVectorCPP)
# set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
add_executable(TestVectorCPP vector.cpp)
target_include_directories(TestVectorCPP PUBLIC ..)
target_link_libraries(TestVectorCPP LibraryCPP)
add_test(TestVectorCPP TestVectorCPP)
set_tests_properties(TestVectorCPP PROPERTIES TIMEOUT 10)
4 changes: 2 additions & 2 deletions LibraryCPP/Tests/vector.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <iostream>

Check notice on line 1 in LibraryCPP/Tests/vector.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/Tests/vector.cpp

File LibraryCPP/Tests/vector.cpp does not conform to Custom style guidelines. (lines 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 15, 16, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 55, 56, 57, 58, 60, 61, 62, 63, 64, 65, 67, 68, 69, 71)
#include "vector.h"

int main()
Expand All @@ -12,8 +12,8 @@
return 1;
}

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
vector_set(vector, i, i);
for (size_t i = 0; i < vector_size(vector); ++i)
vector_set(vector, i, static_cast<Data>(i));

for (size_t i = 0 ; i < vector_size(vector) ; ++i)
{
Expand Down
98 changes: 56 additions & 42 deletions LibraryCPP/list.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
#include <cstddef>
#include "list.h"

Check notice on line 1 in LibraryCPP/list.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/list.cpp

File LibraryCPP/list.cpp does not conform to Custom style guidelines. (lines 5, 6, 7, 8, 12, 13, 14, 15, 16, 17, 18, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 35, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 50, 51, 53, 54, 56, 57, 58, 59, 60, 61, 65, 66, 69, 70, 73)
#include <cstddef> // äëÿ NULL
#include <cstdlib> // äëÿ malloc/free

struct ListItem
{
};

struct List
{
};

List *list_create()
{
return new List;
List* list_create() {
List* list = new List;
list->head = nullptr;
list->tail = nullptr;
return list;
}

void list_delete(List *list)
{
// TODO: free items
void list_delete(List* list) {
ListItem* current = list->head;
while (current) {
ListItem* next = current->next;
delete current;
current = next;
}
delete list;
}

ListItem *list_first(List *list)
{
return NULL;
void list_insert(List* list, int data) {
ListItem* new_item = new ListItem{ data, nullptr, nullptr };
if (!list->head) {
list->head = new_item;
list->tail = new_item;
}
else {
new_item->next = list->head;
list->head->prev = new_item;
list->head = new_item;
}
}

Data list_item_data(const ListItem *item)
{
return (Data)0;
}
void list_insert_after(List* list, ListItem* item, int data) {
if (!item) return;
ListItem* new_item = new ListItem{ data, nullptr, nullptr };
new_item->next = item->next;
new_item->prev = item;

ListItem *list_item_next(ListItem *item)
{
return NULL;
if (item->next) {
item->next->prev = new_item;
}
else {
list->tail = new_item;
}
item->next = new_item;
}

ListItem *list_item_prev(ListItem *item)
{
return NULL;
}
void list_erase_first(List* list) {
if (!list->head) return;

ListItem *list_insert(List *list, Data data)
{
return NULL;
}
ListItem* to_delete = list->head;
list->head = list->head->next;

ListItem *list_insert_after(List *list, ListItem *item, Data data)
{
return NULL;
if (list->head) {
list->head->prev = nullptr;
}
else {
list->tail = nullptr;
}
delete to_delete;
}

ListItem *list_erase_first(List *list)
{
return NULL;
ListItem* list_first(const List* list) {
return list->head;
}

ListItem *list_erase_next(List *list, ListItem *item)
{
return NULL;
int list_item_data(const ListItem* item) {
return item ? item->data : 0;
}

ListItem* list_item_next(const ListItem* item) {
return item ? item->next : nullptr;
}
58 changes: 26 additions & 32 deletions LibraryCPP/list.h
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
#ifndef LIST_H

Check notice on line 1 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/list.h

File LibraryCPP/list.h does not conform to Custom style guidelines. (lines 4, 5, 6, 7, 10, 11, 12, 16, 19, 22, 25, 28, 31, 34, 37)
#define LIST_H

// List
// Stores integer values inside
typedef int Data;
struct ListItem {
int data;
ListItem* next;

Check failure on line 6 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:6:5 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'ListItem'
ListItem* prev;

Check failure on line 7 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:7:5 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'ListItem'
};

struct List;
struct ListItem;
struct List {
ListItem* head;

Check failure on line 11 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:11:5 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'ListItem'
ListItem* tail;

Check failure on line 12 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:12:5 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'ListItem'
};

// Creates new list
List *list_create();
// Ñîçäàåò íîâûé ñïèñîê
List* list_create();

Check failure on line 16 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:16:1 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'List'

// Destroys the list and frees the memory
void list_delete(List *list);
// Óäàëÿåò ñïèñîê
void list_delete(List* list);

Check failure on line 19 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:19:18 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'List'

// Retrieves the first item from the list
ListItem *list_first(List *list);
// Âñòàâëÿåò ýëåìåíò â íà÷àëî ñïèñêà
void list_insert(List* list, int data);

Check failure on line 22 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:22:18 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'List'

// Extracts data from the list item
Data list_item_data(const ListItem *item);
// Âñòàâëÿåò ýëåìåíò ïîñëå óêàçàííîãî óçëà
void list_insert_after(List* list, ListItem* item, int data);

Check failure on line 25 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:25:24 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'List'

Check failure on line 25 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:25:36 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'ListItem'

// Returns list item following after the specified one
ListItem *list_item_next(ListItem *item);
// Óäàëÿåò ïåðâûé ýëåìåíò ñïèñêà
void list_erase_first(List* list);

Check failure on line 28 in LibraryCPP/list.h

View workflow job for this annotation

GitHub Actions / cpp-linter

LibraryCPP/list.h:28:23 [clang-diagnostic-error]

must use 'struct' tag to refer to type 'List'

// Returns previous element for the specified item.
// Not applicable for the singly linked lists.
ListItem *list_item_prev(ListItem *item);
// Âîçâðàùàåò ïåðâûé ýëåìåíò ñïèñêà
ListItem* list_first(const List* list);

// Inserts new list item into the beginning
ListItem *list_insert(List *list, Data data);
// Âîçâðàùàåò äàííûå èç ýëåìåíòà ñïèñêà
int list_item_data(const ListItem* item);

// Inserts new list item after the specified item
ListItem *list_insert_after(List *list, ListItem *item, Data data);
// Âîçâðàùàåò ñëåäóþùèé ýëåìåíò ñïèñêà
ListItem* list_item_next(const ListItem* item);

// Deletes the first list item.
// Returns pointer to the item next to the deleted one.
ListItem *list_erase_first(List *list);

// Deletes the list item following the specified one.
// Returns pointer to the item next to the deleted one.
// Should be O(1)
ListItem *list_erase_next(List *list, ListItem *item);

#endif
#endif // LIST_H
52 changes: 31 additions & 21 deletions LibraryCPP/queue.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
#include "queue.h"

Check notice on line 1 in LibraryCPP/queue.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/queue.cpp

File LibraryCPP/queue.cpp does not conform to Custom style guidelines. (lines 4, 5, 6, 10, 11, 15, 16, 18, 19, 20, 21, 22, 23, 24, 28, 29, 31, 35, 36, 38, 42)
#include <cstddef> // äëÿ NULL

struct Queue
{
};

Queue *queue_create()
{
return new Queue;
Queue* queue_create() {
Queue* queue = new Queue;
queue->list = list_create();
return queue;
}

void queue_delete(Queue *queue)
{
// TODO: free queue items
void queue_delete(Queue* queue) {
list_delete(queue->list);
delete queue;
}

void queue_insert(Queue *queue, Data data)
{
}
void queue_insert(Queue* queue, int data) {
if (!queue->list) return;

Data queue_get(const Queue *queue)
{
return (Data)0;
// Äîáàâëÿåì ýëåìåíò â êîíåö î÷åðåäè
if (list_first(queue->list) == nullptr) {
list_insert(queue->list, data); // Åñëè î÷åðåäü ïóñòà, äîáàâëÿåì â íà÷àëî
}
else {
ListItem* tail = queue->list->tail;
list_insert_after(queue->list, tail, data);
}
}

void queue_remove(Queue *queue)
{
int queue_get(const Queue* queue) {
if (queue_empty(queue)) return 0;

// Âîçâðàùàåì äàííûå ïåðâîãî ýëåìåíòà
return list_item_data(list_first(queue->list));
}

bool queue_empty(const Queue *queue)
{
return true;
void queue_remove(Queue* queue) {
if (!queue->list || queue_empty(queue)) return;

// Óäàëÿåì ïåðâûé ýëåìåíò ñïèñêà
list_erase_first(queue->list);
}

bool queue_empty(const Queue* queue) {
return list_first(queue->list) == nullptr;
}
36 changes: 17 additions & 19 deletions LibraryCPP/queue.h
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
#ifndef QUEUE_H

Check notice on line 1 in LibraryCPP/queue.h

View workflow job for this annotation

GitHub Actions / cpp-linter

Run clang-format on LibraryCPP/queue.h

File LibraryCPP/queue.h does not conform to Custom style guidelines. (lines 6, 7, 11, 14, 17, 20, 23, 26)
#define QUEUE_H

// Queue
// Stores integer values inside
typedef int Data;
#include "list.h"

struct Queue;
struct Queue {
List* list;
};

// Create empty queue
Queue *queue_create();
// Ñîçäàåò íîâóþ î÷åðåäü
Queue* queue_create();

// Deletes queue
void queue_delete(Queue *queue);
// Óäàëÿåò î÷åðåäü
void queue_delete(Queue* queue);

// Includes new element into the queue
// Should be O(1) on average
void queue_insert(Queue *queue, Data data);
// Äîáàâëÿåò ýëåìåíò â êîíåö î÷åðåäè
void queue_insert(Queue* queue, int data);

// Retrieves first element from the queue
Data queue_get(const Queue *queue);
// Âîçâðàùàåò ïåðâûé ýëåìåíò î÷åðåäè
int queue_get(const Queue* queue);

// Removes first element from the queue
// Should be O(1) on average
void queue_remove(Queue *queue);
// Óäàëÿåò ïåðâûé ýëåìåíò î÷åðåäè
void queue_remove(Queue* queue);

// Returns true if the queue is empty
bool queue_empty(const Queue *queue);
// Ïðîâåðÿåò, ïóñòà ëè î÷åðåäü
bool queue_empty(const Queue* queue);

#endif
#endif // QUEUE_H
Loading
Loading