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 37c3cdd

Browse files
main
1 parent 22795e6 commit 37c3cdd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1171
-336
lines changed

‎fawad‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fawad

‎nbactions.xml‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.vmArgs></exec.vmArgs>
14+
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
15+
<exec.appArgs></exec.appArgs>
16+
<exec.mainClass>linklist.node.SinglyLinkedList</exec.mainClass>
17+
<exec.executable>java</exec.executable>
18+
</properties>
19+
</action>
20+
<action>
21+
<actionName>debug</actionName>
22+
<packagings>
23+
<packaging>jar</packaging>
24+
</packagings>
25+
<goals>
26+
<goal>process-classes</goal>
27+
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
28+
</goals>
29+
<properties>
30+
<exec.vmArgs>-agentlib:jdwp=transport=dt_socket,server=n,address=${jpda.address}</exec.vmArgs>
31+
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
32+
<exec.appArgs></exec.appArgs>
33+
<exec.mainClass>linklist.node.SinglyLinkedList</exec.mainClass>
34+
<exec.executable>java</exec.executable>
35+
<jpda.listen>true</jpda.listen>
36+
</properties>
37+
</action>
38+
<action>
39+
<actionName>profile</actionName>
40+
<packagings>
41+
<packaging>jar</packaging>
42+
</packagings>
43+
<goals>
44+
<goal>process-classes</goal>
45+
<goal>org.codehaus.mojo:exec-maven-plugin:3.1.0:exec</goal>
46+
</goals>
47+
<properties>
48+
<exec.vmArgs></exec.vmArgs>
49+
<exec.args>${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}</exec.args>
50+
<exec.mainClass>linklist.node.SinglyLinkedList</exec.mainClass>
51+
<exec.executable>java</exec.executable>
52+
<exec.appArgs></exec.appArgs>
53+
</properties>
54+
</action>
55+
</actions>

‎pom.xml‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
<artifactId>DataStructures</artifactId>
66
<version>1.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
8+
9+
<dependencies>
10+
11+
<!-- Other dependencies -->
12+
13+
14+
<dependency>
15+
<groupId>com.mysql</groupId>
16+
<artifactId>mysql-connector-j</artifactId>
17+
<version>8.0.33</version>
18+
<scope>provided</scope>
19+
</dependency>
20+
</dependencies>
821
<properties>
922
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1023
<maven.compiler.source>19</maven.compiler.source>

‎src/main/java/linklist/node/SinglyLinkedList.java‎

