diff --git a/1]. DSA/1]. Data Structures/10]. Queue/C++/_01)_implementation_of_queue_using_linked_list.cpp b/1]. DSA/1]. Data Structures/10]. Queue/C++/_01)_implementation_of_queue_using_linked_list.cpp new file mode 100644 index 000000000..d66861bcd --- /dev/null +++ b/1]. DSA/1]. Data Structures/10]. Queue/C++/_01)_implementation_of_queue_using_linked_list.cpp @@ -0,0 +1,100 @@ +#include +using namespace std; + +class node + { + public: + int data; + node* next; + node(int x) + { + data = x; + next = NULL; + } + }; + +class que + { + node* front; + node* back; + + public: + + que() + { + front = NULL; + back = NULL; + } + + void push(int x); + void pop(); + int peek(); + bool isempty(); + }; + +void que::push(int x) + { + node* n = new node(x); + + if(isempty()) + { + front = n; + back = n; + } + + back->next = n; + back = n; + + } + +void que::pop() + { + if(isempty()) + { + cout<<"Queue is empty"; + return ; + } + node* todelete = front; + front = front->next; + delete todelete; + } + +int que::peek() + { + if(isempty()) + { + cout<<"Queue is empty"; + exit(1); + } + return front->data; + } + +bool que::isempty() + { + if(front == NULL) + { + return true; + } + else{return false;} + } + +int main() +{ + que q; + int n,m,x; + + cin>>n; + for(int i=0;i>x; + q.push(x); + } + + while(!q.isempty()) + { + cout< +using namespace std; + +class que + { + stacks1; + + public: + + void push(int x) + { + s1.push(x); + } + + int pop() + { + if(s1.empty()) + { + cout<<"Empty Queue"; + return -1; + } + + int x = s1.top(); + s1.pop(); + + if(s1.empty()) + { + return x; + } + int item = pop(); + s1.push(x); + return item; + + } + + bool isempty() + { + if(s1.empty()){ return true;} + return false; + } + + }; + +int main() +{ + que q; + int n,x; + cin>>n; + + for(int i=0;i>x; + q.push(x); + } + + while(!q.isempty()) + { + cout< +using namespace std; + +class que + { + stacks1; + stacks2; + + public: + + void push(int x) + { + s1.push(x); + } + + int pop() + { + if(s1.empty() and s2.empty()) + { + cout<<"Empty Queue"; + return -1; + } + + if(s2.empty()) + { + while(!s1.empty()) + { + s2.push(s1.top()); + s1.pop(); + } + } + int topval = s2.top(); + s2.pop(); + return topval; + } + + bool isempty() + { + if(s1.empty() and s2.empty()){ return true;} + return false; + } + + }; + +int main() +{ + que q; + int n,x; + cin>>n; + + for(int i=0;i>x; + q.push(x); + } + + while(!q.isempty()) + { + cout<