Home queue12.cpp
Post
Cancel

queue12.cpp

queue12.cpp

Go Back

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Link to Challenge: https://www.w3resource.com/cpp-exercises/queue/index.php

#include <iostream>
#include <vector>

using namespace std;

class Queue{
    private:

        struct node{
            int data;
            node* next;
        };

        node* head = nullptr;
    public:

        Queue(){

        }

        Queue(std::vector<int> arr){
            enqueue(arr);
        }

        void enqueue(std::vector<int> arr){
            node* tmp = nullptr;
            node* current = nullptr;
            for (int i = 0; i < arr.size(); i++){
                if (head == nullptr){
                    head = new node{arr[i], nullptr};
                    current = head;
                }
                else {
                    tmp = new node{arr[i], nullptr};
                    current -> next = tmp;
                    current = current -> next;
                }
            }
        }

        void dequeue(){
            node* tmp = head;
            head = head -> next;
            tmp -> next = nullptr;
            delete tmp;
        }

        int secondLowest(){
            int biggest = 0;
            int smallest = head -> data;
            for (node* i = head -> next; i != nullptr; i = i -> next){
                if (i -> data < smallest){
                    smallest = i -> data;
                }
                if (i -> data > biggest){
                    biggest = i -> data;
                }
            }
            // cout << "Smallest: " << smallest << endl;
            int secondSmallest = biggest;
            bool isFound = false;
            for (node* i = head; i != nullptr; i = i -> next){
                if (i -> data > smallest && i -> data < secondSmallest){
                    secondSmallest = i -> data;
                    isFound = true;
                }
            }
            if (!isFound){
                return 0;
            }
            return secondSmallest;
        }

        void print(){
            cout << "Queue Elements: ";
            for (node* i = head; i != nullptr; i = i -> next){
                cout << i -> data << " ";
            }
            cout << endl;
        }
};

int main(){
    Queue q1({5, 10, 11, 2, 4, 6, 8, 100, 95, 45, 36});
    q1.print();
    if (q1.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << q1.secondLowest() << endl;
    }
    Queue q2({1, 2, 5, 7, 100, 49, 60});
    q2.print();
    if (q2.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q2.secondLowest() << endl;
    }
    Queue q3({2, 1, 5, 7, 100, 49, 60});
    q3.print();
    if (q3.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q3.secondLowest() << endl;
    }
    Queue q4({2, 2, 1, 5, 7, 100, 49, 60});
    q4.print();
    if (q4.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q4.secondLowest() << endl;
    }
    Queue q5({1, 2, 2, 1, 5, 7, 100, 49, 60});
    q5.print();
    if (q5.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q5.secondLowest() << endl;
    }
    Queue q6({1, 6, 6, 7, 19, 25, 36, 49, 60});
    q6.print();
    if (q6.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q6.secondLowest() << endl;
    }
    Queue q7({1, 1, 1});
    q7.print();
    if (q7.secondLowest() == 0){
        cout << "No second lowest element" << endl;
    }
    else {
        cout << "Second Lowest: " << q7.secondLowest() << endl;
    }
    return 0;
}

Console Output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Queue Elements: 5 10 11 2 4 6 8 100 95 45 36 
4
Queue Elements: 1 2 5 7 100 49 60
Second Lowest: 2
Queue Elements: 2 1 5 7 100 49 60
Second Lowest: 2
Queue Elements: 2 2 1 5 7 100 49 60
Second Lowest: 2
Queue Elements: 1 2 2 1 5 7 100 49 60
Second Lowest: 2
Queue Elements: 1 6 6 7 19 25 36 49 60
Second Lowest: 6
Queue Elements: 1 1 1
No second lowest element

Go Back

This post is licensed under CC BY 4.0 by the author.