Skip to content

Commit dcdcfb4

Browse files
committed
All DSA Problems
0 parents  commit dcdcfb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1896
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Ignore Visual Studio Code specific files
2+
.vscode/
3+
4+
# Ignore compiled executables and object files
5+
*.exe
6+
*.out
7+
*.o
8+
9+
# Ignore temporary files created by VS Code extensions
10+
tempCodeRunnerFile.cpp

Graph/BFS.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
#include<vector>
3+
#include<queue>
4+
using namespace std;
5+
vector<int>bfs_of_graph(vector<vector<int>>& adj, int V){
6+
vector<int> ans;
7+
vector<int> vis(V,0);
8+
queue<int> q;
9+
q.push(0);
10+
vis[0] = 1;
11+
while(!q.empty()){
12+
int curr = q.front();
13+
q.pop();
14+
vis[curr] = 1;
15+
ans.push_back(curr);
16+
for(auto & it : adj[curr]){
17+
if(vis[it] == 0){
18+
vis[it] = 1;
19+
q.push(it);
20+
}
21+
}
22+
}
23+
return ans;
24+
}
25+
int main(){
26+
int V = 5;
27+
vector<vector<int>> adj(V);
28+
adj[0] = {1,2};
29+
adj[1] = {0,3};
30+
adj[2] = {0,4};
31+
adj[3] = {1};
32+
adj[4] = {2};
33+
vector<int> result = bfs_of_graph(adj,V);
34+
cout <<"BFS Traversal: " ;
35+
for(int node : result){
36+
cout << node << " ";
37+
}
38+
cout << endl;
39+
return 0;
40+
}

Graph/DFS.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
void dfs(vector<int>& ans, vector<int>& vis, int node, vector<int> adj[]){
5+
vis[node] = 1;
6+
ans.push_back(node);
7+
for(auto &it : adj[node]){
8+
if(vis[it]){
9+
vis[it] = 1;
10+
dfs(ans,vis, it, adj);
11+
}
12+
}
13+
}
14+
vector<int> dfs_of_graph(int V, vector<int> adj[]){
15+
vector<int> ans;
16+
vector<int> vis(V, 0);
17+
for(int i = 0 ; i < V ; i++){
18+
if(vis[0] == 0){
19+
dfs(ans, vis, i adj);
20+
}
21+
}
22+
return ans;
23+
}
24+
int main() {
25+
int V = 5;
26+
vector<int> adj[V];
27+
adj[0].push_back(1);
28+
adj[0].push_back(2);
29+
adj[1].push_back(0);
30+
adj[1].push_back(4);
31+
adj[2].push_back(0);
32+
adj[2].push_back(3);
33+
adj[3].push_back(2);
34+
adj[4].push_back(1);
35+
vector<int> result = dfs_of_graph(adj, V);
36+
cout << "DFS Traversal: ";
37+
for(auto node : result) {
38+
cout << node << " ";
39+
}
40+
cout << endl;
41+
return 0;
42+
}

Leetcode_Practice_Questions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 27cd74f977317b55a93b5512ee1cb5f3b8492f72

Linked_List

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 99fea4f0db9a702e089a605394af7b8398bde0e2

