Skip to content

Instantly share code, notes, and snippets.

@daniilgri
Last active February 26, 2019 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniilgri/46c1b278646b5f6286d49cec9f3a59a6 to your computer and use it in GitHub Desktop.
Save daniilgri/46c1b278646b5f6286d49cec9f3a59a6 to your computer and use it in GitHub Desktop.
New version!!!
#include <string>
#include "DinLst.h"
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
List* ReadFromFile(string fName, string lName) {
ifstream fin(fName);
List* list = NULL;
if (!fin.is_open()) {
cout << "Ошибка открытия файла" << endl;
}
else {
cout << "Файл открыт" << endl;
string str;
int i = 0;
while (!fin.eof()) {
str = "";
getline(fin, str);
if (i == 0) {
list = CreateList(lName, str);
}
else {
AddHead(list, str);
}
++i;
}
list->size = i;
fin.close();
}
return list;
}
Itm* CrItm(int Id, string Inf)
{
Itm *t = new Itm;
t->next = t->prev = NULL;
t->id = Id;
t->Info = Inf;
return t;
}
List * CreateList(string Name, string Inf) {
List *lst = new List;
lst->Name = Name;
lst->getId = 0;
lst->bg = CrItm(lst->getId, Inf);
lst->end = lst->bg;
return lst;
}
Itm* AddHead(List* lst, string Inf) {
lst->cur = CrItm(++lst->getId, Inf);
lst->bg->prev = lst->cur;
lst->cur->next = lst->bg;
lst->bg = lst->cur;
lst->size++;
return lst->bg;
}
void ViewLst(List* lst) {
cout << " " << lst->Name << " " << endl;
lst->cur = lst->bg;
while (lst->cur) {
cout << lst->cur->id << " " << lst->cur->Info << " " << endl;
lst->cur = lst->cur->next;
}
}
void DelHead(List* lst) {
if (lst->bg == lst->end)
DelLst(lst);
else
{
lst->cur = lst->bg;
lst->bg = lst->bg->next;
lst->bg->prev = NULL;
delete lst->cur;
lst->size--;
}
}
void DelElem(List* lst, int id) {
int bgid = lst->bg->id;
if(bgid == id || id == 0 || id < 0 || id >= lst->size) {
cout << "Неправильный id..." << endl;
return;
}
lst->cur = lst->end;
while (lst->cur) {
if (lst->cur->id == id) {
lst->cur->next->prev = lst->cur->prev;
lst->cur = lst->cur->prev;
}
lst->cur = lst->cur->prev;
}
DelHead(lst);
lst->size--;
}
void DelLst(List* lst) {
while (lst->bg != lst->end) DelHead(lst);
delete lst->bg;
delete lst;
}
void ViewLstBk(List* lst) {
cout << " " << lst->Name << " " << endl;
lst->cur = lst->end;
while (lst->cur) {
cout << lst->cur->id << " " << lst->cur->Info << " " << endl;
lst->cur = lst->cur->prev;
}
}
//---------------------------------------------------------------------------
#ifndef DinLstH
#define DinLstH
//---------------------------------------------------------------------------
#endif
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <string>
#include <fstream>
#define Max_ch 100
struct Itm {
int id;
std::string Info;
Itm *next, *prev;
};
struct List {
std::string Name;
int getId;
Itm *bg, *cur, *end;
int size;
};
List * CreateList(std::string Name, std::string Inf);
Itm* AddHead(List* lst, std::string Inf);
Itm *CrItm(int Id);
void ViewLst(List* lst);
void DelHead(List* lst);
void DelLst(List* lst);
void ViewLstBk(List* lst);
List* ReadFromFile(std::string fName, std::string lName);
void DelElem(List* lst, int id);
#include <iostream>
#include "DinLst.h"
#include <windows.h>
#include <fstream>
#include <string>
int main()
{
SetConsoleOutputCP(1251);
SetConsoleCP(1251);
std::string path = "C:\\Users\\sparta\\source\\repos\\ConsoleApplication1\\ConsoleApplication1\\mocks.txt";
List* myList = ReadFromFile(path, "Data");
ViewLstBk(myList);
DelElem(myList, 0);
ViewLstBk(myList);
DelLst(myList);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment