-
-
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 6 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,94 @@ | ||
#include<iostream> | ||
#include<memory> | ||
#include<cassert> | ||
|
||
namespace my { | ||
const char endl = '\n'; | ||
/** | ||
* implementation using linked list | ||
* [value][next] -> [value][next] -> ... -> [value][next] | ||
* (front Node) (intermediat Nodes) (rear Node) | ||
*/ | ||
template<typename T> | ||
struct Node { | ||
/** | ||
* next: will store right Node address | ||
*/ | ||
T value; | ||
std::shared_ptr<Node<T>> next; | ||
Node(const T& V) : value(V) { } | ||
}; | ||
|
||
template<typename T> | ||
class queue { | ||
private: | ||
/** | ||
* front_pointer: points to left most node | ||
* count: keeps track of current number of elements present in queue | ||
* rear_pointer: points to most recent Node added into the queue, which is right most Node | ||
*/ | ||
std::shared_ptr<Node<T>> front_pointer; | ||
std::shared_ptr<Node<T>> rear_pointer; | ||
size_t count; | ||
public: | ||
queue() : count(0ULL) { } | ||
|
||
void push(const T& element) { | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto new_node = std::make_shared<Node<T>>(element); | ||
if (count > 0) { | ||
rear_pointer->next = new_node; | ||
rear_pointer = new_node; | ||
} else { | ||
rear_pointer = front_pointer = new_node; | ||
} | ||
count = count + 1; | ||
} | ||
|
||
void dequeue() { | ||
if (count > 1) { | ||
front_pointer = front_pointer->next; | ||
count = count - 1; | ||
} else if (count == 1) { | ||
front_pointer.reset(); | ||
rear_pointer.reset(); | ||
count = count - 1; | ||
} | ||
} | ||
|
||
T& front() { | ||
assert(count > 0 && "calling front on an empty queue"); | ||
return front_pointer->value; | ||
} | ||
|
||
T const& front() const { | ||
assert(count > 0 && "calling front on an empty queue"); | ||
return front_pointer->value; | ||
} | ||
|
||
size_t size() const { return count; } | ||
|
||
bool empty() const { return count == 0; } | ||
|
||
~queue() { | ||
while (front_pointer.get() != nullptr) { | ||
front_pointer = front_pointer->next; | ||
} | ||
} | ||
}; | ||
} | ||
|
||
int main() { | ||
my::queue<int> Q; | ||
Q.push(-1); | ||
Q.push(1); | ||
Q.push(2); | ||
Q.push(3); | ||
std::cout << "count: " << Q.size() << my::endl; | ||
Q.front() = 0; | ||
|
||
while (Q.empty() != true) { | ||
std::cout << "element: " << Q.front() << my::endl; | ||
Q.dequeue(); | ||
} | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,82 @@ | ||
#include<iostream> | ||
ishwar00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#include<cassert> | ||
#include<memory> | ||
|
||
namespace my { | ||
const char endl = '\n'; | ||
/** | ||
* implementation using linked list | ||
* [value][next] -> [value][next] -> ... -> [value][next] | ||
* (top Node) (intermediat Nodes) | ||
* left most Node represents top element of stack | ||
*/ | ||
template<typename T> | ||
struct Node { | ||
/** | ||
* next: will store right Node address | ||
*/ | ||
T value; | ||
std::unique_ptr<Node<T>> next; | ||
Node(const T& V) : value(V) { } | ||
}; | ||
|
||
template<typename T> | ||
class stack { | ||
private: | ||
/** | ||
* top_pointer: points to left most node | ||
* count: keeps track of current number of elements present in stack | ||
*/ | ||
std::unique_ptr<Node<T>> top_pointer; | ||
size_t count; | ||
public: | ||
stack() : count(0ULL) { } | ||
|
||
void push(const T& element) { | ||
auto new_node = std::make_unique<Node<T>>(element); | ||
new_node->next = std::move(top_pointer); | ||
top_pointer = std::move(new_node); | ||
count = count + 1; | ||
} | ||
|
||
void pop() { | ||
if (count > 0) { | ||
top_pointer = std::move(top_pointer->next); | ||
count = count - 1; | ||
} | ||
} | ||
|
||
T& top() { | ||
assert(count > 0 and "calling top() on an empty stack"); | ||
return top_pointer->value; | ||
} | ||
// returning mutable reference can very be usefull if someone wants to modify top element | ||
|
||
T const& top() const { | ||
assert(count > 0 and "calling top() on an empty stack"); | ||
return top_pointer->value; | ||
} | ||
|
||
size_t size() const { return count; } | ||
|
||
bool empty() const { return count == 0; } | ||
|
||
~stack() { | ||
while (top_pointer.get() != nullptr) { | ||
top_pointer = std::move(top_pointer->next); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
int main() { | ||
my::stack<int> S; | ||
S.push(3); | ||
std::cout << "size: " << S.size() << my::endl; | ||
S.top() = 10; | ||
while (S.empty() != true) { | ||
std::cout << "element: " << S.top() << my::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.