0% found this document useful (0 votes)
41 views6 pages

DSL5

The document discusses deleting a node from a linked list by key. It provides code to: 1) Find the node to delete by traversing the list and tracking the previous node 2) Update the next pointer of the previous node to skip over the node to delete 3) Free the memory of the deleted node It also provides information on implementing a circular linked list by connecting the next pointer of the last node to the head node, and a doubly linked list where each node contains pointers to the previous and next nodes.

Uploaded by

FOOTBALL GURUS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views6 pages

DSL5

The document discusses deleting a node from a linked list by key. It provides code to: 1) Find the node to delete by traversing the list and tracking the previous node 2) Update the next pointer of the previous node to skip over the node to delete 3) Free the memory of the deleted node It also provides information on implementing a circular linked list by connecting the next pointer of the last node to the head node, and a doubly linked list where each node contains pointers to the previous and next nodes.

Uploaded by

FOOTBALL GURUS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

To remove the node containing the integer 10,


i) Find the previous node of the node to be deleted which is 3 in this case.
ii) Change the next of the previous node.
iii) Free memory for the node to be deleted.

2. Java code for deleting node

class LinkedList {
Node head;

class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}

void deleteNode(int key)


{

Node temp = head, prev = null;

if (temp != null && temp.data == key) {


head = temp.next; // Changed head
return;
}

while (temp != null && temp.data != key) {


prev = temp;
temp = temp.next;
}

// If key was not present in linked list


if (temp == null)
return;

// Unlink the node from linked list


prev.next = temp.next;
}

/* Inserts a new Node at front of the list. */


public void push(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}

public void printList()


{
Node tnode = head;
while (tnode != null) {
System.out.print(tnode.data + " ");
tnode = tnode.next;
}
}

public static void main(String[] args)


{
LinkedList llist = new LinkedList();

llist.push(5);
llist.push(3);
llist.push(10);
llist.push(7);
llist.push(19);

System.out.println("\nCreated Linked list is:");


llist.printList();

llist.deleteNode(10); // Delete node with data 10

System.out.println(
"\nLinked List after Deletion of 10:");
llist.printList();
}
}
3.

4. To implement a circularly linked list, we must;

i) Create and define a Node class which represents the nodes in the list.
ii) Define another class for creating the circular linked list
Diagram of Circularly linked list

Head

5 3 10 7 19

5.

class main
{
static class circular
{
int data;
Node next;
};

static Node rotate(Node head)

Node start = head;

while (head.next != null)


head = head.next;

head.next = start;
return start;
}

static Node push(Node head, int data)


{

Node newNode = new Node();

newNode.data = data;

newNode.next = (head);

(head) = newNode;

return head;
}

static void displayList( Node node)


{
Node start = node;

while (node.next != start)


{
System.out.print(" "+ node.data);
node = node.next;
}

System.out.print(" " + node.data);


}
public static void main(String args[])
{

Node head = null;

head = push(head, 5);


head = push(head, 3);
head = push(head, 10);
head = push(head, 7);
head = push(head, 19);

rotate(head);

System.out.print("Display list: \n");


displayList(head);
}
}

6. In a Doubly Linked we can traverse in both directions since each node has the address
of its previous and the next node.

iii) DLL nodes contains 3 fields known as data field, a previous link field and a
next link field.

7. I) find(Eben)
Step 1: IF HEAD == NULL
Step 2: Set pointer = HEAD
Step 3: Set i = 0
Step 4: Repeat step 5 to 7 while pointer != NULL
Step 5: IF pointer → data = F
Step 6: i = i + 1
Step 7: pointer = pointer → next
Step 8: Exit

ii) Step 1: IF pointer = NULL


Step 2: SET newnode = pointer
Step 3: SET pointer = pointer -> NEXT
Step 4: SET newnode -> DATA = VAL
Step 5: SET newnode -> PREV = NULL
Step 6: SET newnode -> NEXT = START
Step 7: SET head -> PREV = newnode
Step 8: SET head = newnode
Step 9: EXIT

You might also like