-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlnkdlist_insert.java
More file actions
55 lines (51 loc) · 1.1 KB
/
Copy pathlnkdlist_insert.java
File metadata and controls
55 lines (51 loc) · 1.1 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
import java.util.*;
class lnkdlist_insert
{
class Node
{
int data;
Node next;
Node(int d)
{
data=d;
next=null;
}
}
Node head;
public void insert(int el)
{ Node ele =new Node(el);
if(head==null)
{
head=ele;
}
else
{
ele.next=head;
head=ele;
}
}
public void disp()
{
System.out.println("elements in linked list:");
Node curr=head;
while(curr.next!=null)
{
System.out.print(curr.data+" ");
curr=curr.next;
}
System.out.println(curr.data);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
linkedlist_beginsert on= new linkedlist_beginsert();
for(int i=0;i<n;i++)
{
int e=sc.nextInt();
on.insert(e);
}
sc.close();
on.disp();
}
}