Added my works from 2022
This commit is contained in:
114
task8.h
Normal file
114
task8.h
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
#pragma once
|
||||
|
||||
namespace task8 {
|
||||
|
||||
struct Node
|
||||
{
|
||||
public:
|
||||
int key;
|
||||
Node* next;
|
||||
};
|
||||
|
||||
Node* newNode(int key); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>
|
||||
Node* rand_list(int size); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void print_list(Node* head); //<2F><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void cycle(Node*&head, int offset); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
int init()
|
||||
{
|
||||
int n; cout << "Enter size: "; cin >> n;
|
||||
Node* lst = rand_list(n);
|
||||
print_list(lst);
|
||||
int offset; cout << "Enter offset: "; cin >> offset;
|
||||
|
||||
cycle(lst, offset);
|
||||
print_list(lst);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Node* newNode(int key)
|
||||
{
|
||||
Node* node = new Node;
|
||||
node->key = key;
|
||||
node->next = nullptr;
|
||||
return node;
|
||||
}
|
||||
|
||||
Node* rand_list(int size)
|
||||
{
|
||||
Node* head = newNode(rand());
|
||||
Node* last = newNode(rand());
|
||||
head->next = last;
|
||||
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
Node* node = newNode(i+1);
|
||||
|
||||
last->next = node;
|
||||
last = node;
|
||||
}
|
||||
|
||||
return head->next->next;
|
||||
}
|
||||
|
||||
void push_back(Node* head, int key)
|
||||
{
|
||||
Node* last = nullptr;
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
last = ptr;
|
||||
ptr = ptr->next;
|
||||
}
|
||||
ptr = newNode(key);
|
||||
last->next = ptr;
|
||||
}
|
||||
|
||||
Node* push_forward(Node* head, int key)
|
||||
{
|
||||
Node* node = newNode(key);
|
||||
node->next = head;
|
||||
return node;
|
||||
}
|
||||
|
||||
void print_list(Node* head)
|
||||
{
|
||||
Node* ptr = head;
|
||||
while (ptr)
|
||||
{
|
||||
cout << ptr->key << " -> ";
|
||||
ptr = ptr->next;
|
||||
}
|
||||
cout << "nullptr" << endl;
|
||||
}
|
||||
|
||||
void cycle(Node*& head, int offset)
|
||||
{
|
||||
int amt = 0;
|
||||
Node* ptr = head;
|
||||
while (ptr->next)
|
||||
{
|
||||
ptr = ptr->next;
|
||||
amt++;
|
||||
}
|
||||
ptr->next = head;
|
||||
Node* prv = ptr;
|
||||
|
||||
if (offset < 0)
|
||||
{
|
||||
offset = abs(amt+1 + offset);
|
||||
}
|
||||
|
||||
for (int i = 0; i < offset; i++)
|
||||
{
|
||||
prv = prv->next;
|
||||
head = head->next;
|
||||
}
|
||||
prv->next = nullptr;
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user