├── .DS_Store ├── DSAwithCpp ├── .DS_Store ├── CircularLinkedList.cpp ├── Circularqueue.cpp ├── DijklatrasAlgo.cpp ├── DoublyCircularLinkedList.cpp ├── DoublyLinkedList.cpp ├── DynamicQueue.cpp ├── DynamicStack.cpp ├── List.cpp ├── MinSpanTree.cpp ├── PostFixEvaluation.cpp ├── PostfixGenerator.cpp ├── PrefixGenerator.cpp ├── Prorityqueue.cpp ├── Queue.cpp └── Stack.cpp └── DSAwithJava ├── .DS_Store ├── .idea ├── .gitignore ├── misc.xml ├── modules.xml └── vcs.xml ├── Algos ├── BinarySearch.java ├── CeaserCipher.java ├── MinMax.java └── VignereCipher.java ├── BinaryTreeStructure ├── BinaryNode.java └── BinaryTree.java ├── DSAwithJava.iml ├── LinkList ├── .DS_Store ├── ArrayList.java ├── DoublyLinkedList.java ├── LinkedList.java ├── LinkedListTests.java ├── List.java ├── ListNode.java └── Test.java ├── LinkedQueue ├── .DS_Store ├── ArrayQueue.java ├── CircularQueue.java ├── LinkedList.java ├── Node.java ├── PriorityLinked.java ├── PriorityQueue.java └── Queue.java ├── LinkedStack ├── .DS_Store ├── ArrayStack.java ├── LinkedList.java ├── ListNode.java └── Stack.java └── out └── production └── DSAwithJava ├── .idea ├── .gitignore ├── misc.xml ├── modules.xml └── vcs.xml ├── Algos ├── BinarySearch.class ├── CeaserCipher.class ├── MinMax.class └── VignereCipher.class ├── BinaryTreeStructure ├── BinaryNode.class └── BinaryTree.class ├── DSAwithJava.iml ├── LinkList ├── ArrayList.class ├── DoublyLinkedList.class ├── LinkedList.class ├── LinkedListTests.class ├── List.class ├── ListNode.class └── Test.class ├── LinkedQueue ├── ArrayQueue.class ├── CircularQueue.class ├── LinkedList.class ├── Node.class ├── PriorityLinked.class ├── PriorityQueue.class └── Queue.class └── LinkedStack ├── ArrayStack.class ├── LinkedList.class ├── ListNode.class └── Stack.class /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/.DS_Store -------------------------------------------------------------------------------- /DSAwithCpp/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithCpp/.DS_Store -------------------------------------------------------------------------------- /DSAwithCpp/CircularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template 5 | class queue{ 6 | 7 | private: 8 | class node{ 9 | public: 10 | k data; 11 | node *next; 12 | 13 | node(){ 14 | this->next=NULL; 15 | } 16 | 17 | node(k ta){ 18 | this->data=ta; 19 | this->next=NULL; 20 | } 21 | }; 22 | node *head; 23 | node *tail; 24 | 25 | public: 26 | 27 | queue(){ 28 | this->head=NULL; 29 | this->tail=NULL; 30 | } 31 | 32 | bool IsEmpty(){ 33 | return head==NULL; 34 | } 35 | 36 | 37 | void Enqueue(k datum){ 38 | node *newnode = new node(datum); 39 | if(IsEmpty()){ 40 | head=tail=newnode; 41 | }else{ 42 | tail->next=newnode; 43 | tail=newnode; 44 | } 45 | tail->next=head; 46 | } 47 | 48 | 49 | k getdata(){ 50 | k d; 51 | cout<<"Enter data : "<>d; 53 | return d; 54 | 55 | } 56 | 57 | k dequeue(){ 58 | if(IsEmpty()){ 59 | throw std::underflow_error("Queue is Empty"); 60 | }else { 61 | node *todel=head; 62 | k ret = head->data; 63 | 64 | if(head==tail){ 65 | head=tail=NULL; 66 | }else{ 67 | head=head->next; 68 | tail->next=head; 69 | } 70 | delete todel; 71 | return ret; 72 | } 73 | } 74 | 75 | 76 | void makeempty(){ 77 | while(!IsEmpty()){ 78 | node *todel=head; 79 | head=head->next; 80 | delete todel; 81 | } 82 | } 83 | 84 | void display(){ 85 | if(IsEmpty()){ 86 | cout<<"The Queue is empty"<data<<" "; 91 | temp=temp->next; 92 | }while(temp!=head); 93 | cout< stack; 100 | 101 | cout<<"Menu"<>n; 112 | switch ((n)) 113 | { 114 | case 1: 115 | if(stack.IsEmpty()){ 116 | cout<<"Stack is empty"< 2 | using namespace std; 3 | #define max 10 4 | 5 | class Queue{ 6 | private: 7 | 8 | int data[max]; 9 | int rear; 10 | int front; 11 | 12 | 13 | public: 14 | Queue(){ 15 | this->rear=-1; 16 | this->front=-1; 17 | } 18 | protected: 19 | void makeEmpty(){ 20 | rear=-1; 21 | front=-1; 22 | } 23 | 24 | bool checkEmpty(){ 25 | return rear==-1&&front==-1; 26 | } 27 | 28 | bool IsFull(){ 29 | return (rear%max)==((front-1)%max); 30 | } 31 | 32 | void Enqueue(int val){ 33 | if(!IsFull()){ 34 | if(checkEmpty()){ 35 | ++front; 36 | } 37 | ++rear; 38 | data[rear%max]=val; 39 | } 40 | } 41 | 42 | int Dequeue(){ 43 | int val=0; 44 | if(!checkEmpty()){ 45 | val=data[front%max]; 46 | if(front%max==(rear-1)%max){ 47 | makeEmpty(); 48 | } 49 | else{ 50 | ++front; 51 | } 52 | } 53 | return val; 54 | } 55 | 56 | 57 | void traverse(){ 58 | if(!checkEmpty()){ 59 | cout<<"The data in the Queue are : "; 60 | for(int i=front;i<=rear;++i){ 61 | cout<>n; 83 | switch (n) 84 | { 85 | case 1: 86 | makeEmpty(); 87 | break; 88 | 89 | 90 | case 2: 91 | if(checkEmpty()){ 92 | cout<<"The List is empty"<>k; 113 | Enqueue(k); 114 | break; 115 | 116 | case 5: 117 | 118 | cout< 2 | #include 3 | 4 | #define V 9 5 | 6 | int minDistance(int dist[], bool sptSet[]) 7 | { 8 | int min = INT_MAX, min_index; 9 | 10 | for (int v = 0; v < V; v++) 11 | if (sptSet[v] == false && dist[v] <= min) 12 | min = dist[v], min_index = v; 13 | 14 | return min_index; 15 | } 16 | 17 | void printSolution(int dist[], int n) 18 | { 19 | printf("Vertex Distance from Source\n"); 20 | for (int i = 0; i < V; i++) 21 | printf("\t%d \t\t\t\t %d\n", i, dist[i]); 22 | } 23 | 24 | void dijkstra(int graph[V][V], int src) 25 | { 26 | int dist[V]; 27 | 28 | 29 | bool sptSet[V]; 30 | 31 | for (int i = 0; i < V; i++) 32 | dist[i] = INT_MAX, sptSet[i] = false; 33 | 34 | dist[src] = 0; 35 | 36 | for (int count = 0; count < V - 1; count++) { 37 | int u = minDistance(dist, sptSet); 38 | sptSet[u] = true; 39 | for (int v = 0; v < V; v++) 40 | if (!sptSet[v] && graph[u][v] 41 | && dist[u] != INT_MAX 42 | && dist[u] + graph[u][v] < dist[v]) 43 | dist[v] = dist[u] + graph[u][v]; 44 | } 45 | printSolution(dist, V); 46 | } 47 | 48 | int main() 49 | { 50 | int graph[V][V] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, 51 | { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, 52 | { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, 53 | { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, 54 | { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, 55 | { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, 56 | { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, 57 | { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, 58 | { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; 59 | 60 | dijkstra(graph, 0); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /DSAwithCpp/DoublyCircularLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class linkedlist{ 5 | private: 6 | class node{ 7 | public: 8 | int data; 9 | node *next; 10 | node *prev; 11 | 12 | node(){ 13 | this->next=NULL; 14 | this->prev=NULL; 15 | } 16 | 17 | node(int d){ 18 | this->data=d; 19 | this->next=NULL; 20 | this->prev=NULL; 21 | } 22 | }; 23 | 24 | 25 | node *head; 26 | node *tail; 27 | 28 | public: 29 | linkedlist(){ 30 | this->head=NULL; 31 | this->tail=NULL; 32 | } 33 | 34 | private: 35 | int getdata(){ 36 | int a; 37 | cout<<"Enter data : "; 38 | cin>>a; 39 | return a; 40 | } 41 | 42 | int getpos(){ 43 | int po; 44 | cout<<"Enter the position : "; 45 | cin>>po; 46 | return po; 47 | } 48 | 49 | bool IsEmpty(){ 50 | return head==NULL; 51 | } 52 | 53 | bool IsOne(){ 54 | return head->next==head; 55 | } 56 | 57 | public: 58 | void insertop(){ 59 | node *newnode=new node(getdata()); 60 | if(IsEmpty()){ 61 | head=tail=newnode; 62 | head->next=tail; 63 | tail->prev=head; 64 | }else{ 65 | newnode->next=head; 66 | newnode->prev=tail; 67 | tail->next=newnode; 68 | head->prev=newnode; 69 | head=newnode; 70 | } 71 | } 72 | 73 | 74 | void insertat(){ 75 | int pos = getpos(); 76 | if(pos<1){ 77 | cout<<"Error"<next; 84 | if(temp==head){ 85 | cout<<"Error"<next==head){ 90 | insertend(); 91 | }else{ 92 | node *newnode = new node (getdata()); 93 | newnode-> next=temp->next; 94 | newnode->prev=temp; 95 | temp->next=newnode; 96 | newnode->next->prev=newnode; 97 | } 98 | } 99 | } 100 | 101 | void insertend(){ 102 | node *newnode = new node(getdata()); 103 | if(IsEmpty()){ 104 | head=tail=newnode; 105 | head->next=tail; 106 | tail->next=head; 107 | }else{ 108 | newnode->prev=tail; 109 | newnode->next=head; 110 | tail->next=newnode; 111 | head->prev=newnode; 112 | tail=newnode; 113 | } 114 | } 115 | 116 | 117 | 118 | void deletetop(){ 119 | if(IsEmpty()){ 120 | cout<<"Error "<next; 128 | head->prev=tail; 129 | tail->next=head; 130 | } 131 | } 132 | 133 | void deletend(){ 134 | if(IsEmpty()){ 135 | cout<<"Error"<prev; 143 | tail->next=head; 144 | head->prev=tail; 145 | delete todel; 146 | } 147 | } 148 | 149 | 150 | void deleteat(){ 151 | int pos=getpos(); 152 | if(IsEmpty()||pos<1){ 153 | cout<<"Error"<next; 160 | if(temp==head||temp->next==head){ 161 | cout<<"Error"<next==tail){ 166 | deletend(); 167 | }else{ 168 | node *todel = temp->next; 169 | temp->next=todel->next; 170 | todel->next->prev=temp; 171 | delete todel; 172 | } 173 | } 174 | } 175 | 176 | void display(){ 177 | if(IsEmpty()){ 178 | cout<<"Error"<data<<" "; 183 | temp=temp->next; 184 | }while(temp!=head); 185 | cout<data << " "; 196 | temp = temp->prev; 197 | } while(temp != tail); 198 | cout << endl; 199 | } 200 | } 201 | 202 | }; 203 | 204 | int main(){ 205 | linkedlist l; 206 | cout<<"Press 1 to insert top"<>n; 219 | switch(n){ 220 | case 1: 221 | l.insertop(); 222 | break; 223 | 224 | case 2: 225 | l.insertend(); 226 | break; 227 | 228 | case 3: 229 | l.insertat(); 230 | break; 231 | 232 | case 4: 233 | l.deletetop(); 234 | break; 235 | 236 | case 5: 237 | l.deletend(); 238 | break; 239 | 240 | case 6: 241 | l.deleteat(); 242 | break; 243 | 244 | case 7: 245 | l.display(); 246 | break; 247 | 248 | 249 | default: 250 | n=8; 251 | } 252 | }while (n!=8); 253 | 254 | return 0; 255 | } -------------------------------------------------------------------------------- /DSAwithCpp/DoublyLinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class node{ 5 | public : 6 | int data; 7 | node *next; 8 | node *prev; 9 | 10 | node(){ 11 | } 12 | 13 | node(int x) : data(x), next(NULL), prev(NULL) {} 14 | 15 | }; 16 | 17 | class linkedlist{ 18 | private: 19 | node *head; 20 | public: 21 | linkedlist(){ 22 | this->head=NULL; 23 | } 24 | 25 | private: 26 | int getdata(){ 27 | int a; 28 | cout<<"Enter data : "; 29 | cin>>a; 30 | return a; 31 | } 32 | 33 | 34 | bool checkone(){ 35 | return head->next==NULL; 36 | } 37 | 38 | bool checkempty(){ 39 | return head==NULL; 40 | } 41 | 42 | public: 43 | void inserttop(){ 44 | node *newnode=new node(getdata()); 45 | if(!checkempty()){ 46 | newnode->next=head; 47 | head->prev=newnode; 48 | } 49 | head=newnode; 50 | } 51 | 52 | void insertend(){ 53 | node *newnode=new node(getdata()); 54 | if(checkempty()){ 55 | head=newnode; 56 | }else{ 57 | node *temp=head; 58 | while(temp->next!=NULL){ 59 | temp=temp->next; 60 | } 61 | temp->next=newnode; 62 | newnode->prev=temp; 63 | } 64 | } 65 | 66 | void deletetop(){ 67 | if(!checkempty()){ 68 | node *todel=head; 69 | head=head->next; 70 | if(head!=NULL){ 71 | head->prev=NULL; 72 | } 73 | delete todel; 74 | } 75 | } 76 | 77 | void deletend(){ 78 | if(checkempty()){ 79 | cout<<"the list is empty"<next->next!=NULL){ 85 | temp=temp->next; 86 | } 87 | node *todel=temp->next; 88 | delete todel; 89 | temp->next=NULL; 90 | } 91 | } 92 | 93 | void display(){ 94 | node *temp=head; 95 | while(temp!=NULL){ 96 | cout<data<<" "; 97 | temp=temp->next; 98 | } 99 | cout<>pos; 107 | if(pos<1||checkempty()){ 108 | cout<<"Low Indexing Error"<next; 117 | if(temp==NULL||temp->next==NULL){ 118 | cout<<"High Indexing Error"<next->next==NULL){ 123 | deletend(); 124 | } 125 | else{ 126 | node *todel=temp->next; 127 | temp->next=todel->next; 128 | todel->next->prev=temp; 129 | delete todel; 130 | } 131 | 132 | 133 | }} 134 | 135 | void insertat(){ 136 | cout<<"Enter the position to insertat : "; 137 | int pos; 138 | cin>>pos; 139 | if(pos==1){ 140 | inserttop(); 141 | return; 142 | }else if(pos<1||checkempty()){ 143 | cout<<"Low Indexing Error"<next; 149 | if(temp==NULL){ 150 | cout<<"High Indexing Error"<next==NULL){ 155 | insertend(); 156 | }else{ 157 | node *newnode=new node(getdata()); 158 | newnode->next=temp->next; 159 | newnode->prev=temp; 160 | newnode->next->prev=newnode; 161 | temp->next=newnode; 162 | } 163 | } 164 | } 165 | 166 | 167 | }; 168 | 169 | int main(){ 170 | linkedlist l; 171 | cout<<"Press 1 to insert top"<>n; 184 | switch(n){ 185 | case 1: 186 | l.inserttop(); 187 | break; 188 | 189 | case 2: 190 | l.insertend(); 191 | break; 192 | 193 | case 3: 194 | l.insertat(); 195 | break; 196 | 197 | case 4: 198 | l.deletetop(); 199 | break; 200 | 201 | case 5: 202 | l.deletend(); 203 | break; 204 | 205 | case 6: 206 | l.deleteat(); 207 | break; 208 | 209 | case 7: 210 | l.display(); 211 | break; 212 | 213 | 214 | default: 215 | n=8; 216 | } 217 | }while (n!=8); 218 | 219 | return 0; 220 | } -------------------------------------------------------------------------------- /DSAwithCpp/DynamicQueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Queue{ 5 | 6 | private: 7 | class node{ 8 | public: 9 | int data; 10 | node *next; 11 | 12 | node (int value){ 13 | this->data=value; 14 | this->next=NULL; 15 | } 16 | }; 17 | 18 | node *head; 19 | node *tail; 20 | 21 | 22 | public: 23 | Queue(){ 24 | this->head=NULL; 25 | this->tail= NULL; 26 | } 27 | 28 | private: 29 | int getdata(){ 30 | int d; 31 | cout<<"Enter data : "; 32 | cin>>d; 33 | return d; 34 | } 35 | 36 | public: 37 | bool checknull(){ 38 | return head==NULL; 39 | } 40 | bool checkone(){ 41 | return head==head->next; 42 | } 43 | 44 | 45 | public: 46 | void Enqueue(){ 47 | node *newnode = new node(getdata()); 48 | if(checknull()){ 49 | head=tail=newnode; 50 | }else{ 51 | tail->next = newnode; 52 | tail = newnode; 53 | } 54 | tail->next = head; 55 | } 56 | 57 | int Dequeue(){ 58 | if(checknull()){ 59 | throw std::underflow_error("Queue is empty. Pop operation cannot be performed."); 60 | }else{ 61 | node *todel = head; 62 | int toret = head->data; 63 | if(checkone()){ 64 | head=tail=NULL; 65 | }else{ 66 | head=head->next; 67 | tail->next=head; 68 | } 69 | delete todel; 70 | return toret; 71 | } 72 | return 1; 73 | } 74 | 75 | 76 | void display(){ 77 | if(!checknull()){ 78 | node *temp = head; 79 | do{ 80 | cout<data<<" "; 81 | temp=temp->next; 82 | }while(temp!=head); 83 | cout<>n; 101 | switch ((n)) 102 | { 103 | case 1: 104 | if(stack.checknull()){ 105 | cout<<"Queue is empty"< 2 | using namespace std; 3 | 4 | template 5 | class Stack{ 6 | private : 7 | class node { 8 | public: 9 | t data ; 10 | node *next; 11 | 12 | 13 | node(){ 14 | } 15 | 16 | node(t d){ 17 | this->data=d; 18 | } 19 | }; 20 | 21 | node *head; 22 | 23 | 24 | public: 25 | Stack(){ 26 | this->head=NULL; 27 | } 28 | 29 | bool Isempty(){ 30 | return head==NULL; 31 | } 32 | 33 | 34 | void push(){ 35 | node *newnode = new node(getdata()); 36 | if(!Isempty()){ 37 | newnode->next=head; 38 | } 39 | head=newnode; 40 | } 41 | 42 | t pop(){ 43 | if(Isempty()){ 44 | throw std::underflow_error("Stack is empty. Pop operation cannot be performed."); 45 | }else{ 46 | node *todel=head; 47 | head=head->next; 48 | t ret=todel->data; 49 | delete todel; 50 | return ret; 51 | } 52 | } 53 | 54 | void makeempty(){ 55 | while(head!=NULL){ 56 | node *del = head; 57 | head=head->next; 58 | delete del; 59 | } 60 | } 61 | 62 | t getdata(){ 63 | t d; 64 | cout<<"Enter data : "; 65 | cin>>d; 66 | return d; 67 | 68 | } 69 | 70 | void display(){ 71 | if(!Isempty()){ 72 | node *temp = head; 73 | while(temp!=NULL){ 74 | cout<data<<" "; 75 | temp=temp->next; 76 | } 77 | cout< stack; 84 | 85 | cout<<"Menu"<>n; 97 | switch ((n)) 98 | { 99 | case 1: 100 | if(stack.Isempty()){ 101 | cout<<"Stack is empty"< 2 | using namespace std; 3 | #define max 4 4 | 5 | class list{ 6 | int count; 7 | int data[max]; 8 | 9 | public: 10 | list(){ 11 | this->count=-1; 12 | } 13 | 14 | bool checkfull(){ 15 | if(count==max-1){ 16 | cout<<"The data is full"<>data[count]; 38 | } 39 | } 40 | 41 | void deletedata(){ 42 | int pos; 43 | cout<<"Insert the data postion ."<>pos; 45 | if(checkmep()||pos>=max){ 46 | cout<<"Error"<>n; 78 | switch (n) 79 | { 80 | case 1: 81 | l.checkmep(); 82 | break; 83 | 84 | case 2: 85 | l.checkfull(); 86 | break; 87 | 88 | case 3: 89 | l.makeempty(); 90 | break; 91 | 92 | case 4: 93 | l.insert(); 94 | break; 95 | 96 | case 5: 97 | l.deletedata(); 98 | break; 99 | 100 | case 6: 101 | l.display(); 102 | break; 103 | 104 | default : 105 | n=7; 106 | break; 107 | } 108 | }while(n!=7); 109 | return 0; 110 | } -------------------------------------------------------------------------------- /DSAwithCpp/MinSpanTree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | #define V 6 6 | 7 | int minKey(int key[], bool mstSet[]) { 8 | int min = INT_MAX, min_index; 9 | for (int v = 0; v < V; v++) 10 | if (mstSet[v] == false && key[v] < min) 11 | min = key[v], min_index = v; 12 | return min_index; 13 | } 14 | 15 | void printMST(int parent[], int graph[V][V]) { 16 | cout << "Edge \tWeight\n"; 17 | for (int i = 1; i < V; i++) 18 | cout << parent[i] << " - " << i << "\t" << graph[i][parent[i]] << "\n"; 19 | } 20 | 21 | void primMST(int graph[V][V]) { 22 | int parent[V]; 23 | int key[V]; 24 | bool mstSet[V]; 25 | 26 | 27 | for (int i = 0; i < V; i++) 28 | key[i] = INT_MAX, mstSet[i] = false; 29 | 30 | key[0] = 0; 31 | parent[0] = -1; 32 | 33 | for (int count = 0; count < V - 1; count++) { 34 | int u = minKey(key, mstSet); 35 | mstSet[u] = true; 36 | for (int v = 0; v < V; v++) 37 | if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) 38 | parent[v] = u, key[v] = graph[u][v]; 39 | } 40 | 41 | printMST(parent, graph); 42 | } 43 | 44 | int main() { 45 | int graph[V][V] = { 46 | {0, 10, 20, 0, 0, 0}, 47 | {10, 0, 0, 50, 10, 0}, 48 | {20, 0, 0, 20, 33, 0}, 49 | {0, 50, 20, 0, 20, 2}, 50 | {0, 10, 33, 20, 0, 1}, 51 | {0, 0, 0, 2, 1, 0} 52 | }; 53 | primMST(graph); 54 | return 0; 55 | } -------------------------------------------------------------------------------- /DSAwithCpp/PostFixEvaluation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class datain{ 6 | public : 7 | int count,sount,numcount; 8 | char input[20],scan[20],opcode[20]; 9 | int num[20]; 10 | 11 | datain(){ 12 | this->count=-1; 13 | this->sount=-1; 14 | this->numcount=-1; 15 | } 16 | 17 | int checkalphabet(char z){ 18 | if((z>='a'&&z<='z')||(z>='A'&&z<='Z')) return 1; 19 | else return 0; 20 | } 21 | 22 | int add(int a,int b){ 23 | return a+b; 24 | } 25 | 26 | int sub(int a,int b){ 27 | return a-b; 28 | } 29 | 30 | int div(int a,int b){ 31 | return a/b; 32 | } 33 | 34 | int mul(int a,int b){ 35 | return a*b; 36 | } 37 | 38 | int power(int a,int b){ 39 | if(b==0){ 40 | return 1; 41 | }else if(b==1){ 42 | return a; 43 | }else{ 44 | return a*power(a,b-1); 45 | } 46 | } 47 | 48 | 49 | void pop(){ 50 | ++count; 51 | scan[count]=opcode[sount-1]; 52 | opcode[sount-1]=opcode[sount]; 53 | --sount; 54 | } 55 | 56 | void evaluate(){ 57 | if(opcode[sount-1]=='$'||opcode[sount-1]=='^'){ 58 | 59 | if(opcode[sount]=='+'||opcode[sount]=='-'||opcode[sount]=='/'||opcode[sount]=='*'||opcode[sount]=='^'||opcode[sount]=='$'){ 60 | pop(); 61 | } 62 | } 63 | else if(opcode[sount-1]=='/'||opcode[sount-1]=='*'){ 64 | if(opcode[sount]=='+'||opcode[sount]=='-'||opcode[sount]=='/'||opcode[sount]=='*'){ 65 | pop(); 66 | } 67 | } 68 | else if(opcode[sount-1]=='+'||opcode[sount-1]=='-'){ 69 | if(opcode[sount]=='-'||opcode[sount]=='+'){ 70 | pop(); 71 | } 72 | } 73 | } 74 | 75 | void push(){ 76 | ++count; 77 | scan[count]=opcode[sount]; 78 | --sount; 79 | } 80 | 81 | void getinput(){ 82 | cout<<"Enter the given operation below : "; 83 | cin>>input; 84 | } 85 | 86 | void getdifferentdata(){ 87 | for(int i=0;i0){ 96 | evaluate(); 97 | } 98 | if(opcode[sount]==')'){ 99 | sount--; 100 | while(opcode[sount]!='('){ 101 | push(); 102 | } 103 | --sount; 104 | } 105 | } 106 | } 107 | while(sount!=-1){ 108 | push(); 109 | } 110 | } 111 | 112 | void seescaned(){ 113 | cout<<"The postfix notation is : "; 114 | for(int i=0;i<=count;++i){ 115 | cout<>num[numcount]; 126 | }else{ 127 | if(scan[i]=='+'){ 128 | num[numcount-1]=add(num[numcount],num[numcount-1]); 129 | }else if(scan[i]=='-'){ 130 | num[numcount-1]=sub(num[numcount-1],num[numcount]); 131 | }else if(scan[i]=='*'){ 132 | num[numcount-1]=mul(num[numcount],num[numcount-1]); 133 | }else if(scan[i]=='/'){ 134 | num[numcount-1]=div(num[numcount-1],num[numcount]); 135 | }else if(scan[i]=='&'){ 136 | num[numcount-1]=power(num[numcount-1],num[numcount]); 137 | } 138 | --numcount; 139 | } 140 | } 141 | cout<<"The result of the evaluation is :"< 2 | #include 3 | using namespace std; 4 | 5 | class datain{ 6 | public : 7 | int count,sount; 8 | char input[20],scan[20],opcode[20]; 9 | 10 | datain(){ 11 | this->count=-1; 12 | this->sount=-1; 13 | } 14 | int checkalphabet(char z){ 15 | if((z>='a'&&z<='z')||(z>='A'&&z<='Z')) return 1; 16 | else return 0; 17 | } 18 | 19 | void pop(){ 20 | ++count; 21 | scan[count]=opcode[sount-1]; 22 | opcode[sount-1]=opcode[sount]; 23 | --sount; 24 | } 25 | void evaluate(){ 26 | if(opcode[sount-1]=='&'){ 27 | if(opcode[sount]=='-'||opcode[sount]=='+'||opcode[sount]=='*'||opcode[sount]=='/'){ 28 | pop(); 29 | } 30 | } 31 | else if(opcode[sount-1]=='/'||opcode[sount-1]=='*'){ 32 | if(opcode[sount]=='+'||opcode[sount]=='-'){ 33 | pop(); 34 | } 35 | } 36 | } 37 | 38 | 39 | 40 | void push(){ 41 | ++count; 42 | scan[count]=opcode[sount]; 43 | --sount; 44 | } 45 | 46 | void getinput(){ 47 | cout<<"Enter the given operation below : "; 48 | cin>>input; 49 | } 50 | 51 | void getdifferentdata(){ 52 | for(int i=0;i0){ 61 | evaluate(); 62 | } 63 | if(opcode[sount]==')'){ 64 | sount--; 65 | while(opcode[sount]!='('){ 66 | push(); 67 | } 68 | --sount; 69 | } 70 | } 71 | } 72 | while(sount>-1){ 73 | push(); 74 | }} 75 | 76 | void seeopcode(){ 77 | cout<<"The opcodes are :"< 2 | #include 3 | using namespace std; 4 | 5 | class datain{ 6 | public : 7 | int count,sount; 8 | char input[20],scan[20],opcode[20],revinp[20],revopcode[20]; 9 | 10 | datain(){ 11 | this->count=-1; 12 | this->sount=-1; 13 | } 14 | 15 | int checkalphabet(char z){ 16 | if((z>='a'&&z<='z')||(z>='A'&&z<='Z')) return 1; 17 | else return 0; 18 | } 19 | 20 | void pop(){ 21 | ++count; 22 | scan[count]=opcode[sount-1]; 23 | opcode[sount-1]=opcode[sount]; 24 | --sount; 25 | } 26 | 27 | void evaluate(){ 28 | if(opcode[sount-1]=='&'){ 29 | if(opcode[sount]=='-'||opcode[sount]=='+'||opcode[sount]=='*'||opcode[sount]=='/'){ 30 | pop(); 31 | } 32 | }else if(opcode[sount-1]=='/'){ 33 | if(opcode[sount]=='+'||opcode[sount]=='-'||opcode[sount-1]=='*'){ 34 | pop(); 35 | } 36 | } else if(opcode[sount-1]=='*'){ 37 | if(opcode[sount]=='+'||opcode[sount]=='-'){ 38 | pop(); 39 | } 40 | }else if(opcode[sount-1]=='+'){ 41 | if(opcode[sount]=='-'){ 42 | pop(); 43 | } 44 | } 45 | } 46 | 47 | void push(){ 48 | ++count; 49 | scan[count]=opcode[sount]; 50 | --sount; 51 | } 52 | 53 | void getinput() { 54 | cout << "Enter the given operation below: "; 55 | cin>>revinp; 56 | for(int i=0;i0){ 82 | evaluate(); 83 | } 84 | if(opcode[sount]==')'){ 85 | sount--; 86 | while(opcode[sount]!='('){ 87 | push(); 88 | } 89 | --sount; 90 | } 91 | } 92 | } 93 | while(sount>-1){ 94 | push(); 95 | } 96 | } 97 | 98 | void seeopcode(){ 99 | cout<<"The opcodes are :"< 2 | using namespace std; 3 | #define max 10 4 | 5 | class Queue{ 6 | private: 7 | 8 | int data[max]; 9 | int rear; 10 | int front; 11 | 12 | 13 | public: 14 | Queue(){ 15 | this->rear=-1; 16 | this->front=0; 17 | } 18 | protected: 19 | void makeEmpty(){ 20 | rear=-1; 21 | front=0; 22 | } 23 | 24 | bool checkEmpty(){ 25 | return rearfront&&data[temp-1]>val){ 42 | data[temp]=data[temp-1]; 43 | --temp; 44 | } 45 | data[temp]=val; 46 | 47 | } 48 | } 49 | 50 | int Dequeue(){ 51 | int val=0; 52 | if(!checkEmpty()){ 53 | val=data[front]; 54 | ++front; 55 | } 56 | return val; 57 | } 58 | 59 | 60 | void traverse(){ 61 | if(!checkEmpty()){ 62 | cout<<"The data in the Queue are : "; 63 | for(int i=front;i<=rear;++i){ 64 | cout<>n; 86 | switch (n) 87 | { 88 | case 1: 89 | makeEmpty(); 90 | break; 91 | 92 | 93 | case 2: 94 | if(checkEmpty()){ 95 | cout<<"The List is empty"<>k; 116 | Enqueue(k); 117 | break; 118 | 119 | case 5: 120 | 121 | cout< 2 | using namespace std; 3 | 4 | #define max 10 5 | 6 | 7 | class Queue{ 8 | private: 9 | int rear; 10 | int front; 11 | int data[max]; 12 | 13 | public: 14 | Queue(){ 15 | this->front=0; 16 | this->rear=-1; 17 | } 18 | 19 | 20 | protected: 21 | int getdata(){ 22 | cout<<"enter data : "; 23 | int k; 24 | cin>>k; 25 | return k; 26 | } 27 | 28 | 29 | void makeempty(){ 30 | front=0; 31 | rear=-1; 32 | } 33 | 34 | bool checkEmpty(){ 35 | return front==0&&rear==-1; 36 | } 37 | 38 | bool checkFull(){ 39 | return rear==max-1; 40 | } 41 | 42 | void Enqueue(int val){ 43 | if(!checkFull()){ 44 | ++rear; 45 | data[rear]=val; 46 | } 47 | } 48 | 49 | 50 | int dequeue(){ 51 | int toret = 0; 52 | if(!checkEmpty()){ 53 | toret=data[front]; 54 | ++front; 55 | } 56 | return toret; 57 | } 58 | 59 | 60 | void traverse(){ 61 | for(int i = front ; i <=rear;++i){ 62 | cout<>n; 82 | switch (n) 83 | { 84 | case 1: 85 | makeempty(); 86 | break; 87 | 88 | 89 | case 2: 90 | if(checkEmpty()){ 91 | cout<<"The Stack is empty"< 2 | using namespace std; 3 | #define max 3 4 | 5 | class stack{ 6 | public: 7 | int data[10]; 8 | int count; 9 | 10 | 11 | stack(){ 12 | this->count=-1; 13 | } 14 | 15 | 16 | void makeempty(){ 17 | count = -1; 18 | } 19 | 20 | 21 | int checkempty(){ 22 | if(count<0) 23 | return 1; 24 | else return 0; 25 | } 26 | 27 | 28 | int checkfull(){ 29 | if((count+1)==max) 30 | return 1; 31 | else return 0; 32 | 33 | } 34 | 35 | void push(){ 36 | if(checkfull()){ 37 | cout<<"The data cannot be entered "<>data[count]; 43 | } 44 | } 45 | 46 | void pop(){ 47 | if(checkempty()){ 48 | cout<<"The list is empty so you cannot enter the data"<>n; 94 | switch(n){ 95 | case 1: 96 | l.makeempty(); 97 | break; 98 | case 2: 99 | if(l.checkempty()){ 100 | cout<<"the given data is empty"< 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DSAwithJava/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DSAwithJava/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DSAwithJava/Algos/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package Algos; 2 | public class BinarySearch { 3 | 4 | public String toHex(int num){ 5 | StringBuilder sb = new StringBuilder(); 6 | while(num>0){ 7 | int remainder = num%16; 8 | num = num/16; 9 | if(remainder<10){ 10 | sb.append(remainder); 11 | } else { 12 | sb.append(gethexVal(remainder)); 13 | } 14 | } 15 | sb=sb.reverse(); 16 | return sb.toString(); 17 | } 18 | 19 | public char gethexVal(int num){ 20 | return (char)(55+num); 21 | } 22 | } -------------------------------------------------------------------------------- /DSAwithJava/Algos/CeaserCipher.java: -------------------------------------------------------------------------------- 1 | package Algos; 2 | 3 | public class CeaserCipher{ 4 | private int keyInc; 5 | public String encrypt(String message,char key){ 6 | char[] chars = message.toCharArray(); 7 | key=Character.toUpperCase(key); 8 | keyInc = (int)key-'A'; 9 | for(int i=0;i=nums[end]){ 12 | return start; 13 | }else return end; 14 | } 15 | else{ 16 | int Index1= GetMax(nums,start,(start+end)/2); 17 | int Index2= GetMax(nums,((start+end)/2)+1,end); 18 | if(nums[Index1]=node.value){ 16 | return binaryInsert(rootNode.left,node); 17 | }else return binaryInsert(rootNode.right,node); 18 | } 19 | 20 | 21 | public static boolean search(int searchVal,BinaryNode rootNode){ 22 | if(rootNode==null) return false; 23 | if(rootNode.value==searchVal) return true; 24 | if(rootNode.value>searchVal){ 25 | return search(searchVal,rootNode.left); 26 | }else return search(searchVal,rootNode.right); 27 | } 28 | 29 | public static boolean search(BinaryNode searchVal,BinaryNode rootNode){ 30 | if(rootNode==null) return false; 31 | if(rootNode.value==searchVal.value) return true; 32 | if(rootNode.value>searchVal.value){ 33 | return search(searchVal,rootNode.left); 34 | }else return search(searchVal,rootNode.right); 35 | } 36 | 37 | public static void inorderDisplay(BinaryNode rootNode, List nums){ 38 | if(rootNode==null){ 39 | return; 40 | } 41 | inorderDisplay(rootNode.left,nums); 42 | nums.add(rootNode.value); 43 | inorderDisplay(rootNode.right,nums); 44 | } 45 | 46 | public static void preorderDisplay(BinaryNode rootNode, List nums){ 47 | if(rootNode==null){ 48 | return; 49 | } 50 | nums.add(rootNode.value); 51 | preorderDisplay(rootNode.left,nums); 52 | preorderDisplay(rootNode.right,nums); 53 | } 54 | 55 | public static void postorderDisplay(BinaryNode rootNode, List nums){ 56 | if(rootNode==null){ 57 | return; 58 | } 59 | nums.add(rootNode.value); 60 | postorderDisplay(rootNode.left,nums); 61 | postorderDisplay(rootNode.right,nums); 62 | } 63 | 64 | public static List breadthFirstSearch(BinaryNode rootNode){ 65 | if(rootNode==null) return null; 66 | Queue queue = (Queue) new LinkedList(); 67 | List list = new ArrayList<>(); 68 | list.add(rootNode.value); 69 | queue.add(rootNode); 70 | while (!queue.isEmpty()){ 71 | BinaryNode current = queue.poll(); 72 | list.add(current.value); 73 | if(current.left!=null){ 74 | queue.add(current.left); 75 | } 76 | if (current.right!=null){ 77 | queue.add(current.right); 78 | } 79 | } 80 | return list; 81 | } 82 | 83 | public BinaryNode sortedArrayToBST(int[] nums) { 84 | if(nums.length==0){return null;} 85 | else { 86 | return binaryInsert(nums,0,nums.length-1); 87 | } 88 | } 89 | 90 | private BinaryNode binaryInsert(int[] nums,int start,int end ){ 91 | if(start>end) return null; 92 | int mid = (start+end)/2; 93 | BinaryNode root = new BinaryNode(nums[mid]); 94 | root.right = binaryInsert(nums,mid+1,end); 95 | root.left = binaryInsert(nums,start,mid-1); 96 | return root; 97 | } 98 | 99 | public BinaryNode sortedListToBST(Node head) { 100 | if (head == null) return null; 101 | return buildBST(head, null); 102 | } 103 | 104 | private BinaryNode buildBST(Node head, Nodetail) { 105 | if (head == tail) return null; 106 | Node slow = head; 107 | Node fast = head; 108 | while (fast != tail && fast.next != tail) { 109 | fast = fast.next.next; 110 | slow = slow.next; 111 | } 112 | BinaryNode root = new BinaryNode(slow.val); 113 | root.left = buildBST(head, slow); 114 | root.right = buildBST(slow.next, tail); 115 | return root; 116 | } 117 | 118 | public int maxDepth(BinaryNode root) { 119 | if(root == null) return 0; 120 | if(root.right == null && root.left== null) return 1; 121 | return 1+Math.max(maxDepth(root.right),maxDepth(root.left)); 122 | } 123 | 124 | public boolean isSymmetric(BinaryNode root) { 125 | if(root == null) return true; 126 | if(root.right == null && root.left==null) return true; 127 | return checkSymmetric(root.right,root.left); 128 | } 129 | 130 | private boolean checkSymmetric(BinaryNode leftNode, BinaryNode rightNode){ 131 | if((leftNode ==null & rightNode !=null )||(rightNode==null &&leftNode!=null)){ 132 | return false; 133 | } 134 | if(rightNode==null&&leftNode==null){ 135 | return true; 136 | } 137 | if(rightNode.value == leftNode.value){ 138 | return (checkSymmetric(leftNode.left,rightNode.right)&&checkSymmetric(leftNode.right,rightNode.left)); 139 | }else return false; 140 | } 141 | } 142 | 143 | -------------------------------------------------------------------------------- /DSAwithJava/DSAwithJava.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/LinkList/.DS_Store -------------------------------------------------------------------------------- /DSAwithJava/LinkList/ArrayList.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class ArrayList implements List{ 4 | private T[] arr; 5 | public int count; 6 | private int capacity; 7 | 8 | public ArrayList(){ 9 | this.capacity = 10; 10 | this.arr = (T[]) new Object[this.capacity]; 11 | this.count = 0; 12 | } 13 | 14 | public ArrayList(int capacity){ 15 | this.capacity = capacity; 16 | arr = (T[]) new Object[capacity]; 17 | count = 0; 18 | } 19 | 20 | @Override 21 | public boolean isEmpty(){ 22 | return count == 0; 23 | } 24 | 25 | @Override 26 | public boolean insert(T value){ 27 | if(count+1 == this.capacity) return false; 28 | arr[count++] = value; 29 | return true; 30 | } 31 | 32 | @Override 33 | public boolean insert(T value,int index){ 34 | if(index < 0 || index > this.count) return false; 35 | if(count+1 == this.capacity)return false; 36 | if(index == count) return insert(value); 37 | ++count; 38 | for(int i = this.count; i > index; i--){ 39 | arr[i]=arr[i-1]; 40 | } 41 | arr[index]=value; 42 | return true; 43 | } 44 | 45 | @Override 46 | public T remove()throws Exception{ 47 | if(isEmpty()) throw new Exception("List is empty"); 48 | return arr[count--]; 49 | } 50 | 51 | @Override 52 | public T remove(int index) throws Exception{ 53 | if(index < 0 || index >= this.capacity) throw new IndexOutOfBoundsException("Index out of bounds"); 54 | T item = arr[index]; 55 | for(int i = index; i < this.count; i++){ 56 | arr[i]=arr[i+1]; 57 | } 58 | --count; 59 | return item; 60 | } 61 | 62 | @Override 63 | public T get(int index) throws Exception { 64 | if(isEmpty()||index<0||index>=count) throw new Exception("Error.The index does not exist"); 65 | return arr[index]; 66 | } 67 | 68 | @Override 69 | public void reverse() { 70 | int left = 0; 71 | int right = this.count - 1; 72 | 73 | while (left < right) { 74 | T temp = this.arr[left]; 75 | arr[left] = arr[right]; 76 | arr[right] = temp; 77 | left++; 78 | right--; 79 | } 80 | } 81 | 82 | @Override 83 | public void Traverse(){ 84 | if(!isEmpty()){ 85 | for(int i = 0 ; i < count; i++){ 86 | System.out.print(arr[i]+" "); 87 | } 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/DoublyLinkedList.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class DoublyLinkedList implements List { 4 | 5 | private ListNode head; 6 | private ListNode tail; 7 | public int count; 8 | 9 | public DoublyLinkedList(){ 10 | this.head = null; 11 | this.tail = null; 12 | this.count = 0; 13 | } 14 | 15 | @Override 16 | public boolean isEmpty(){ 17 | return head == null; 18 | } 19 | 20 | public boolean insertTop(T value){ 21 | ListNode newNode = new ListNode(value); 22 | if(isEmpty()){ 23 | head = tail = newNode; 24 | head.next = tail; 25 | head.prev = tail; 26 | } else { 27 | newNode.next = head; 28 | head.prev = newNode; 29 | head = newNode; 30 | } 31 | tail.next = head; 32 | head.prev = tail; 33 | ++count; 34 | return true; 35 | } 36 | 37 | public boolean insertTop(ListNode newNode){ 38 | if(isEmpty()){ 39 | head = tail = newNode; 40 | head.next = tail; 41 | head.prev = tail; 42 | } else { 43 | newNode.next = head; 44 | head.prev = newNode; 45 | head = newNode; 46 | } 47 | tail.next = head; 48 | head.prev = tail; 49 | ++count; 50 | return true; 51 | } 52 | 53 | @Override 54 | public boolean insert(T value){ 55 | ListNode newNode = new ListNode(value); 56 | if(isEmpty()){ 57 | head = tail = newNode; 58 | head.next = tail; 59 | head.prev = tail; 60 | } else { 61 | newNode.prev = tail; 62 | tail.next = newNode; 63 | tail = newNode; 64 | } 65 | head.prev = tail; 66 | tail.next = head; 67 | ++count; 68 | return true; 69 | } 70 | 71 | 72 | public boolean insert(ListNode newNode){ 73 | if(isEmpty()){ 74 | head = tail = newNode; 75 | head.next = tail; 76 | head.prev = tail; 77 | } else { 78 | newNode.prev = tail; 79 | tail.next = newNode; 80 | tail = newNode; 81 | } 82 | head.prev = tail; 83 | tail.next = head; 84 | ++count; 85 | return true; 86 | } 87 | 88 | @Override 89 | public boolean insert(T value, int index){ 90 | if(index < 0 || index > count) { 91 | return false; 92 | } 93 | if(index == 0) { 94 | return insertTop(value); 95 | } else if(index == count) { 96 | return insert(value); 97 | } 98 | 99 | ListNode temp = head; 100 | for(int i = 0; i < index - 1; ++i){ 101 | temp = temp.next; 102 | } 103 | ListNode newNode = new ListNode<>(value); 104 | newNode.next = temp.next; 105 | newNode.prev = temp; 106 | temp.next.prev = newNode; 107 | temp.next = newNode; 108 | ++count; 109 | return true; 110 | } 111 | 112 | 113 | public boolean insert(ListNode newNode, int index) throws Exception { 114 | if(index < 0 || index > count) { 115 | throw new Exception("Index out of bounds"); 116 | } 117 | if(index == 0) { 118 | return insertTop(newNode); 119 | } else if(index == count) { 120 | return insert(newNode); 121 | } 122 | 123 | ListNode temp = head; 124 | for(int i = 0; i < index - 1; ++i){ 125 | temp = temp.next; 126 | } 127 | newNode.next = temp.next; 128 | newNode.prev = temp; 129 | temp.next.prev = newNode; 130 | temp.next = newNode; 131 | ++count; 132 | return true; 133 | } 134 | 135 | public T removeTop() throws Exception { 136 | if(isEmpty()) { 137 | throw new NullPointerException("List is empty"); 138 | } 139 | ListNode temp = head; 140 | if(head == tail) { 141 | head = tail = null; 142 | } else { 143 | head = head.next; 144 | head.prev = tail; 145 | tail.next = head; 146 | } 147 | --count; 148 | return temp.val; 149 | } 150 | 151 | @Override 152 | public T remove() throws Exception { 153 | if(isEmpty()) { 154 | throw new NullPointerException("List is empty"); 155 | } 156 | ListNode temp = tail; 157 | if(head == tail) { 158 | head = tail = null; 159 | } else { 160 | tail = tail.prev; 161 | tail.next = head; 162 | head.prev = tail; 163 | } 164 | --count; 165 | return temp.val; 166 | } 167 | 168 | @Override 169 | public T get(int index) throws Exception{ 170 | if(index < 0 || index >= count) { 171 | throw new Exception("Index out of bounds"); 172 | } 173 | if(index == 0) { 174 | return head.val; 175 | } 176 | ListNode temp = head; 177 | for(int i = 0; i < index; ++i) { 178 | temp = temp.next; 179 | } 180 | return temp.val; 181 | } 182 | @Override 183 | public T remove(int index) throws Exception { 184 | if(index < 0 || index >= count) { 185 | throw new Exception("Index out of bounds"); 186 | } 187 | if(index == 0) { 188 | return removeTop(); 189 | } 190 | if(index == count - 1) { 191 | return remove(); 192 | } 193 | 194 | ListNode temp = head; 195 | for(int i = 0; i < index; ++i) { 196 | temp = temp.next; 197 | } 198 | temp.prev.next = temp.next; 199 | temp.next.prev = temp.prev; 200 | --count; 201 | return temp.val; 202 | } 203 | 204 | public Boolean Search(T object) { 205 | ListNode temp = head; 206 | if(isEmpty()) return false; 207 | do { 208 | if(temp.val.equals(object)) { 209 | return true; 210 | } 211 | temp = temp.next; 212 | } while(temp != head); 213 | return false; 214 | } 215 | 216 | @Override 217 | public void Traverse() { 218 | if(!isEmpty()) { 219 | ListNode temp = head; 220 | do { 221 | System.out.print(temp.val + " "); 222 | temp = temp.next; 223 | } while(temp != head); 224 | System.out.println(); 225 | } 226 | } 227 | 228 | public void TraverseReverse() { 229 | if(!isEmpty()) { 230 | ListNode temp = tail; 231 | do { 232 | System.out.print(temp.val + " "); 233 | temp = temp.prev; 234 | } while(temp != tail); 235 | System.out.println(); 236 | } 237 | } 238 | 239 | @Override 240 | public void reverse() { 241 | if (head == null || head.next == null) return; 242 | 243 | ListNode prev = null; 244 | ListNode current = head; 245 | ListNode next = null; 246 | while (current != null) { 247 | next = current.next; 248 | current.next = prev; 249 | prev = current; 250 | current = next; 251 | } 252 | this.head = prev; 253 | } 254 | } -------------------------------------------------------------------------------- /DSAwithJava/LinkList/LinkedList.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class LinkedListimplements List{ 4 | 5 | public ListNode head; 6 | public int count; 7 | 8 | public LinkedList(){ 9 | this.head = null; 10 | this.count = 0; 11 | } 12 | 13 | @Override 14 | public boolean isEmpty(){ 15 | return head == null; 16 | } 17 | 18 | public boolean insertTop(T value){ 19 | ListNode Node = new ListNode(value); 20 | if(head != null){ 21 | head.prev = Node; 22 | Node.next = head; 23 | } 24 | head = Node; 25 | ++count; 26 | return true; 27 | } 28 | 29 | public boolean insertTop(ListNode Node){ 30 | if(head != null){ 31 | head.prev = Node; 32 | Node.next = head; 33 | } 34 | head = Node; 35 | ++count; 36 | return true; 37 | } 38 | 39 | @Override 40 | public boolean insert(T value){ 41 | ListNode Node = new ListNode(value); 42 | if (head == null){ 43 | head = Node; 44 | ++count; 45 | return true; 46 | } 47 | ListNode temp = head; 48 | while(temp.next!=null){ 49 | temp = temp.next; 50 | } 51 | temp.next = Node; 52 | Node.prev = temp; 53 | ++count; 54 | return true; 55 | } 56 | 57 | public boolean insert(ListNode Node){ 58 | if (head == null){ 59 | head = Node; 60 | ++count; 61 | return true; 62 | } 63 | ListNode temp = head; 64 | while(temp.next!=null){ 65 | temp = temp.next; 66 | } 67 | temp.next = Node; 68 | Node.prev = temp; 69 | ++count; 70 | return true; 71 | } 72 | 73 | @Override 74 | public boolean insert(T value,int index){ 75 | if(head == null || index < 0){ 76 | return false; 77 | } 78 | if(index == 0||count == 0){ 79 | return insertTop(value); 80 | } 81 | if(index > count+1){ 82 | return false; 83 | 84 | } 85 | ListNode temp = head; 86 | for(int i = 0 ; i < index - 1 ;++i){ 87 | temp = temp.next; 88 | } 89 | if(temp.next==null){ 90 | return insert(value); 91 | } 92 | ListNode newNode = new ListNode<>(value); 93 | newNode.next = temp.next; 94 | temp.next.prev = newNode; 95 | newNode.prev = temp; 96 | temp.next = newNode; 97 | ++count; 98 | return true; 99 | } 100 | 101 | public boolean insert(ListNode newNode,int index)throws Exception{ 102 | if(head == null || index < 0){ 103 | throw new Exception(); 104 | } 105 | if(index == 0||count == 0){ 106 | return insertTop(newNode); 107 | } 108 | if(index > count+1) { 109 | throw new Exception("Out of bounds"); 110 | } 111 | ListNode temp = head; 112 | for(int i = 0 ; i < index - 1 ;++i){ 113 | temp = temp.next; 114 | } 115 | if(temp.next == null){ 116 | return insert(newNode); 117 | } 118 | newNode.next = temp.next; 119 | temp.next.prev = newNode; 120 | newNode.prev = temp; 121 | temp.next = newNode; 122 | ++count; 123 | return true; 124 | } 125 | 126 | 127 | public T removeTop() throws Exception{ 128 | if(head == null){ 129 | throw new NullPointerException(); 130 | } 131 | ListNode temp = head; 132 | head = head.next; 133 | if (head != null) { 134 | head.prev = null; 135 | } 136 | --count; 137 | return temp.val; 138 | } 139 | 140 | @Override 141 | public T remove() throws Exception{ 142 | if(head == null){ 143 | throw new NullPointerException(); 144 | } 145 | if(head.next==null){ 146 | T x = head.val; 147 | head=null; 148 | return x; 149 | } 150 | ListNode temp = head; 151 | while(temp.next.next!=null){ 152 | temp=temp.next; 153 | } 154 | T valX = temp.next.val; 155 | temp.next=null; 156 | --count; 157 | return valX; 158 | } 159 | 160 | @Override 161 | public T remove(int index) throws Exception{ 162 | if(head == null ||index < 0||index >= count){ 163 | throw new NullPointerException(); 164 | } 165 | if(index == 0){ 166 | return this.removeTop(); 167 | } 168 | ListNode temp = head; 169 | for(int i = 0 ; i < index - 1 ;++i){ 170 | temp = temp.next; 171 | } 172 | ListNode Delete = temp.next; 173 | if(Delete.next == null){ 174 | temp.next = null; 175 | --count; 176 | return Delete.val; 177 | } 178 | temp.next = Delete.next; 179 | Delete.next.prev = temp; 180 | --count; 181 | return Delete.val; 182 | } 183 | 184 | public Boolean Search(T object){ 185 | ListNode temp = head; 186 | while(temp!=null){ 187 | if(temp.val.equals(object)){ 188 | return true; 189 | } 190 | temp = temp.next; 191 | } 192 | return false; 193 | } 194 | 195 | 196 | 197 | public T peek() throws Exception{ 198 | if(head==null){ 199 | throw new NullPointerException(); 200 | } 201 | return head.val; 202 | } 203 | 204 | public int length(){ 205 | return this.count; 206 | } 207 | 208 | 209 | @Override 210 | public void Traverse(){ 211 | if(head!=null){ 212 | ListNode temp = head; 213 | while(temp!=null){ 214 | System.out.println(temp.val); 215 | temp = temp.next; 216 | } 217 | } 218 | } 219 | 220 | @Override 221 | public T get(int index) throws Exception{ 222 | if(index < 0 || index >= count) { 223 | throw new Exception("Index out of bounds"); 224 | } 225 | if(index == 0) { 226 | return head.val; 227 | } 228 | ListNode temp = head; 229 | for(int i = 0; i < index; ++i) { 230 | temp = temp.next; 231 | } 232 | return temp.val; 233 | } 234 | 235 | @Override 236 | public void reverse() { 237 | if (head == null || head.next == null) return; 238 | 239 | ListNode prev = null; 240 | ListNode current = head; 241 | ListNode next = null; 242 | while (current != null) { 243 | next = current.next; 244 | current.next = prev; 245 | prev = current; 246 | current = next; 247 | } 248 | this.head = prev; 249 | } 250 | } 251 | 252 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/LinkedListTests.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class LinkedListTests { 4 | public static ListNode removeDuplicates(ListNode head){ 5 | if(head==null||head.next==null) return head; 6 | ListNode temp = head; 7 | ListNode tempNext = head.next; 8 | 9 | while(tempNext!=null){ 10 | if(temp.val.equals(tempNext.val)){ 11 | tempNext = tempNext.next; 12 | temp.next = tempNext; 13 | }else{ 14 | temp = tempNext; 15 | tempNext = tempNext.next; 16 | } 17 | } 18 | return head; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/List.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public interface List { 4 | public boolean isEmpty(); 5 | public boolean insert(T value); 6 | public boolean insert(T value, int index); 7 | public T remove()throws Exception; 8 | public T remove(int index)throws Exception; 9 | public T get(int index)throws Exception; 10 | public void reverse(); 11 | public void Traverse(); 12 | } 13 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/ListNode.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class ListNode { 4 | public T val; 5 | public ListNode next; 6 | public ListNode prev; 7 | 8 | public ListNode(T val){ 9 | this.val = val; 10 | this.next = null; 11 | this.prev = null; 12 | } 13 | 14 | public ListNode(){ 15 | this.val = null; 16 | this.next = null; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /DSAwithJava/LinkList/Test.java: -------------------------------------------------------------------------------- 1 | package LinkList; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | LinkedList list = new LinkedList<>(); 6 | list.insertTop(5); 7 | list.insertTop(5); 8 | list.insertTop(5); 9 | list.insertTop(5); 10 | list.insertTop(4); 11 | list.insertTop(4); 12 | list.insertTop(3); 13 | list.insertTop(3); 14 | list.insertTop(3); 15 | list.insertTop(2); 16 | list.insertTop(1); 17 | list.insertTop(0); 18 | list.insertTop(0); 19 | list.Traverse(); 20 | LinkedListTests.removeDuplicates(list.head); 21 | System.out.println(); 22 | list.Traverse(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/LinkedQueue/.DS_Store -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/ArrayQueue.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public class ArrayQueue implements Queue { 4 | 5 | private K[] arr; 6 | private int front; 7 | private int rear; 8 | public int count; 9 | private int capacity; 10 | 11 | public ArrayQueue() { 12 | this.capacity = 15; 13 | this.arr = (K[])new Object[capacity]; 14 | this.front = 0; 15 | this.rear = -1; 16 | this.count = 0; 17 | } 18 | 19 | public ArrayQueue(int capacity) { 20 | this.capacity = capacity; 21 | this.arr = (K[])new Object[capacity]; 22 | this.front = 0; 23 | this.rear = -1; 24 | this.count = 0; 25 | } 26 | 27 | @Override 28 | public boolean isEmpty() { 29 | return rear implements Queue { 4 | 5 | protected int front; 6 | protected int rear; 7 | protected int capacity; 8 | protected Type[] arr; 9 | 10 | public CircularQueue(int capacity) { 11 | this.capacity = capacity; 12 | arr = (Type[]) new Object[capacity]; 13 | front = 0; 14 | rear = 0; 15 | } 16 | 17 | public CircularQueue() { 18 | this.capacity = 15; 19 | arr = (Type[]) new Object[capacity]; 20 | front = 0; 21 | rear = 0; 22 | } 23 | 24 | @Override 25 | public boolean isEmpty() { 26 | return (rear + 1) % capacity == front; 27 | } 28 | 29 | public boolean isFull() { 30 | return (rear + 1) % capacity == front; 31 | } 32 | 33 | @Override 34 | public boolean enqueue(Type item) throws Exception { 35 | if (isFull()) { 36 | return false; 37 | } 38 | arr[rear++] = item; 39 | rear%= capacity; 40 | return true; 41 | } 42 | 43 | @Override 44 | public Type dequeue() throws Exception { 45 | if (isEmpty()) { 46 | throw new Exception("Queue is empty"); 47 | } 48 | Type item = arr[front++]; 49 | front %= capacity; 50 | return item; 51 | } 52 | 53 | @Override 54 | public Type peek() throws Exception { 55 | if (isEmpty()) { 56 | throw new Exception("Queue is empty"); 57 | } 58 | return arr[front]; 59 | } 60 | 61 | @Override 62 | public void Traverse() { 63 | if (isEmpty()) { 64 | System.out.println("Queue is empty"); 65 | return; 66 | } 67 | System.out.print("Queue elements: "); 68 | for (int i = front; i != rear; i = (i + 1) % capacity) { 69 | System.out.print(arr[i] + " "); 70 | } 71 | System.out.println(); 72 | } 73 | 74 | @Override 75 | public int length() { 76 | if (rear >= front) { 77 | return rear - front; 78 | } else { 79 | return capacity - front + rear; 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/LinkedList.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public class LinkedList implements Queue{ 4 | 5 | protected Node head; 6 | protected Node tail; 7 | public int count; 8 | 9 | public LinkedList(){ 10 | this.head = null; 11 | this.tail = null; 12 | this.count = 0; 13 | } 14 | 15 | @Override 16 | public boolean isEmpty(){ 17 | return head == null; 18 | } 19 | 20 | 21 | public boolean InsertTop(Type value){ 22 | Node newNode = new Node(value); 23 | if(isEmpty()){ 24 | head = tail = newNode; 25 | }else{ 26 | newNode.next = head; 27 | head = newNode; 28 | } 29 | tail.next = head; 30 | ++count; 31 | return true; 32 | } 33 | 34 | public boolean InsertTop(Node newNode){ 35 | if(isEmpty()){ 36 | head = tail = newNode; 37 | }else{ 38 | newNode.next = head; 39 | head = newNode; 40 | } 41 | tail.next = head; 42 | ++count; 43 | return true; 44 | } 45 | 46 | @Override 47 | public boolean enqueue(Type value){ 48 | Node newNode = new Node<>(value); 49 | if (isEmpty()){ 50 | head = tail = newNode; 51 | }else{ 52 | tail.next = newNode; 53 | tail = newNode; 54 | } 55 | tail.next = head; 56 | ++count; 57 | return true; 58 | } 59 | 60 | 61 | public boolean enqueue(Node newNode){ 62 | if (isEmpty()){ 63 | head = tail = newNode; 64 | }else{ 65 | tail.next = newNode; 66 | tail = newNode; 67 | } 68 | tail.next = head; 69 | ++count; 70 | return true; 71 | } 72 | 73 | public boolean Insert(Type value,int index)throws Exception{ 74 | if(head == null || index < 0){ 75 | throw new Exception("Unmatched index"); 76 | } 77 | if(index == 0||count == 0){ 78 | return InsertTop(value); 79 | } 80 | if(index > count){ 81 | index = index % count; 82 | } 83 | Node temp = head; 84 | for(int i = 0 ; i < index - 1 ;++i){ 85 | temp = temp.next; 86 | } 87 | if(temp.next==head){ 88 | return enqueue(value); 89 | } 90 | Node newNode = new Node<>(value); 91 | newNode.next = temp.next; 92 | temp.next = newNode; 93 | ++count; 94 | return true; 95 | } 96 | 97 | public boolean Insert(Node newNode,int index)throws Exception{ 98 | if(head == null || index < 0){ 99 | throw new Exception("Unmatched index"); 100 | } 101 | if(index == 0||count == 0){ 102 | return InsertTop(newNode); 103 | } 104 | if(index > count){ 105 | index = index % count; 106 | } 107 | Node temp = head; 108 | for(int i = 0 ; i < index - 1 ;++i){ 109 | temp = temp.next; 110 | } 111 | if(temp.next==head) { 112 | return enqueue(newNode); 113 | } 114 | newNode.next = temp.next; 115 | temp.next = newNode; 116 | ++count; 117 | return true; 118 | } 119 | 120 | 121 | @Override 122 | public Type dequeue() throws Exception{ 123 | if(isEmpty()){ 124 | throw new NullPointerException("Is Empty"); 125 | } 126 | Node temp = head; 127 | if(head.next == head){ 128 | head = tail = null; 129 | }else{ 130 | head = head.next; 131 | tail.next = head; 132 | } 133 | --count; 134 | return temp.val; 135 | } 136 | 137 | 138 | public Type RemoveEnd() throws Exception{ 139 | if(isEmpty()){ 140 | throw new NullPointerException("Is Empty"); 141 | } 142 | Node temp =tail; 143 | if(head.next==head){ 144 | head=tail=null; 145 | }else{ 146 | Node temp2 = head; 147 | while(temp2.next != tail){ 148 | temp2 = temp2.next; 149 | } 150 | tail = temp2; 151 | temp2.next = head; 152 | } 153 | --count; 154 | return temp.val; 155 | } 156 | 157 | 158 | public Type Remove(int index) throws Exception { 159 | if (isEmpty() || index < 0) { 160 | throw new NullPointerException("Error"); 161 | } 162 | if (index >= count && count != 0) { 163 | index = index % count; 164 | } 165 | if (index == 0) { 166 | return dequeue(); 167 | } 168 | Node temp = head; 169 | for (int i = 0; i < index - 1; ++i) { 170 | temp = temp.next; 171 | } 172 | if (temp.next == tail) { 173 | return RemoveEnd(); 174 | } 175 | Node toDelete = temp.next; 176 | temp.next = toDelete.next; 177 | --count; 178 | return toDelete.val; 179 | } 180 | 181 | public Boolean Search(Type object){ 182 | if(isEmpty()){ 183 | return false; 184 | } 185 | Node temp = head; 186 | do{ 187 | if(temp.val==object){ 188 | return true; 189 | } 190 | temp = temp.next; 191 | }while(temp!=head); 192 | return false; 193 | } 194 | 195 | 196 | @Override 197 | public Type peek() throws Exception{ 198 | if(head==null){ 199 | throw new NullPointerException("Is Empty cannot Peek"); 200 | } 201 | return head.val; 202 | } 203 | 204 | @Override 205 | public void Traverse(){ 206 | if(!isEmpty()){ 207 | Node temp = head; 208 | do{ 209 | System.out.println(temp.val); 210 | temp = temp.next; 211 | }while(temp!=head); 212 | } 213 | } 214 | 215 | @Override 216 | public int length(){ 217 | return this.count; 218 | } 219 | } 220 | 221 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/Node.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public class Node{ 4 | public T val; 5 | public Node next; 6 | 7 | public Node(){ 8 | } 9 | 10 | public Node(T val){ 11 | this.val = val; 12 | this.next = null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/PriorityLinked.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public class PriorityLinked extends LinkedList implements Queue{ 4 | 5 | @Override 6 | public boolean enqueue(Type object) { 7 | Node newNode = new Node<>(object); 8 | if (super.isEmpty()) { 9 | super.head = newNode; 10 | return true; 11 | } 12 | double newValue = object.doubleValue(); 13 | if (newValue <=super.head.val.doubleValue()) { 14 | return super.InsertTop(object); 15 | } 16 | Node temp =super.head; 17 | while (temp.next != null && newValue>temp.next.val.doubleValue()) { 18 | temp = temp.next; 19 | } 20 | newNode.next =temp.next; 21 | temp.next= newNode; 22 | return true; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/PriorityQueue.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public class PriorityQueue extends CircularQueue implements Queue { 4 | @Override 5 | public boolean enqueue(Type t) throws Exception { 6 | if (super.isFull()) return false; 7 | if (super.isEmpty()) return super.enqueue(t); 8 | 9 | double newValue = t.doubleValue(); 10 | 11 | if (super.arr[super.front].doubleValue() > newValue) { 12 | if (super.front != 0) { 13 | super.front = (super.front - 1 + super.capacity) % super.capacity; 14 | super.arr[super.front] = t; 15 | } else { 16 | super.front = super.capacity - 1; 17 | super.arr[super.front] = t; 18 | } 19 | return true; 20 | } 21 | int i = super.front; 22 | while (i != super.rear && super.arr[i % super.capacity].doubleValue() < newValue) { 23 | i = (i + 1) % super.capacity; 24 | } 25 | if (i == super.rear) { 26 | super.rear = (super.rear + 1) % super.capacity; 27 | super.arr[super.rear] = t; 28 | return true; 29 | } 30 | int k = super.rear; 31 | super.rear = (super.rear + 1) % super.capacity; 32 | while (k != i) { 33 | super.arr[(k + 1) % super.capacity] = super.arr[k % super.capacity]; 34 | k = (k - 1 + super.capacity) % super.capacity; 35 | } 36 | super.arr[i % super.capacity] = t; 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedQueue/Queue.java: -------------------------------------------------------------------------------- 1 | package LinkedQueue; 2 | 3 | public interface Queue{ 4 | public boolean isEmpty(); 5 | public boolean enqueue(Type item) throws Exception; 6 | public Type dequeue()throws Exception; 7 | public Type peek()throws Exception; 8 | public void Traverse(); 9 | public int length(); 10 | } 11 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedStack/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/LinkedStack/.DS_Store -------------------------------------------------------------------------------- /DSAwithJava/LinkedStack/ArrayStack.java: -------------------------------------------------------------------------------- 1 | package LinkedStack; 2 | 3 | public class ArrayStack implements Stack { 4 | 5 | private T[] array; 6 | public int count; 7 | 8 | public ArrayStack() { 9 | this.count = 0; 10 | this.array = (T[]) new Object[10]; 11 | } 12 | 13 | public ArrayStack(int capacity) { 14 | this.count = 0; 15 | this.array = (T[]) new Object[capacity]; 16 | } 17 | 18 | @Override 19 | public boolean isEmpty() { 20 | return count == 0; 21 | } 22 | 23 | @Override 24 | public int length(){ 25 | return this.count; 26 | } 27 | 28 | 29 | @Override 30 | public boolean push(T object){ 31 | array[count++] = object; 32 | return true; 33 | } 34 | 35 | @Override 36 | public T pop() throws Exception { 37 | return array[--count]; 38 | } 39 | 40 | public T peek() throws Exception { 41 | return array[count-1]; 42 | } 43 | 44 | @Override 45 | public void Traverse() { 46 | for (int i = 0; i < count; i++) { 47 | System.out.println(array[i]); 48 | } 49 | } 50 | 51 | 52 | } 53 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedStack/LinkedList.java: -------------------------------------------------------------------------------- 1 | package LinkedStack; 2 | 3 | public class LinkedList implements Stack{ 4 | 5 | private ListNode head; 6 | public int count; 7 | 8 | public LinkedList(){ 9 | this.head = null; 10 | this.count = 0; 11 | } 12 | 13 | @Override 14 | public boolean isEmpty(){ 15 | return head == null; 16 | } 17 | 18 | @Override 19 | public boolean push(T value){ 20 | ListNode Node = new ListNode(value); 21 | if(head != null){ 22 | Node.next = head; 23 | } 24 | head = Node; 25 | ++count; 26 | return true; 27 | } 28 | 29 | 30 | public boolean push(ListNode Node){ 31 | if(head != null){ 32 | Node.next = head; 33 | } 34 | head = Node; 35 | ++count; 36 | return true; 37 | } 38 | 39 | public boolean InsertEnd(T value){ 40 | ListNode Node = new ListNode(value); 41 | if (head == null){ 42 | head = Node; 43 | ++count; 44 | return true; 45 | } 46 | ListNode temp = head; 47 | while(temp.next!=null){ 48 | temp = temp.next; 49 | } 50 | temp.next = Node; 51 | ++count; 52 | return true; 53 | } 54 | 55 | public boolean InsertEnd(ListNode Node){ 56 | if (head == null){ 57 | head = Node; 58 | ++count; 59 | return true; 60 | } 61 | ListNode temp = head; 62 | while(temp.next!=null){ 63 | temp = temp.next; 64 | } 65 | temp.next = Node; 66 | ++count; 67 | return true; 68 | } 69 | 70 | public boolean Insert(T value,int index)throws Exception{ 71 | if(head == null || index < 0){ 72 | throw new Exception(); 73 | } 74 | if(index == 0||count == 0){ 75 | return push(value); 76 | } 77 | if(index > count){ 78 | throw new Exception("Out of bounds"); 79 | } 80 | ListNode temp = head; 81 | for(int i = 0 ; i < index - 1 ;++i){ 82 | temp = temp.next; 83 | } 84 | ListNode newNode = new ListNode<>(value); 85 | newNode.next = temp.next; 86 | temp.next = newNode; 87 | ++count; 88 | return true; 89 | } 90 | 91 | public boolean Insert(ListNode newNode,int index)throws Exception{ 92 | if(head == null || index < 0){ 93 | throw new Exception(); 94 | } 95 | if(index == 0||count == 0){ 96 | return push(newNode); 97 | } 98 | if(index > count){ 99 | throw new Exception("Out of bounds"); 100 | } 101 | ListNode temp = head; 102 | for(int i = 0 ; i < index - 1 ;++i){ 103 | temp = temp.next; 104 | } 105 | newNode.next = temp.next; 106 | temp.next = newNode; 107 | ++count; 108 | return true; 109 | } 110 | 111 | 112 | @Override 113 | public T pop() throws Exception{ 114 | if(head==null){ 115 | throw new NullPointerException(); 116 | } 117 | ListNode temp = head; 118 | head = head.next; 119 | --count; 120 | return temp.val; 121 | } 122 | 123 | public T deleteEnd() throws Exception{ 124 | if(head == null){ 125 | throw new NullPointerException(); 126 | } 127 | if(head.next==null){ 128 | T x = head.val; 129 | head=null; 130 | return x; 131 | } 132 | ListNode temp = head; 133 | while(temp.next.next!=null){ 134 | temp=temp.next; 135 | } 136 | T retval = temp.next.val; 137 | temp.next=null; 138 | --count; 139 | return retval; 140 | } 141 | 142 | public T Remove(int index) throws Exception{ 143 | 144 | if(head == null || index < 0){ 145 | throw new NullPointerException(); 146 | } 147 | if(index>count||count!=0){ 148 | throw new Exception("Out of bounds"); 149 | } 150 | if(index == 0){ 151 | return pop(); 152 | } 153 | ListNode temp = head; 154 | for(int i = 0 ; i < index ;++i){ 155 | temp = temp.next; 156 | } 157 | if(temp.next==null){ 158 | return deleteEnd(); 159 | } 160 | ListNode newNode = temp; 161 | temp.next = temp.next.next; 162 | --count; 163 | return newNode.val; 164 | } 165 | 166 | public Boolean Search(T object){ 167 | ListNode temp = head; 168 | while(temp!=null){ 169 | if(temp.val==object){ 170 | return true; 171 | } 172 | temp = temp.next; 173 | } 174 | return false; 175 | } 176 | 177 | 178 | @Override 179 | public T peek() throws Exception{ 180 | if(head==null){ 181 | throw new NullPointerException(); 182 | } 183 | return head.val; 184 | } 185 | 186 | @Override 187 | public int length(){ 188 | return this.count; 189 | } 190 | 191 | @Override 192 | public void Traverse() { 193 | if (head != null) { 194 | ListNode temp = head; 195 | while (temp != null) { 196 | System.out.print(temp.val+" "); 197 | temp = temp.next; 198 | } 199 | System.out.println(); 200 | } 201 | } 202 | 203 | public void reverseList() { 204 | if (head == null || head.next == null) return; 205 | 206 | ListNode prev = null; 207 | ListNode current = head; 208 | ListNode next = null; 209 | while (current != null) { 210 | next = current.next; 211 | current.next = prev; 212 | prev = current; 213 | current = next; 214 | } 215 | head = prev; 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /DSAwithJava/LinkedStack/ListNode.java: -------------------------------------------------------------------------------- 1 | package LinkedStack; 2 | 3 | 4 | public class ListNode{ 5 | public T val; 6 | public ListNode next; 7 | 8 | public ListNode(){} 9 | 10 | public ListNode(T data){ 11 | this.val = data; 12 | this.next = null; 13 | } 14 | } -------------------------------------------------------------------------------- /DSAwithJava/LinkedStack/Stack.java: -------------------------------------------------------------------------------- 1 | package LinkedStack; 2 | 3 | public interface Stack { 4 | public boolean push(T object); 5 | public T pop()throws Exception; 6 | public void Traverse(); 7 | public boolean isEmpty(); 8 | public T peek() throws Exception; 9 | public int length(); 10 | } 11 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Editor-based HTTP Client requests 5 | /httpRequests/ 6 | # Datasource local storage ignored files 7 | /dataSources/ 8 | /dataSources.local.xml 9 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/Algos/BinarySearch.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/Algos/BinarySearch.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/Algos/CeaserCipher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/Algos/CeaserCipher.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/Algos/MinMax.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/Algos/MinMax.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/Algos/VignereCipher.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/Algos/VignereCipher.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/BinaryTreeStructure/BinaryNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/BinaryTreeStructure/BinaryNode.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/BinaryTreeStructure/BinaryTree.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/BinaryTreeStructure/BinaryTree.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/DSAwithJava.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/ArrayList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/ArrayList.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/DoublyLinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/DoublyLinkedList.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/LinkedList.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/LinkedListTests.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/LinkedListTests.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/List.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/List.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/ListNode.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkList/Test.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkList/Test.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/ArrayQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/ArrayQueue.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/CircularQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/CircularQueue.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/LinkedList.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/Node.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/Node.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/PriorityLinked.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/PriorityLinked.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/PriorityQueue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/PriorityQueue.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedQueue/Queue.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedQueue/Queue.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedStack/ArrayStack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedStack/ArrayStack.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedStack/LinkedList.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedStack/LinkedList.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedStack/ListNode.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedStack/ListNode.class -------------------------------------------------------------------------------- /DSAwithJava/out/production/DSAwithJava/LinkedStack/Stack.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodeAvecAsnit/DSA/c9ef9f5c04a0aac0d066addd93fcac14270e29ba/DSAwithJava/out/production/DSAwithJava/LinkedStack/Stack.class --------------------------------------------------------------------------------