Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement