Repository navigation
Expand file tree
/
Copy pathSinglyLinkedList.java
More file actions
75 lines (75 loc) · 2.18 KB
/
Copy pathSinglyLinkedList.java
File metadata and controls
75 lines (75 loc) · 2.18 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
class Page {
String data;
Page next;
public Page(String data) {
this.data = data;
this.next = null;
}
}
class BrowsingHistory{
Page head;
public BrowsingHistory() {
this.head = null;
}
public void addPage(String data) {
Page newPage = new Page(data);
newPage.next = head;
head = newPage;
}
public void delete(String data){
if(head==null){
System.out.println("the history is empty");
}
if(head.data.equals(data)){
head=head.next;
System.out.println("deleted page"+data);
}
Page curr=head;
Page prev=null;
while(curr!=null && !curr.data.equals(data)){
prev=curr;
curr=curr.next;
}
if(curr==null){
System.out.println(data+"page not found!!!");
}
prev.next=curr.next;
System.out.println("deleted page"+data);
}
public void search(String data){
Page curr=head;
while(curr!=null){
if(curr.data.equals(data)){
System.out.println("founded "+data);
return;
}
curr=curr.next;
}
System.out.println("Not found "+data);
}
public void displayHistory() {
System.out.println("-------BROWSING HISTORY---------");
Page current = head;
while (current != null) {
System.out.println(current.data );
current = current.next;
}
System.out.println("null");
System.out.println("-------------X------------");
}
}
public class SinglyLinkedList{
public static void main(String args[]){
BrowsingHistory hist=new BrowsingHistory();
hist.addPage("geeks for geeks ");
hist.addPage("Google news");
hist.addPage("Weather report");
hist.addPage("chatgpt");
hist.addPage("claude ai");
hist.displayHistory();
hist.delete("chatgpt");
hist.search("Google news");
hist.search("chatgpt");
hist.displayHistory();
}
}