0% found this document useful (0 votes)
28 views5 pages

FRQFF See Gre

Uploaded by

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

FRQFF See Gre

Uploaded by

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

Course Name Data Structures

Course No CCCS224
Lab No 7
Chapter Double, circular linked list

Double, circular Linked List

Exercise 1 :

Let a list composed by a set of elements. Each element is represented as follow:


Address of the Address of the next
Name
previous element element
Address of the current element
Given the following linked list of 5 elements.
@2105 E @1300 @1200 C @1450
@1200 @1300
@1300 A @NULL
@1450 @1965 B @1200
@2105 @NULL D @2105
@1965
a. Draw the previous list and indicate its type

b. The type of the list is:


_________________________________________________________
c. Fill the address of nodes when the node C is removed.

E C

A
B
D

d. Fill the address of nodes when the node C is inserted in the same previous address @1300
but between D and B.

E C

A
B
D

e. Implement the previous linked list (questions a, c and d. of course without


considering the address)
Exercise 2 :

Let a list composed by a set of elements. Each element is represented as follow:


Address of the Address of the next
Name
previous element element
Address of the current element

Given the following linked list of 5 elements.


@1300 E @2105 @1450 C @1200
@1200 @1300
@1965 A @1300
@1450 @1200 B @1965
@2105 @2105 D @1450
@1965

1. Draw the previous list and indicate its type.


2. Write the code to add the element F after the element C.
3. write the code to delete the element E.
4. Implement the function display to show all the list elements.
} class Node
;String data
;Node prev
;Node next

} public Node(String data)


;this.data = data
;this.prev = null
;this.next = null
{
{

} public class DoubleCircularLinkedList


;Node head

} ()public void display


} if (head == null)
;System.out.println("The list is empty.")
;return
{
;Node temp = head
} do
;System.out.print(temp.data + " ")
;temp = temp.next
;while (temp != head) {
;()System.out.println
{

} public void insertAtHead(String data)


;Node newNode = new Node(data)
} if (head == null)
;head = newNode
;newNode.next = newNode
;newNode.prev = newNode
} else {
;Node tail = head.prev
;newNode.next = head
;newNode.prev = tail
;head.prev = newNode
;tail.next = newNode
;head = newNode
{
{

} public void deleteNode(Node node)


} if (head == node && head.next == head)
;head = null
;return
{
;Node prevNode = node.prev
;Node nextNode = node.next
;prevNode.next = nextNode
;nextNode.prev = prevNode
} if (node == head)
;head = nextNode
{
{

} public void insertAfter(Node node, String data)


;Node newNode = new Node(data)
;Node nextNode = node.next
;newNode.next = nextNode
;newNode.prev = node
;node.next = newNode
;nextNode.prev = newNode
{

} public static void main(String[] args)


;()DoubleCircularLinkedList list = new DoubleCircularLinkedList
;list.insertAtHead("D")
;list.insertAtHead("B")
;list.insertAtHead("C")
;list.insertAtHead("E")

;System.out.println("Original list:")
;()list.display

;Node nodeC = list.head.next


;list.deleteNode(nodeC)
;System.out.println("After deleting C:")
;()list.display

;Node nodeD = list.head.prev


;list.insertAfter(nodeD, "C")
;System.out.println("After reinserting C:")
;()list.display
{
{
} class Node
;String data
;Node prev
;Node next

} public Node(String data)


;this.data = data
;this.prev = null
;this.next = null
{
{

} public class DoubleCircularLinkedList


;Node head

} ()public void display


} if (head == null)
;System.out.println("The list is empty.")
;return
{
;Node temp = head
} do
;System.out.print(temp.data + " ")
;temp = temp.next
;while (temp != head) {
;()System.out.println
{

} public void insertAtEnd(String data)


;Node newNode = new Node(data)
} if (head == null)
;head = newNode
;newNode.next = newNode
;newNode.prev = newNode
} else {
;Node tail = head.prev
;newNode.next = head
;newNode.prev = tail
;tail.next = newNode
;head.prev = newNode
{
{

} public void insertAfter(Node node, String data)


;Node newNode = new Node(data)
;Node nextNode = node.next
;newNode.next = nextNode
;newNode.prev = node
;node.next = newNode
;nextNode.prev = newNode
{
} public void deleteNode(Node node)
} if (head == node && head.next == head)
;head = null
;return
{
;Node prevNode = node.prev
;Node nextNode = node.next
;prevNode.next = nextNode
;nextNode.prev = prevNode
} if (node == head)
;head = nextNode
{
{

} public Node find(String data)


;Node temp = head
} do
} if (temp.data.equals(data))
;return temp
{
;temp = temp.next
;while (temp != head) {
;return null
{

} public static void main(String[] args)


DoubleCircularLinkedList list = new
;()DoubleCircularLinkedList

Create the initial list: E -> C -> B -> A -> D //


;list.insertAtEnd("E")
;list.insertAtEnd("C")
;list.insertAtEnd("B")
;list.insertAtEnd("A")
;list.insertAtEnd("D")

;System.out.println("Original list:")
;()list.display

Add element F after C //


;Node nodeC = list.find("C")
} if (nodeC != null)
;list.insertAfter(nodeC, "F")
{
;System.out.println("After inserting F after C:")
;()list.display

Delete element E //
;Node nodeE = list.find("E")
} if (nodeE != null)

You might also like