-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0242728
commit d958d99
Showing
4 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
...DSA/1]. Data Structures/10]. Queue/C++/_01)_implementation_of_queue_using_linked_list.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <bits/stdc++.h> | ||
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<n;i++) | ||
{ | ||
cin>>x; | ||
q.push(x); | ||
} | ||
|
||
while(!q.isempty()) | ||
{ | ||
cout<<q.peek()<<" "; | ||
q.pop(); | ||
} | ||
|
||
return 0; | ||
} |
62 changes: 62 additions & 0 deletions
62
...SA/1]. Data Structures/10]. Queue/C++/_02)_implementation_of_queue_using_single_stack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class que | ||
{ | ||
stack<int>s1; | ||
|
||
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<n;i++) | ||
{ | ||
cin>>x; | ||
q.push(x); | ||
} | ||
|
||
while(!q.isempty()) | ||
{ | ||
cout<<q.pop()<<" "; | ||
} | ||
|
||
return 0; | ||
} |
63 changes: 63 additions & 0 deletions
63
1]. DSA/1]. Data Structures/10]. Queue/C++/_03)_queue_implem_using_2_stack.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class que | ||
{ | ||
stack<int>s1; | ||
stack<int>s2; | ||
|
||
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<n;i++) | ||
{ | ||
cin>>x; | ||
q.push(x); | ||
} | ||
|
||
while(!q.isempty()) | ||
{ | ||
cout<<q.pop()<<" "; | ||
} | ||
|
||
return 0; | ||
} |
Binary file added
BIN
+1.88 MB
1]. DSA/1]. Data Structures/10]. Queue/C++/_04)_queue_implem_using_2_stack.exe
Binary file not shown.