Skip to content

Instantly share code, notes, and snippets.

@vbifonixor vbifonixor/List.h
Created Jun 16, 2016

Embed
What would you like to do?
#include <stdlib.h>
#include <cstdio>
namespace List {
unsigned int LastKey = 0;
struct Node {
unsigned int Key;
int Value;
struct Node *Next, *Prev;
};
typedef struct Node *PNode;
PNode Head = NULL;
PNode CreateNode() {
PNode NewNode = (PNode)malloc(sizeof(struct Node));
printf("Введите значение: ");
std::cin>>NewNode->Value;
NewNode->Key = ++LastKey;
NewNode->Next = NewNode->Prev = NULL;
return NewNode;
}
void PrintFullList() {
PNode TempNode = Head;
if (Head == NULL) {
std::cout << "Список пуст.\n";
return;
}
std::cout<<"Список:\n";
int i = 1;
printf("%8s | %8s | %10s\n", "п/п", "Ключ", "Значение");
while (TempNode != NULL) {
printf("%8d | %8d | %10d\n", i, TempNode->Key, TempNode->Value);
i++;
TempNode = TempNode->Next;
}
printf("В списке %d элементов.\n", i - 1);
}
void AddFirst() {
PNode NewNode = CreateNode();
NewNode->Next = Head;
if (Head != NULL)Head->Prev = NewNode;
Head = NewNode;
std::cout<<"Готово\n";
}
void AddBefore(PNode NewNode, PNode TargetNode) {
TargetNode->Prev->Next = NewNode;
NewNode->Prev = TargetNode->Prev;
TargetNode->Prev = NewNode;
NewNode->Next = TargetNode;
std::cout << "Готово\n";
}
void AddLast() {
PNode NewNode = CreateNode();
PNode TempNode = Head;
if (Head == NULL)Head = NewNode;
else {
while (TempNode->Next) {
TempNode = TempNode->Next;
}
TempNode->Next = NewNode;
NewNode->Prev = TempNode;
}
std::cout << "Готово\n";
}
PNode FindByKey(int Key) {
PNode TargetNode = Head;
while (TargetNode->Next && Key != TargetNode->Key) {
TargetNode = TargetNode->Next;
}
return TargetNode;
}
PNode FindByValue(int Value) {
PNode TargetNode = Head;
while (TargetNode->Next && Value != TargetNode->Value) {
TargetNode = TargetNode->Next;
}
return TargetNode;
}
void DeleteNode(PNode TargetNode) {
if (Head == TargetNode)Head = TargetNode->Next;
else {
PNode TempNode = Head;
while (TempNode && TempNode->Next != TargetNode) {
TempNode = TempNode->Next;
}
TempNode->Next = TargetNode->Next;
TempNode->Prev = TargetNode->Prev;
}
free(TargetNode);
std::cout << "Готово\n";
}
void PopFirst() {
if (Head == NULL) {
std::cout << "Список пуст!\n";
return;
}
PNode TempNode = Head;
if (Head->Next == NULL)Head = NULL;
else {
Head = Head->Next;
free(TempNode);
}
std::cout << "Готово\n";
}
void Pop() {
if (Head == NULL) {
std::cout << "Список пуст!\n";
return;
}
PNode TempNode = Head;
while (TempNode->Next)TempNode = TempNode->Next;
DeleteNode(TempNode);
}
int Count() {
PNode TempNode = Head;
int Counter = 0;
if (Head == NULL)return Counter;
while (TempNode) {
TempNode = TempNode->Next;
Counter++;
}
return Counter;
}
void Destroy() {
PNode TempNode = Head;
if (Head == NULL)return;
while (TempNode->Next) {
TempNode = TempNode->Next;
free(TempNode->Prev);
}
free(TempNode);
}
void Find() {
char dec[4];
PNode Result = NULL;
std::cout << "Искать по значению?(Yes|No) ";
std::cin >> dec;
if (dec, "Yes") {
int Value;
std::cout << "Введите значение: ";
std::cin >> Value;
Result = FindByValue(Value);
if (Result == NULL) {
std::cout << "Не найдено\n";
return;
}
}
else {
int Key;
std::cout << "Введите ключ: ";
std::cin >> Key;
Result = FindByKey(Key);
if (Result == NULL) {
std::cout << "Не найдено\n";
return;
}
}
std::cout << "Результат:\n";
printf("%8s | %8s | %10s\n", "п/п", "Ключ", "Значение");
printf("%8d | %8d | %10d\n", 1, Result->Key, Result->Value);
puts("");
}
void Delete() {
int Key;
std::cout << "Введите ключ: ";
std::cin >> Key;
PNode TargetNode = List::FindByKey(Key);
if (TargetNode == NULL) {
std::cout << "Данный элемент отстутствует в списке!\n";
return;
}
DeleteNode(TargetNode);
}
void Task2() {
int i;
std::cout << "Введите i : ";
std::cin >> i;
if (i <= 1 || i>Count()) {
std::cout << "Вставить элемент перед данным номером нельзя.\n";
return;
}
PNode TempNode = Head;
for (int n = 1; n < i; n++)TempNode = TempNode->Next;
PNode NewNode = CreateNode();
AddBefore(NewNode, TempNode);
}
void Task15() {
int Key;
puts("Введите ключ:");
scanf("%d", &Key);
PNode TargetNode = FindByKey(Key);
if(!TargetNode->Next) {
puts('Ошибка!')
return;
}
PNode NextTarget = TargetNode->Next;
TargetNode->Prev->Next = NextTarget;
NextTarget->Prev = TargetNode->Prev;
TargetNode->Next = NextTarget->Next;
TargetNode->Next->Prev = TargetNode;
TargetNode->Prev = NextTarget;
NextTarget->Next = TargetNode;
if(key == 0) {
Head = NextTarget;
}
}
void Task19() {
if (Head == NULL || Head->Next == NULL)puts("Не хватает элементов для перестановки!");
PNode LastNode = Head;
while (LastNode->Next != NULL)LastNode = LastNode->Next;
LastNode->Prev->Next = Head;
LastNode->Next = Head->Next;
Head->Prev = LastNode->Prev;
Head->Next = NULL;
LastNode->Prev = NULL;
Head = LastNode;
std::cout << "Готово\n";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.