-
Notifications
You must be signed in to change notification settings - Fork 0
/
ass1_6.java
157 lines (133 loc) · 3.15 KB
/
ass1_6.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.Scanner;
class Node
{
int node[]=new int[100];
int last,start;
Node()
{
last=start=0;
}
void append(int d_data)
{
node[last++]=d_data;
System.out.println("Your node "+d_data+" is successfully appended");
}
void insert(int pos,int d_data)
{
int i;
for(i=start;i<=last;i++)
{
if(node[i]==pos)
{
int temp[]=new int[100];
for(int j=0;j<=i;j++)
temp[j]=node[j];
temp[i+1]=d_data;
for(int j=i+1;j<=last;j++)
temp[j+1]=node[j];
node=temp;
System.out.println("Your node "+d_data+" is successfully inserted");
break;
}
}
if(i==last+1)
{
System.out.println("Your given position not found!!");
System.out.println("Your node "+d_data+" is appended at the end of the list");
}
last++;
}
void delete(int d_data)
{
int i;
for(i=start;i<=last;i++)
{
if(node[i]==d_data)
{
System.out.println("Your node "+d_data+" is successfully deleted");
for(i=i;i<last;i++)
node[i]=node[i+1];
break;
}
}
if(i==last+1)
System.out.println("There is no such node to delete!!");
else
last--;
}
void search(int d_data)
{
int i;
for(i=start;i<=last;i++)
{
if(node[i]==d_data)
{
System.out.println("Your node "+d_data+" is found");
break;
}
}
if(i==last+1)
System.out.println("There is no such node in the list!!");
}
void display()
{
int i;
System.out.print("\nYour linked list is:");
for(i=start;i<last-1;i++)
System.out.print(node[i]+"->");
System.out.print(node[i]);
}
}
class ass1_6
{
public static void main(String s[])
{
int ch,data,pos;
char c;
Node n=new Node();
Scanner sc=new Scanner(System.in);
do
{
System.out.println("This is a program of Linked list.");
System.out.println("1. Append Operation");
System.out.println("2. Insert Operation");
System.out.println("3. Delete Operation");
System.out.println("4. Search Operation");
System.out.println("5. Display");
System.out.print("Give your choice: ");
ch=sc.nextInt();
switch(ch)
{
case 1: System.out.println("\nYou selected Append operation");
System.out.print("Enter Data: ");
data=sc.nextInt();
n.append(data);
break;
case 2: System.out.println("\nYou selected Insert operation");
System.out.print("Enter Data after which you want to insert: ");
pos=sc.nextInt();
System.out.print("Enter Data: ");
data=sc.nextInt();
n.insert(pos,data);
break;
case 3: System.out.println("\nYou selected Delete Operation");
System.out.print("Which node you want to delete ? ");
data=sc.nextInt();
n.delete(data);
break;
case 4: System.out.println("You selected search operation");
System.out.print("Which element you want to search ?");
data=sc.nextInt();
n.search(data);
break;
case 5: System.out.println("You selected Display function");
n.display();
break;
default: System.out.println("Wrong Choice!!");
}
System.out.print("\nDo you want to continue ?(y/n): ");
sc.nextLine();
c=sc.next().charAt(0);
}while(c=='y');
}
}