Skip to content

Instantly share code, notes, and snippets.

@daniilgri
Created June 23, 2019 12:23
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/e2d7ebe2bb742aa40209953181472f41 to your computer and use it in GitHub Desktop.
Save daniilgri/e2d7ebe2bb742aa40209953181472f41 to your computer and use it in GitHub Desktop.
Lab 1 DinLST
#include "DinLst.h"
#include <string>
#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;
}
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;
}
void Past(List* lst, int id, string str) {
lst->cur = lst->end;
while (lst->cur) {
if (lst->cur->id == id) {
Itm* t = CrItm(id+1, str);
t->next = lst->cur;
t->prev = lst->cur->prev;
lst->cur->prev = t;
t->prev->next = t;
}
else if (lst->cur->id == id+1) {
lst->cur->id = id + 2;
}
lst->cur = lst->cur->prev;
}
}
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;
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;
}
}
void DelElem(List* lst, int id) {
int bgid = lst->bg->id;
if (bgid == id || id == 0 || id < 0) {
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->prev->next = lst->cur->next;
lst->cur = lst->cur->prev;
}
lst->cur = lst->cur->prev;
}
}
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;
}
}
#pragma once
//---------------------------------------------------------------------------
#ifndef DinLstH
#define DinLstH
//---------------------------------------------------------------------------
#endif
#include <stdio.h>
#include <conio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <string>
#include <fstream>
struct Itm {
int id;
std::string Info;
Itm *next, *prev;
};
struct List {
std::string Name;
int getId;
Itm *bg, *cur, *end;
};
List * CreateList(std::string Name, std::string Inf);
Itm* AddHead(List* lst, std::string Inf);
Itm *CrItm(int Id, std::string Inf);
void ViewLst(List* lst);
void DelHead(List* lst);
void DelLst(List* lst);
void Past(List* lst, int id, std::string str);
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\\lab1\\lab1\\data.txt";
List* myList = ReadFromFile(path, "Data");
ViewLstBk(myList);
Past(myList, 1, "MFC");
ViewLst(myList);
DelLst(myList);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment