forked from Audacity21/DSA-Codes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.java
51 lines (49 loc) · 1.18 KB
/
LinkedList.java
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
import java.util.*;
public class LinkedList
{
int x;
LinkedList next;
public static LinkedList push(int a, LinkedList first)
{
LinkedList node=new LinkedList();
node.x=a;
if(first==null)
{
first=node;
node.next=null;
}
else
{
LinkedList i;
for(i=first;i.next!=null;i=i.next);
i.next=node;
node.next=null;
}
return first;
}
public static void display(LinkedList first)
{
for(LinkedList i=first;i!=null;i=i.next)
{
System.out.print(i.x+" ");
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
LinkedList first=null;
boolean f=true;
do
{
System.out.println("Enter a Number");
int n=sc.nextInt();
first=push(n,first);
System.out.println("Do you want to enter more? 1/0");
int g=sc.nextInt();
if(g!=1)
f=false;
}while(f);
display(first);
sc.close();
}
}