├── Parent ├── README.md ├── STACK │ └── main.c ├── calloc basic │ └── source code ├── circularlinked list │ └── Main.java ├── deletion of a bst │ └── source code ├── depth of the tree │ └── Main.java ├── double ended queue │ └── Main.java ├── doubly │ └── Main.java ├── heterogeneous array list │ └── source code ├── malloc basic │ └── main.c ├── queue using array implementation │ └── source code ├── realloc │ └── main.c ├── relloc still user input │ └── main.c ├── reverse a singly list │ └── Main.java ├── simple array insertion │ └── Main.java ├── singly linked insertion and display │ └── main.c ├── singly linked list basic │ └── main.c ├── singly linked list in java using switch │ └── Main.java ├── singly linked list in java │ └── Main.java ├── stack │ └── Main.java ├── tree │ └── Main.java └── valid paranthesis │ └── Main.java ├── README.md ├── STACK └── main.c ├── calloc basic └── source code ├── circularlinked list └── Main.java ├── deletion of a bst └── source code ├── depth of the tree └── Main.java ├── double ended queue └── Main.java ├── doubly └── Main.java ├── heterogeneous array list └── source code ├── malloc basic └── main.c ├── queue using array implementation └── source code ├── realloc └── main.c ├── relloc still user input └── main.c ├── reverse a singly list └── Main.java ├── simple array insertion └── Main.java ├── singly linked insertion and display └── main.c ├── singly linked list basic └── main.c ├── singly linked list in java using switch └── Main.java ├── singly linked list in java └── Main.java ├── stack └── Main.java ├── tree └── Main.java └── valid paranthesis └── Main.java /Parent/README.md: -------------------------------------------------------------------------------- 1 | # DataStructure_Java 2 | # DataStructure_Java 3 | -------------------------------------------------------------------------------- /Parent/STACK/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define s 5 4 | int top=-1,a[5]; 5 | void push(); 6 | void pop(); 7 | void max(); 8 | void print(); 9 | void min(); 10 | void odd(); 11 | void even(); 12 | int main() 13 | { 14 | int ch; 15 | while(1) 16 | { 17 | 18 | printf("\n1.PUSH operation"); 19 | printf("\n2.POP operation"); 20 | printf("\n3.PRINT operation"); 21 | printf("\n4.maximum "); 22 | printf("\n5.minimum "); 23 | printf("\n6.odd "); 24 | printf("\n7.even "); 25 | printf("\n"); 26 | printf("enter your choice"); 27 | scanf("%d",&ch); 28 | switch(ch) 29 | { 30 | case 1: 31 | push(); 32 | break; 33 | case 2: 34 | pop(); 35 | break; 36 | case 3: 37 | print(); 38 | break; 39 | case 4: 40 | max(); 41 | case 8: 42 | exit(1); 43 | break; 44 | case 5: 45 | min(); 46 | break; 47 | case 6: 48 | odd(); 49 | break; 50 | 51 | case 7: 52 | even(); 53 | break; 54 | 55 | default: 56 | printf("\n Enter correct choice:"); 57 | break; 58 | } 59 | } 60 | } 61 | void push(){ 62 | int x; 63 | if(top==s-1) 64 | { 65 | printf("stack overflow"); 66 | } 67 | else 68 | { 69 | printf("enter the element to be inserted:"); 70 | scanf("%d",&x); 71 | top=top+1; 72 | a[top]=x; 73 | } 74 | } 75 | void pop() 76 | { 77 | if(top==-1) 78 | { 79 | printf("stack overflow"); 80 | } 81 | else 82 | { 83 | printf("the deleted element is:"); 84 | printf("%d",a[top]); 85 | top=top-1; 86 | } 87 | } 88 | void print() 89 | { 90 | if(top==-1) 91 | { 92 | printf("stack overflow"); 93 | } 94 | else 95 | { 96 | for(int i=0;i>=0;--i) 97 | { 98 | printf("%d",a[i]); 99 | } 100 | } 101 | 102 | } 103 | void max() 104 | { 105 | int max=0; 106 | for(int i=0;i<=top;i++) 107 | { 108 | if(a[i]>=max) 109 | { 110 | max=a[i]; 111 | } 112 | } 113 | printf("%d",max); 114 | 115 | } 116 | void min() 117 | { 118 | int min=a[0]; 119 | for(int i=0;i<=top;i++) 120 | { 121 | if(a[i]<=min) 122 | { 123 | min=a[i]; 124 | } 125 | } 126 | printf("%d",min); 127 | 128 | } 129 | void odd() 130 | { 131 | for(int i=0;i<=top;i++) 132 | { 133 | if(a[i]%2!=0) 134 | { 135 | printf("%d",a[i]); 136 | } 137 | } 138 | 139 | } 140 | void even() 141 | { 142 | for(int i=0;i<=top;i++) 143 | { 144 | if(a[i]%2==0) 145 | { 146 | printf("%d",a[i]); 147 | } 148 | } 149 | 150 | } -------------------------------------------------------------------------------- /Parent/calloc basic/source code: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)calloc(n,sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;ilen) 53 | { 54 | System.out.println("enter valid position"); 55 | } 56 | else*/ if(pos==0) 57 | { 58 | node tem=head; 59 | head=t; 60 | t.next=tem; 61 | }else{ 62 | int a=1; 63 | node temp=head; 64 | while(a!=pos) 65 | { 66 | temp=temp.next; 67 | a++; 68 | } 69 | node te=temp.next; 70 | temp.next=t; 71 | t.next=te; 72 | } 73 | } 74 | void display() 75 | { 76 | if(head==null) 77 | { 78 | System.out.print("list is empty"); 79 | } 80 | else{ 81 | node temp=head; 82 | do{ 83 | System.out.print(temp.data+" "); 84 | temp=temp.next; 85 | 86 | }while(temp!=head); 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /Parent/deletion of a bst/source code: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | BST o=new BST(); 6 | o.insert(5); 7 | o.insert(3); 8 | o.insert(1); 9 | o.insert(11); 10 | o.insert(6); 11 | o.insert(12); 12 | o.insert(13); 13 | o.delete(o.Root,3); 14 | o.delete(o.Root,1); 15 | o.delete(o.Root,11); 16 | o.inorder(o.Root); 17 | } 18 | } 19 | class node{ 20 | int data; 21 | node left,right; 22 | node(int value){ 23 | data=value; 24 | left=right=null; 25 | } 26 | } 27 | class BST{ 28 | void inorder(node temp){ 29 | if(temp!=null){ 30 | inorder(temp.left); 31 | System.out.print(temp.data+" "); 32 | inorder(temp.right); 33 | } 34 | } 35 | node Root; 36 | void insert(int key){ 37 | Root=inserting(Root,key); 38 | } 39 | node inserting(node duplicateRoot,int key){ 40 | if(duplicateRoot==null) 41 | { 42 | duplicateRoot=new node(key); 43 | return duplicateRoot; 44 | } 45 | if(key>duplicateRoot.data) 46 | duplicateRoot.right=inserting(duplicateRoot.right,key); 47 | else if(keytemp.data) 57 | temp.right=delete(temp.right,target); 58 | else if(targetdupRoot.data){ 37 | dupRoot.right=insertRecursively(dupRoot.right,key); 38 | } 39 | else if(key=l|| pos<0){ 84 | System.out.println("enter valid position"); 85 | } 86 | else{ 87 | int i=0; 88 | node temp=head; 89 | node temp1=head; 90 | while(i!=pos){ 91 | temp1=temp; 92 | temp=temp.next; 93 | i++; 94 | } 95 | 96 | temp1.next=t; 97 | t.prev=temp1; 98 | t.next=temp; 99 | temp.prev=t; 100 | } 101 | 102 | } 103 | void length() 104 | { 105 | l=0; 106 | node temp=head; 107 | do { 108 | temp =temp.next; 109 | l++; 110 | 111 | } while(temp!=tail); 112 | //System.out.println(l+1); 113 | } 114 | 115 | } -------------------------------------------------------------------------------- /Parent/heterogeneous array list/source code: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main { 3 | public static void main(String[] args) { 4 | arrayList1 a=new arrayList1(); 5 | a.insert(1); 6 | a.insert(2); 7 | a.insert(3); 8 | a.insert(4); 9 | a.insert(5); 10 | a.display(); 11 | arrayList1 b=new arrayList1(); 12 | b.insert("a"); 13 | b.insert("b"); 14 | b.insert("c"); 15 | b.insert("d"); 16 | b.insert("e"); 17 | b.display(); 18 | } 19 | } 20 | class arrayList1{ 21 | final static int initialSize=4; 22 | private anytype a[]=(anytype[])new Object[initialSize]; 23 | int index=0,capacity=initialSize; 24 | 25 | void insert(anytype value){ 26 | if(index==capacity) 27 | grow(); 28 | a[index++]=value; 29 | } 30 | 31 | void grow(){ 32 | capacity=capacity*2; 33 | a=Arrays.copyOf(a,capacity); 34 | } 35 | 36 | void display(){ 37 | if(index==0) 38 | System.out.println("List Is Empty"); 39 | else{ 40 | for(int i=0;i 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)malloc(n*sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;i 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)malloc(n*sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;i 3 | #include 4 | int main() 5 | { 6 | int n,*p=NULL; 7 | scanf("%d",&n); 8 | p=(int*)malloc(n*sizeof(int)); 9 | if(p==NULL) 10 | { 11 | 12 | printf("no memory allocated"); 13 | } 14 | else 15 | { 16 | for(int i=0;icapacity||pos<0) 51 | { 52 | System.out.println("enter valid position"); 53 | return; 54 | } 55 | if(pos==index) 56 | { 57 | a[pos]=value; 58 | return; 59 | } 60 | else 61 | { 62 | if(index==capacity) 63 | { 64 | grow(); 65 | } 66 | 67 | for(int i=index;i>pos;i--) 68 | { 69 | a[i]=a[i-1]; 70 | } 71 | a[pos]=value; 72 | index++; 73 | } 74 | 75 | 76 | } 77 | void DeleteParticularIndex(int pos){ 78 | if(index==0) 79 | { 80 | System.out.println("no element to delete"); 81 | return; 82 | } 83 | if(pos>=capacity){ 84 | System.out.println("give correct index"); 85 | return; 86 | } 87 | else{ 88 | for(int i=pos;i 3 | #include 4 | void insert(); 5 | void display(); 6 | struct node{ 7 | int data; 8 | struct node*next; 9 | }*head=NULL,*ne,*temp; 10 | 11 | int main() 12 | { 13 | int n; 14 | head=(struct node*)malloc(sizeof(struct node)); 15 | printf("enter the head value:"); 16 | scanf("%d",&head->data); 17 | head->next=NULL; 18 | temp=head; 19 | printf("enter how many node created:"); 20 | int k; 21 | scanf("%d",&k); 22 | for(int i=0;idata); 35 | temp->next=ne; 36 | temp=ne; 37 | ne->next=NULL; 38 | 39 | 40 | } 41 | 42 | void display() 43 | { 44 | temp=head; 45 | while(temp!=NULL) 46 | { 47 | printf("%d ",temp->data); 48 | temp=temp->next; 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /Parent/singly linked list basic/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | struct node{ 5 | int data; 6 | struct node*next; 7 | }*head=NULL; 8 | 9 | int main() 10 | { 11 | int n; 12 | struct node*temp,*ne; 13 | head=(struct node*)malloc(sizeof(struct node)); 14 | printf("enter the head value:"); 15 | scanf("%d",&head->data); 16 | head->next=NULL; 17 | temp=head; 18 | int choice; 19 | do 20 | { 21 | ne=(struct node*)malloc(sizeof(struct node)); 22 | printf("enter the element to be inserted:"); 23 | scanf("%d",&ne->data); 24 | temp->next=ne; 25 | temp=ne; 26 | ne->next=NULL; 27 | printf("enter your choice:"); 28 | scanf("%d",&choice); 29 | 30 | 31 | }while(choice!=0); 32 | temp=head; 33 | while(temp!=NULL) 34 | { 35 | printf("%d ",temp->data); 36 | temp=temp->next; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /Parent/singly linked list in java using switch/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main (String[]args) 5 | { 6 | System.out.println 7 | ("**********************welcome to kidumutti shop******************************"); 8 | Scanner scanner = new Scanner (System.in); 9 | Singly a = new Singly (); 10 | a.length (); 11 | int choice = 0, value = 0, pos = 0; 12 | do 13 | { 14 | System.out.println ("1. Insert at beginning"); 15 | System.out.println ("2. Insert at end"); 16 | System.out.println ("3. InsertAtMiddle"); 17 | System.out.println ("4. DeleteBegin"); 18 | System.out.println ("5. DeleteAtMid"); 19 | System.out.println ("6. DeleteAtEnd"); 20 | System.out.println ("7. length"); 21 | System.out.println ("8. Display"); 22 | System.out.println ("0. Exit"); 23 | System.out.print ("Enter your choice: "); 24 | choice = scanner.nextInt (); 25 | switch (choice) 26 | { 27 | case 1: 28 | System.out.print ("Enter value: "); 29 | value = scanner.nextInt (); 30 | a.InsertAtBegin (value); 31 | System.out.println ("value is successfully inserted"); 32 | break; 33 | case 2:System.out.print ("Enter value: "); 34 | value = scanner.nextInt (); 35 | a.InsertAtEnd (value); 36 | System.out.println ("value is successfully inserted"); 37 | break; 38 | case 3:System.out.println ("Enter value to InsertAtMiddle: "); 39 | value = scanner.nextInt (); 40 | System.out.println ("Enter position"); 41 | pos = scanner.nextInt (); 42 | a.InsertAtMiddle (value, pos); 43 | System.out.println ("value is successfully inserted"); 44 | break; 45 | case 4:a.DeleteBegin (); 46 | System.out.println ("value is successfully delted"); 47 | break; 48 | case 5:System.out.println ("enter the position to be deleted"); 49 | pos = scanner.nextInt (); 50 | a.DeleteAtMid (pos); 51 | System.out.println ("value is successfully deleted"); 52 | break; 53 | case 6:a.DeleteAtEnd (); 54 | System.out.println ("value is successfully deleted"); 55 | break; 56 | 57 | case 7:a.length (); 58 | break; 59 | case 8:a.Display (); 60 | break; 61 | case 0:System.out.println ("mudunchu poda"); 62 | break; 63 | 64 | default:System.out.println ("Invalid choice."); 65 | } 66 | } 67 | while (choice != 0); 68 | 69 | } 70 | } 71 | 72 | class Singly 73 | { 74 | int len = 0; 75 | node head; 76 | class node 77 | { 78 | int data; 79 | node next; 80 | node (int value) 81 | { 82 | data = value; 83 | 84 | } 85 | } 86 | void InsertAtEnd (int insertValue) 87 | { 88 | node t = new node (insertValue); 89 | if (head == null) 90 | { 91 | head = t; 92 | } 93 | else 94 | { 95 | node temp = head; 96 | while (temp.next != null) 97 | { 98 | temp = temp.next; 99 | } 100 | temp.next = t; 101 | 102 | } 103 | 104 | 105 | } 106 | void Display () 107 | { 108 | if (head == null) 109 | { 110 | System.out.println ("list is empty"); 111 | } 112 | else 113 | { 114 | node temp = head; 115 | while (temp != null) 116 | { 117 | System.out.print (temp.data + " "); 118 | temp = temp.next; 119 | } 120 | System.out.println (); 121 | 122 | } 123 | } 124 | void InsertAtMiddle (int insertValue, int pos) 125 | { 126 | node t = new node (insertValue); 127 | if (head == null) 128 | { 129 | System.out.println ("list is empty"); 130 | } 131 | if (pos > len) 132 | 133 | { 134 | System.out.println ("enter valid position"); 135 | } 136 | else if (pos == 0) 137 | { 138 | node tem = head; 139 | head = t; 140 | t.next = tem; 141 | } 142 | else 143 | { 144 | int a = 1; 145 | node temp = head; 146 | while (a != pos) 147 | { 148 | temp = temp.next; 149 | a++; 150 | } 151 | node te = temp.next; 152 | temp.next = t; 153 | t.next = te; 154 | } 155 | } 156 | void InsertAtBegin (int insertValue) 157 | { 158 | node t = new node (insertValue); 159 | if (head == null) 160 | { 161 | head = t; 162 | } 163 | else 164 | { 165 | node temp = head; 166 | head = t; 167 | head.next = temp; 168 | } 169 | } 170 | void DeleteAtEnd () 171 | { 172 | if (head == null) 173 | { 174 | System.out.println ("no element to delete"); 175 | } 176 | node temp = head; 177 | while (temp.next.next != null) 178 | { 179 | temp = temp.next; 180 | } 181 | temp.next = null; 182 | } 183 | void DeleteBegin () 184 | { 185 | if (head == null) 186 | { 187 | System.out.println ("no element to delete"); 188 | } 189 | node temp = head.next; 190 | head = temp; 191 | 192 | } 193 | void DeleteAtMid (int pos) 194 | { 195 | if (pos >= len) 196 | { 197 | System.out.println ("enter valid position"); 198 | } 199 | else{ 200 | if (head == null) 201 | { 202 | System.out.println ("no element to delete"); 203 | } 204 | node temp = head; 205 | 206 | if (pos == 0) 207 | { 208 | head = head.next; 209 | 210 | } 211 | else 212 | { 213 | int a = 1; 214 | while (a != pos) 215 | { 216 | temp = temp.next; 217 | a++; 218 | } 219 | node te = temp.next; 220 | temp.next = te.next; 221 | //te=null; 222 | 223 | } 224 | } 225 | } 226 | void length () 227 | { 228 | node temp = head; 229 | while (temp != null) 230 | { 231 | len++; 232 | temp = temp.next; 233 | } 234 | 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /Parent/singly linked list in java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main 2 | { 3 | public static void main(String[] args) { 4 | System.out.println("Hello World"); 5 | 6 | Singly a=new Singly(); 7 | a.InsertAtEnd(2); 8 | a.InsertAtEnd(3); 9 | a.InsertAtEnd(4); 10 | a.InsertAtBegin(9); 11 | a.InsertAtMiddle(8,34); 12 | a.Display(); 13 | a.DeleteAtEnd(); 14 | a.Display(); 15 | a.DeleteBegin(); 16 | a.Display(); 17 | a.DeleteAtMid(0); 18 | a.Display(); 19 | a.length(); 20 | 21 | } 22 | } 23 | class Singly{ 24 | int len=0; 25 | node head; 26 | class node{ 27 | int data; 28 | node next; 29 | node(int value){ 30 | data=value; 31 | 32 | } 33 | } 34 | void InsertAtEnd(int insertValue){ 35 | node t=new node(insertValue); 36 | if(head==null){ 37 | head=t; 38 | } 39 | else{ 40 | node temp=head; 41 | while(temp.next!=null) 42 | { 43 | temp=temp.next; 44 | } 45 | temp.next=t; 46 | 47 | } 48 | 49 | 50 | } 51 | void Display() 52 | { 53 | if(head==null) 54 | { 55 | System.out.println("list is empty"); 56 | } 57 | else{ 58 | node temp=head; 59 | while(temp!=null) 60 | { 61 | System.out.print(temp.data); 62 | temp=temp.next; 63 | } 64 | System.out.println(); 65 | 66 | } 67 | } 68 | void InsertAtMiddle(int insertValue,int pos){ 69 | node t=new node(insertValue); 70 | if(pos>len) 71 | { 72 | System.out.println("enter valid position"); 73 | } 74 | else if(pos==0) 75 | { 76 | node tem=head; 77 | head=t; 78 | t.next=tem; 79 | }else{ 80 | int a=1; 81 | node temp=head; 82 | while(a!=pos) 83 | { 84 | temp=temp.next; 85 | a++; 86 | } 87 | node te=temp.next; 88 | temp.next=t; 89 | t.next=te; 90 | } 91 | } 92 | void InsertAtBegin(int insertValue){ 93 | node t=new node(insertValue); 94 | if(head==null){ 95 | head=t; 96 | } 97 | else{ 98 | node temp=head; 99 | head=t; 100 | head.next=temp; 101 | } 102 | } 103 | void DeleteAtEnd(){ 104 | if(head==null) 105 | { 106 | System.out.println("no element to delete"); 107 | } 108 | node temp=head; 109 | while(temp.next.next!=null) 110 | { 111 | temp=temp.next; 112 | } 113 | temp.next=null; 114 | } 115 | void DeleteBegin(){ 116 | node temp=head.next; 117 | head=temp; 118 | 119 | } 120 | void DeleteAtMid(int pos){ 121 | node temp=head; 122 | if(pos>len) 123 | { 124 | System.out.println("enter valid position"); 125 | } 126 | else if(pos==0) 127 | { 128 | head=head.next; 129 | 130 | } 131 | else{ 132 | int a=1; 133 | while(a!=pos) 134 | { 135 | temp=temp.next; 136 | a++; 137 | } 138 | node te=temp.next; 139 | temp.next=te.next; 140 | // te=null; 141 | 142 | } 143 | } 144 | void length() 145 | { 146 | node temp=head; 147 | while(temp!=null) 148 | { 149 | len++; 150 | temp=temp.next; 151 | } 152 | System.out.println(len); 153 | } 154 | } -------------------------------------------------------------------------------- /Parent/stack/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | 6 | Scanner scanner=new Scanner(System.in); 7 | int capacity=scanner.nextInt(); 8 | int choice,value; 9 | Stack a=new Stack(capacity); 10 | do 11 | { 12 | System.out.println ("1. push operation"); 13 | System.out.println ("2. pop operation"); 14 | System.out.println ("3. peek operation"); 15 | System.out.println ("4. isempty operation"); 16 | System.out.println ("5. is full operation"); 17 | System.out.println ("6. Display"); 18 | System.out.println ("0. Exit"); 19 | System.out.print ("Enter your choice: "); 20 | choice = scanner.nextInt (); 21 | switch (choice) 22 | { 23 | case 1: 24 | System.out.print ("Enter valueto be push: "); 25 | value = scanner.nextInt (); 26 | a.push(value); 27 | System.out.println ("value is successfully pushed"); 28 | break; 29 | case 2: 30 | a.pop(); 31 | System.out.println ("value is successfully poped"); 32 | break; 33 | case 3: 34 | a.peek(); 35 | break; 36 | case 4: 37 | a.isempty(); 38 | break; 39 | case 5: 40 | a.isfull(); 41 | break; 42 | case 6: 43 | a.display(); 44 | break; 45 | case 0:System.out.println ("mudunchu poda"); 46 | break; 47 | default:System.out.println ("Invalid choice."); 48 | } 49 | } 50 | while (choice != 0); 51 | 52 | 53 | } 54 | } 55 | class Stack{ 56 | final int size; 57 | int top=-1; 58 | int a[]; 59 | Stack(int cap){ 60 | size=cap; 61 | a=new int[size]; 62 | } 63 | void push(int data) 64 | { 65 | if(top==size-1) 66 | { 67 | System.out.println("stack is overflow"); 68 | } 69 | else{ 70 | 71 | a[++top]=data; 72 | } 73 | } 74 | void pop() 75 | { 76 | if(top==-1) 77 | { 78 | System.out.println("stack is underflow"); 79 | } 80 | else{ 81 | top--; 82 | } 83 | } 84 | void display(){ 85 | if(top==-1){ 86 | System.out.println("stack is empty"); 87 | } 88 | else{ 89 | for(int i=0;i<=top;i++) 90 | { 91 | System.out.print(a[i]+" "); 92 | } 93 | System.out.println(" "); 94 | } 95 | 96 | } 97 | void peek() 98 | { 99 | if(top==-1) 100 | { 101 | System.out.println("stack is underflow"); 102 | } 103 | else{ 104 | System.out.println("the peek element is:"+a[top]); 105 | } 106 | } 107 | void isfull() 108 | { 109 | if(top==size) 110 | { 111 | System.out.println("stack is full"); 112 | } 113 | else{ 114 | System.out.println("Stack is not full"); 115 | } 116 | } 117 | void isempty() 118 | { 119 | if(top==-1) 120 | { 121 | System.out.println("Stack is empty"); 122 | } 123 | else{ 124 | System.out.println("Stack is not empty"); 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /Parent/tree/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | BST o=new BST(); 7 | for(int i=0;i<5;i++) 8 | o.insert(sc.nextInt()); 9 | System.out.print("Inorder :"); 10 | o.inorder(o.Realroot); 11 | System.out.println(); 12 | System.out.print("preorder :"); 13 | o.preorder(o.Realroot); 14 | System.out.println(); 15 | System.out.print("postorder :"); 16 | o.postorder(o.Realroot); 17 | System.out.println(); 18 | System.out.println(o.Search(o.Realroot,4)); 19 | o.count(o.Realroot); 20 | System.out.println(o.cout); 21 | o.minimum(o.Realroot); 22 | o.maximum(o.Realroot); 23 | o.secondmaximum(o.Realroot); 24 | o.secondminimum(o.Realroot); 25 | } 26 | } 27 | class node{ 28 | int data; 29 | node left,right; 30 | node(int key){ 31 | data=key; 32 | left=right=null; 33 | } 34 | } 35 | 36 | class BST{ 37 | node Realroot; 38 | void insert(int key){ 39 | Realroot=insertRecursively(Realroot,key); 40 | } 41 | node insertRecursively(node dupRoot,int key){ 42 | 43 | if(dupRoot==null){ 44 | dupRoot=new node(key); 45 | return dupRoot; 46 | } 47 | if(key>dupRoot.data){ 48 | dupRoot.right=insertRecursively(dupRoot.right,key); 49 | } 50 | else if(keytemp.data){ 90 | return Search(temp.right,key); 91 | } 92 | else{ 93 | return true; 94 | } 95 | } 96 | int cout=1; 97 | void count(node root) 98 | { 99 | if(root!=null) 100 | { 101 | count(root.left); 102 | // System.out.print(root.data+" "); 103 | cout=cout+1; 104 | count(root.right); 105 | } 106 | } 107 | int minva=0; 108 | int maxva=0; 109 | void minimum(node temp) 110 | { 111 | if(temp==null) 112 | { 113 | System.out.println("tree is empty"); 114 | } 115 | else{ 116 | while(temp!=null) 117 | { 118 | minva=temp.data; 119 | temp=temp.left; 120 | } 121 | System.out.println("minimum value is :"+ minva); 122 | } 123 | 124 | } 125 | void maximum(node temp) 126 | { 127 | if(temp==null) 128 | { 129 | System.out.println("tree is empty"); 130 | } 131 | else{ 132 | while(temp!=null) 133 | { 134 | maxva=temp.data; 135 | temp=temp.right; 136 | } 137 | System.out.println("maximum value is :"+ maxva); 138 | } 139 | 140 | } 141 | int secmax=0; 142 | void secondmaximum(node temp) 143 | { 144 | if(temp==null) 145 | { 146 | System.out.println("tree is empty"); 147 | } 148 | else{ 149 | int m=temp.data; 150 | while(temp!=null) 151 | { 152 | m=temp.data; 153 | secmax=m; 154 | temp=temp.right; 155 | } 156 | System.out.println("second maximum value is :"+ secmax); 157 | } 158 | 159 | } 160 | int secmin=0; 161 | void secondminimum(node temp) 162 | { 163 | if(temp==null) 164 | { 165 | System.out.println("tree is empty"); 166 | } 167 | else{ 168 | while(temp.left!=null) 169 | { 170 | secmin=temp.data; 171 | temp=temp.left; 172 | } 173 | System.out.println("second minimum value is :"+ secmin); 174 | } 175 | 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /Parent/valid paranthesis/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | 6 | Scanner scanner=new Scanner(System.in); 7 | int capacity=scanner.nextInt(); 8 | int choice,value; 9 | Stack stack=new Stack(capacity); 10 | String s=scanner.next(); 11 | for(char c : s.toCharArray()) 12 | { 13 | if(c=='{') 14 | { 15 | stack.push('}'); 16 | } 17 | else if(c=='(') 18 | { 19 | stack.push(')'); 20 | } 21 | else if(c=='[') 22 | { 23 | stack.push(']'); 24 | } 25 | else if(stack.isempty() || stack.pop() != c) 26 | { 27 | System.out.println("false"); 28 | return; 29 | } 30 | } 31 | System.out.println("true"); 32 | return; 33 | } 34 | } 35 | 36 | class Stack{ 37 | final int size; 38 | int top=-1; 39 | int a[]; 40 | Stack(int cap){ 41 | size=cap; 42 | a=new int[size]; 43 | } 44 | void push(char data) 45 | { 46 | if(top==size-1) 47 | { 48 | System.out.println("stack is overflow"); 49 | } 50 | else{ 51 | 52 | a[++top]=data; 53 | } 54 | } 55 | char pop() 56 | { 57 | if(top==-1) 58 | { 59 | System.out.println("stack is underflow"); 60 | } 61 | else{ 62 | top--; 63 | } 64 | //return true; 65 | } 66 | boolean isempty() 67 | { 68 | if(top==-1) 69 | { 70 | return false; 71 | } 72 | return true; 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DataStructure_Java 2 | # DataStructure_Java 3 | -------------------------------------------------------------------------------- /STACK/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define s 5 4 | int top=-1,a[5]; 5 | void push(); 6 | void pop(); 7 | void max(); 8 | void print(); 9 | void min(); 10 | void odd(); 11 | void even(); 12 | int main() 13 | { 14 | int ch; 15 | while(1) 16 | { 17 | 18 | printf("\n1.PUSH operation"); 19 | printf("\n2.POP operation"); 20 | printf("\n3.PRINT operation"); 21 | printf("\n4.maximum "); 22 | printf("\n5.minimum "); 23 | printf("\n6.odd "); 24 | printf("\n7.even "); 25 | printf("\n"); 26 | printf("enter your choice"); 27 | scanf("%d",&ch); 28 | switch(ch) 29 | { 30 | case 1: 31 | push(); 32 | break; 33 | case 2: 34 | pop(); 35 | break; 36 | case 3: 37 | print(); 38 | break; 39 | case 4: 40 | max(); 41 | case 8: 42 | exit(1); 43 | break; 44 | case 5: 45 | min(); 46 | break; 47 | case 6: 48 | odd(); 49 | break; 50 | 51 | case 7: 52 | even(); 53 | break; 54 | 55 | default: 56 | printf("\n Enter correct choice:"); 57 | break; 58 | } 59 | } 60 | } 61 | void push(){ 62 | int x; 63 | if(top==s-1) 64 | { 65 | printf("stack overflow"); 66 | } 67 | else 68 | { 69 | printf("enter the element to be inserted:"); 70 | scanf("%d",&x); 71 | top=top+1; 72 | a[top]=x; 73 | } 74 | } 75 | void pop() 76 | { 77 | if(top==-1) 78 | { 79 | printf("stack overflow"); 80 | } 81 | else 82 | { 83 | printf("the deleted element is:"); 84 | printf("%d",a[top]); 85 | top=top-1; 86 | } 87 | } 88 | void print() 89 | { 90 | if(top==-1) 91 | { 92 | printf("stack overflow"); 93 | } 94 | else 95 | { 96 | for(int i=0;i>=0;--i) 97 | { 98 | printf("%d",a[i]); 99 | } 100 | } 101 | 102 | } 103 | void max() 104 | { 105 | int max=0; 106 | for(int i=0;i<=top;i++) 107 | { 108 | if(a[i]>=max) 109 | { 110 | max=a[i]; 111 | } 112 | } 113 | printf("%d",max); 114 | 115 | } 116 | void min() 117 | { 118 | int min=a[0]; 119 | for(int i=0;i<=top;i++) 120 | { 121 | if(a[i]<=min) 122 | { 123 | min=a[i]; 124 | } 125 | } 126 | printf("%d",min); 127 | 128 | } 129 | void odd() 130 | { 131 | for(int i=0;i<=top;i++) 132 | { 133 | if(a[i]%2!=0) 134 | { 135 | printf("%d",a[i]); 136 | } 137 | } 138 | 139 | } 140 | void even() 141 | { 142 | for(int i=0;i<=top;i++) 143 | { 144 | if(a[i]%2==0) 145 | { 146 | printf("%d",a[i]); 147 | } 148 | } 149 | 150 | } -------------------------------------------------------------------------------- /calloc basic/source code: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)calloc(n,sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;ilen) 53 | { 54 | System.out.println("enter valid position"); 55 | } 56 | else*/ if(pos==0) 57 | { 58 | node tem=head; 59 | head=t; 60 | t.next=tem; 61 | }else{ 62 | int a=1; 63 | node temp=head; 64 | while(a!=pos) 65 | { 66 | temp=temp.next; 67 | a++; 68 | } 69 | node te=temp.next; 70 | temp.next=t; 71 | t.next=te; 72 | } 73 | } 74 | void display() 75 | { 76 | if(head==null) 77 | { 78 | System.out.print("list is empty"); 79 | } 80 | else{ 81 | node temp=head; 82 | do{ 83 | System.out.print(temp.data+" "); 84 | temp=temp.next; 85 | 86 | }while(temp!=head); 87 | } 88 | } 89 | } -------------------------------------------------------------------------------- /deletion of a bst/source code: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | BST o=new BST(); 6 | o.insert(5); 7 | o.insert(3); 8 | o.insert(1); 9 | o.insert(11); 10 | o.insert(6); 11 | o.insert(12); 12 | o.insert(13); 13 | o.delete(o.Root,3); 14 | o.delete(o.Root,1); 15 | o.delete(o.Root,11); 16 | o.inorder(o.Root); 17 | } 18 | } 19 | class node{ 20 | int data; 21 | node left,right; 22 | node(int value){ 23 | data=value; 24 | left=right=null; 25 | } 26 | } 27 | class BST{ 28 | void inorder(node temp){ 29 | if(temp!=null){ 30 | inorder(temp.left); 31 | System.out.print(temp.data+" "); 32 | inorder(temp.right); 33 | } 34 | } 35 | node Root; 36 | void insert(int key){ 37 | Root=inserting(Root,key); 38 | } 39 | node inserting(node duplicateRoot,int key){ 40 | if(duplicateRoot==null) 41 | { 42 | duplicateRoot=new node(key); 43 | return duplicateRoot; 44 | } 45 | if(key>duplicateRoot.data) 46 | duplicateRoot.right=inserting(duplicateRoot.right,key); 47 | else if(keytemp.data) 57 | temp.right=delete(temp.right,target); 58 | else if(targetdupRoot.data){ 37 | dupRoot.right=insertRecursively(dupRoot.right,key); 38 | } 39 | else if(key=l|| pos<0){ 84 | System.out.println("enter valid position"); 85 | } 86 | else{ 87 | int i=0; 88 | node temp=head; 89 | node temp1=head; 90 | while(i!=pos){ 91 | temp1=temp; 92 | temp=temp.next; 93 | i++; 94 | } 95 | 96 | temp1.next=t; 97 | t.prev=temp1; 98 | t.next=temp; 99 | temp.prev=t; 100 | } 101 | 102 | } 103 | void length() 104 | { 105 | l=0; 106 | node temp=head; 107 | do { 108 | temp =temp.next; 109 | l++; 110 | 111 | } while(temp!=tail); 112 | //System.out.println(l+1); 113 | } 114 | 115 | } -------------------------------------------------------------------------------- /heterogeneous array list/source code: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main { 3 | public static void main(String[] args) { 4 | arrayList1 a=new arrayList1(); 5 | a.insert(1); 6 | a.insert(2); 7 | a.insert(3); 8 | a.insert(4); 9 | a.insert(5); 10 | a.display(); 11 | arrayList1 b=new arrayList1(); 12 | b.insert("a"); 13 | b.insert("b"); 14 | b.insert("c"); 15 | b.insert("d"); 16 | b.insert("e"); 17 | b.display(); 18 | } 19 | } 20 | class arrayList1{ 21 | final static int initialSize=4; 22 | private anytype a[]=(anytype[])new Object[initialSize]; 23 | int index=0,capacity=initialSize; 24 | 25 | void insert(anytype value){ 26 | if(index==capacity) 27 | grow(); 28 | a[index++]=value; 29 | } 30 | 31 | void grow(){ 32 | capacity=capacity*2; 33 | a=Arrays.copyOf(a,capacity); 34 | } 35 | 36 | void display(){ 37 | if(index==0) 38 | System.out.println("List Is Empty"); 39 | else{ 40 | for(int i=0;i 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)malloc(n*sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;i 3 | #include 4 | 5 | 6 | int main() 7 | { 8 | int n,*p=NULL; 9 | scanf("%d",&n); 10 | p=(int*)malloc(n*sizeof(int)); 11 | if(p==NULL) 12 | { 13 | 14 | printf("no memory allocated"); 15 | } 16 | else 17 | { 18 | for(int i=0;i 3 | #include 4 | int main() 5 | { 6 | int n,*p=NULL; 7 | scanf("%d",&n); 8 | p=(int*)malloc(n*sizeof(int)); 9 | if(p==NULL) 10 | { 11 | 12 | printf("no memory allocated"); 13 | } 14 | else 15 | { 16 | for(int i=0;icapacity||pos<0) 51 | { 52 | System.out.println("enter valid position"); 53 | return; 54 | } 55 | if(pos==index) 56 | { 57 | a[pos]=value; 58 | return; 59 | } 60 | else 61 | { 62 | if(index==capacity) 63 | { 64 | grow(); 65 | } 66 | 67 | for(int i=index;i>pos;i--) 68 | { 69 | a[i]=a[i-1]; 70 | } 71 | a[pos]=value; 72 | index++; 73 | } 74 | 75 | 76 | } 77 | void DeleteParticularIndex(int pos){ 78 | if(index==0) 79 | { 80 | System.out.println("no element to delete"); 81 | return; 82 | } 83 | if(pos>=capacity){ 84 | System.out.println("give correct index"); 85 | return; 86 | } 87 | else{ 88 | for(int i=pos;i 3 | #include 4 | void insert(); 5 | void display(); 6 | struct node{ 7 | int data; 8 | struct node*next; 9 | }*head=NULL,*ne,*temp; 10 | 11 | int main() 12 | { 13 | int n; 14 | head=(struct node*)malloc(sizeof(struct node)); 15 | printf("enter the head value:"); 16 | scanf("%d",&head->data); 17 | head->next=NULL; 18 | temp=head; 19 | printf("enter how many node created:"); 20 | int k; 21 | scanf("%d",&k); 22 | for(int i=0;idata); 35 | temp->next=ne; 36 | temp=ne; 37 | ne->next=NULL; 38 | 39 | 40 | } 41 | 42 | void display() 43 | { 44 | temp=head; 45 | while(temp!=NULL) 46 | { 47 | printf("%d ",temp->data); 48 | temp=temp->next; 49 | } 50 | } 51 | 52 | -------------------------------------------------------------------------------- /singly linked list basic/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | struct node{ 5 | int data; 6 | struct node*next; 7 | }*head=NULL; 8 | 9 | int main() 10 | { 11 | int n; 12 | struct node*temp,*ne; 13 | head=(struct node*)malloc(sizeof(struct node)); 14 | printf("enter the head value:"); 15 | scanf("%d",&head->data); 16 | head->next=NULL; 17 | temp=head; 18 | int choice; 19 | do 20 | { 21 | ne=(struct node*)malloc(sizeof(struct node)); 22 | printf("enter the element to be inserted:"); 23 | scanf("%d",&ne->data); 24 | temp->next=ne; 25 | temp=ne; 26 | ne->next=NULL; 27 | printf("enter your choice:"); 28 | scanf("%d",&choice); 29 | 30 | 31 | }while(choice!=0); 32 | temp=head; 33 | while(temp!=NULL) 34 | { 35 | printf("%d ",temp->data); 36 | temp=temp->next; 37 | } 38 | } 39 | 40 | -------------------------------------------------------------------------------- /singly linked list in java using switch/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main (String[]args) 5 | { 6 | System.out.println 7 | ("**********************welcome to kidumutti shop******************************"); 8 | Scanner scanner = new Scanner (System.in); 9 | Singly a = new Singly (); 10 | a.length (); 11 | int choice = 0, value = 0, pos = 0; 12 | do 13 | { 14 | System.out.println ("1. Insert at beginning"); 15 | System.out.println ("2. Insert at end"); 16 | System.out.println ("3. InsertAtMiddle"); 17 | System.out.println ("4. DeleteBegin"); 18 | System.out.println ("5. DeleteAtMid"); 19 | System.out.println ("6. DeleteAtEnd"); 20 | System.out.println ("7. length"); 21 | System.out.println ("8. Display"); 22 | System.out.println ("0. Exit"); 23 | System.out.print ("Enter your choice: "); 24 | choice = scanner.nextInt (); 25 | switch (choice) 26 | { 27 | case 1: 28 | System.out.print ("Enter value: "); 29 | value = scanner.nextInt (); 30 | a.InsertAtBegin (value); 31 | System.out.println ("value is successfully inserted"); 32 | break; 33 | case 2:System.out.print ("Enter value: "); 34 | value = scanner.nextInt (); 35 | a.InsertAtEnd (value); 36 | System.out.println ("value is successfully inserted"); 37 | break; 38 | case 3:System.out.println ("Enter value to InsertAtMiddle: "); 39 | value = scanner.nextInt (); 40 | System.out.println ("Enter position"); 41 | pos = scanner.nextInt (); 42 | a.InsertAtMiddle (value, pos); 43 | System.out.println ("value is successfully inserted"); 44 | break; 45 | case 4:a.DeleteBegin (); 46 | System.out.println ("value is successfully delted"); 47 | break; 48 | case 5:System.out.println ("enter the position to be deleted"); 49 | pos = scanner.nextInt (); 50 | a.DeleteAtMid (pos); 51 | System.out.println ("value is successfully deleted"); 52 | break; 53 | case 6:a.DeleteAtEnd (); 54 | System.out.println ("value is successfully deleted"); 55 | break; 56 | 57 | case 7:a.length (); 58 | break; 59 | case 8:a.Display (); 60 | break; 61 | case 0:System.out.println ("mudunchu poda"); 62 | break; 63 | 64 | default:System.out.println ("Invalid choice."); 65 | } 66 | } 67 | while (choice != 0); 68 | 69 | } 70 | } 71 | 72 | class Singly 73 | { 74 | int len = 0; 75 | node head; 76 | class node 77 | { 78 | int data; 79 | node next; 80 | node (int value) 81 | { 82 | data = value; 83 | 84 | } 85 | } 86 | void InsertAtEnd (int insertValue) 87 | { 88 | node t = new node (insertValue); 89 | if (head == null) 90 | { 91 | head = t; 92 | } 93 | else 94 | { 95 | node temp = head; 96 | while (temp.next != null) 97 | { 98 | temp = temp.next; 99 | } 100 | temp.next = t; 101 | 102 | } 103 | 104 | 105 | } 106 | void Display () 107 | { 108 | if (head == null) 109 | { 110 | System.out.println ("list is empty"); 111 | } 112 | else 113 | { 114 | node temp = head; 115 | while (temp != null) 116 | { 117 | System.out.print (temp.data + " "); 118 | temp = temp.next; 119 | } 120 | System.out.println (); 121 | 122 | } 123 | } 124 | void InsertAtMiddle (int insertValue, int pos) 125 | { 126 | node t = new node (insertValue); 127 | if (head == null) 128 | { 129 | System.out.println ("list is empty"); 130 | } 131 | if (pos > len) 132 | 133 | { 134 | System.out.println ("enter valid position"); 135 | } 136 | else if (pos == 0) 137 | { 138 | node tem = head; 139 | head = t; 140 | t.next = tem; 141 | } 142 | else 143 | { 144 | int a = 1; 145 | node temp = head; 146 | while (a != pos) 147 | { 148 | temp = temp.next; 149 | a++; 150 | } 151 | node te = temp.next; 152 | temp.next = t; 153 | t.next = te; 154 | } 155 | } 156 | void InsertAtBegin (int insertValue) 157 | { 158 | node t = new node (insertValue); 159 | if (head == null) 160 | { 161 | head = t; 162 | } 163 | else 164 | { 165 | node temp = head; 166 | head = t; 167 | head.next = temp; 168 | } 169 | } 170 | void DeleteAtEnd () 171 | { 172 | if (head == null) 173 | { 174 | System.out.println ("no element to delete"); 175 | } 176 | node temp = head; 177 | while (temp.next.next != null) 178 | { 179 | temp = temp.next; 180 | } 181 | temp.next = null; 182 | } 183 | void DeleteBegin () 184 | { 185 | if (head == null) 186 | { 187 | System.out.println ("no element to delete"); 188 | } 189 | node temp = head.next; 190 | head = temp; 191 | 192 | } 193 | void DeleteAtMid (int pos) 194 | { 195 | if (pos >= len) 196 | { 197 | System.out.println ("enter valid position"); 198 | } 199 | else{ 200 | if (head == null) 201 | { 202 | System.out.println ("no element to delete"); 203 | } 204 | node temp = head; 205 | 206 | if (pos == 0) 207 | { 208 | head = head.next; 209 | 210 | } 211 | else 212 | { 213 | int a = 1; 214 | while (a != pos) 215 | { 216 | temp = temp.next; 217 | a++; 218 | } 219 | node te = temp.next; 220 | temp.next = te.next; 221 | //te=null; 222 | 223 | } 224 | } 225 | } 226 | void length () 227 | { 228 | node temp = head; 229 | while (temp != null) 230 | { 231 | len++; 232 | temp = temp.next; 233 | } 234 | 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /singly linked list in java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main 2 | { 3 | public static void main(String[] args) { 4 | System.out.println("Hello World"); 5 | 6 | Singly a=new Singly(); 7 | a.InsertAtEnd(2); 8 | a.InsertAtEnd(3); 9 | a.InsertAtEnd(4); 10 | a.InsertAtBegin(9); 11 | a.InsertAtMiddle(8,34); 12 | a.Display(); 13 | a.DeleteAtEnd(); 14 | a.Display(); 15 | a.DeleteBegin(); 16 | a.Display(); 17 | a.DeleteAtMid(0); 18 | a.Display(); 19 | a.length(); 20 | 21 | } 22 | } 23 | class Singly{ 24 | int len=0; 25 | node head; 26 | class node{ 27 | int data; 28 | node next; 29 | node(int value){ 30 | data=value; 31 | 32 | } 33 | } 34 | void InsertAtEnd(int insertValue){ 35 | node t=new node(insertValue); 36 | if(head==null){ 37 | head=t; 38 | } 39 | else{ 40 | node temp=head; 41 | while(temp.next!=null) 42 | { 43 | temp=temp.next; 44 | } 45 | temp.next=t; 46 | 47 | } 48 | 49 | 50 | } 51 | void Display() 52 | { 53 | if(head==null) 54 | { 55 | System.out.println("list is empty"); 56 | } 57 | else{ 58 | node temp=head; 59 | while(temp!=null) 60 | { 61 | System.out.print(temp.data); 62 | temp=temp.next; 63 | } 64 | System.out.println(); 65 | 66 | } 67 | } 68 | void InsertAtMiddle(int insertValue,int pos){ 69 | node t=new node(insertValue); 70 | if(pos>len) 71 | { 72 | System.out.println("enter valid position"); 73 | } 74 | else if(pos==0) 75 | { 76 | node tem=head; 77 | head=t; 78 | t.next=tem; 79 | }else{ 80 | int a=1; 81 | node temp=head; 82 | while(a!=pos) 83 | { 84 | temp=temp.next; 85 | a++; 86 | } 87 | node te=temp.next; 88 | temp.next=t; 89 | t.next=te; 90 | } 91 | } 92 | void InsertAtBegin(int insertValue){ 93 | node t=new node(insertValue); 94 | if(head==null){ 95 | head=t; 96 | } 97 | else{ 98 | node temp=head; 99 | head=t; 100 | head.next=temp; 101 | } 102 | } 103 | void DeleteAtEnd(){ 104 | if(head==null) 105 | { 106 | System.out.println("no element to delete"); 107 | } 108 | node temp=head; 109 | while(temp.next.next!=null) 110 | { 111 | temp=temp.next; 112 | } 113 | temp.next=null; 114 | } 115 | void DeleteBegin(){ 116 | node temp=head.next; 117 | head=temp; 118 | 119 | } 120 | void DeleteAtMid(int pos){ 121 | node temp=head; 122 | if(pos>len) 123 | { 124 | System.out.println("enter valid position"); 125 | } 126 | else if(pos==0) 127 | { 128 | head=head.next; 129 | 130 | } 131 | else{ 132 | int a=1; 133 | while(a!=pos) 134 | { 135 | temp=temp.next; 136 | a++; 137 | } 138 | node te=temp.next; 139 | temp.next=te.next; 140 | // te=null; 141 | 142 | } 143 | } 144 | void length() 145 | { 146 | node temp=head; 147 | while(temp!=null) 148 | { 149 | len++; 150 | temp=temp.next; 151 | } 152 | System.out.println(len); 153 | } 154 | } -------------------------------------------------------------------------------- /stack/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | 6 | Scanner scanner=new Scanner(System.in); 7 | int capacity=scanner.nextInt(); 8 | int choice,value; 9 | Stack a=new Stack(capacity); 10 | do 11 | { 12 | System.out.println ("1. push operation"); 13 | System.out.println ("2. pop operation"); 14 | System.out.println ("3. peek operation"); 15 | System.out.println ("4. isempty operation"); 16 | System.out.println ("5. is full operation"); 17 | System.out.println ("6. Display"); 18 | System.out.println ("0. Exit"); 19 | System.out.print ("Enter your choice: "); 20 | choice = scanner.nextInt (); 21 | switch (choice) 22 | { 23 | case 1: 24 | System.out.print ("Enter valueto be push: "); 25 | value = scanner.nextInt (); 26 | a.push(value); 27 | System.out.println ("value is successfully pushed"); 28 | break; 29 | case 2: 30 | a.pop(); 31 | System.out.println ("value is successfully poped"); 32 | break; 33 | case 3: 34 | a.peek(); 35 | break; 36 | case 4: 37 | a.isempty(); 38 | break; 39 | case 5: 40 | a.isfull(); 41 | break; 42 | case 6: 43 | a.display(); 44 | break; 45 | case 0:System.out.println ("mudunchu poda"); 46 | break; 47 | default:System.out.println ("Invalid choice."); 48 | } 49 | } 50 | while (choice != 0); 51 | 52 | 53 | } 54 | } 55 | class Stack{ 56 | final int size; 57 | int top=-1; 58 | int a[]; 59 | Stack(int cap){ 60 | size=cap; 61 | a=new int[size]; 62 | } 63 | void push(int data) 64 | { 65 | if(top==size-1) 66 | { 67 | System.out.println("stack is overflow"); 68 | } 69 | else{ 70 | 71 | a[++top]=data; 72 | } 73 | } 74 | void pop() 75 | { 76 | if(top==-1) 77 | { 78 | System.out.println("stack is underflow"); 79 | } 80 | else{ 81 | top--; 82 | } 83 | } 84 | void display(){ 85 | if(top==-1){ 86 | System.out.println("stack is empty"); 87 | } 88 | else{ 89 | for(int i=0;i<=top;i++) 90 | { 91 | System.out.print(a[i]+" "); 92 | } 93 | System.out.println(" "); 94 | } 95 | 96 | } 97 | void peek() 98 | { 99 | if(top==-1) 100 | { 101 | System.out.println("stack is underflow"); 102 | } 103 | else{ 104 | System.out.println("the peek element is:"+a[top]); 105 | } 106 | } 107 | void isfull() 108 | { 109 | if(top==size) 110 | { 111 | System.out.println("stack is full"); 112 | } 113 | else{ 114 | System.out.println("Stack is not full"); 115 | } 116 | } 117 | void isempty() 118 | { 119 | if(top==-1) 120 | { 121 | System.out.println("Stack is empty"); 122 | } 123 | else{ 124 | System.out.println("Stack is not empty"); 125 | } 126 | } 127 | } -------------------------------------------------------------------------------- /tree/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | public static void main(String[] args) { 5 | Scanner sc=new Scanner(System.in); 6 | BST o=new BST(); 7 | for(int i=0;i<5;i++) 8 | o.insert(sc.nextInt()); 9 | System.out.print("Inorder :"); 10 | o.inorder(o.Realroot); 11 | System.out.println(); 12 | System.out.print("preorder :"); 13 | o.preorder(o.Realroot); 14 | System.out.println(); 15 | System.out.print("postorder :"); 16 | o.postorder(o.Realroot); 17 | System.out.println(); 18 | System.out.println(o.Search(o.Realroot,4)); 19 | o.count(o.Realroot); 20 | System.out.println(o.cout); 21 | o.minimum(o.Realroot); 22 | o.maximum(o.Realroot); 23 | o.secondmaximum(o.Realroot); 24 | o.secondminimum(o.Realroot); 25 | } 26 | } 27 | class node{ 28 | int data; 29 | node left,right; 30 | node(int key){ 31 | data=key; 32 | left=right=null; 33 | } 34 | } 35 | 36 | class BST{ 37 | node Realroot; 38 | void insert(int key){ 39 | Realroot=insertRecursively(Realroot,key); 40 | } 41 | node insertRecursively(node dupRoot,int key){ 42 | 43 | if(dupRoot==null){ 44 | dupRoot=new node(key); 45 | return dupRoot; 46 | } 47 | if(key>dupRoot.data){ 48 | dupRoot.right=insertRecursively(dupRoot.right,key); 49 | } 50 | else if(keytemp.data){ 90 | return Search(temp.right,key); 91 | } 92 | else{ 93 | return true; 94 | } 95 | } 96 | int cout=1; 97 | void count(node root) 98 | { 99 | if(root!=null) 100 | { 101 | count(root.left); 102 | // System.out.print(root.data+" "); 103 | cout=cout+1; 104 | count(root.right); 105 | } 106 | } 107 | int minva=0; 108 | int maxva=0; 109 | void minimum(node temp) 110 | { 111 | if(temp==null) 112 | { 113 | System.out.println("tree is empty"); 114 | } 115 | else{ 116 | while(temp!=null) 117 | { 118 | minva=temp.data; 119 | temp=temp.left; 120 | } 121 | System.out.println("minimum value is :"+ minva); 122 | } 123 | 124 | } 125 | void maximum(node temp) 126 | { 127 | if(temp==null) 128 | { 129 | System.out.println("tree is empty"); 130 | } 131 | else{ 132 | while(temp!=null) 133 | { 134 | maxva=temp.data; 135 | temp=temp.right; 136 | } 137 | System.out.println("maximum value is :"+ maxva); 138 | } 139 | 140 | } 141 | int secmax=0; 142 | void secondmaximum(node temp) 143 | { 144 | if(temp==null) 145 | { 146 | System.out.println("tree is empty"); 147 | } 148 | else{ 149 | int m=temp.data; 150 | while(temp!=null) 151 | { 152 | m=temp.data; 153 | secmax=m; 154 | temp=temp.right; 155 | } 156 | System.out.println("second maximum value is :"+ secmax); 157 | } 158 | 159 | } 160 | int secmin=0; 161 | void secondminimum(node temp) 162 | { 163 | if(temp==null) 164 | { 165 | System.out.println("tree is empty"); 166 | } 167 | else{ 168 | while(temp.left!=null) 169 | { 170 | secmin=temp.data; 171 | temp=temp.left; 172 | } 173 | System.out.println("second minimum value is :"+ secmin); 174 | } 175 | 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /valid paranthesis/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Main 3 | { 4 | public static void main(String[] args) { 5 | 6 | Scanner scanner=new Scanner(System.in); 7 | int capacity=scanner.nextInt(); 8 | int choice,value; 9 | Stack stack=new Stack(capacity); 10 | String s=scanner.next(); 11 | for(char c : s.toCharArray()) 12 | { 13 | if(c=='{') 14 | { 15 | stack.push('}'); 16 | } 17 | else if(c=='(') 18 | { 19 | stack.push(')'); 20 | } 21 | else if(c=='[') 22 | { 23 | stack.push(']'); 24 | } 25 | else if(stack.isempty() || stack.pop() != c) 26 | { 27 | System.out.println("false"); 28 | return; 29 | } 30 | } 31 | System.out.println("true"); 32 | return; 33 | } 34 | } 35 | 36 | class Stack{ 37 | final int size; 38 | int top=-1; 39 | int a[]; 40 | Stack(int cap){ 41 | size=cap; 42 | a=new int[size]; 43 | } 44 | void push(char data) 45 | { 46 | if(top==size-1) 47 | { 48 | System.out.println("stack is overflow"); 49 | } 50 | else{ 51 | 52 | a[++top]=data; 53 | } 54 | } 55 | char pop() 56 | { 57 | if(top==-1) 58 | { 59 | System.out.println("stack is underflow"); 60 | } 61 | else{ 62 | top--; 63 | } 64 | //return true; 65 | } 66 | boolean isempty() 67 | { 68 | if(top==-1) 69 | { 70 | return false; 71 | } 72 | return true; 73 | 74 | } 75 | 76 | } --------------------------------------------------------------------------------