Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct item {
- int data;
- item *next, *prev;
- };
- item *head, *tail, *cur;
- void create() {
- int n;
- cout << "Введите кол-во элементов: ";
- cin >> n;
- if (!n)return;
- //{cur->next = new item; cur = cur->next; cin >> cur->data; cur->prev = cur; cur->next = NULL;
- // Create NEW Node
- head = new item;
- head->prev = NULL;
- head->next = NULL;
- cout << "Введите элементы: ";
- cin >> head->data;
- cur = tail = head;
- // Fill
- for (int i = 1; i < n; i++) {
- cur->next = new item;
- cur = cur->next;
- cur->next = NULL;
- tail->next = cur;
- cur->prev = tail;
- tail = cur;
- cin >> cur->data;
- }
- }
- void show() {
- cur = head;
- while (cur) {
- cout << cur->data << " ";
- cur = cur->next;
- } cout << endl;
- }
- void showback() {
- cur = tail;
- while (cur) {
- cout << cur->data << " ";
- cur = cur->prev;
- }
- cout << endl;
- }
- void destroy() {
- while (tail) {
- cur = tail;
- tail = cur->prev;
- delete cur;
- }
- head = tail;
- }
- void quantity()
- {
- cur = tail;
- int quantity = 0;
- while (cur)
- {
- quantity++;
- cur = cur->prev;
- }
- cout << quantity << endl;
- }
- double average() {
- double sr = 0.0, i = 0.0;
- cur = head;
- while (cur) {
- sr += cur->data;
- cur = cur->next;
- i++;
- }
- cout << "\nCписок: "; show();
- sr /= i;
- return sr;
- }
- void adding(int n) {
- item *x = new item;
- x->data = n;
- x->next = NULL;
- x->prev = tail;
- tail->next = x;
- x->prev = tail;
- tail = x;
- cout << "\nИзменённый список: "; show(); cout << endl;
- }
- void adding(int ind, int n) {
- item *NewItem = new(item);
- NewItem->data = n;
- NewItem->prev = NULL;
- NewItem->next = NULL;
- if (head == NULL) {//список пуст
- head = NewItem;
- }
- else {//список не пуст
- item *cur = head;
- for (int i = 1; i < ind && cur->next != NULL; i++)
- cur = cur->next;
- if (ind == 0) {
- //вставляем новый элемент на первое место
- NewItem->next = head;
- head->prev = NewItem;
- head = NewItem;
- }
- else {//вставляем новый элемент на непервое место
- if (cur->next != NULL) cur->next->prev = NewItem;
- NewItem->next = cur->next;
- cur->next = NewItem;
- NewItem->prev = cur;
- cur = NewItem;
- }
- }
- show();
- }
- void Delete(){
- cur = tail;
- tail = cur->prev;
- delete cur;
- tail->next = NULL;
- show();
- }
- void Delete(int ind) {
- item *ptr;//вспомогательный указатель
- item *cur = head;
- for (int i = 1; i < ind && cur != NULL; i++)
- cur = cur->next;
- if (cur != NULL) {//проверка на корректность
- if (cur->prev == NULL) {//удаляем первый элемент
- head = head->next;
- delete(cur);
- head->prev = NULL;
- cur = head;
- }
- else {//удаляем непервый элемент
- if (cur->next == NULL) {
- //удаляем последний элемент
- cur->prev->next = NULL;
- delete(cur);
- cur = head;
- }
- else {//удаляем непервый и непоследний элемент
- ptr = cur->next;
- cur->prev->next = cur->next;
- cur->next->prev = cur->prev;
- delete(cur);
- cur = ptr;
- }
- }
- }
- show();
- }
- int main() {
- int z, m;
- setlocale(LC_ALL, "Russian");
- create();
- cout << "Прямой список: ";
- show();
- cout << "Обратный список: ";
- showback();
- cout << "Количество элементов в списке: ";
- quantity();
- cout << "Среднее арифметическое: " << average() << "\n";
- cout << "Введите элемент для добавления в конец списка: ";
- cin >> z;
- adding(z);
- cout << endl;
- cout << "Введите индекс и элемент для добавления: ";
- cin >> m >> z;
- adding(m, z);
- cout << endl;
- cout << "Список с удаленным последним элементом::";
- Delete();
- cout << "Введите индекс удаляемого элемента: ";
- cin >> m;
- Delete(m);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement