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
114 changes: 114 additions & 0 deletions ViktorovaHW5_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include <iostream>

const int n = 3;
struct Stack
{
int A[n];
int count{};
};

void Creation(Stack* p)
{
p->count = 0;
}

int Full(Stack* p)
{
if (p->count == 0) {
return 1;
}
else if (p->count == n) {
return -1;
}
else {
return 0;
}
}

void Add(Stack* p)
{
int value{};
std::cout << "Please enter your element ";
std::cin >> value;
p->A[p->count] = value;
p->count++;
}

void Delete(Stack* p)
{
p->count--;
}

int Top(Stack* p)
{
return p->A[p->count - 1];
}

int Size(Stack* p)
{
return p->count;
}

int main()
{
Stack s;
Creation(&s);
char number;
while(true)
{
std::cout << "1. Add the element" << std::endl;
std::cout << "2. Delete the element" <<std:: endl;
std::cout << "3. Top element" << std::endl;
std::cout << "4. Size of the Stack" <<std:: endl;
std::cout << "0. Exit" << std::endl;
std::cout << "Number of command ";
std:: cin >> number;
switch (number)
{
case '1':
if (Full(&s) == -1) {
std::cout << std::endl << "Stack is full\n";
}
else
{
Add(&s);
std::cout << std::endl << "The element was added to the stack\n";
} break;

case '2':
if (Full(&s) == 1) {
std::cout << std::endl << "Stack is empty\n";
}
else
{
Delete(&s);
std::cout << std::endl << "The element was deleted from the Stack\n";
} break;

case '3':
if (Full(&s) == 1) {
std::cout <<std:: endl << "Stack is empty\n";
}
else {
std::cout << "\nTop element: " << Top(&s) << "\n\n";
}
break;

case '4':
if (Full(&s) == 1) {
std::cout << std::endl << "Stack is empty\n";
}
else {
std::cout << "\nSize of the Stack: " << Size(&s) << "\n\n";
}
break;

case '0': break;
default:
std::cout << std::endl << "Command is not defined\n";
break;
}
}

return 0;
}
103 changes: 103 additions & 0 deletions ViktorovaHW5_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#include <iostream>

struct DoubleList
{
int data{};
DoubleList* next;
DoubleList* prev;
};
DoubleList* head;

void AddList(int value, int position)
{
DoubleList* node = new DoubleList;
node->data = value;
if (head == NULL)
{
node->next = node;
node->prev = node;
head = node;
}
else
{
DoubleList* p = head;
for (int i = position; i > 1; i--) p = p->next;
p->prev->next = node;
node->prev = p->prev;
node->next = p;
p->prev = node;
}
std::cout << "\nThe element was added\n";
}

int DeleteList(int position)
{
if (head == NULL) {
std::cout << "\nList is empty\n"; return 0;
}
if (head == head->next)
{
delete head;
head = NULL;
}
else
{
DoubleList* a = head;
for (int i = position; i > 1; i--) a = a->next;
if (a == head) head = a->next;
a->prev->next = a->next;
a->next->prev = a->prev;
delete a;
}
std::cout << "\nThe element was deleted\n";
}

void PrintList()
{
if (head == NULL) {
std::cout << "\nList is empty\n";
}
else
{
DoubleList* a = head;
std::cout << "\nList elements:";
do
{
std::cout << a->data << " ";
a = a->next;
} while (a != head);
std::cout << "\n\n";
}
}

int main()
{
int value, position, number;
while(true)
{
std::cout << "1. Add the element" <<std:: endl;
std::cout << "2. Delete the element" << std::endl;
std::cout << "3. Display list" <<std:: endl;
std::cout << "0. Exit" <<std:: endl;
std::cout << "\nCommand number";
std::cin >> number;
switch (number)
{
case 1:
std::cout << "Value = > ";
std::cin >> value;
std::cout << "Position = > ";
std::cin >> position;
AddList(value, position);
break;
case 2:
std::cout << "Position = ";
std::cin >> position;
DeleteList(position);
break;
case 3: PrintList();
break;
}
}
return 0;
}
93 changes: 93 additions & 0 deletions ViktorovaHW5_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <iostream>

const int N = 4;
struct Queue {
int data[N];
int last;
};
void Creation(Queue* Q) { Q->last = 0; }
bool Full(Queue* Q) {
if (Q->last == 0) {
return true;
}
else {
return false;
}
}
void Add(Queue* Q) {
if (Q->last == N) {
std::cout << std::endl << "Queue is full" << std::endl;
return;
}
int value;
std::cout << std::endl << "Element value is -->";
std::cin >> value;
Q->data[Q->last++] = value;
std::cout << std::endl << "Element was added to queue" << std::endl;
}
void Delete(Queue* Q) {
for (int i = 0; i < Q->last && i < N; i++) {
Q->data[i] = Q->data[i + 1];
Q->last--;
}
}
int Top(Queue* Q) { return Q->data[0]; }
int Size(Queue* Q) { return Q->last; }

int main() {
Queue Q;
Creation(&Q);
char number{};
while (true) {
std::cout << "1. Add the element" << std::endl;
std::cout << "2. Delete the element" << std::endl;
std::cout << "3. Display top element" << std::endl;
std::cout << "4. Find out the queue size" << std::endl;
std::cout << "0. Exit\n";
std::cout << " Command number ";

std::cin >> number;
switch (number) {
case '1':
Add(&Q);
break;

case '2':
if (Full(&Q)) {
std::cout << std::endl << "Queue is empty" << std::endl;
}
else {

Delete(&Q);
std::cout << std::endl
<< "Element was deleted from the queue" << std::endl;
}
break;

case '3':
if (Full(&Q)) {
std::cout << std::endl << "Queue is empty" << std::endl;
}
else {
std::cout << std::endl << "Top element " << Top(&Q) << std::endl;
}
break;

case '4':
if (Full(&Q)) {
std::cout << std::endl << "Queue is empty" << std::endl;
}
else {
std::cout << std::endl << "Queue size is: " << Size(&Q) << std::endl;
}
break;

case '0':
break;
default:
std::cout << std::endl << "Command isn`t defined";
break;
}
}
return 0;
}