Skip to content

Commit ae1f7ac

Browse files
committed
Initial Commit
0 parents  commit ae1f7ac

File tree

5 files changed

+402
-0
lines changed

5 files changed

+402
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Praharsh Jain
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.

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Data Structures
2+
==========
3+
4+
This repository contains my implementation of common data structures in C++.
5+
6+
Will be updated periodically. You are free to use these codes wherever you want without permission. This comes without any warranties though!
7+
8+
Pull requests
9+
----------------
10+
If you spot any errors in the implementation(s), please let me know by submitting a relevant pull request.
11+
12+
License
13+
----------------
14+
MIT

linked_list.cpp

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#include <iostream>
2+
using namespace std;
3+
//generic implementation of Singly Linked List using templates
4+
template <class T>
5+
struct node
6+
{
7+
T data;
8+
struct node *next;
9+
};
10+
template <class T>
11+
class LinkedList
12+
{
13+
private:
14+
int size;
15+
struct node<T> *start;
16+
17+
public:
18+
LinkedList()
19+
{
20+
size = 0;
21+
start = NULL;
22+
}
23+
struct node<T> *create_node(T value)
24+
{
25+
struct node<T> *temp = new (struct node<T>);
26+
temp->next = NULL;
27+
temp->data = value;
28+
return temp;
29+
}
30+
// functions to insert values
31+
void
32+
insert_begin(T value)
33+
{
34+
struct node<T> *temp = create_node(value);
35+
if (start != NULL)
36+
{
37+
temp->next = start;
38+
}
39+
start = temp;
40+
size++;
41+
}
42+
void insert(T value)
43+
{
44+
struct node<T> *temp = start;
45+
if (temp != NULL)
46+
{
47+
while (temp->next != NULL)
48+
temp = temp->next;
49+
temp->next = create_node(value);
50+
size++;
51+
}
52+
else
53+
{
54+
insert_begin(value);
55+
}
56+
}
57+
void insert_pos(T value, int pos)
58+
{
59+
if (pos == 1)
60+
{
61+
insert_begin(value);
62+
}
63+
else if (pos == size + 1)
64+
{
65+
insert(value);
66+
}
67+
else if (pos <= size)
68+
{
69+
struct node<T> *curr = create_node(value), *temp = start;
70+
for (int i = 1; i < pos - 1; i++)
71+
{
72+
temp = temp->next;
73+
}
74+
curr->next = temp->next;
75+
temp->next = curr;
76+
size++;
77+
}
78+
else
79+
{
80+
cout << "\nInsertion at position " << pos << " is not possible\n";
81+
}
82+
}
83+
// search the list
84+
int search(T value)
85+
{
86+
struct node<T> *temp = start;
87+
for (int pos = 1; temp != NULL; pos++)
88+
{
89+
if (temp->data == value)
90+
{
91+
return pos;
92+
}
93+
temp = temp->next;
94+
}
95+
return -1;
96+
}
97+
// delete from list
98+
void remove_pos(int pos)
99+
{
100+
if (pos > 1 && pos <= size)
101+
{
102+
struct node<T> *curr = NULL, *temp = start;
103+
for (int i = 1; i < pos - 1; i++)
104+
{
105+
temp = temp->next;
106+
}
107+
curr = temp->next;
108+
temp->next = curr->next;
109+
delete curr;
110+
size--;
111+
}
112+
else if (pos == 1 && size >= pos)
113+
{
114+
struct node<T> *temp = start->next;
115+
delete start;
116+
start = temp;
117+
size--;
118+
}
119+
else
120+
{
121+
cout << "\nNo such entry in list\n";
122+
}
123+
}
124+
void remove(T value)
125+
{
126+
remove_pos(search(value));
127+
}
128+
void reverse()
129+
{
130+
if (start != NULL && start->next != NULL)
131+
{
132+
struct node<T> *p1 = NULL, *curr = start, *p2 = start->next;
133+
while (p2 != NULL)
134+
{
135+
curr->next = p1;
136+
p1 = curr;
137+
curr = p2;
138+
p2 = p2->next;
139+
}
140+
curr->next=p1;
141+
start = curr;
142+
}
143+
}
144+
void sort()
145+
{
146+
T x;
147+
struct node<T>* p1=NULL,*p2=NULL,*last=NULL;
148+
if(start!=NULL && start->next!=NULL)
149+
{
150+
for(p1=start;p1!=NULL;p1=p1->next)
151+
{
152+
for(p2=start;p2->next!=last;p2=p2->next)
153+
{
154+
if(p2->data > p1->data)
155+
{
156+
x=p1->data;
157+
p1->data=p2->data;
158+
p2->data=x;
159+
}
160+
if(p2->next==last)
161+
{ last=p2;
162+
}
163+
}
164+
}
165+
166+
}
167+
}
168+
// getters
169+
int getSize()
170+
{
171+
return size;
172+
}
173+
struct node<T> *getStart()
174+
{
175+
return start;
176+
}
177+
};
178+
179+
int main()
180+
{
181+
LinkedList<int> ll;
182+
ll.insert_begin(5);
183+
ll.insert_begin(6);
184+
ll.insert(78);
185+
ll.insert_pos(54, 4);
186+
ll.insert_pos(8, 4);
187+
ll.insert_pos(7, 1);
188+
ll.insert_pos(8888, 12);
189+
//7 6 5 78 8 54
190+
struct node<int> *temp = ll.getStart();
191+
for (; temp != NULL; temp = temp->next)
192+
cout << temp->data << "\n";
193+
cout << "\nsize=" << ll.getSize() << "\n";
194+
cout << "\n78 found at position: " << ll.search(78) << "\n";
195+
ll.remove(78);
196+
ll.remove_pos(5);
197+
ll.remove_pos(1);
198+
ll.remove_pos(10);
199+
//6 5 8
200+
temp = ll.getStart();
201+
for (; temp != NULL; temp = temp->next)
202+
cout << temp->data << "\n";
203+
cout << "\nsize=" << ll.getSize() << "\n";
204+
ll.reverse();
205+
temp = ll.getStart();
206+
for (; temp != NULL; temp = temp->next)
207+
cout << temp->data << "\n";
208+
cout << "\nsize=" << ll.getSize() << "\n";
209+
ll.insert(-1);
210+
ll.insert(34);
211+
ll.sort();
212+
//-1 5 6 8 34
213+
temp = ll.getStart();
214+
for (; temp != NULL; temp = temp->next)
215+
cout << temp->data << "\n";
216+
cout << "\nsize=" << ll.getSize() << "\n";
217+
return 0;
218+
}

queue.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
using namespace std;
4+
const int MAX_SIZE = 100;
5+
//generic implementation of standard Queue data structure using templates
6+
template <class T>
7+
class Queue
8+
{
9+
private:
10+
T data[MAX_SIZE];
11+
int front;
12+
int rear;
13+
14+
public:
15+
Queue()
16+
{
17+
front = 0;
18+
rear = 0;
19+
}
20+
21+
void Enqueue(T element)
22+
{
23+
if (Size() == MAX_SIZE - 1)
24+
cout << "Queue full\n";
25+
data[rear] = element;
26+
rear = ++rear % MAX_SIZE;
27+
}
28+
29+
int Dequeue()
30+
{
31+
if (isEmpty())
32+
cout << "Queue empty\n";
33+
T ret = data[front];
34+
front = ++front % MAX_SIZE;
35+
return ret;
36+
}
37+
38+
int Front()
39+
{
40+
if (isEmpty())
41+
cout << "Queue empty\n";
42+
return data[front];
43+
}
44+
45+
int Size()
46+
{
47+
return abs(rear - front);
48+
}
49+
50+
bool isEmpty()
51+
{
52+
return (front == rear) ? true : false;
53+
}
54+
};
55+
56+
int main()
57+
{
58+
Queue<int> q;
59+
if (q.isEmpty())
60+
{
61+
cout << "Queue is empty" << endl;
62+
}
63+
// Enqueue elements
64+
q.Enqueue(100);
65+
q.Enqueue(200);
66+
q.Enqueue(300);
67+
// Size of queue
68+
cout << "Size of queue = " << q.Size() << endl;
69+
// Front element
70+
cout << q.Front() << endl;
71+
// Dequeue elements
72+
cout << q.Dequeue() << endl;
73+
cout << q.Dequeue() << endl;
74+
cout << q.Dequeue() << endl;
75+
return 0;
76+
}

0 commit comments

Comments
 (0)