Skip to content

Instantly share code, notes, and snippets.

@mayank-verma-cs19
Created March 8, 2021 14:49
Show Gist options
  • Save mayank-verma-cs19/3faae8a9d8ef7c1b21410bfc2e823235 to your computer and use it in GitHub Desktop.
Save mayank-verma-cs19/3faae8a9d8ef7c1b21410bfc2e823235 to your computer and use it in GitHub Desktop.
/* Created by IntelliJ IDEA.
* Author: MAYANK VERMA
* Date: 08-03-2021
* Time: 19:00
* 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