├── deleting an desire element ├── double linked list ├── inserting at begin and end └── linkedList ├── Node.java ├── SingleLinkedList.java └── linkedListFull.java /deleting an desire element: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node { 5 | public: 6 | int key; 7 | Node* next; 8 | }; 9 | Node* newNode(int data) { 10 | Node* n = new Node(); 11 | n->key = data; 12 | n->next = NULL; 13 | return n; 14 | } 15 | Node* a(int data, Node* head) { 16 | Node* nn = newNode(data); 17 | if (head == NULL) { 18 | head = nn; 19 | } else { 20 | nn->next = head; 21 | head = nn; 22 | } 23 | return head; 24 | } 25 | Node* b(int data, Node* head) { 26 | Node* nn = newNode(data); 27 | if (head == NULL) { 28 | head = nn; 29 | } else { 30 | Node* temp = head; 31 | while (temp->next != NULL) { 32 | temp = temp->next; 33 | } 34 | temp->next = nn; 35 | } 36 | return head; 37 | } 38 | Node* d(int v, Node* head) { 39 | if (head==NULL) { 40 | cout<<"nothing" << endl; 41 | return head; 42 | } 43 | if (v == 1) { 44 | head = head->next; 45 | return head; 46 | } 47 | Node* temp = head; 48 | int i=1; 49 | while(temp != NULL && inext; 51 | i++; 52 | } 53 | temp->next = temp->next->next; 54 | return head; 55 | } 56 | Node* c(Node* head) { 57 | Node* temp = head; 58 | while (temp != NULL) { 59 | cout << temp->key << " -> "; 60 | temp = temp->next; 61 | } 62 | cout << "NULL" << endl; 63 | return head; 64 | } 65 | 66 | int main() { 67 | Node* head = NULL; 68 | head = a(30, head); 69 | head = a(20, head); 70 | head = a(10, head); 71 | head = a(5, head); 72 | head = b(40, head); 73 | head = d(4, head); 74 | head = c(head); 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /double linked list: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //class 5 | 6 | class Node 7 | { 8 | public: 9 | Node*prev; 10 | int key; 11 | Node*next; 12 | }; 13 | 14 | //creating newnode 15 | 16 | Node*newnode(int data) 17 | { 18 | Node*n=new Node(); 19 | n->prev=NULL; 20 | n->key=data; 21 | n->next=NULL; 22 | return n; 23 | } 24 | 25 | //inserting at first 26 | 27 | Node*insert_begin(int data,Node*head) 28 | { 29 | Node*nn=newnode(data); 30 | if(head==NULL) 31 | { 32 | head=nn; 33 | } 34 | else 35 | { 36 | head->prev=nn; 37 | nn->next=head; 38 | head=nn; 39 | } 40 | return head; 41 | } 42 | 43 | //inserting at end 44 | 45 | Node*insert_end(int data,Node*head) 46 | { 47 | Node*nn=newnode(data); 48 | if(head==NULL) 49 | { 50 | head=nn; 51 | } 52 | else 53 | { 54 | Node*temp=head; 55 | while(temp->next!=NULL) 56 | { 57 | temp=temp->next; 58 | } 59 | temp->next=nn; 60 | nn->prev=temp; 61 | } 62 | return head; 63 | } 64 | 65 | //inserting at middle 66 | 67 | Node*insert_mid(int data,Node*head,int pos) 68 | { 69 | Node*nn=newnode(data); 70 | if(head==NULL) 71 | { 72 | head=nn; 73 | } 74 | else 75 | { 76 | int count=1; 77 | Node*temp=head; 78 | while(temp->next!=NULL) 79 | { 80 | count++; 81 | if(count==pos) 82 | { 83 | break; 84 | } 85 | temp=temp->next; 86 | } 87 | if(count==pos) 88 | { 89 | nn->next=temp->next; 90 | temp->next=nn; 91 | } 92 | else if(pos==1) 93 | { 94 | head=insert_begin(data,head); 95 | } 96 | else 97 | { 98 | cout<<"Invalid Position"<next; 116 | head->prev=NULL; 117 | temp->next=NULL; 118 | } 119 | return head; 120 | } 121 | 122 | //delete at end 123 | 124 | Node*delete_end(Node*head) 125 | { 126 | if(head==NULL) 127 | { 128 | cout<<"Empty"<next==NULL) 131 | { 132 | head=NULL; 133 | } 134 | else 135 | { 136 | Node*temp=head; 137 | while(temp->next->next!=NULL) 138 | { 139 | temp=temp->next; 140 | } 141 | temp->next=NULL; 142 | temp->next->prev=NULL; 143 | } 144 | return head; 145 | } 146 | 147 | //delete at middle 148 | 149 | Node*delete_mid(Node*head,int pos) 150 | { 151 | int count=1; 152 | if(head==NULL) 153 | { 154 | cout<<"Empty"<next->next!=NULL) 168 | { 169 | count++; 170 | Node*temp1=temp->next; 171 | Node*temp2=temp1->next; 172 | if(temp1->next!=NULL) 173 | { 174 | if(count==pos) 175 | { 176 | temp->next=temp2; 177 | temp1->next=NULL; 178 | temp1->prev=NULL; 179 | temp2->prev=temp; 180 | break; 181 | } 182 | } 183 | temp=temp->next; 184 | } 185 | 186 | } 187 | return head; 188 | } 189 | 190 | //searching an element 191 | 192 | Node* search(int data,Node*head) 193 | { 194 | Node*temp=head; 195 | int count=0; 196 | while(temp->next!=NULL) 197 | { 198 | if(temp->key==data) 199 | { 200 | count++; 201 | cout<<"Value present"<next; 204 | } 205 | if(count==0) 206 | { 207 | cout<<"Value not present"<next==NULL) 222 | { 223 | cout<<"List contain one value"<next; 230 | while(temp3!=NULL) 231 | { 232 | temp2->next=temp1; 233 | temp1=temp2; 234 | temp2=temp3; 235 | temp3=temp2->next; 236 | } 237 | temp2->next=temp1; 238 | head=temp2; 239 | } 240 | return head; 241 | } 242 | 243 | //to remove duplicates 244 | 245 | /*Node*duplicate(Node*head) 246 | { 247 | Node*temp=head; 248 | while(head!=NULL) 249 | { 250 | Node*temp1=temp->next; 251 | if(temp->key!=temp1->key) 252 | { 253 | temp=temp->next; 254 | } 255 | else if(temp->key==temp->next) 256 | { 257 | temp->; 258 | } 259 | } 260 | }*/ 261 | 262 | //display function 263 | //To display the linked list 264 | 265 | Node*display(Node*head) 266 | { 267 | Node*temp=head; 268 | while(temp!=NULL) 269 | { 270 | cout<key<<"->"; 271 | temp=temp->next; 272 | } 273 | cout<<"NULL"; 274 | cout< 2 | using namespace std; 3 | //single linked list 4 | //define node 5 | class Node 6 | { 7 | public: 8 | int key;//data 9 | Node*next;//address 10 | }; 11 | //creating node 12 | Node*newnode(int data) 13 | { 14 | Node*n=new Node();//object creation 15 | n-> key=data;//object asign to key 16 | n->next=NULL;//address field 17 | return n; 18 | } 19 | //insertion of data 20 | Node*insert_begin(int data,Node*head) 21 | { 22 | Node*m=newnode(data); 23 | if(head==NULL) 24 | { 25 | head =m; 26 | } 27 | else 28 | { 29 | m->next=head; 30 | head=m; 31 | } 32 | return head; 33 | } 34 | //insertion at end 35 | Node*insert_end(int data,Node*head) 36 | { 37 | Node*m=newnode(data); 38 | if(head==NULL) 39 | { 40 | head =m; 41 | } 42 | else 43 | { 44 | Node*temp=head; 45 | while(temp->next!=NULL) 46 | { 47 | temp=temp->next; 48 | } 49 | temp->next=m; 50 | } 51 | return head; 52 | } 53 | //insertion at middle 54 | Node*mid_insert(int data,Node*head,int pos) 55 | { 56 | Node*m=newnode(data); 57 | if(head==NULL) 58 | { 59 | head=m; 60 | } 61 | else 62 | { 63 | int count=1; 64 | Node*temp=head; 65 | while(temp->next!=NULL) 66 | { 67 | count++; 68 | if(count==pos) 69 | { 70 | break; 71 | } 72 | temp=temp->next; 73 | } 74 | if(count==pos) 75 | { 76 | m->next=temp->next; 77 | temp->next=m; 78 | } 79 | else if(pos==1) 80 | { 81 | head=insert_begin(data,head); 82 | } 83 | else 84 | { 85 | cout<<"invalid"<key<<"->"; 98 | temp=temp->next; 99 | } 100 | cout<<"NULL"; 101 | cout<next; 114 | } 115 | return head; 116 | } 117 | //deletion at end 118 | Node*del_end(Node*head) 119 | { 120 | if(head==NULL) 121 | { 122 | cout<<"list is empty"; 123 | } 124 | if(head->next==NULL) 125 | { 126 | head=del_begin(head); 127 | } 128 | else 129 | { 130 | Node*temp=head; 131 | while(temp->next->next!=NULL) 132 | { 133 | temp=temp->next; 134 | } 135 | temp->next=NULL; 136 | } 137 | return head; 138 | } 139 | //deletion at middle 140 | Node*del_mid(Node*head,int pos) 141 | { 142 | if(head==NULL) 143 | { 144 | cout<<" Not delete"; 145 | } 146 | else if(pos==1) 147 | { 148 | head=del_begin(head); 149 | } 150 | else 151 | { 152 | int count=1; 153 | Node*temp=head; 154 | while(temp!=NULL) 155 | { 156 | count++; 157 | Node*temp1=temp->next; 158 | if(count==pos) 159 | { 160 | temp->next=temp1->next; 161 | temp1=NULL; 162 | } 163 | temp=temp->next; 164 | } 165 | return head; 166 | } 167 | } 168 | //Searching the element in linked list; 169 | Node*search_element(int data,Node*head) 170 | { 171 | Node*temp=head; 172 | int count=0; 173 | while(temp->next!=NULL) 174 | { 175 | if(temp->key==data) 176 | { 177 | count++; 178 | cout<<"yes"<next; 181 | } 182 | if(count==0){ 183 | cout<<"no"<next==NULL) 196 | { 197 | cout<<"List contain one value"<next; 204 | while(temp2!=NULL) 205 | { 206 | temp1->next = temp; 207 | temp = temp1; 208 | temp1 = temp2; 209 | temp2 = temp1->next; 210 | } 211 | temp1->next = temp; 212 | head = temp1; 213 | } 214 | return head; 215 | } 216 | //main function 217 | int main() 218 | { 219 | Node*head=NULL; 220 | head=insert_begin(30,head); 221 | head=insert_begin(79,head); 222 | head=insert_end(3,head); 223 | head=insert_begin(380,head); 224 | head=insert_end(392,head); 225 | head=del_end(head); 226 | head=del_begin(head); 227 | head=mid_insert(5,head,1); 228 | head=insert_end(55,head); 229 | head=del_mid(head,2); 230 | //head=search_element(30,head); 231 | head=display(head); 232 | head=reserve(head); 233 | head=display(head); 234 | } 235 | -------------------------------------------------------------------------------- /linkedList/Node.java: -------------------------------------------------------------------------------- 1 | public class Node { 2 | int data; 3 | Node next; 4 | Node(int data) 5 | { 6 | this.data=data; 7 | this.next=null; 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /linkedList/SingleLinkedList.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class SingleLinkedList { 4 | public static void main(String[] args) { 5 | // Node head=new Node(15); 6 | // head.next=new Node(20); 7 | // head.next.next=new Node(30); 8 | // head.next.next.next=new Node(40); 9 | // Scanner sc=new Scanner(System.in); 10 | Node head = null; 11 | Scanner sc = new Scanner(System.in); 12 | System.out.print("Enter the Number of elements: "); 13 | int n0 = sc.nextInt(); 14 | for(int i=0;i "); 111 | temp = temp.next; 112 | } 113 | System.out.println("null"); 114 | } 115 | 116 | public static Node reverse(Node headNode) 117 | { 118 | Node next=null; 119 | Node current=headNode; 120 | Node prev=null; 121 | while (current != null) { 122 | next = current.next; 123 | current.next = prev; 124 | prev = current; 125 | current = next; 126 | } 127 | headNode=prev; 128 | return headNode; 129 | } 130 | 131 | public static Node insertAtBeginning(Node headNode,int x) 132 | { 133 | Node c=new Node(x); 134 | c.next=headNode; 135 | headNode=c; 136 | return headNode; 137 | } 138 | public static Node insertAtEnd(Node headNode,int y) 139 | { 140 | if (headNode == null) { 141 | return new Node(y); 142 | } 143 | Node temp1=headNode; 144 | while(temp1.next!=null) 145 | { 146 | temp1=temp1.next; 147 | } 148 | Node t=new Node(y); 149 | temp1.next=t; 150 | return headNode; 151 | } 152 | public static Node insertAtMiddle(Node headNode,int y,int place) 153 | { 154 | Node temp = headNode; 155 | int count = 0; 156 | while (temp != null && count < place - 1) { 157 | temp = temp.next; 158 | count++; 159 | } 160 | Node q=new Node(y); 161 | q.next=temp.next; 162 | // temp.next=null; 163 | temp.next=q; 164 | return headNode; 165 | } 166 | public static Node deleteAtBeginning(Node headNode) 167 | { 168 | Node temp=headNode; 169 | headNode = headNode.next; 170 | temp.next=null; 171 | return headNode; 172 | } 173 | public static Node deleteAtEnd(Node headNode) 174 | { 175 | Node temp=headNode; 176 | while(temp.next.next!=null) 177 | { 178 | temp=temp.next; 179 | } 180 | temp.next=null; 181 | return headNode; 182 | } 183 | public static Node deleteAtMiddle(Node headNode,int pos) 184 | { 185 | int count=0; 186 | Node temp=headNode; 187 | while (count"); 29 | // temp = temp.next; 30 | // } 31 | // System.out.print("null"); 32 | System.out.print(headNode.data+"->"); 33 | display(headNode.next); 34 | } 35 | } 36 | public static Node insertionBegin(Node headNode,int data) 37 | { 38 | Node temp = new Node(data); 39 | if(headNode==null) 40 | { 41 | headNode = temp; 42 | } 43 | else 44 | { 45 | temp.next = headNode; 46 | headNode = temp; 47 | } 48 | return headNode; 49 | } 50 | public static Node insertionEnd(Node headNode,int data) 51 | { 52 | Node temp = new Node(data); 53 | if(headNode==null) 54 | { 55 | headNode = temp; 56 | } 57 | else 58 | { 59 | Node temp1 = headNode; 60 | while(temp1.next != null) 61 | { 62 | temp1 = temp1.next; 63 | } 64 | temp1.next = temp; 65 | } 66 | return headNode; 67 | } 68 | public static Node insertionSpecificPos(Node headNode,int data,int pos) 69 | { 70 | Node temp = new Node(data); 71 | if(headNode==null) 72 | { 73 | headNode = temp; 74 | } 75 | if(pos == 1) 76 | { 77 | headNode = insertionBegin(headNode, data); 78 | return headNode; 79 | } 80 | int count = 1; 81 | Node temp1 = headNode; 82 | while(temp1.next != null) 83 | { 84 | count++; 85 | if(count == pos) 86 | { 87 | Node temp2 = temp1.next; 88 | temp1.next = temp; 89 | temp.next = temp2; 90 | break; 91 | } 92 | temp1 = temp1.next; 93 | } 94 | if(temp1.next == null) 95 | { 96 | temp1.next = temp; 97 | } 98 | return headNode; 99 | } 100 | public static Node insertionMiddle(Node head,int data) 101 | { 102 | Node temp = new Node(data); 103 | if(head==null) 104 | { 105 | System.out.println("There no elements in the List"); 106 | } 107 | else 108 | { 109 | int count = 0; 110 | Node temp1 = head; 111 | while(temp1.next!=null) 112 | { 113 | count++; 114 | temp1 = temp1.next; 115 | } 116 | int pos = 0; 117 | count = count/2; 118 | temp1 = head; 119 | while(temp1.next!=null) 120 | { 121 | if(pos==count) 122 | { 123 | temp.next = temp1.next; 124 | temp1.next = temp; 125 | break; 126 | } 127 | pos++; 128 | temp1 = temp1.next; 129 | } 130 | } 131 | return head; 132 | } 133 | public static Node reverseList(Node head) 134 | { 135 | if(head==null) 136 | { 137 | System.out.println("There are No elements in the list"); 138 | } 139 | else{ 140 | Node temp = null; 141 | Node temp1 = head; 142 | Node temp2 = null; 143 | while(temp1!=null) 144 | { 145 | temp2 = temp1.next; 146 | temp1.next = temp; 147 | temp = temp1; 148 | temp1 = temp2; 149 | } 150 | head = temp; 151 | } 152 | return head; 153 | } 154 | public static Node deleteByValue(Node head,int data) 155 | { 156 | if(head==null) 157 | { 158 | System.out.println("There are No elements in the list"); 159 | } 160 | else 161 | { 162 | Node temp = head; 163 | while(temp.next!=null) 164 | { 165 | 166 | if(temp.next.data == data) 167 | { 168 | temp.next = temp.next.next; 169 | break; 170 | } 171 | temp = temp.next; 172 | } 173 | if(temp.data == data) 174 | { 175 | head = deleteLast(head); 176 | } 177 | else 178 | { 179 | System.out.println("The Element is not found in the"); 180 | } 181 | } 182 | return head; 183 | } 184 | public static Node deleteSpecificPos(Node head,int pos) 185 | { 186 | if(head == null) 187 | { 188 | System.out.println("There is no elements in the list"); 189 | } 190 | else if(pos==1) 191 | { 192 | head = deleteFirst(head); 193 | return head; 194 | } 195 | else 196 | { 197 | int count = 1; 198 | Node temp = head; 199 | temp = head; 200 | while(temp.next!=null) 201 | { 202 | count++; 203 | if(count == pos) 204 | { 205 | System.out.println("The Element Deleted is : "+temp.next.data); 206 | temp.next = temp.next.next; 207 | break; 208 | } 209 | temp = temp.next; 210 | } 211 | if(temp.next==null && count==pos) 212 | { 213 | head = deleteLast(head); 214 | } 215 | else{ 216 | System.out.println("Index Out Of Bounce!!!Enter Within the Size"); 217 | } 218 | } 219 | return head; 220 | } 221 | public static Node deleteFirst(Node head) 222 | { 223 | if(head == null) 224 | { 225 | System.out.println("There is no elements in the Linked List"); 226 | } 227 | else{ 228 | System.out.println("The Element Deleted is : "+head.data); 229 | Node temp = head; 230 | head = head.next; 231 | temp.next = null; 232 | } 233 | return head; 234 | } 235 | public static Node deleteLast(Node head) 236 | { 237 | if(head == null) 238 | { 239 | System.out.println("There is no elements in the Linked List"); 240 | } 241 | else if(head.next == null) 242 | { 243 | System.out.println("The Element Deleted is : "+head.data); 244 | head = head.next; 245 | } 246 | else 247 | { 248 | Node temp1 = null; 249 | Node temp = head; 250 | while(temp.next != null) 251 | { 252 | temp1 = temp; 253 | temp = temp.next; 254 | } 255 | System.out.println("The Element Deleted is : "+temp.data); 256 | temp1.next = null; 257 | } 258 | return head; 259 | } 260 | public static boolean elementSearch(Node head,int num) 261 | { 262 | Node temp = head; 263 | while(temp != null) 264 | { 265 | if(temp.data == num) 266 | { 267 | return true; 268 | } 269 | temp = temp.next; 270 | } 271 | return false; 272 | } 273 | public static void main(String[] args) { 274 | Node head = null; 275 | Scanner sc = new Scanner(System.in); 276 | System.out.println("==================================LINKED LIST OPERATION==============================="); 277 | while(true) 278 | { 279 | System.out.println(); 280 | System.out.println(); 281 | System.out.println("---------Menu of Operation in the linked list---------------"); 282 | System.out.println("1 -> Inserting an element"); 283 | System.out.println("2 -> Deleting an element"); 284 | System.out.println("3 -> Reverse of the Linked List"); 285 | System.out.println("4 -> Display the Elements in the Linked List"); 286 | System.out.println("5 -> Search a particular number"); 287 | System.out.println("6 -> Exit"); 288 | System.out.print("Enter your choice from the above Menu : "); 289 | int choice = sc.nextInt(); 290 | if(choice == 1) 291 | { 292 | System.out.println("+++++++++++++Menu of Operation in the Insertion of Linked List++++++++++++"); 293 | System.out.println("1 -> Insert Element Continuously"); 294 | System.out.println("2 -> Insert Element at the Begin"); 295 | System.out.println("3 -> Insert Element at the End"); 296 | System.out.println("4 -> Insert Element at middle"); 297 | System.out.println("5 -> Insert Element at the Specific Position"); 298 | System.out.print("Enter your choice from the above Insertion Menu : "); 299 | int insert = sc.nextInt(); 300 | if(insert == 1) 301 | { 302 | System.out.print("Enter the Number of Elements you need to insert : "); 303 | int n = sc.nextInt(); 304 | for(int i=0;i Delete Element at the Begin"); 347 | System.out.println("2 -> Delete Element at the End"); 348 | System.out.println("3 -> Delete Element by the value"); 349 | System.out.println("4 -> Delete at the Specific pos"); 350 | System.out.print("Enter your choice from the above Deletion Menu : "); 351 | int del = sc.nextInt(); 352 | if(del == 1) 353 | { 354 | head = deleteFirst(head); 355 | } 356 | else if(del == 2) 357 | { 358 | head = deleteLast(head); 359 | } 360 | else if(del == 3) 361 | { 362 | System.out.print("Enter the value to Delete : "); 363 | int num = sc.nextInt(); 364 | head = deleteByValue(head,num ); 365 | System.out.println("The Element Deleted is : "+num); 366 | } 367 | else if(del == 4) 368 | { 369 | System.out.print("Enter the position to Delete : "); 370 | int pos = sc.nextInt(); 371 | head = deleteSpecificPos(head, pos); 372 | } 373 | } 374 | else if(choice == 3) 375 | { 376 | System.out.print("The Linked list After the Reversing is : "); 377 | head = reverseList(head); 378 | display(head); 379 | } 380 | else if(choice == 4) 381 | { 382 | System.out.print("The Elements in the Linked List is : "); 383 | display(head); 384 | } 385 | else if(choice == 5) 386 | { 387 | System.out.print("Enter the number to Check : "); 388 | int num = sc.nextInt(); 389 | System.out.println("The element "+num+" is found or not : "+elementSearch(head, num)); 390 | } 391 | else if(choice == 6) 392 | { 393 | System.out.println("=============================END OF THE OPERATION================================="); 394 | break; 395 | } 396 | else 397 | { 398 | System.out.println("Enter the valid Choice!!!!!"); 399 | } 400 | } 401 | } 402 | } --------------------------------------------------------------------------------