Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- struct t_List
- {
- double Data;
- t_List * Pred, *Next;
- };
- t_List * CreateList(int count)
- {
- t_List *Curr = 0, *Next = 0;
- for (unsigned i = 1; i <= count; ++i)
- {
- Curr = new t_List;
- Curr->Next = Next;
- if (Next)
- Next->Pred = Curr;
- Next = Curr;
- }
- return Curr;
- }
- void Print(t_List *Curr)
- {
- t_List *Dop;
- while (Curr)
- {
- cout << Curr->Data << "\t";
- Dop = Curr->Next; //Переход к следующему эл-ту
- Curr = Dop;
- }
- cout << endl;
- }
- void Print_1(t_List *Curr) {
- if (Curr)
- {
- cout << Curr->Data << "\t";
- Print_1(Curr->Next); //переход к следующему элементу
- }
- else cout << "\n";
- }
- void DelList(t_List *&Beg) //удаление списка
- {
- t_List *Buf;
- while (Beg) //Beg & Buf - NANI?
- {
- Buf = Beg->Next;
- delete Beg;
- Beg = Buf;
- }
- }
- t_List *LItem(t_List *Beg, int Ind) //доступ к любому эл-ту списка по его индексу
- {
- while (Beg && Ind--)
- Beg = Beg->Next;
- return Beg;
- }
- t_List *Insert(t_List *Curr, int Ind, int DataItem) //вставка эл-та с заданным индексом
- {
- --Ind;
- t_List *NewItem = new (t_List);
- NewItem->Data = DataItem;
- NewItem->Next = 0;
- if (!Curr) //список пуст
- Curr = NewItem; //создаем первый элемент списка
- else
- {
- for (int i = 1; i < Ind && Curr->Next != NULL; ++i)
- Curr = Curr->Next;
- if (!Ind) //вставляем новый элемент на первое место
- {
- NewItem->Next = Curr;
- Curr = NewItem;
- }
- else {//вставляем новый элемент на непервое место
- if (Curr->Next != NULL)
- NewItem->Next = Curr->Next;
- Curr->Next = NewItem;
- }
- }
- return Curr;
- }
- unsigned LengthList(t_List * Beg)
- {
- unsigned Length = 0;
- while (Beg)
- {
- ++Length;
- Beg = Beg->Next;
- }
- return Length;
- }
- void DelItem(t_List * &Curr, unsigned Index)
- {
- if (Index >= LengthList(Curr))
- return;
- t_List * Item;
- if (!Index)
- {
- Item = Curr->Next;
- delete Curr;
- Curr = Item;
- Curr->Pred = 0;
- return;
- }
- Item = LItem(Curr, Index - 1);
- t_List * DItem = Item->Next;
- Item->Next = DItem->Next;
- Item->Next->Pred = Item;
- delete DItem;
- }
- void FillArray (int* const arr, const int size)
- {
- for (int i = 0; i < size; i++)
- {
- arr[i] = rand() % 10;
- }
- }
- void ShowArray(const int* const arr, const int size)
- {
- for (int i = 0; i < size; i++)
- {
- cout << arr[i] << "\t";
- }
- cout << endl;
- }
- void push_back(int *&arr, int &size,int value)
- {
- int *newArray = new int[size + 1];
- for (int i = 0; i < size; i++)
- {
- newArray[i] = arr[i];
- }
- newArray[size] = value;
- size++;
- delete[] arr;
- arr = newArray;
- }
- void pop_back(int *&arr, int &size)
- {
- size--;
- int *newArray = new int[size];
- for (int i = 0; i < size; i++)
- {
- newArray[i] = arr[i];
- }
- delete[] arr;
- arr = newArray;
- }
- int main()
- {
- setlocale(0, "");
- int n, size, k;
- cout << "Введите размерность:" << endl;
- cin >> size;
- int *arr = new int[size];
- FillArray(arr, size);
- cout << "Ващ массив:" << endl;
- ShowArray(arr, size);
- t_List *List = CreateList(size);
- t_List * El = List;
- while (El)
- {
- El->Data = rand() % 100;
- El = El->Next;
- }
- El = List;
- cout << "Ващ список:" << endl;
- Print(List);
- cout << "добавить элемент:" << endl;
- cin >> n;
- push_back(arr, size, n);
- cout << "Ващ массив:" << endl;
- ShowArray(arr, size);
- Insert(List, 4, n);
- cout << "Ващ список:" << endl;
- Print_1(List);
- cout << "удалили элемент:" << endl;
- pop_back(arr, size);
- cout << "Ващ массив:" << endl;
- ShowArray(arr, size);
- DelItem(List, 4);
- cout << "Ващ список:" << endl;
- Print_1(List);
- cout << "значение какого элемента вы хотите получить?:" << endl;
- cin >> k;
- El = LItem(List, k);
- cout << El << endl;
- cout << arr[k-1]<<endl;
- delete[] arr;
- DelList(List);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement