Skip to content

Commit ac0c096

Browse files
authoredOct 11, 2023
Merge pull request #166 from K-Saniya/K-Saniya-Singly-Linked-List-Code-branch
Added code for Singly Linked List
2 parents 7d0c15e + b9ffd41 commit ac0c096

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
 

‎singlyLinkedList.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import java.util.*;
2+
3+
class Node{
4+
int data;
5+
Node next;
6+
7+
Node()
8+
{
9+
next=null;
10+
}
11+
Node(int data)
12+
{
13+
this.data=data;
14+
next=null;
15+
}
16+
17+
}
18+
class LinkedList
19+
{int count=1;
20+
int data;
21+
Node head;
22+
Node ptr;
23+
Node temp;
24+
LinkedList()
25+
{
26+
head=null;
27+
}
28+
void insert(int data)
29+
{
30+
temp=new Node(data);
31+
if(head==null)
32+
{
33+
head=temp;
34+
}
35+
else
36+
{
37+
ptr=head;
38+
while(ptr.next!=null)
39+
{
40+
ptr = ptr.next;
41+
}
42+
ptr.next = temp;
43+
}
44+
}
45+
46+
void display()
47+
{
48+
Node ptr=head;
49+
while(ptr!=null)
50+
{
51+
System.out.println("data is= "+ptr.data);
52+
ptr=ptr.next;
53+
}
54+
55+
}
56+
57+
58+
}
59+
60+
61+
62+
public class Main
63+
{
64+
public static void main(String[] args) {
65+
Scanner sc=new Scanner(System.in);
66+
int y,d;
67+
//creating object of LinkedList class
68+
LinkedList l =new LinkedList();
69+
//user input for linked list data
70+
do{
71+
System.out.println("enter data =");
72+
d=sc.nextInt();
73+
l.insert(d);
74+
System.out.println("if you want to add more enter 1 else enter 0");
75+
y=sc.nextInt();
76+
77+
}while(y==1);
78+
79+
//display method for the stored data
80+
l.display();
81+
82+
}
83+
}
84+
85+

0 commit comments

Comments
 (0)
Please sign in to comment.