Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct elem {
- int key;
- elem *next;
- }*start = nullptr;
- void list() {
- if(start) {
- elem *pointer = start;
- while(pointer) {
- cout << pointer->key << "\t";
- pointer = pointer->next;
- }
- } else {
- cout << "\nEmpty";
- }
- }
- void add_b(int n) {
- elem *pointer = start;
- start = new elem;
- start->key = n;
- start->next = pointer;
- }
- void add_e(int n) {
- elem *pointer = new elem;
- pointer->key = n;
- pointer->next = nullptr;
- if(start) {
- elem *helper = start;
- while(helper->next != nullptr)
- helper = helper->next;
- helper->next = pointer;
- } else
- start = pointer;
- }
- void add_bk(int n, int k) {
- elem *pointer = start; // ukazatelqt pointer sochi kum nachaloto na spisuka
- while(pointer->key != k && pointer->next) // vurti, dokato sreshtne k ili ima sledvasht element, ako nqma, shte spre da vurti cikula
- pointer = pointer->next;
- if(pointer->key == k) { // ako kliuchovata stoinost na ukazatelq e ravna na k
- elem *helper = new elem; // suzdavame pomoshten ukazatel
- helper->next = pointer->next; // sledvashtiqt element na pomoshtniqt ukazatel e sledvashtiqt element na pointer ukazatelq
- pointer->next = helper; // sledvashtiqt element na ukazatelq pointer e raven na tekushtiqt ukazatel helper
- // obrushtat se dvete kliuchovi stoinosti, s cel da se dobavi predi tozi element
- helper->key = k;
- pointer->key = n;
- } else
- cout << "K ne e nameren";
- }
- void add_ak(int n, int k) {
- elem *pointer = start; // ukazatelqt pointer sochi kum nachaloto na spisuka
- while(pointer->key != k && pointer->next) // vurti, dokato sreshtne k ili ima sledvasht element, ako nqma, shte spre da vurti cikula
- pointer = pointer->next; // ukazatelqt pointer se premestva s edin element napred
- if(pointer->key == k) {
- elem *helper = new elem;
- helper->next = pointer->next;
- pointer->next = helper;
- // obrushtat se dvete kliuchovi stoinosti, s cel da se dobavi sled tozi element
- helper->key = n;
- pointer->key = k;
- } else
- cout << "K ne e nameren";
- }
- void add_sorted(int n) {
- if(start == nullptr || start->key > n) { // ako e purvi element ili ukazatelqt za nachalo ima po-golqma kliuchova stoinost ot n
- add_b(n); // dobavi v nachaloto
- } else {
- elem *pointer = start; // sochi kum nachaloto na spisuka
- while(pointer->key <= n && pointer->next) // dokato ukazatelqt ima kliuchova stoinost po-malka ili ravna na n i ima sledvasht element
- pointer = pointer->next; // premestva se napred kum sledvashtiqt element
- if(pointer->key >= n) // ako ukazatelqt ima kliuchova stoinost po-golqma ili rawna na n
- add_bk(n, pointer->key); // dobavi sled tazi kliuchova stoinost v spisuka
- else // v protiven sluchai
- add_e(n); // dobavi nakraq
- }
- }
- int del_k(int k) {
- if(start) {
- elem *pointer = start;
- elem *helper = pointer;
- while(pointer->key != k && pointer->next) {// dokato e razlichno ot k i ima sledvasht element
- helper = pointer; // predi da premine kum sledvashtiqt element, zapazva tekushtiqt v nov ukazatel
- pointer = pointer->next; // premestva se napred kum sledvashtiqt element
- }
- if(pointer->key == k) {
- if(start->key == k) // ako purviqt element e raven na k
- start = start->next; // purviqt element se premestva s edin element napred
- else
- helper->next = pointer->next; // preskacha elementa
- delete pointer; // iztriva go
- return 1;
- } else {
- cout << "Nqmam k, nqa kvo da triq";
- return 0;
- }
- } else
- cout << "Prazen spisuk";
- return 0;
- }
- int main() {
- int k;
- do {
- cout << "\nDigit: ";
- cin >> k;
- if(k) {
- add_sorted(k);
- list();
- }
- } while(k != 0);
- cout << "Vuvedi stoinost, koqto iskash da iztriesh: ";
- cin >> k;
- del_k(k); // iztriva elementa sus stoinost k
- list(); // izvejda spisuka
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement