├── .gitattributes ├── .github └── FUNDING.yml ├── LICENSE ├── Queue.cpp ├── Stack.cpp └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: ['https://www.cyfylabs.com'] 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 harismuneer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Queue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | template 7 | 8 | class Queue 9 | { 10 | private: 11 | T* arr; 12 | int front; 13 | int rear; 14 | int maxSize; 15 | int currSize; 16 | 17 | public: 18 | 19 | //Constructor with Default Parameters 20 | Queue (int s = 10) 21 | { 22 | if(s<=0) 23 | s = 10; 24 | arr = new T [s]; 25 | maxSize = s; 26 | currSize = 0; 27 | front = 0; 28 | rear = -1; 29 | } 30 | 31 | 32 | //Destructor 33 | ~Queue () 34 | { 35 | if(arr) 36 | delete arr; 37 | } 38 | 39 | 40 | //To check whether the queue is empty 41 | bool IsEmpty() 42 | { 43 | return (currSize == 0); 44 | } 45 | 46 | 47 | //To check whether the queue is full 48 | bool IsFull() 49 | { 50 | return (currSize == maxSize); 51 | } 52 | 53 | 54 | //Copy Constructor 55 | Queue (Queue& obj) 56 | { 57 | arr = new T [obj.maxSize]; 58 | 59 | for (int i = 0; i < obj.maxSize; ++i) 60 | { 61 | arr[i] = obj.arr[i]; 62 | } 63 | 64 | currSize = obj.currSize; 65 | maxSize = obj.maxSize; 66 | front = obj.front; 67 | rear = obj.rear; 68 | } 69 | 70 | 71 | //Overloaded Assignment Operator 72 | Queue& operator = (Queue& rhs) 73 | { 74 | if(this != &rhs) 75 | { 76 | if(arr) 77 | delete arr; 78 | 79 | arr = new T [rhs.maxSize]; 80 | 81 | for (int i = 0; i < rhs.maxSize; ++i) 82 | { 83 | arr[i] = rhs.arr[i]; 84 | } 85 | 86 | currSize = rhs.currSize; 87 | maxSize = rhs.maxSize; 88 | front = rhs.front; 89 | rear = rhs.rear; 90 | } 91 | 92 | return *this; 93 | } 94 | 95 | 96 | //To add an element at the rear of the queue 97 | bool Enqueue( T& data) 98 | { 99 | if(!IsFull()) 100 | { 101 | if(rear == maxSize - 1)//To aid wraparound 102 | rear = -1; 103 | 104 | arr[++rear] = data; 105 | currSize++; 106 | 107 | return true; 108 | } 109 | 110 | return false; 111 | } 112 | 113 | 114 | //To remove an element from the front of the Queue 115 | bool Dequeue ( T& data ) 116 | { 117 | if(!IsEmpty()) 118 | { 119 | if(front == maxSize)//To aid wraparound 120 | { 121 | front = 0; 122 | } 123 | 124 | data = arr[front++]; 125 | currSize--; 126 | 127 | return true; 128 | } 129 | 130 | return false; 131 | } 132 | 133 | 134 | void displayFunctions() 135 | { 136 | cout << "\n\nThe class Queue has the following functions : "; 137 | cout << "\n1)Enqueue \n2)Dequeue \n3)IsEmpty \n4)IsFull "; 138 | cout << "\nEnter the option number. To exit, enter -1.\nOption Number : "; 139 | } 140 | 141 | void displayQueue() 142 | { 143 | T temp; 144 | cout << "\n\nThe queue is : "; 145 | 146 | 147 | while( !IsEmpty()) 148 | { 149 | Dequeue(temp); 150 | cout << endl << temp; 151 | } 152 | } 153 | }; 154 | 155 | //-------------Class Ended-------------------------------- 156 | 157 | void driver() 158 | { 159 | int size; 160 | cout << "Enter the maxsize of the queue : "; 161 | cin >> size; 162 | Queue que1(size); 163 | 164 | int userInput = 0; 165 | int data; 166 | 167 | while (userInput != -1) 168 | { 169 | que1.displayFunctions(); 170 | cin >> userInput; 171 | 172 | if (userInput == 1) 173 | { 174 | cout << "\nEnter the data to insert into the queue : "; 175 | cin >> data; 176 | 177 | if ( que1.Enqueue(data) ) 178 | cout << "\nEnqueue Successful."; 179 | else 180 | cout << "\nEnqueue Unsuccessful."; 181 | } 182 | 183 | else if (userInput == 2) 184 | { 185 | if( que1.Dequeue(data) ) 186 | cout << "\nThe Dequeued data item is : " << data; 187 | else 188 | cout << "\nDequeue Unsuccessful."; 189 | } 190 | 191 | else if (userInput == 3) 192 | { 193 | if (que1.IsEmpty()) 194 | cout << "\nThe queue is empty."; 195 | else 196 | cout << "\nThe queue isn't empty."; 197 | } 198 | 199 | else if (userInput == 4) 200 | { 201 | if (que1.IsFull()) 202 | cout << "\nThe queue is full."; 203 | else 204 | cout << "\nThe queue isn't full."; 205 | } 206 | } 207 | 208 | 209 | cout << "\nTesting the copy constructor :"; 210 | Queue que2 = que1; 211 | 212 | cout << "\nqueue 2 is a copy of queue 1 constructed through Copy Constructor."; 213 | if(! que1.IsEmpty() ) 214 | que1.displayQueue(); 215 | else 216 | cout << "\nqueue 1 is empty."; 217 | 218 | if(! que2.IsEmpty() ) 219 | que2.displayQueue(); 220 | else 221 | cout << "\nqueue 2 is empty."; 222 | 223 | cout << "\n\nSimilarly the overloaded assignment operator can be tested. " << endl; 224 | } 225 | 226 | 227 | 228 | 229 | int main () 230 | { 231 | driver(); 232 | return 0; 233 | } 234 | -------------------------------------------------------------------------------- /Stack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | //Templatized Stack Class 7 | template 8 | 9 | class Stack 10 | { 11 | 12 | private: 13 | 14 | T* stk; 15 | 16 | int stkPtr; 17 | int maxSize; 18 | 19 | public: 20 | 21 | //Constructor with Default Parameters 22 | Stack (int s = 10) 23 | { 24 | if(s<=0) 25 | s = 10; 26 | 27 | stk = new T [s]; 28 | maxSize = s; 29 | stkPtr = -1; 30 | } 31 | 32 | 33 | //Destructor 34 | ~Stack () 35 | { 36 | if(stk) 37 | delete stk; 38 | } 39 | 40 | 41 | //Top Function to view the data on the top of the Stack 42 | bool Top (T& data) 43 | { 44 | if(!IsEmpty()) 45 | { 46 | data = stk [stkPtr]; 47 | return true; 48 | } 49 | 50 | return false; 51 | } 52 | 53 | 54 | //Function to check if Stack is empty 55 | bool IsEmpty () 56 | { 57 | return (stkPtr == -1); 58 | } 59 | 60 | 61 | //Function to check if Stack is full 62 | bool IsFull () 63 | { 64 | return (stkPtr == maxSize -1); 65 | } 66 | 67 | 68 | //Function to push data into the top of the Stack 69 | bool Push (T& data) 70 | { 71 | if(!IsFull()) 72 | { 73 | stk [++stkPtr] = data; 74 | return true; 75 | } 76 | 77 | return false; 78 | } 79 | 80 | 81 | //Function to pop data from the top of the Stack 82 | bool Pop(T& data) 83 | { 84 | if(!IsEmpty()) 85 | { 86 | data = stk [stkPtr--]; 87 | return true; 88 | } 89 | 90 | return false; 91 | } 92 | 93 | 94 | //Copy Constructor 95 | Stack(Stack& obj) 96 | { 97 | stk = new T [obj.maxSize]; 98 | 99 | for (int i = 0; i < obj.maxSize; ++i) 100 | { 101 | stk[i] = obj.stk[i]; 102 | } 103 | 104 | maxSize = obj.maxSize; 105 | stkPtr = obj.stkPtr; 106 | } 107 | 108 | 109 | //Overloaded Assignment Operator 110 | Stack& operator = (Stack& rhs) 111 | { 112 | if(this != &rhs) 113 | { 114 | if(stk) 115 | delete stk; 116 | 117 | stk = new T [rhs.maxSize]; 118 | 119 | for (int i = 0; i < rhs.maxSize; ++i) 120 | { 121 | stk[i] = rhs.stk[i]; 122 | } 123 | 124 | maxSize = rhs.maxSize; 125 | stkPtr = rhs.stkPtr; 126 | } 127 | 128 | return *this; 129 | } 130 | 131 | 132 | void displayFunctions() 133 | { 134 | cout << "\n\nThis class Stack has the following functionality : "; 135 | cout << "\n1)Push \n2)Pop \n3)Top \n4)IsEmpty \n5)IsFull "; 136 | cout << "\nEnter the option number. To exit, enter -1.\nOption Number : "; 137 | } 138 | 139 | 140 | void displayStack() 141 | { 142 | T temp; 143 | cout << "\n\nThe stack is : "; 144 | 145 | 146 | while( !IsEmpty()) 147 | { 148 | Pop(temp); 149 | cout << endl << temp; 150 | } 151 | } 152 | }; 153 | //--------------Ended Stack Class--------------------- 154 | 155 | void driver() 156 | { 157 | int size; 158 | cout << "Enter the maxsize of the stack : "; 159 | cin >> size; 160 | Stack stack1(size); 161 | 162 | int userInput = 0; 163 | int data; 164 | 165 | while (userInput != -1) 166 | { 167 | stack1.displayFunctions(); 168 | cin >> userInput; 169 | 170 | if (userInput == 1) 171 | { 172 | cout << "\nEnter the data to push into the stack : "; 173 | cin >> data; 174 | 175 | if ( stack1.Push(data) ) 176 | cout << "\nPush Successful."; 177 | else 178 | cout << "\nPush Unsuccessful."; 179 | } 180 | 181 | else if (userInput == 2) 182 | { 183 | if( stack1.Pop(data) ) 184 | cout << "\nThe popped data item is : " << data; 185 | else 186 | cout << "\nPop Unsuccessful."; 187 | } 188 | 189 | else if (userInput == 3) 190 | { 191 | if (stack1.Top(data)) 192 | cout <<"\nThe element at the top of the stack is : " << data; 193 | else 194 | cout << "\nTop Unsuccessful."; 195 | } 196 | 197 | else if (userInput == 4) 198 | { 199 | if (stack1.IsEmpty()) 200 | cout << "\nThe stack is empty."; 201 | else 202 | cout << "\nThe stack isn't empty."; 203 | } 204 | 205 | else if (userInput == 5) 206 | { 207 | if (stack1.IsFull()) 208 | cout << "\nThe stack is full."; 209 | else 210 | cout << "\nThe stack isn't full."; 211 | } 212 | } 213 | 214 | 215 | 216 | cout << "\nTesting the copy constructor :"; 217 | Stack stack2 = stack1; 218 | 219 | cout << "\nStack 2 is a copy of Stack 1 constructed through Copy Constructor."; 220 | if(! stack1.IsEmpty() ) 221 | stack1.displayStack(); 222 | else 223 | cout << "\nStack 1 is empty."; 224 | 225 | if(! stack2.IsEmpty() ) 226 | stack2.displayStack(); 227 | else 228 | cout << "\nStack 2 is empty."; 229 | 230 | cout << "\n\nSimilarly the overloaded assignment operator can be tested. " << endl; 231 | } 232 | 233 | 234 | 235 | int main () 236 | { 237 | driver (); 238 | return 0; 239 | } 240 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🎈 Queue-and-Stack 2 | 3 | views 4 | [![Open Source Love svg1](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](#) 5 | [![GitHub Forks](https://img.shields.io/github/forks/harismuneer/Queue-and-Stack.svg?style=social&label=Fork&maxAge=2592000)](https://www.github.com/harismuneer/Queue-and-Stack/fork) 6 | [![GitHub Issues](https://img.shields.io/github/issues/harismuneer/Queue-and-Stack.svg?style=flat&label=Issues&maxAge=2592000)](https://www.github.com/harismuneer/Queue-and-Stack/issues) 7 | [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat&label=Contributions&colorA=red&colorB=black )](#) 8 | 9 | 10 | The datastructures queue and stack are implemented using C++. 11 | 12 | Each datastructure is implemented in a separate file along with an interface which can be used to test the functionalities provided by the data structures. 13 | 14 | 15 |
16 | 17 | ## Author 18 | You can get in touch with me on my LinkedIn Profile: [![LinkedIn Link](https://img.shields.io/badge/Connect-harismuneer-blue.svg?logo=linkedin&longCache=true&style=social&label=Follow)](https://www.linkedin.com/in/harismuneer) 19 | 20 | You can also follow my GitHub Profile to stay updated about my latest projects: [![GitHub Follow](https://img.shields.io/badge/Connect-harismuneer-blue.svg?logo=Github&longCache=true&style=social&label=Follow)](https://github.com/harismuneer) 21 | 22 | If you liked the repo then kindly support it by giving it a star ⭐ and share in your circles so more people can benefit from the effort. 23 | 24 | ## Contributions Welcome 25 | [![GitHub Issues](https://img.shields.io/github/issues/harismuneer/Queue-and-Stack.svg?style=flat&label=Issues&maxAge=2592000)](https://www.github.com/harismuneer/Queue-and-Stack/issues) 26 | 27 | If you find any bugs, have suggestions, or face issues: 28 | 29 | - Open an Issue in the Issues Tab to discuss them. 30 | - Submit a Pull Request to propose fixes or improvements. 31 | - Review Pull Requests from other contributors to help maintain the project's quality and progress. 32 | 33 | This project thrives on community collaboration! Members are encouraged to take the initiative, support one another, and actively engage in all aspects of the project. Whether it’s debugging, fixing issues, or brainstorming new ideas, your contributions are what keep this project moving forward. 34 | 35 | With modern AI tools like ChatGPT, solving challenges and contributing effectively is easier than ever. Let’s work together to make this project the best it can be! 🚀 36 | 37 | ## License 38 | [![MIT](https://img.shields.io/cocoapods/l/AFNetworking.svg?style=style&label=License&maxAge=2592000)](../master/LICENSE) 39 | 40 | Copyright (c) 2018-present, harismuneer 41 | 42 | 43 | 44 |
45 | 46 |

Waving hand 47 | Hey there, I'm Haris Muneer 👨🏻‍💻 48 |

49 | 50 | 51 | Total Github Stars 52 | Total Github Followers 53 | 54 |
55 | 56 | - 🛠️ Product Builder: Agile Product Manager with 5+ years of hands-on experience delivering SaaS solutions across sales, recruiting, AI, social media, and public sector domains. Background in Computer Science, with a proven track record of scaling products from inception to $XXM+ ARR, launching 3 top-ranking tools on Product Hunt, and developing solutions adopted by 250+ B2B clients in 40+ countries. 57 | 58 | - 🌟 Open Source Advocate: Passionate about making technology accessible, I’ve developed and open-sourced several software projects for web, mobile, desktop, and AI on my GitHub profile. These projects have been used by thousands of learners worldwide to enhance their skills and knowledge. 59 | 60 | - 📫 How to Reach Me: To learn more about my skills and work, visit my LinkedIn profile. For collaboration or inquiries, feel free to reach out via email. 61 | 62 |
63 | 64 |

🤝 Follow my journey

65 |

66 | 67 | 68 | 69 | 70 |

71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | --------------------------------------------------------------------------------