README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<<<<<<< HEAD
2+
# C++ Data Structures & Algorithms (DSA) + LeetCode Solutions
3+
4+
![Language](https://img.shields.io/badge/Language-C%2B%2B-blue.svg)
5+
![License](https://img.shields.io/badge/License-MIT-green.svg)
6+
![Repo Status](https://img.shields.io/badge/Status-Active-brightgreen.svg)
7+
8+
Welcome to **C++ DSA & LeetCode Solutions**—your all-in-one resource for mastering data structures, algorithms and acing coding interviews!
9+
Welcome to **C++ DSA & LeetCode Solutions**—your all-in-one resource for mastering data structures, algorithms and acing coding interviews!
10+
This repository features clean, well-documented C++ implementations covering foundational CS concepts *and* a growing library of LeetCode problem solutions.
11+
12+
---
13+
14+
## 🚩 Features
15+
16+
- **Core DSA modules:** Binary search, sorting, arrays, and more.
17+
- **Extensive LeetCode practice:** Solutions mapped by problem number, ideal for interviews and self-study.
18+
- **Clear folder structure:** Easily find what you need, whether it’s theory or practice.
19+
- **Beginner-friendly:** Accessible explanations and simple build instructions.
20+
21+
---
22+
23+
## 📁 Repository Structure
24+
25+
DSA/
26+
├── Leetcode_Practice_Questions/ # LeetCode solutions
27+
│ ├── LC_1.cpp
28+
│ ├── LC_2.cpp
29+
│ └── README.md
30+
├── binarysearch.cpp # Core algorithms/data structures
31+
├── sort_0_&_1.cpp
32+
├── ...
33+
└── README.md # Start here!
34+
35+
```
36+
cppdsa/
37+
├── Leetcode_Practice_Questions/ # LeetCode solutions
38+
│ ├── LC_1.cpp
39+
│ ├── LC_2.cpp
40+
│ └── ...
41+
├── binarysearch.cpp # Core algorithms/data structures
42+
├── sort_0_&_1.cpp
43+
└── README.md # Start here!
44+
```
45+
46+
### Highlights
47+
48+
- **Leetcode_Practice_Questions**:
49+
- **Leetcode_Practice_Questions**:
50+
Organized, numbered solutions with a focused [README](./Leetcode_Practice_Questions/README.md).
51+
52+
- **Core DSA files**:
53+
Example:
54+
- `binarysearch.cpp`: Classic binary search implementation
55+
- `sort_0_&_1.cpp`: Fast 0/1 segregation in arrays
56+
- **Core DSA files**:
57+
Example:
58+
- `binarysearch.cpp`: Classic binary search implementation
59+
- `sort_0_&_1.cpp`: Fast 0/1 segregation in arrays
60+
- (More coming soon!)
61+
62+
---
63+
64+
## 🧑‍💻 Usage & Getting Started
65+
66+
1. **Clone the Repository**
67+
git clone https://github.com/alisamad1/DSA.git
68+
```bash
69+
git clone https://github.com/your-username/your-repo-name.git
70+
```
71+
2. **Enter the Directory**
72+
cd DSA
73+
```bash
74+
cd cppdsa
75+
```
76+
3. **Compile & Run Any File**
77+
- With `g++` (example for binarysearch):
78+
```
79+
g++ binarysearch.cpp -o binarysearch
80+
./binarysearch
81+
```
82+
```bash
83+
g++ binarysearch.cpp -o binarysearch
84+
./binarysearch
85+
```
86+
- Or open files using your favorite C++ IDE (e.g., VS Code, CLion).
87+
88+
---
89+
90+
## ✍️ Contributing
91+
92+
Contributions, bug reports, and suggestions are welcome!
93+
Whether you spot a typo, want to add a new algorithm, or have ideas for improvement, just open an issue or pull request.
94+
Contributions, bug reports, and suggestions are welcome!
95+
Whether you spot a typo, want to add a new algorithm, or have ideas for improvement, just open an issue or pull request on the new repository.
96+
97+
---
98+
99+
## 💡 Why Use This Repository?
100+
101+
- **Exam & Interview Prep:** Spot-on for students and job-seekers.
102+
- **Learning by Examples:** Study code, understand logic, and solve problems hands-on.
103+
- **Open Source:** Freely use and extend under the MIT License.
104+
105+
---
106+
107+
## 📫 Contact
108+
109+
Questions? Ideas?
110+
Questions? Ideas?
111+
Let’s connect!
112+
113+
- **LinkedIn:** Ali Us Samad
114+
- **LinkedIn:** Ali Us Samad
115+
- **Email:** aliussamad@gmail.com
116+
117+
---
118+
119+
## ⭐ Support
120+
121+
If this project helps you, please ⭐️ this repo and share it to support more learners!

Recursion

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 73b3106f285e245aad37f9fde24ff69e1e8aced2

Stack&Queues/Problem_1.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//Implementation of Stack Using Linked List in C++
2+
#include<iostream>
3+
using namespace std;
4+
struct Node{
5+
int data;
6+
Node* next;
7+
}
8+
Node * top;
9+
void push(int data){
10+
Node * temp = new Node();
11+
if(!temp){
12+
cout<<"Stack Overflow"<<endl;
13+
}
14+
temp -> data = data;
15+
temp -> next = top;
16+
temp = top;
17+
}
18+
int isEmpty(){
19+
return top == NULL;
20+
}
21+
int peek(){
22+
if(!isEmpty()){
23+
return top->data;
24+
}
25+
else{
26+
exit(1);
27+
}
28+
}
29+
void pop(){
30+
Node * temp;
31+
if(top == NULL){
32+
cout<<"Stack Underflow"<<endl;
33+
exit(1);
34+
}
35+
else{
36+
temp = top;
37+
top = top->next;
38+
free(top);
39+
}
40+
}

Stack&Queues/Problem_2.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Implementation of Queues using Linked List
2+
#include<iostream>
3+
using namespace std;
4+
class Node{
5+
// node class for queue
6+
public:
7+
int data;
8+
Node* next;
9+
//constructor
10+
Node(int data){
11+
this->data = data;
12+
this->next = NULL;
13+
}
14+
};
15+
class Queue{
16+
private:
17+
Node *rear,*front;
18+
public:
19+
Queue(){
20+
front = rear = NULL;
21+
}
22+
void Enqueue(int x){
23+
Node* temp = new Node(x);
24+
if(rear == NULL){
25+
front = rear = temp;
26+
return;
27+
}
28+
rear->next = temp;
29+
rear = temp;
30+
}
31+
void deQueue(){
32+
if(front == NULL){
33+
cout<<"Queue is Empty"<<endl;
34+
return;
35+
}
36+
Node* OldFront = front;
37+
Node = front->next;
38+
if(front == NULL){
39+
rear = NULL;
40+
}
41+
delete OldFront;
42+
}
43+
void display(){
44+
if(front == NULL){
45+
cout<<"queue is empty"<<endl;
46+
return;
47+
}
48+
Node* temp = front;
49+
while(temp!=NULL){
50+
cout<<temp->data<<" ";
51+
temp = temp->next;
52+
}
53+
cout<<endl;
54+
}
55+
};
56+
int main(){
57+
Queue q;
58+
q.Enqueue(10);
59+
q.Enqueue(20);
60+
q.Enqueue(30);
61+
cout<<"Queue after EnQueue operations: "<<endl;
62+
q.display();
63+
q.deQueue();
64+
q.display();
65+
return 0;
66+
}

Stack&Queues/Problem_3.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//implentation of stack using queue
2+
#include<iostream>
3+
#include<queue>
4+
using namespace std;
5+
class MyStack {
6+
queue<int>q;
7+
public:
8+
void push(int x){
9+
q.push(x);
10+
int n = q.size();
11+
for(int i = 0; i<n-1; i++){
12+
int value = q.front();
13+
q.pop();
14+
q.push(value);
15+
}
16+
}
17+
int pop(){
18+
if(q.empty()){
19+
cout<<"stack underflow. No elements to pop out"<<endl;
20+
return -1;
21+
}
22+
int value = q.front();
23+
q.pop();
24+
return value;
25+
}
26+
int top(){
27+
if(q.empty()){
28+
cout<<"Stack Underflow. No elements to pop out"<<endl;
29+
return -1;
30+
}
31+
return q.front();
32+
}
33+
int size(){
34+
return q.size();
35+
}
36+
bool isEmpty(){
37+
return q.empty();
38+
}
39+
};
40+
int main(){
41+
MyStack s;
42+
s.push(10);
43+
s.push(20);
44+
s.push(30);
45+
cout<<"Stack top "<<s.top()<<endl;
46+
cout<<"Stack Size "<<s.size()<<endl;
47+
cout<<"Popped Element "<<s.pop()<<endl;
48+
cout<<"Stack top after popping element "<<s.top()<<endl;
49+
return 0;
50+
}

0 commit comments

Comments
 (0)