Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct element
- {
- int key;
- element*next;
- }*f = NULL, *l = NULL; // f - first ; l - last
- void push(int n)
- {
- element *pointer = l;
- l = new element;
- l->key = n;
- l->next = NULL;
- if (pointer)
- pointer->next = l;
- else
- f = l;
- }
- int pop(int &n)
- {
- if (f) {
- element *pointer = f;
- n = f->key;
- f = (f->next);
- delete pointer;
- if (f == NULL) {
- l = NULL;
- }
- return 1;
- }
- else
- return 0;
- }
- int del_k(int k)
- {
- if (f) {
- element *pointer = f;
- element *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 (f->key == k) // ako purviqt element e raven na k
- f = f->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 << "Prazna opashka !!";
- return 0;
- }
- void list() {
- if (f) {
- element *pointer = f;
- while (pointer) {
- cout << "\t" << pointer->key;
- pointer = pointer->next;
- }
- }
- else {
- cout << "\nEmpty";
- }
- }
- int main()
- {
- int k, n = 0;
- do {
- cout << "\n Digit: ";
- cin >> k;
- push(k);
- n++;
- } while (k > 0);
- cout << "\n Opashkata: ";
- list();
- cout << endl;
- cout << "\n Opashkata sled iztritiq posleden: ";
- while (pop(k)) {
- if (n != 1) {
- cout << "\t" << k;
- push(k);
- n--;
- }
- else
- pop(k);
- }
- cout << endl << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement