-
-
Notifications
You must be signed in to change notification settings - Fork 359
Stack queue in cpp #936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ShadowMitia
merged 10 commits into
algorithm-archivists:main
from
ishwar00:stack_queue_in_cpp
Jan 12, 2022
Merged
Stack queue in cpp #936
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1b19196
adding c++ implementation of stack and queue
ishwar00 feb723c
improved some comments
ishwar00 2a89d60
Update contents/stacks_and_queues/code/c++/queue.cpp
ishwar00 1bfc0c4
Update contents/stacks_and_queues/code/c++/queue.cpp
ishwar00 9d9a245
improved implementation
ishwar00 7af16dd
fixed queue implementation
ishwar00 7221c9b
renamed queue API push as enqueue
ishwar00 b11e846
Merge branch 'main' into stack_queue_in_cpp
ishwar00 b878ed0
Rename code folder to cpp
ShadowMitia 00ee930
Inline newlines
ShadowMitia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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<iostream> | ||
using namespace std; | ||
|
||
namespace my { | ||
/** | ||
* parameterised type implementation using linked list | ||
* [value][next] -> [value][next] -> ... -> [value][next] -> [value][next] | ||
* (front Node) (intermediat Nodes) (rear Node) (dummy Node) | ||
*/ | ||
template<typename T> | ||
struct Node { | ||
/** | ||
* next: will store right Node address | ||
*/ | ||
T value; | ||
Node<T>* next; | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Node(const T& V) : value(V), next(nullptr) { } | ||
}; | ||
|
||
template<typename T> | ||
class queue { | ||
private: | ||
/** | ||
* _front : points to left most node | ||
* count: keeps track of current number of elements present in queue excluding dummy Node | ||
* rear: points to most recent Node added into the queue, which is just left size of dummy Node | ||
*/ | ||
Node<T>* _front; | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Node<T>* rear; | ||
size_t count; | ||
public: | ||
queue() : _front(nullptr), rear(nullptr), count(0ULL) { | ||
_front = rear = new Node<T>(0); // creating a dummy Node | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void push(const T& element) { | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Node<T>* new_node = new Node<T>(element); // create New Node | ||
if (count > 0) { | ||
new_node->next = rear->next; // make new Node point to dummy Node | ||
rear->next = new_node; // make rear Node point to new Node | ||
rear = new_node; // make rear Node's pointer to point to new Node | ||
} else { | ||
new_node->next = rear; | ||
rear = _front = new_node; | ||
} | ||
count = count + 1; | ||
} | ||
|
||
void dequeue() { | ||
if (count > 0) { | ||
Node<T>* buffer = _front; | ||
_front = _front->next; | ||
count = count - 1; | ||
delete buffer; | ||
} | ||
rear = (count != 0) ? rear : _front; | ||
} | ||
|
||
T& front() const { return _front->value; } | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// returning reference can very usefull if someone wants to modify _front element | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
size_t size() const { return count; } | ||
|
||
bool empty() const { return count == 0; } | ||
|
||
~queue() { | ||
for (Node<T>* pointer = _front; pointer != nullptr;) { | ||
Node<T>* buffer = pointer; | ||
pointer = pointer->next; | ||
delete buffer; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
int main() { | ||
my::queue<int> Q; | ||
|
||
Q.push(0); | ||
Q.push(1); | ||
Q.push(2); | ||
Q.push(3); | ||
cout << "count: " << Q.size() << endl; | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Q.front() = 10; | ||
|
||
while (Q.empty() != true) { | ||
cout << "element: " << Q.front() << endl; | ||
Q.dequeue(); | ||
} | ||
|
||
Q.push(3); | ||
Q.push(6); | ||
cout << "count: " << Q.size() << endl; | ||
while (Q.empty() != true) { | ||
cout << "element: " << Q.front() << endl; | ||
Q.dequeue(); | ||
} | ||
return 0; | ||
} | ||
|
This file contains hidden or 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,80 @@ | ||
#include<iostream> | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using namespace std; | ||
|
||
namespace my { | ||
/** | ||
*implementation using parameterised linked list | ||
* [value][next] -> [value][next] -> ... -> [value][next] | ||
* (top Node) (intermediat Nodes) (dummy Node) | ||
*/ | ||
template<typename T> | ||
struct Node { | ||
/** | ||
* next: will store right Node address | ||
*/ | ||
T value; | ||
Node<T>* next; | ||
Node(const T& V) : value(V), next(nullptr) { } | ||
}; | ||
|
||
template<typename T> | ||
class stack { | ||
private: | ||
/** | ||
* _top: points to left most node | ||
* count: keeps track of current number of elements present in stack excluding dummy Node | ||
*/ | ||
Node<T>* _top; | ||
size_t count; | ||
public: | ||
stack() : _top(nullptr), count(0ULL) { | ||
_top = new Node<T>(0); // creating a dummy node | ||
} | ||
|
||
void push(const T& element) { | ||
Node<T>* buffer = new Node<T>(element); | ||
buffer->next = _top; | ||
_top = buffer; | ||
count = count + 1; | ||
} | ||
|
||
void pop() { | ||
if (count > 0) { | ||
Node<T>* buffer = _top; | ||
_top = _top->next; | ||
count = count - 1; | ||
delete buffer; | ||
} | ||
} | ||
|
||
T& top() const { return _top->value; } | ||
// returning reference can very usefull if someone wants to modify top element | ||
|
||
size_t size() const { return count; } | ||
|
||
bool empty() const { return count == 0; } | ||
|
||
~stack() { | ||
for (Node<T>* pointer = _top; pointer != nullptr;) { | ||
Node<T>* buffer = pointer; | ||
pointer = pointer->next; | ||
delete buffer; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
int main() { | ||
my::stack<int> S; | ||
S.push(0); | ||
S.push(1); | ||
S.push(2); | ||
S.push(3); | ||
cout << "size: " << S.size() << endl; | ||
S.top() = 10; | ||
while (S.empty() != true) { | ||
cout << "element: " << S.top() << endl; | ||
S.pop(); | ||
} | ||
return 0; | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.