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 396fd4f

Browse files
update
1 parent e39fa51 commit 396fd4f

31 files changed

+944
-0
lines changed

‎pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mycompany</groupId>
5+
<artifactId>DataStructures</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
<properties>
9+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<maven.compiler.source>19</maven.compiler.source>
11+
<maven.compiler.target>19</maven.compiler.target>
12+
<exec.mainClass>com.mycompany.datastructures.DataStructures</exec.mainClass>
13+
</properties>
14+
</project>
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package linklist.node;
6+
7+
/**
8+
*
9+
* @author fawad
10+
*/
11+
public class CircularLinkedList {
12+
13+
Node last; // last node of the circular linked list
14+
15+
// Node class
16+
static class Node {
17+
18+
int data;
19+
Node next;
20+
21+
Node(int data) {
22+
this.data = data;
23+
next = null;
24+
}
25+
}
26+
27+
// Constructor to initialize the last node of the circular linked list
28+
CircularLinkedList() {
29+
last = null;
30+
}
31+
32+
// Display the circular linked list
33+
public void display() {
34+
if (last == null) {
35+
System.out.println("Circular linked list is empty");
36+
return;
37+
}
38+
Node temp = last.next;
39+
do {
40+
System.out.print(temp.data + " ");
41+
temp = temp.next;
42+
} while (temp != last.next);
43+
System.out.println();
44+
}
45+
46+
// Count the number of nodes in the circular linked list
47+
public int countNodes() {
48+
int count = 0;
49+
if (last == null) {
50+
return count;
51+
}
52+
Node temp = last.next;
53+
do {
54+
count++;
55+
temp = temp.next;
56+
} while (temp != last.next);
57+
return count;
58+
}
59+
60+
// Find a node with a given key value
61+
public Node find(int key) {
62+
if (last == null) {
63+
return null;
64+
}
65+
Node temp = last.next;
66+
do {
67+
if (temp.data == key) {
68+
return temp;
69+
}
70+
temp = temp.next;
71+
} while (temp != last.next);
72+
return null;
73+
}
74+
75+
// Add a new node with given data at the beginning of the circular linked list
76+
public void addAtFirst(int data) {
77+
Node newNode = new Node(data);
78+
if (last == null) {
79+
last = newNode;
80+
last.next = last;
81+
} else {
82+
newNode.next = last.next;
83+
last.next = newNode;
84+
}
85+
}
86+
87+
// Add a new node with given data at the end of the circular linked list
88+
public void addAtEnd(int data) {
89+
Node newNode = new Node(data);
90+
if (last == null) {
91+
last = newNode;
92+
last.next = last;
93+
} else {
94+
newNode.next = last.next;
95+
last.next = newNode;
96+
last = newNode;
97+
}
98+
}
99+
100+
// Add a new node with given data after the node with a given key value
101+
public void addAfter(int key, int data) {
102+
Node newNode = new Node(data);
103+
if (last == null) {
104+
System.out.println("Circular linked list is empty");
105+
return;
106+
}
107+
Node temp = find(key);
108+
if (temp == null) {
109+
System.out.println("Node with key " + key + " not found");
110+
return;
111+
}
112+
newNode.next = temp.next;
113+
temp.next = newNode;
114+
if (temp == last) {
115+
last = newNode;
116+
}
117+
}
118+
119+
// Delete the node with given data from the circular linked list
120+
public void delete(int key) {
121+
if (last == null) {
122+
System.out.println("Circular linked list is empty");
123+
return;
124+
}
125+
Node temp = last.next;
126+
Node prev = last;
127+
do {
128+
if (temp.data == key) {
129+
break;
130+
}
131+
prev = temp;
132+
temp = temp.next;
133+
} while (temp != last.next);
134+
if (temp == last.next && temp == last) {
135+
last = null;
136+
} else if (temp == last.next) {
137+
last.next = temp.next;
138+
} else if (temp == last) {
139+
prev.next = last.next;
140+
141+
last = prev;
142+
} else {
143+
prev.next = temp.next;
144+
}
145+
}
146+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package linklist.node;
6+
7+
/**
8+
*
9+
* @author fawad
10+
*/
11+
public class DoublyLinkedList {
12+
13+
Node head; // head of the linked list
14+
15+
// Node class
16+
static class Node {
17+
18+
int data;
19+
Node next;
20+
Node prev;
21+
22+
Node(int data) {
23+
this.data = data;
24+
next = null;
25+
prev = null;
26+
}
27+
}
28+
29+
// Constructor to initialize the head of the linked list
30+
DoublyLinkedList() {
31+
head = null;
32+
}
33+
34+
// Display the linked list
35+
public void display() {
36+
Node temp = head;
37+
while (temp != null) {
38+
System.out.print(temp.data + " ");
39+
temp = temp.next;
40+
}
41+
System.out.println();
42+
}
43+
44+
// Count the number of nodes in the linked list
45+
public int countNodes() {
46+
int count = 0;
47+
Node temp = head;
48+
while (temp != null) {
49+
count++;
50+
temp = temp.next;
51+
}
52+
return count;
53+
}
54+
55+
// Find a node with a given key value
56+
public Node find(int key) {
57+
Node temp = head;
58+
while (temp != null) {
59+
if (temp.data == key) {
60+
return temp;
61+
}
62+
temp = temp.next;
63+
}
64+
return null;
65+
}
66+
67+
// Add a new node with given data at the beginning of the linked list
68+
public void addAtFirst(int data) {
69+
Node newNode = new Node(data);
70+
if (head == null) {
71+
head = newNode;
72+
} else {
73+
head.prev = newNode;
74+
newNode.next = head;
75+
head = newNode;
76+
}
77+
}
78+
79+
// Add a new node with given data at the end of the linked list
80+
public void addAtEnd(int data) {
81+
Node newNode = new Node(data);
82+
if (head == null) {
83+
head = newNode;
84+
} else {
85+
Node temp = head;
86+
while (temp.next != null) {
87+
temp = temp.next;
88+
}
89+
temp.next = newNode;
90+
newNode.prev = temp;
91+
}
92+
}
93+
94+
// Add a new node with given data after the node with a given key value
95+
public void addAfter(int key, int data) {
96+
Node newNode = new Node(data);
97+
Node temp = find(key);
98+
if (temp == null) {
99+
System.out.println("Node with key " + key + " not found");
100+
return;
101+
}
102+
if (temp.next != null) {
103+
temp.next.prev = newNode;
104+
newNode.next = temp.next;
105+
}
106+
temp.next = newNode;
107+
newNode.prev = temp;
108+
}
109+
110+
// Delete the node with given data from the linked list
111+
public void delete(int key) {
112+
Node temp = find(key);
113+
if (temp == null) {
114+
System.out.println("Node with key " + key + " not found");
115+
return;
116+
}
117+
if (temp.prev != null) {
118+
temp.prev.next = temp.next;
119+
} else {
120+
head = temp.next;
121+
}
122+
if (temp.next != null) {
123+
temp.next.prev = temp.prev;
124+
}
125+
}
126+
}

0 commit comments

Comments
(0)

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