0% found this document useful (0 votes)
19 views

Data Structures

Data structure pdf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Data Structures

Data structure pdf
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

2022/10/20

Solution
Data Structure

HTTPS://T.ME/SOFTWARE_ENG_ALAZHAR
SOFTWARE ENGINEERING
1- Given following data structure of Single linked list :
class ListNode
{ int item ;
ListNode next ;
….
}
Choose the correct answer :
A- Suppose reference refers to a node in a List (using the ListNode ) .
What statement changes reference so that it refers to the next node?

1- reference++ ;
2- reference = next ;
3- reference+= next ;
4- reference = reference.next ;

B- Suppose p refers to a node in a List (using the ListNode ) .


What boolean expression will be true when p refers to the last node of the
List?

1- (p == null)
2- (p.next == null)
3- (p.item == null)
4- (p.item == 0)
5- None of the above.

C- Which boolean expression indicates whether the items in two nodes (n


and m) are the same. Assume that neither n nor m is null.

1- n == m
2- n.item == m.item
3- n.next == m.next
4- None of the above
2- Given following data structures of a double linked list :
class ListNode
{
String info ;
ListNode prev ;
ListNode next ;
…….
}
Write a Java method which prints the content of double linked list in reverse order.

Solution :
class LinkedList {

listNode N;

class listNode {

String info;
listNode prev;
listNode next;

public listNode(String info, listNode prev, listNode next) {


this.info = info;
this.prev = prev;
this.next = next;
}

public listNode(String info) {


this(info, null, null);
}

public listNode() {
this(null, null, null);
}

void reverse() {
listNode temp = null;
listNode current = N;
while (current != null) {
temp = current.prev;
current.prev = current.next;
current.next = temp;
current = current.prev;
}
if (temp != null) {
N = temp.prev;
}
}
void add(String new_info) {
listNode new_node = new listNode(new_info);
new_node.prev = null;
new_node.next = N;
if (N != null) {
N.prev = new_node;
}
N = new_node;
}

void printList(listNode node) {


while (node != null) {
System.out.print(node.info + " ");
node = node.next;
}
System.out.println("");
}

public static void main(String[] args) {


LinkedList list = new LinkedList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
System.out.println("Original linked list ");
list.reverse();
list.printList(list.N);
list.reverse();
System.out.println("");
System.out.println("The reversed Linked List is ");
list.printList(list.N);
System.out.println("");
}
}
3- Given following ADT of a single linked list containing integer values :
class ListNode
{
int info;
ListNode link;

}
Write a JAVA method
which reads the values of a given single linked list and creates two lists, one list contains
the odd values and the other list contains the even values of the list.
Solution :
class LinkedList {

ListNode firstNode;

class ListNode {
int info;
ListNode link;

public ListNode(int info, ListNode link) {


this.info = info;
this.link = link;
}

public ListNode() {
this(0, null);
}
public ListNode(int info) {
this(info, null);
}

public int getInfo() {


return info;
}

public void print() {


ListNode N;
N = firstNode;
while (N != null) {
System.out.print(N.info+" ");
N = N.link;
}
System.out.println("");
}

public void newNode(int info) {


if (firstNode.link == null) {
}
ListNode newNode = new ListNode();
newNode.info = info;
newNode.link = firstNode.link;
firstNode.link = newNode;
}
void add(int new_info) {
ListNode new_node = new ListNode(new_info);
new_node.link = firstNode;
firstNode = new_node;
}

public static void main(String[] args) {


LinkedList list = new LinkedList();
list.add(30);
list.add(4);
list.add(5);
list.add(17);
list.add(10);
LinkedList L1 = new LinkedList();
LinkedList L2 = new LinkedList();
System.out.println("Original linked list ");
list.print();
while(list.firstNode != null ){
if(list.firstNode.info % 2 == 0){
L2.add(list.firstNode.info);
// list.firstNode = list.firstNode.link;
}else{
L1.add(list.firstNode.info);
}
list.firstNode = list.firstNode.link;
}
System.out.println("List even : ");
L2.print();
System.out.println("List odd : ");
L1.print();
}
}

Solution by https://t.me/software_AUG

You might also like