Datasets:

ArXiv:
License:
File size: 1,202 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

class Insertion{
 static  Node head;

  static class Node{
    
    int data;
    Node next;

    Node(int d){
      data = d;
      next = null;
    }
  }

  // Insertion at the head 
  void push(Node n, int x){

    Node t = new Node(x);

    if(n==null){
      head=t;
    }
    else{
      t.next = head;
      head = t;
    }
  }

  void Append(Node n, int x){

    Node t = new Node(x);
    if(n==null){
     head = t; 
    }

    else{

      while(n.next!=null){
        n = n.next;
      }
      n.next = t;
      t = n;
    }
  }

  int Length(Node p){
    int len = 0;
    
    while(p!=null){
      len++;
      p = p.next;
    }
    return len;
  }

  void After(Node p, int index,int val){

    Node t = new Node(val);
    for(int i=1; i<Length(p); ++i){

      if(i==index){

        t.next = p.next;
        p.next =t;
        
      }
      p = p.next;
    }

  }

  void Disp(Node p)
  {
    if(p!=null){
      System.out.print(p.data+" ");
      Disp(p.next);
    }
  }

  public static void main(String[] args){
    Insertion l = new Insertion();
    l.push(head,3);
    l.push(head,5);
    l.Append(head,45);
    l.Append(head,34);
    l.After(head,2,54);
    l.Disp(head);
  }
}