Skip to content

Commit fbbd446

Browse files
author
Bhrigu Kansra
authored
Merge branch 'master' into master
2 parents 90744a0 + 71001bf commit fbbd446

19 files changed

+985
-21
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#Defined Node
2+
class ListNode:
3+
def __init__(self, data):
4+
"constructor class to initiate this object"
5+
6+
# store data
7+
self.data = data
8+
9+
# store reference (next item)
10+
self.next = None
11+
12+
# store reference (previous item)
13+
self.previous = None
14+
return
15+
16+
def has_value(self, value):
17+
"method to compare the value with the node data"
18+
if self.data == value:
19+
return True
20+
else:
21+
return False
22+
23+
#Doubly Linked List
24+
25+
class DoubleLinkedList:
26+
def __init__(self):
27+
"constructor to initiate this object"
28+
29+
self.head = None
30+
self.tail = None
31+
return
32+
33+
def list_length(self):
34+
"returns the number of list items"
35+
36+
count = 0
37+
current_node = self.head
38+
39+
while current_node is not None:
40+
# increase counter by one
41+
count = count + 1
42+
43+
# jump to the linked node
44+
current_node = current_node.next
45+
46+
return count
47+
48+
def output_list(self):
49+
"outputs the list (the value of the node, actually)"
50+
current_node = self.head
51+
52+
while current_node is not None:
53+
print(current_node.data)
54+
55+
56+
current_node = current_node.next
57+
58+
return
59+
60+
def unordered_search (self, value):
61+
"search the linked list for the node that has this value"
62+
63+
64+
current_node = self.head
65+
66+
67+
node_id = 1
68+
69+
70+
results = []
71+
72+
while current_node is not None:
73+
if current_node.has_value(value):
74+
results.append(node_id)
75+
76+
77+
current_node = current_node.next
78+
node_id = node_id + 1
79+
80+
return results
81+
82+
#Adding nodes
83+
84+
def add_list_item(self, item):
85+
"add an item at the end of the list"
86+
87+
if isinstance(item, ListNode):
88+
if self.head is None:
89+
self.head = item
90+
item.previous = None
91+
item.next = None
92+
self.tail = item
93+
else:
94+
self.tail.next = item
95+
item.previous = self.tail
96+
self.tail = item
97+
98+
return
99+
100+
#Removing Nodes
101+
102+
def remove_list_item_by_id(self, item_id):
103+
"remove the list item with the item id"
104+
105+
current_id = 1
106+
current_node = self.head
107+
108+
while current_node is not None:
109+
previous_node = current_node.previous
110+
next_node = current_node.next
111+
112+
if current_id == item_id:
113+
# if this is the first node (head)
114+
if previous_node is not None:
115+
previous_node.next = next_node
116+
if next_node is not None:
117+
next_node.previous = previous_node
118+
else:
119+
self.head = next_node
120+
if next_node is not None:
121+
next_node.previous = None
122+
# we don't have to look any further
123+
return
124+
125+
# needed for the next iteration
126+
current_node = next_node
127+
current_id = current_id + 1
128+
129+
return
130+
131+

Data Structures/StackUsingArray.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
template <typename T>
2+
class Stack {
3+
T *data;
4+
int nextIndex;
5+
int totalSize;
6+
7+
public :
8+
9+
Stack(int size) {
10+
data = new T[size];
11+
nextIndex = 0;
12+
totalSize = size;
13+
}
14+
15+
~Stack() {
16+
delete [] data;
17+
}
18+
19+
int getSize() {
20+
return nextIndex;
21+
}
22+
23+
bool isEmpty() {
24+
if(nextIndex == 0)
25+
return true;
26+
else
27+
return false;
28+
}
29+
30+
T top() {
31+
if(isEmpty()) {
32+
cout << "Stack empty ! " << endl;
33+
return -1;
34+
}
35+
return data[nextIndex - 1];
36+
}
37+
38+
void push(T element) {
39+
if(totalSize == nextIndex) {
40+
T *temp = new T[2 * totalSize];
41+
for(int i = 0; i < totalSize; i++) {
42+
temp[i] = data[i];
43+
}
44+
totalSize *= 2;
45+
delete [] data;
46+
data = temp;
47+
}
48+
data[nextIndex] = element;
49+
nextIndex++;
50+
}
51+
52+
T pop() {
53+
if(isEmpty()) {
54+
cout << "Stack empty ! " << endl;
55+
return -1;
56+
}
57+
nextIndex--;
58+
return data[nextIndex];
59+
}
60+
61+
};