Lines changed: 94 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,128 +8,148 @@
88
*
99
* @author fawad
1010
*/
11-
publicclass SinglyLinkedList {
11+
class Node {
1212

13-
Node head; // head of the linked list
13+
int data;
14+
Node next;
1415

15-
// Node class
16-
static class Node {
16+
public Node(int data) {
17+
this.data = data;
18+
}
19+
}
1720

18-
int data;
19-
Node next;
21+
public class SinglyLinkedList {
2022

21-
Node(int data) {
22-
this.data = data;
23-
next = null;
24-
}
25-
}
23+
private Node head;
24+
private int size;
2625

27-
// Display the linked list
28-
public void display() {
29-
Node temp = head;
30-
while (temp != null) {
31-
System.out.print(temp.data + " ");
32-
temp = temp.next;
33-
}
34-
System.out.println();
26+
public SinglyLinkedList() {
27+
head = null;
28+
size = 0;
29+
}
30+
public int size(){
31+
return size;
32+
}
33+
public boolean isEmpty() {
34+
return head == null;
3535
}
3636

37-
// Count the number of nodes in the linked list
38-
public int countNodes() {
39-
int count = 0;
40-
Node temp = head;
41-
while (temp != null) {
42-
count++;
43-
temp = temp.next;
37+
public void addFirst(int value) {
38+
Node n = new Node(value);
39+
if (isEmpty()) {
40+
head = n;
41+
size++;
42+
} else {
43+
n.next = head;
44+
head = n;
45+
size++;
4446
}
45-
return count;
4647
}
4748

48-
// Find a node with a given key value
49-
public Node find(int key) {
50-
Node temp = head;
51-
while (temp != null) {
52-
if (temp.data == key) {
53-
return temp;
49+
public void addAtEnd(int value) {
50+
Node n = new Node(value);
51+
if (isEmpty()) {
52+
head = n;
53+
size++;
54+
} else {
55+
Node p = head;
56+
while (p.next != null) {
57+
p = p.next;
5458
}
55-
temp = temp.next;
59+
p.next = n;
60+
size++;
5661
}
57-
return null;
5862
}
5963

60-
// Add a new node with given data at the beginning of the linked list
61-
public void addAtFirst(int data) {
62-
Node newNode = new Node(data);
63-
newNode.next = head;
64-
head = newNode;
64+
public void addAfter(int key, int value) {
65+
Node n = new Node(value);
66+
Node p = find(key);
67+
if (p == null) {
68+
System.out.println("Node with key " + key + " not found");
69+
return;
70+
}
71+
n.next = p.next;
72+
p.next = n;
73+
size++;
74+
6575
}
6676

67-
// Add a new node with given data at the end of the linked list
68-
public void addAtEnd(int data) {
69-
Node newNode = new Node(data);
70-
if (head == null) {
71-
head = newNode;
77+
public void delete(int key) {
78+
Node temp = head;
79+
Node prev = null;
80+
81+
if (temp != null && temp.data == key) {
82+
head = temp.next; // Change the head node
83+
size--;
7284
return;
7385
}
74-
Nodetemp = head;
75-
while (temp.next != null) {
86+
while (temp != null && temp.data != key) {
87+
prev = temp;
7688
temp = temp.next;
89+
7790
}
78-
temp.next = newNode;
79-
}
8091

81-
// Add a new node with given data after the node with a given key value
82-
public void addAfter(int key, int data) {
83-
Node newNode = new Node(data);
84-
Node temp = find(key);
8592
if (temp == null) {
86-
System.out.println("Node with key " + key + " not found");
93+
System.out.println("Key is not found.");
8794
return;
8895
}
89-
newNode.next = temp.next;
90-
temp.next = newNode;
96+
prev.next = temp.next;
97+
size--;
9198
}
9299

93-
// Delete the last node in the linked list
94100
public void deleteFromEnd() {
95101
if (head == null) {
96102
System.out.println("Linked list is empty");
97103
return;
98104
}
99105
if (head.next == null) {
100106
head = null;
107+
size--;
101108
return;
102109
}
103110
Node temp = head;
104111
while (temp.next.next != null) {
105112
temp = temp.next;
106113
}
107114
temp.next = null;
115+
size--;
108116
}
117+
public void display() {
109118

110-
public void delete(int key) {
111-
Node temp = head;
112-
Node prev = null;
113-
114-
// If the head node itself contains the key to be deleted
115-
if (temp != null && temp.data == key) {
116-
head = temp.next; // Change the head node
117-
return;
119+
if (isEmpty()) {
120+
System.out.println("List is Empty.");
121+
} else {
122+
Node n = head;
123+
while (n != null) {
124+
System.out.print(n.data + "\t");
125+
n = n.next;
126+
}
127+
System.out.println("");
118128
}
129+
}
119130

120-
// Search for the key to be deleted, keep track of the previous node as well
121-
while (temp != null && temp.data != key) {
122-
prev = temp;
131+
public Node find(int key) {
132+
Node temp = head;
133+
while (temp != null) {
134+
if (temp.data == key) {
135+
return temp;
136+
}
123137
temp = temp.next;
124138
}
139+
return null;
140+
}
125141

126-
// If key was not present in the linked list
127-
if (temp == null) {
128-
return;
129-
}
142+
public static void main(String[] args) {
143+
SinglyLinkedList n = new SinglyLinkedList();
144+
n.addFirst(12);
145+
n.addAtEnd(20);
146+
n.addAfter(12, 18);
147+
System.out.println(n.size());
148+
n.display();
149+
n.delete(18);
150+
n.display();
151+
System.out.println(n.size());
130152

131-
// Unlink the node from the linked list
132-
prev.next = temp.next;
133153
}
134154

135155
}

0 commit comments

Comments
(0)

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