-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedListComplete.java
More file actions
112 lines (77 loc) · 2.29 KB
/
SinglyLinkedListComplete.java
File metadata and controls
112 lines (77 loc) · 2.29 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
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Dell
*
*/
import java.io.*;
import java.util.*;
//complete linked list (Singly)
public class SinglyLinkedListComplete {
public static Node head;
//to insert an element in the list
public static String insert(int e) {
if (head == null) {
Node first = new Node();
first.data = e;
first.next = head;
head = first;
return "Insert successFul, the list was empty";
} else {
Node later = new Node();
later.data = e;
later.next = head;
head = later;
return "insert successful the list is not empty";
}
}
//to delete an element in the list
public static void delete(int key) {
// Node toDel = head;
Node temp = head;
while (temp.next.data != key) {
temp = temp.next;
}
temp.next = temp.next.next;
}
//to search an element in the list
public static String search(int key) {
Node temp = head;
while (temp.data != key && temp.next != null) {
temp = temp.next;
}
if (temp.data == key) {
return "success";
//System.out.println("success");
} else {
return "failure";
}
}
//to print()
public static void print() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + "->");
temp = temp.next;
}
}
public static void main(String[] args) {
head = null;
System.out.println(insert(4));
System.out.println(insert(10));
System.out.println(insert(14));
System.out.println(insert(24));
System.out.println(insert(30));
System.out.println(insert(54));
print();
System.out.println("");
delete(10);
print();
//String result = search(11);
System.out.println("\nresult of search :: " + search(90));
}
}