-
Notifications
You must be signed in to change notification settings - Fork 109
/
singlyLinkedList.java
85 lines (70 loc) · 1.38 KB
/
singlyLinkedList.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
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
import java.util.*;
class Node{
int data;
Node next;
Node()
{
next=null;
}
Node(int data)
{
this.data=data;
next=null;
}
}
class LinkedList
{int count=1;
int data;
Node head;
Node ptr;
Node temp;
LinkedList()
{
head=null;
}
void insert(int data)
{
temp=new Node(data);
if(head==null)
{
head=temp;
}
else
{
ptr=head;
while(ptr.next!=null)
{
ptr = ptr.next;
}
ptr.next = temp;
}
}
void display()
{
Node ptr=head;
while(ptr!=null)
{
System.out.println("data is= "+ptr.data);
ptr=ptr.next;
}
}
}
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int y,d;
//creating object of LinkedList class
LinkedList l =new LinkedList();
//user input for linked list data
do{
System.out.println("enter data =");
d=sc.nextInt();
l.insert(d);
System.out.println("if you want to add more enter 1 else enter 0");
y=sc.nextInt();
}while(y==1);
//display method for the stored data
l.display();
}
}