Created
February 21, 2017 07:38
-
-
Save pbesra/4c15675423905828cd96a91e77dc4793 to your computer and use it in GitHub Desktop.
custom Linked list java
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
import java.util.*; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import java.lang.*; | |
import java.awt.List; | |
import java.io.*; | |
import java.time.*; | |
import java.math.*; | |
import java.rmi.Remote; | |
import java.sql.Array; | |
public class MyClass { | |
public static void main(String[] args)throws java.lang.Exception{ | |
nodeList nd=new nodeList(); | |
nd.insert(12); | |
nd.insert(34); | |
nd.insert(67); | |
nd.insert(45); | |
nd.insert(6); | |
nd.delete(12); | |
nd.printData(); | |
} | |
} | |
class node{ | |
public node next; | |
public int data; | |
public node(int data){ | |
this.data=data; | |
} | |
} | |
class nodeList{ | |
public node head; | |
public nodeList(){ | |
this.head=null; | |
} | |
public void insert(int data){ | |
node n=new node(data); | |
n.next=head; | |
head=n; | |
} | |
public void delete(int d){ | |
node t=head; | |
int found=0; | |
// to check if first number is d | |
if(t.data==d){ | |
found=1; | |
head=head.next; | |
} | |
// to check other than first number | |
else{ | |
node t1=head; | |
while(t!=null){ | |
if(t.data==d){ | |
found=1; | |
break; | |
} | |
t1=t; | |
t=t.next; | |
} | |
if(found==1){ | |
t1.next=t.next; | |
} | |
} | |
if(found==0){ | |
System.out.println("number not found in linked list"); | |
} | |
} | |
public void printData(){ | |
while(head!=null){ | |
System.out.println(head.data); | |
head=head.next; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment