Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Sstack
- {
- int key;
- Sstack* next;
- }*start = NULL;
- struct Pstack
- {
- int key;
- Pstack* next;
- }*start2 = NULL;
- void addElement(unsigned int& countOfNumber)
- {
- Sstack* s;
- s = start;
- int Snum;
- do
- {
- cout << "Enter Positive Number: ";
- cin >> Snum;
- } while (Snum < 0);
- start = new Sstack;
- start->key = Snum;
- start->next = s;
- countOfNumber++;
- cout << "Element added" << endl;
- cout << endl;
- }
- void addElementToP(unsigned int& countOfNumber, unsigned int n)
- {
- Pstack* s;
- s = start2;
- start2 = new Pstack;
- start2->key = n;
- start2->next = s;
- countOfNumber++;
- cout << "Element added" << endl;
- cout << endl;
- }
- void removeElement(bool isP)
- {
- if (isP) {
- while (start2)
- {
- Pstack* s = start2;
- bool isLast = false;
- do
- {
- if (!s->next) {
- isLast = true;
- break;
- }
- if (s->next != start2) {
- s = s->next;
- }
- } while (s->next != start2);
- if (isLast)
- {
- start2 = NULL;
- delete s;
- }
- else {
- start2 = s;
- delete s;
- }
- }
- }
- else {
- while (start)
- {
- Sstack* s = start;
- bool isLast = false;
- do
- {
- if (!s->next) {
- isLast = true;
- break;
- }
- if (s->next != start) {
- s = s->next;
- }
- } while (s->next != start);
- if (isLast)
- {
- start = NULL;
- delete s;
- }
- else {
- start = s;
- delete s;
- start->next = NULL;
- }
- }
- cout << "Element/s removed\n";
- }
- }
- void printElements()
- {
- Sstack* s = start;
- while (s)
- {
- cout << s->key << " ";
- s = s->next;
- }
- cout << endl;
- if (start2)
- {
- Pstack* s2 = start2;
- while (s2)
- {
- cout << s2->key << " ";
- s2 = s2->next;
- }
- }
- cout << endl;
- }
- void sortElementInPStack(unsigned int countOfNumber)
- {
- unsigned int count = 0;
- Sstack* s = start;
- int smallest = s->key;
- s = s->next;
- removeElement(true);
- while (count < countOfNumber)
- {
- for (int i = count + 1; i < countOfNumber; i++)
- {
- if (smallest > s->key)
- {
- smallest = s->key;
- }
- s = s->next;
- }
- addElementToP(count, smallest);
- s = start;
- if (count < countOfNumber)
- {
- for (int i = 0; i < count; i++)
- {
- s = s->next;
- }
- smallest = s->key;
- }
- }
- }
- int main()
- {
- short choice;
- unsigned int countOfNumber = 0;
- do
- {
- cout << "[1] Add element\n[2] Remove element \n[3] Print elements\n[4] Sort by descending order\n[0] Exit\n";
- cin >> choice;
- switch (choice)
- {
- case 1:
- system("cls");
- addElement(countOfNumber);
- choice = -1;
- break;
- case 2:
- system("cls");
- if (!start) {
- cout << "No elements to remove\n";
- }
- else {
- removeElement(false);
- }
- choice = -1;
- break;
- case 3:
- system("cls");
- if (!start) {
- cout << "No elements to print\n";
- }
- else {
- printElements();
- }
- choice = -1;
- break;
- case 4:
- system("cls");
- if (!start) {
- cout << "No elements to sort\n";
- }
- else {
- if (countOfNumber > 1) {
- sortElementInPStack(countOfNumber);
- }
- else {
- cout << "Only one element\n";
- }
- }
- choice = -1;
- break;
- default:
- break;
- }
- } while (choice < 0 || choice > 4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement