Data Structures
Data Structures
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 ;
1- (p == null)
2- (p.next == null)
3- (p.item == null)
4- (p.item == 0)
5- None of the above.
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() {
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;
}
ListNode firstNode;
class ListNode {
int info;
ListNode link;
public ListNode() {
this(0, null);
}
public ListNode(int info) {
this(info, null);
}
Solution by https://t.me/software_AUG