Created
March 8, 2021 14:47
-
-
Save mayank-verma-cs19/37b11c652d9062f44fe53b354774fe85 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Created by IntelliJ IDEA. | |
* Author: MAYNK VERMA | |
* Date: 08-03-2021 | |
* Time: 15:55 | |
* File: StackMain.java | |
*/ | |
/*Assume that the singly linked list is implemented with a header node without tail node | |
i) Write a class that includes the method to return the size of linked list. | |
ii) Print the linked list. | |
iii) test if the value k is contained in the linked list. | |
iv) Add a value k if it is not contain in the linked list. | |
V) Remove the value k if it is contained in the linked list,*/ | |
public class Question { | |
class Node{ | |
int data; | |
Node next; | |
} | |
Node head; | |
public Question(){ | |
head = null; | |
} | |
public void valueReturn(){ | |
if (head == null){ | |
System.out.println("Linked list is empty"); | |
} | |
else{ | |
int size = 0; | |
Node temp =head; | |
while (temp.next != null){ | |
size = size+ 1; | |
} | |
System.out.println(size); | |
} | |
} | |
public void printLinkedList(){ | |
if (head == null){ | |
System.out.println("No value to print"); | |
} | |
else{ | |
Node temp = head; | |
while (temp.next != null){ | |
System.out.println(temp.data); | |
} | |
System.out.println(temp.data); | |
} | |
} | |
public void cheakValue(int k){ | |
if (head == null){ | |
System.out.println("valule not preasent because"); | |
} | |
else | |
{ | |
int value = k; | |
int i = 0; | |
Node temp = head; | |
while (temp.next != null){ | |
i = i++; | |
if (value == temp.data){ | |
System.out.println("Value present"); | |
return; | |
} | |
else{ | |
System.out.println("Value not preasent"); | |
} | |
temp = temp.next; | |
} | |
} | |
} | |
public void insertion(Node newNode){ | |
if (head == null){ | |
head = newNode; | |
} | |
else | |
{ | |
Node temp = head; | |
while (temp != null){ | |
temp = temp.next; | |
temp.next = newNode; | |
} | |
} | |
} | |
public void delete(int x){ | |
Node temp=head,prev=null; | |
// if(temp!=null && temp.data==x){ | |
// head=temp.next; | |
// return; | |
// } | |
for (int i=0;i<x+1;i++){ | |
if(temp!=null && i==x){ | |
prev=temp; | |
temp=temp.next; | |
} | |
} | |
while (temp!=null && temp.data!=x){ | |
prev=temp; | |
temp=temp.next; | |
} | |
if(temp==null) return; | |
prev.next=temp.next; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment