Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 24 additions & 24 deletions DataStructures/LinkedList.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package DataStructures;
package valueStructures;

import java.util.Scanner;

Expand All @@ -7,23 +7,23 @@ public class LinkedList {
private Node head;
static class Node {
Node next;
int data;
public Node(int data) {
this.data = data;
int value;
public Node(int value) {
this.value = value;
next = null;
}
}

public void print() {
Node node = head;
while(node != null) {
System.out.print(node.data + "\t");
System.out.print(node.value + "\t");
node = node.next;
}
}

private void insertFront(int new_data) {
Node newNode = new Node(new_data);
private void insertFront(int new_value) {
Node newNode = new Node(new_value);
if (head == null) {
head = newNode;
return;
Expand All @@ -32,8 +32,8 @@ private void insertFront(int new_data) {
head = newNode;
}

private void insertBetween(int new_data, Node prevNode, Node nextNode) {
Node newNode = new Node(new_data);
private void insertBetween(int new_value, Node prevNode, Node nextNode) {
Node newNode = new Node(new_value);
if (head == null || prevNode == null) {
head = newNode;
return;
Expand All @@ -42,8 +42,8 @@ private void insertBetween(int new_data, Node prevNode, Node nextNode) {
newNode.next = nextNode;
}

private void insertEnd(int new_data) {
Node newNode = new Node(new_data);
private void insertEnd(int new_value) {
Node newNode = new Node(new_value);
if (head == null) {
head = newNode;
return;
Expand All @@ -56,25 +56,25 @@ private void insertEnd(int new_data) {
newNode.next = null;
}

private void search(int data) {
private void search(int value) {
Node key = head;
if (head.data == data) {
System.out.println("Key Node: " + head.data);
if (head.value == value) {
System.out.println("Key Node: " + head.value);
return;
}
while(key.data != data) {
while(key.value != value) {
key = key.next;
}
System.out.println("Search Node Data is " + key.data);
System.out.println("Search Node value is " + key.value);
}

public void delete(int data) {
public void delete(int value) {
Node temp = head, prev = null;
if (temp != null && temp.data == data) {
if (temp != null && temp.value == value) {
head = temp.next;
return;
}
while(temp != null && temp.data != data) {
while(temp != null && temp.value != value) {
prev = temp;
temp = temp.next;
}
Expand All @@ -95,27 +95,27 @@ public static void main(String args[]) {
list.print();
System.out.println();

System.out.println("Insert Data in Front...");
System.out.println("Insert at the Front...");
list.insertFront(s.nextInt());
list.print();
System.out.println();

System.out.println("Insert Data in between...");
System.out.println("Insert value in between nodes...");
list.insertBetween(s.nextInt(), secondNode, thirdNode);
list.print();
System.out.println();

System.out.println("Insert Data at end...");
System.out.println("Insert at the end...");
list.insertEnd(s.nextInt());
list.print();
System.out.println();

System.out.println("Search Data...");
System.out.println("Search for a value...");
list.search(s.nextInt());
//list.print();
System.out.println();

System.out.println("Delete Node...");
System.out.println("Delete a Node...");
list.delete(s.nextInt());
list.print();
System.out.println();
Expand Down
119 changes: 119 additions & 0 deletions DataStructures/deletekthnodefrmend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
class Node:
def __init__(self, data, next = None):
self.data = data
self.next = next

class LinkedList:

def __init__(self, *args, **kwargs):
self.head = Node(None)
"""
Utility function to insert a node at the beginning
@args:
data: value of node
"""
def push(self, data):
node = Node(data)
node.next = self.head
self.head = node

# Print linked list
def printList(self):
node = self.head
while node.next is not None:
print(node.data, end = " ")
node = node.next

# count number of node in linked list
def countNodes(self):
count = 0
node = self.head
while node.next is not None:
count += 1
node = node.next
return count

"""
Function for swapping kth nodes from
both ends of linked list
"""
def swapKth(self, k):

# Count nodes in linked list
n = self.countNodes()

# check if k is valid
if n<k:
return

"""
If x (kth node from start) and
y(kth node from end) are same
"""
if (2 * k - 1) == n:
return

"""
Find the kth node from beginning of linked list.
We also find previous of kth node because we need
to update next pointer of the previous.
"""
x = self.head
x_prev = Node(None)
for i in range(k - 1):
x_prev = x
x = x.next

"""
Similarly, find the kth node from end and its
previous. kth node from end is (n-k+1)th node
from beginning
"""
y = self.head
y_prev = Node(None)
for i in range(n - k):
y_prev = y
y = y.next

"""
If x_prev exists, then new next of it will be y.
Consider the case when y->next is x, in this case,
x_prev and y are same. So the statement
"x_prev->next = y" creates a self loop. This self
loop will be broken when we change y->next.
"""
if x_prev is not None:
x_prev.next = y

# Same thing applies to y_prev
if y_prev is not None:
y_prev.next = x

"""
Swap next pointers of x and y. These statements
also break self loop if x->next is y or y->next
is x
"""
temp = x.next
x.next = y.next
y.next = temp

# Change head pointers when k is 1 or n
if k == 1:
self.head = y

if k == n:
self.head = x

# Driver Code
llist = LinkedList()
for i in range(8, 0, -1):
llist.push(i)
llist.printList()


for i in range(1, 9):
llist.swapKth(i)
print("Modified List for k = ", i)
llist.printList()
print("\n")
141 changes: 141 additions & 0 deletions DataStructures/deleteprimesfrmlinkedlist.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
class GFG
{

// Structure for a node
static class Node
{
int data;
Node next;
};

// Function to insert a node at the beginning
// of a Circular linked list
static Node push(Node head_ref, int data)
{
Node ptr1 = new Node();
Node temp = head_ref;
ptr1.data = data;
ptr1.next = head_ref;

// If linked list is not null then
// set the next of last node
if (head_ref != null)
{
while (temp.next != head_ref)
temp = temp.next;
temp.next = ptr1;
}
else
ptr1.next = ptr1; // For the first node

head_ref = ptr1;
return head_ref;
}

// Delete the node if it is prime
static Node deleteNode(Node head_ref, Node del)
{
Node temp = head_ref;

// If node to be deleted is head node
if (head_ref == del)
head_ref = del.next;

// traverse list till not found
// delete node
while (temp.next != del)
{
temp = temp.next;
}

// copy address of node
temp.next = del.next;
return head_ref;
}

// Function to check if a number is prime
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;

if (n <= 3)
return true;

// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;

for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;

return true;
}

// Function to delete all prime nodes
// from the singly circular linked list
static Node deletePrimeNodes(Node head)
{
Node ptr = head;

Node next;

// traverse list till the endl
// if node is prime then delete it
do
{
// if node is prime
if (isPrime(ptr.data))
deleteNode(head, ptr);

// point to next node
next = ptr.next;
ptr = next;

}
while (ptr != head);
return head;
}

// Function to print nodes in a
// given singly linked list
static void printList(Node head)
{
Node temp = head;
if (head != null)
{
do
{
System.out.printf("%d ", temp.data);
temp = temp.next;
}
while (temp != head);
}
}

// Driver code
public static void main(String args[])
{
// Initialize lists as empty
Node head = null;

// Created linked list will be
// 9.11.32.6.13.20
head=push(head, 20);
head=push(head, 13);
head=push(head, 6);
head=push(head, 32);
head=push(head, 11);
head=push(head, 9);

System.out.println("Given List : ");
printList(head);

System.out.println( "\nList After deleting prime nodes : ");
head=deletePrimeNodes(head);
printList(head);
}
}
Loading