contributors.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
99
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
1010
8. [Joy Banerjee](https://github.com/joybanerjee08)
11-
9. [Ananya Keshari](https://github.com/ana301)
11+
9. [Nikhil Arora](https://github.com/nikhilarora068)
12+
10. [Ambika](https://github.com/Ambika55)
13+
11. [Kartikey Tripathi](https://github.com/kartikeytripathi)
14+
12. [Ananya Keshari](https://github.com/ana301)

knight-tour.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include<bits/stdc++.h>
2+
# define n 8
3+
using namespace std;
4+
bool issafe(int x,int y,int sol[n][n])
5+
{
6+
return (x<n && x>=0 && y<n && y>=0 && sol[x][y]==-1);
7+
8+
}
9+
bool solve(int x,int y, int mov, int sol[n][n], int xmov[n], int ymov[n])
10+
{
11+
int k,xnext,ynext;
12+
13+
if(mov == n*n)
14+
return true;
15+
16+
for(k=0;k<8;k++)
17+
{
18+
xnext=x+xmov[k];
19+
ynext=y+ymov[k];
20+
21+
if(issafe(xnext,ynext,sol))
22+
{
23+
sol[xnext][ynext]=mov;
24+
25+
if(solve(xnext,ynext,mov+1,sol,xmov,ymov)==true)
26+
return true;
27+
else
28+
sol[xnext][ynext]=-1;
29+
}
30+
}
31+
return false;
32+
}
33+
int main()
34+
{
35+
//initialize();
36+
37+
int sol[n][n];
38+
int i,j;
39+
for(i=0;i<n;i++)
40+
for(j=0;j<n;j++)
41+
sol[i][j]=-1;
42+
43+
int xmov[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
44+
int ymov[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
45+
sol[0][0]=0;
46+
47+
bool flag=solve(0,0,1,sol,xmov,ymov);
48+
if(flag==false)
49+
cout<<"solution doesnot exist \n";
50+
else
51+
{
52+
for(i=0;i<n;i++)
53+
{
54+
for(j=0;j<n;j++)
55+
cout<<sol[i][j]<<" ";
56+
cout<<"\n";
57+
}
58+
}
59+
}

n-queen.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include<bits/stdc++.h>
2+
#define n 4
3+
using namespace std;
4+
bool issafe(int board[n][n], int row, int col)
5+
{
6+
int i,j;
7+
for(i=0;i<n;i++)
8+
if(board[row][i])
9+
return false;
10+
for(i=row,j=col;i>=0 && j>=0;i--,j--)
11+
if(board[i][j])
12+
return false;
13+
for(i=row,j=col;i<n && j>=0; i++,j--)
14+
if(board[i][j])
15+
return false;
16+
17+
return true;
18+
}
19+
bool solve(int board[n][n], int col)
20+
{
21+
if(col>=n)
22+
return true;
23+
int i;
24+
for(i=0;i<n;i++)
25+
{
26+
if(issafe(board, i ,col))
27+
{
28+
board[i][col]=1;
29+
30+
if(solve(board, col+1))
31+
return true;
32+
33+
board[i][col]=0;
34+
}
35+
}
36+
return false;
37+
}
38+
int main()
39+
{
40+
int board[n][n]={{0,0,0,0},
41+
{0,0,0,0},
42+
{0,0,0,0},
43+
{0,0,0,0}};
44+
bool flag;
45+
int i,j;
46+
flag=solve(board,0);
47+
if(flag==false)
48+
cout<<"solution doesnot exist \n";
49+
else
50+
{
51+
for(i=0;i<n;i++)
52+
{
53+
for(j=0;j<n;j++)
54+
cout<<board[i][j]<<" ";
55+
cout<<"\n";
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)