You are on page 1of 7

Subject: Data Structures

Prepared By: Elidon Neziri (Problem 1 and 3)


Emiliano Caushllari ( Problem 2)

Lab work: Arrays, Linked Lists,


Stacks & Queues
Problem 1:Tower of Hanoi Problem 2. Implementing a QueueProblem 3. Problem 3. Doubly Linked List
Problem 1:Tower of Hanoi
‘Tower of Hanoi’ is a mathematical puzzle where we have three main columns A, B and C, and several discs (D1, D2, D3,… etc.). The discs
have different dimensions, and are put in one column from the largest(bottom) to the smallest(top).

The objective of this puzzle is to move all the discs (one by one), by following the rules below:
• Only one disc can be moved at a time;
• Only the disc on top can be moved;
• A larger disc cannot be put above a smaller disc.

• You have to implement the above problem, with one of the data structures we have studied until now. You are free to implement a program
using an iterative or recursive method.

Input of the program:


• The number of discs: 2, 3 etc. discs;
Output of the program:
• All the steps, or the movements of the discs from one column to another are printed;
• You also have to print the elements in each column A,B and C in the end of the program’s execution.
import java.util.Scanner;
The code At first we create the solveTowersOfHanoi function, which will have 4
variables.
public class TowersOfHanoi { The number of discs, the rod that we want to move, the rod which will
public void solveTowersOfHanoi(int n, String fromRod, String otherRod, String destinationRod) help us and the rod where we want to move it.
{
// If only 1 disk, make the move and return.
If number of rods is 1, of course there would be only one step. If there
if(n==1) are more, we move
{
System.out.println(fromRod+" ---> "+destinationRod); the top n-1 disk from A to B using C. We print the disks movements and
return;
}
then we move n-1 disks from B to C using A.
// Move top n-1 disks from A to B using C
solveTowersOfHanoi(n-1, fromRod, destinationRod, otherRod); Now in main, we create the tower of hanoi. We ask the user to put
// Move remaining disks from A to C number of disks and then we use the function on the
System.out.println(fromRod+" ---> "+destinationRod);
new_TowerOfHanoi and execute it by giving them all the required
// Move n-1 disks from B to C `using A
solveTowersOfHanoi(n-1, otherRod, fromRod, destinationRod);
variables.
Then the result will be printed
}

public static void main(String args[])


{
TowersOfHanoi TowersOfHanoi = new TowersOfHanoi();

System.out.println("Enter number of disks : ");

Scanner scanner = new Scanner(System.in);


int n = scanner.nextInt();

System.out.println("Move disks as below illustration.");


TowersOfHanoi.solveTowersOfHanoi(n, "A", "B", "c");
}
}
Problem 2:
Doubly Linked List
• Create a doubly linked list by adding new nodes, so that the list
remains ordered from the smallest to the largest element. You have to
take in consideration two reference nodes: header and trailer.
import java.util.*; The code This method makes sure that oldest entered element is always at the
top of stack 1, so that deQueue operation just pops from stack1. To put
class GFG
{
the element at top of stack1, stack2 is used.
static class Queue
// Dequeue an item from the queue enQueue(q, x):
{
static Stack<Integer> s1 = new Stack<Integer>();
static int deQueue()
{
While stack1 is not empty, push everything from stack1 to stack2. Push
static Stack<Integer> s2 = new Stack<Integer>();
// if first stack is empty x to stack1 (assuming size of stacks is unlimited). Push everything back
if (s1.isEmpty())
static void enQueue(int x)
{ to stack1. Here time complexity will be O(n)
{
System.out.println("Q is Empty");
// Move all elements from s1 to s2
System.exit(0);
deQueue(q):
while (!s1.isEmpty())
{
}
If stack1 is empty then error Pop an item from stack1 and return it Here
s2.push(s1.pop());
//s1.pop();
// Return top of s1 time complexity will be O(1)
int x = s1.peek();
}
s1.pop();
return x;
// Push item into s1
}
s1.push(x);
};

// Push everything back to s1


// Driver code
while (!s2.isEmpty())
public static void main(String[] args)
{
{
s1.push(s2.pop());
Queue q = new Queue();
//s2.pop();
q.enQueue(1);
}
q.enQueue(2);
}
q.enQueue(3);

System.out.println(q.deQueue());
System.out.println(q.deQueue());
System.out.println(q.deQueue());
}
}

// This code is contributed by Prerna Saini


Problem 3:
Doubly Linked List
• Create a doubly linked list by adding new nodes, so that the list
remains ordered from the smallest to the largest element. You have to
take in consideration two reference nodes: header and trailer.
package ascending.linked.list;
class Node
The code First we declare the node with all its elements. After that we create a function to create a new node based
on the data, which will return the new Node.
Secondly we create the function to insert a new node in sorted way in a sorted doubly linked list.
{
int data;
Now if the list is empty, we will assign the newNode to the head.
Node next, prev;
} If node is inserted in the beginning of the linked list, newNode.next will be connnected to the head,
class GFG
{
public static Node head, tail;
head.prev will have the newNode value, and then assign the value of the newNode to the head. After that
// function that creates and returns a new node of the doubly linked list we assign the head to the temporary node called current and we proceed further.
public static Node getNode(int data)
{ Now we will locate the node after which the new node is to be inserted. While the next element is not null
// creating the node
Node newNode = new Node(); and the next.element.data is smaller than the newNode, we make the appropriate links to the newNode's
// defining the data
newNode.data = data; position.
newNode.prev = newNode.next = null;
return newNode; The while cycle will check every node once. This is why I included the function many times in the main
} function. Then, we return head.
// function to insert a new node in sorted way in a sorted doubly linked list
static Node sortedInsert(Node head, Node newNode)
{
Node current; Now we go in the main function and we give head the value null so all the conditions of sort can be met.
// if the list is empty
if (head == null) Then we create the new node and give value x to it by using getNode. Then we use the sortedInsert
head = newNode;
// if node is inserted in the beginning of the linked list
function to sort the node that currently has 1 value. Then we do it again, we create another node and we
else if (head.data >= newNode.data) assign the value y to it. Now we have 2 sorted nodes etc. We continue till we want to stop.
{
newNode.next = head;
head.prev = newNode;
}
head = newNode; In the end, we create a print function using while with head=head.next till it goes null.
else After that, we use the function in main, assign the node to it and the sorted list will be printed.
{
current = head;
// locate the node after which the new node is to be inserted
while (current.next != null &&
current.next.data < newNode.data)
current = current.next;
/* Make the appropriate links */
newNode.next = current.next;
current.next = newNode;
newNode.prev = current; public static void main(String args[]) {
/* start with the empty doubly linked list */
} Node head = null;
return head;
} // insert the following nodes in sorted way
Node new_node = getNode(8);
// printing the list head = sortedInsert(head, new_node);
static void printList(Node head) new_node = getNode(5);
{ head = sortedInsert(head, new_node);
while (head != null) new_node = getNode(3);
{ head = sortedInsert(head, new_node);
System.out.print(head.data + " "); new_node = getNode(10);
head = head.next; head = sortedInsert(head, new_node);
} new_node = getNode(12);
head = sortedInsert(head, new_node);
} new_node = getNode(9);
head = sortedInsert(head, new_node);
System.out.println("Created Doubly Linked List");
printList(head);
}
}

You might also like