Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- using namespace std;
- typedef struct node* link;
- struct node
- {
- int data;
- link next;
- };
- int sz=0;
- static link head;
- int lulz = 5;
- link NEW(int num, link next);
- void shiftRight(node *a, int size, int pos);
- void rtLeft(int *a, int size, int pos);
- void add(int num); // добавление в начало
- void add(int num, int *array);
- void add_rand(int num, int pos); //добавление элемента на место после pos
- void add_rand(int num, int pos, int *array);
- void searchByArg(int arg);
- void searchByArg(int arg, int *array);
- void replaceArg(int arg, int replaceFor);
- void replaceArg(int arg, int replaceFor, int *array);
- void searchByIndex(int index);
- void searchByIndex(int index, int *array);
- void replaceIndex(int index, int replaceFor);
- void replaceIndex(int index, int replaceFor, int *array);
- void deleteEverything(int var);
- void init();
- void del(int pos);
- void del(int pos, int *array);
- int empty();
- void Obmen(int position, int position1);
- void main()
- {
- setlocale(LC_ALL, "russian");
- init();
- int elem,position,position1;
- int choice = 0;
- cout << "Способ представления списка в памяти:\n1. Указатели\n2. Статический массив\n3. Динамический массив" << endl;
- cin >> choice;
- int n;
- if (choice == 3)
- {
- cout << "Введите размер списка" << endl;
- cin >> lulz;
- }
- int *array = new int[lulz];
- int pos, value;
- while (1<2)
- {
- cout << "\n1.Вывести список на экран\n2. Добавить элемент в начало\n3. Добавить элемент в произвольную позицию\n4. Удалить элемент\n5. Поиск элемента\n 6. Получение элемента по индексу\n 7. Замена старого значения элемента на заданное новое\n 8. Установка значения элемента по заданному индексу\n 9. Удалить все\n10. Структура пуста?\n11.Генерация структуры данных с заданным числом элементов\n12.Передвинуть указатель структуры на n элементов вперед, назад(только для списков)\n13.Выход" << endl;
- int x = 0;
- cin >> x;
- if (x == 13)
- break;
- switch (x)
- {
- case 1:
- if (choice == 1)
- {
- link temp = new node;
- temp = head;
- while (temp)
- {
- cout << temp->data << " ";
- temp = temp->next;
- }
- }
- else
- {
- for (int i = 0; i < sz; i++)
- cout << array[i] << " ";
- }
- break;
- case 2:
- cout << "Введите элемент" << endl;
- cin >> value;
- if (choice == 1)
- add(value);
- else
- {
- add(value, array);
- }
- break;
- case 3:
- cout << "Введите позицию" << endl;
- cin >> pos;
- cout << "Введите элемент" << endl;
- cin >> value;
- if (choice == 1)
- add_rand(value, pos);
- else
- add_rand(value, pos, array);
- break;
- case 4:
- cout << "Введите позицию" << endl;
- cin >> pos;
- if (choice == 1)
- del(pos);
- else
- del(pos, array);
- break;
- case 5:
- cout << "Введите аргумент для поиска" << endl;
- cin >> value;
- if (choice == 1)
- searchByArg(value);
- else
- searchByArg(value, array);
- break;
- case 6:
- cout << "Введите индекс" << endl;
- cin >> pos;
- if (choice == 1)
- searchByIndex(pos);
- else
- searchByIndex(pos, array);
- break;
- case 7:
- cout << "Введите аргументы, которые нужно заменить" << endl;
- cin >> value;
- cout << "Введите аргумент для замены" << endl;
- cin >> pos;
- if (choice == 1)
- replaceArg(value, pos);
- else
- replaceArg(value, pos, array);
- break;
- case 8:
- cout << "Введите индекс элемента для замены" << endl;
- cin >> pos;
- cout << "Введите аргумент для замены" << endl;
- cin >> value;
- if (choice == 1)
- replaceIndex(pos, value);
- else
- replaceIndex(pos, value, array);
- break;
- case 9:
- deleteEverything(choice);
- break;
- case 10:
- if (choice = 1)
- {if (head==NULL)
- cout << "Структура пуста"<<endl;
- else
- {
- cout << "Структура не пуста"<<endl;
- }
- }
- else
- {
- if (array[0]=NULL)
- cout << "Структура пуста"<<endl;
- else
- {
- cout << "Структура не пуста"<<endl;
- }
- case 11:
- cout << "Введите количество элементов в списке" << endl;
- cin >> elem;
- cout << "Введите элемент" << endl;
- cin >> value;
- if (choice == 1)
- for (int i = 0; i < elem; i++)
- {
- add(value);
- }
- else if(elem<lulz)
- {for (int i = 0; i < elem; i++)
- {
- add(value,array);
- }
- break;
- }
- else
- {
- cout << "Вы собираетесь добавить слишком много элементов";
- }
- case 12:
- {if (choice == 1){ cout << "Начальная позиция > "; cin >> position; cout << "Конечная позиция > "; cin >> position1; Obmen(position, position1); } break; }
- }
- }
- }
- }
- link NEW(int num, link next)
- {
- link newnode = new node;
- newnode->data = num;
- newnode->next = next;
- return newnode;
- }
- void shiftRight(int *a, int size, int pos)
- {
- int i = size;
- while (i > pos)
- {
- a[i] = a[i - 1];
- i--;
- }
- }
- void shiftLeft(int *a, int size, int pos)
- {
- int i = pos;
- while (i < size - 1)
- {
- a[i] = a[i + 1];
- i++;
- }
- }
- void add(int num)
- {
- if (empty())
- {
- head = NEW(num, NULL);
- }
- else
- {
- link newnode = NEW(num, head);
- head = newnode;
- }
- }
- void add(int num, int *array)
- {
- shiftRight(array, sz, 0);
- array[0] = num;
- if (sz<lulz)
- sz++;
- }
- int empty()
- {
- return head == NULL;
- }
- void init()
- {
- head = NULL;
- }
- void add_rand(int num, int pos)
- {
- if (empty())
- return;
- link temp = head;
- for (int i = pos; i > 1; i--)
- {
- temp = temp->next;
- if (temp == NULL)
- return;
- }
- link newnode = NEW(num, temp->next);
- temp->next = newnode;
- }
- void add_rand(int num, int pos, int *array)
- {
- shiftRight(array, sz, pos);
- array[pos] = num;
- if (sz<lulz)
- sz++;
- }
- void del(int pos)
- {
- if (empty())
- return;
- if (pos == 1)
- {
- link temp = head;
- head = head->next;
- delete temp;
- }
- else
- {
- link temp = head;
- for (int i = pos; i > 2; i--)
- {
- temp = temp->next;
- if (temp == NULL)
- return;
- }
- link temp2 = temp->next;
- temp->next = temp2->next;
- delete temp2;
- }
- }
- void del(int pos, int *array)
- {
- shiftLeft(array, sz, pos - 1);
- sz--;
- }
- void searchByArg(int arg)
- {
- link temp = head;
- while (temp)
- {
- if (temp->data == arg)
- cout << "\n Found it: " << temp->data << endl;
- temp = temp->next;
- }
- }
- void searchByArg(int arg, int *array)
- {
- for (int i = 0; i < sz; i++)
- if (array[i] == arg)
- cout << "\n Found it: " << array[i] << endl;
- }
- void replaceArg(int arg, int replaceFor)
- {
- link temp = head;
- while (temp)
- {
- if (temp->data == arg)
- temp->data = replaceFor;
- temp = temp->next;
- }
- }
- void replaceArg(int arg, int replaceFor, int *array)
- {
- for (int i = 0; i < sz; i++)
- {
- if (array[i] == arg)
- array[i] = replaceFor;
- }
- }
- void searchByIndex(int index)
- {
- link temp = head;
- for (int i = index; i > 1; i--)
- {
- if (temp == NULL)
- return;
- temp = temp->next;
- }
- cout << temp->data << endl;
- }
- void searchByIndex(int index, int *array)
- {
- if (index < sz && index >= 1)
- cout << "Here it is: " << array[index - 1] << endl;
- else
- cout << "Nope" << endl;
- }
- void replaceIndex(int index, int replaceFor)
- {
- link temp = head;
- for (int i = index; i > 1; i--)
- {
- if (temp == NULL)
- return;
- temp = temp->next;
- }
- temp->data = replaceFor;
- }
- void replaceIndex(int index, int replaceFor, int *array)
- {
- if (index < sz && index >= 1)
- array[index - 1] = replaceFor;
- else
- cout << "Nope" << endl;
- }
- void deleteEverything(int var)
- {
- if (var == 1)
- {
- link temp = head;
- while (head)
- {
- temp = head->next;
- delete head;
- head = temp;
- }
- }
- else
- sz = 0;
- }
- void Obmen(int position, int position1)
- {
- int s = 0; float temp;
- if (head == NULL) cout << "\nСписок пуст\n\n";
- else
- {
- node *a = head;
- do
- {
- if (a->data != NULL){ s++; }
- if (s == position){ break; }
- a = a->next;
- } while (a != head);
- s = 0;
- node *a1 = head;
- do
- {
- if (a1->data != NULL){ s++; }
- if (s == position1){ break; }
- a1 = a1->next;
- } while (a1 != head);
- temp = a->data; a->data = a1->data; a1->data = temp;
- }
- }
- //
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement