-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedlist2.java~
More file actions
87 lines (79 loc) · 1.39 KB
/
Linkedlist2.java~
File metadata and controls
87 lines (79 loc) · 1.39 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
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
86
87
import java.util.Scanner;
public class Linkedlist2
{
class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
this.next=null;
}
}
Node head=null;
public void create()
{
int data,n,m;
Scanner ts=new Scanner(System.in);
do
{
System.out.print("Enter data: ");
data=ts.nextInt();
Node newnode=new Node(data);
if(head==null)
{
head=newnode;
}
else
{
System.out.print("At the beginning,Press:1,At the end,Press:2,At the position,Press:2: ");
m=ts.nextInt();
switch(m)
{
case 1:
newnode.next=head;
head=newnode;
break;
case 2:
Node temp=head;
while(temp!=null)
{
temp=temp.next;
}
temp.next=newnode;
break;
case 3:
Node temp1=head;
int p;
Scanner am=new Scanner(System.in);
p=am.nextInt();
for(int i=0;i<(p-1);i++)
{
temp1=temp1.next;
}
newnode.next=temp1.next;
temp1.next=newnode;
}
System.out.print("DO you want to enter more data,if yes Press:1: ");
n=ts.nextInt();
}
}
while(n==1);
}
public void traverse()
{
Node temp=head;
while(temp!=null)
{
System.out.print(temp.data+"-->");
temp=temp.next;
}
}
public static void main(String[] args)
{
Linkedlist2 obj=new Linkedlist2();
obj.create();
obj.traverse();
}
}