-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_list1.java
More file actions
58 lines (48 loc) · 1.18 KB
/
insertion_list1.java
File metadata and controls
58 lines (48 loc) · 1.18 KB
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
//inserting inside a linked list
public class Insertionatfront
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
//inserting a node at front of the list
public void push(int element)
{
Node new_node = new Node(element); //making the new node
new_node.next=head; //next becomes the head
head=new_node; //now head points to the new node
}
public void print()
{
Node n=head;
while(n!=null)
{
System.out.print(n.data+" ");
n=n.next;
}
}
public static void main(String args[])
{
Insertionatfront iaf = new Insertionatfront();
iaf.head = new Node(1);
Node one = new Node(2);
Node two = new Node(3);
Node three = new Node(4);
Node four = new Node(5);
iaf.head.next=one;
one.next=two;
two.next=three;
three.next=four;
iaf.print();
iaf.push(100);
System.out.println();
iaf.print();
}
}