Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 06c6216

Browse files
implementation linkedList with head,tail,addbegining,addbottom options.
1 parent 7c6100e commit 06c6216

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.udemy.dsapart1.linkedLists;
2+
3+
public class DriverForLinkedListApp {
4+
5+
public static void main(String[] args) {
6+
LinkedNodeEntity nodeObj=new LinkedNodeEntity(384.34);
7+
System.out.println("First Node : "+nodeObj);
8+
System.out.println("\nAfter adding 1st node to LinkedList : ");
9+
nodeObj.printDataItemsOfLinkedList(nodeObj);
10+
11+
nodeObj.addNodeAtEndOfLinkList(-34.4);
12+
nodeObj.addNodeAtEndOfLinkList(394.4);
13+
nodeObj.addNodeAtEndOfLinkList(0.344);
14+
nodeObj.addNodeAtEndOfLinkList(540.34);
15+
nodeObj.addNodeAtEndOfLinkList(349.340);
16+
nodeObj.addNodeAtEndOfLinkList(100);
17+
System.out.println("\n");
18+
System.out.println("\nAfter adding multiple nodes to LinkedList : ");
19+
System.out.println("************************************************");
20+
nodeObj.printDataItemsOfLinkedList(nodeObj.getHeadNodeRef());
21+
System.out.println("\nHead Node - "+nodeObj.getHeadNodeRef().getDataValue());
22+
System.out.println("\nTail Node - "+nodeObj.getTailNodeRef().getTailNodeRef());
23+
System.out.println("\nCheck is the node with Value : 0.344 is exists into linkedList - "+nodeObj.checkIsNodeExistsWithAGivenValue(0.344));
24+
System.out.println("\nCheck is the node with Value : 73 is exists into linkedList - "+nodeObj.checkIsNodeExistsWithAGivenValue(73));
25+
System.out.println("\n");
26+
nodeObj.addNodeAtBegeningOfLinkList(44);
27+
nodeObj.addNodeAtBegeningOfLinkList(11);
28+
nodeObj.addNodeAtBegeningOfLinkList(2);
29+
nodeObj.addNodeAtBegeningOfLinkList(22);
30+
System.out.println("\nAfter adding multiple nodes at the begening of LinkedList : ");
31+
System.out.println("******************************************************************");
32+
nodeObj.printDataItemsOfLinkedList(nodeObj.getHeadNodeRef());
33+
}
34+
35+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.udemy.dsapart1.linkedLists;
2+
3+
public class LinkedNodeEntity {
4+
5+
private double dataValue;
6+
private LinkedNodeEntity nextNodeRef;
7+
private LinkedNodeEntity headNodeRef;
8+
private LinkedNodeEntity tailNodeRef;
9+
private LinkedNodeEntity currentNodeRef;
10+
11+
public LinkedNodeEntity(double dataValue) {
12+
this.dataValue = dataValue;
13+
}
14+
15+
/**
16+
* @return the dataValue
17+
*/
18+
public double getDataValue() {
19+
return dataValue;
20+
}
21+
22+
/**
23+
* @return the nextNodeRef
24+
*/
25+
public LinkedNodeEntity getNextNodeRef() {
26+
return nextNodeRef;
27+
}
28+
29+
30+
/**
31+
* @return the tailNodeRef
32+
*/
33+
public LinkedNodeEntity getHeadNodeRef() {
34+
return headNodeRef;
35+
}
36+
37+
/**
38+
* @return the tailNodeRef
39+
*/
40+
public LinkedNodeEntity getTailNodeRef() {
41+
return tailNodeRef;
42+
}
43+
44+
45+
/**
46+
* @return the currentNodeRef
47+
*/
48+
public LinkedNodeEntity getCurrentNodeRef() {
49+
return currentNodeRef;
50+
}
51+
52+
53+
@Override
54+
public String toString() {
55+
return "LinkedNodeEntity [dataValue=" + dataValue + ", nextNodeRef=" + nextNodeRef + "]";
56+
}
57+
58+
59+
public void addNodeAtEndOfLinkList(double nodeDataValue) {
60+
LinkedNodeEntity newNodeObj = new LinkedNodeEntity(nodeDataValue);
61+
if (headNodeRef == null) {
62+
headNodeRef = newNodeObj;
63+
tailNodeRef = newNodeObj;
64+
} else {
65+
tailNodeRef.nextNodeRef = newNodeObj;
66+
newNodeObj.nextNodeRef = null;
67+
tailNodeRef = newNodeObj;
68+
}
69+
}
70+
71+
72+
public void addNodeAtBegeningOfLinkList(double nodeDataValue) {
73+
LinkedNodeEntity newNodeObj = new LinkedNodeEntity(nodeDataValue);
74+
if (headNodeRef == null || tailNodeRef==null) {
75+
headNodeRef = newNodeObj;
76+
tailNodeRef = newNodeObj;
77+
} else {
78+
newNodeObj.nextNodeRef=headNodeRef;
79+
headNodeRef=newNodeObj;
80+
}
81+
}
82+
83+
84+
public void printDataItemsOfLinkedList(LinkedNodeEntity headNodeRef) {
85+
LinkedNodeEntity currentNodeRef = headNodeRef;
86+
while (currentNodeRef != null) {
87+
System.out.print(""+currentNodeRef.dataValue+" --> ");
88+
currentNodeRef = currentNodeRef.nextNodeRef;
89+
}
90+
}
91+
92+
93+
public boolean checkIsNodeExistsWithAGivenValue(double targetValue) {
94+
boolean blnFlag = false;
95+
currentNodeRef = headNodeRef;
96+
while (getCurrentNodeRef() != null) {
97+
if (currentNodeRef.dataValue == targetValue) {
98+
blnFlag = true;
99+
}
100+
currentNodeRef = currentNodeRef.nextNodeRef;
101+
}
102+
return blnFlag;
103+
}
104+
105+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /