5. Given the following definition of class Node:
class Node { public int item; public Node next; public Node ( int
i, Node n ) { item = i; next = n; }; }
and the following declarations:
Node list, p, q;
Assume the structure starts as below. Draw the structure created by
executing the following statements. Each part is independent.
list 1 2 3 4 5
[2] a) p = list; q = null; while ( p != null && p.item <
5 ) { p.item = p.item + 1; q = p.next; p = q.next; };
[2] b) p = list; while ( p != null ) { del(p); p = p.next; }; :
private void del ( Node n ) { n = null; };
[6] c) Assume the declaration of the class Node as above. Write a
method concat that concatenates (joins) list2 to the end of list1.
That is, in the result the first node of list2 should follow the
last node of list1.The method returns resulting list. No new nodes
should be created. If list1 is empty the function should return
list2 as the result.
private Node concat ( Node list1, Node list2 ) {
[2] a) ans.
list 1 2 3 4 5
p =list;
q=null;
while(p!= null && p.item<5){
p.item = p.item+1; 2 2 4 4 5
q = p.next; q=4
p = q.next; p=5
}
After execution of above code the updated list will be 2 2 4 4
5.
This is because after incrementing the first item (1) by 1, p will
move to node 3 by skipping node 2 as p move two nodes (p.next.next)
at a time.
similarly, it will modify 3 to 4 and then again moved to node 5
after skipping node 4.
Now since p.item = 5 and hence it will come out from the while loop
as condition (p.item<5) will be false.
So final list will be 2 2 4 4 5.
[2] b) ans.
p = list;
while(p!=null){
del(p);
p=p.next;
}
private void del(Node n){
n=null;
}
After execution of the above code program will get crash as in
the above code we are trying to access the address of next element
after deleting the current node.
Once we will delete the node p, p will be assigned to null and
hence we can not access the next node through p.
[6] c) ans.
private Node concat(Node list1,Node list2){
Node p,q;
p = list1; // p pointing to start node of list1
q = list2; // q pointing to start node of list2
/*If list1 is empty return list2*/
if(list1==null)
return list2;
/*If list2 is empty return list1*/
if(list2 == null)
return list1;
/*Both lists are not empty*/
while(p != null){
p = p.next //Moved p to end of list1
}
/*Run through the list2 and add each element of list2 to last of
list1*/
while(q != null){
p.item = q.item;
p = p.next;
q = q.next;
}
return p; //Return list1 after concating all element of
list2.
}
5. Given the following definition of class Node: class Node { public int item; public Node...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). /* definition of the list node class */ class Node { friend class LinkedList; private: int value; Node *pNext; public: /* Constructors with No Arguments...
Add the following method to xxxxxp3: // deletes x from sorted list k if exist, k = 0, 1, 2 private void delete(x, k) // returns position of x in sorted list k if exist otherwise, -1. k = 0, 1, 2 private int search(x, k) import java.util.Scanner; class xxxxxp3{ private node[] head = new node[3]; private class node{ int num; node link; node(int x){ num=x; link = null; } } public void prtlst(int k){ System.out.printf("\nContents of List-%d:",k); for(node cur...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...
public class myLinkedList<E extends Comparable<E>>{ private Node<E> head = null; private Node<E> tail = null; public myLinkedList() { head = null; } public void concatenateList (myLinkedList<E> M) {//attach another linkedList refered by M to the end of this linkedList } public int searchElement (E targetElement){//test if a target element can be found on the list, return the occurrences of the target element ...
Class SortedList { Public: SortedType(); int getLength(); //returns size of the list void putItem(Itemtype item); //inserts an item Itemtype getNextItem(); //returns the next item pointed by the index void deleteItem(Itemtype item) //deletes a specified item Private: int length; //length of the list Nodetype* listData //head of the list Nodetype* currentPos; //current pointer to the list } Class Itemtype { Public: Itemtype(); int getValue();// returns the value Private: int value; } struct Nodetype { Itemtype info; Nodetype* next; } Add a...
Need to do Concat public Node next; public Node prev; } // constructor public DSIDequeue() { left = right = null; N = 0; } public boolean isEmpty() { return N == 0; } public int size() { return N; } public void addLeft(double item) { Node n = new Node(item, null, null); if (isEmpty()) { ...
Java Programming: The following is my code: public class KWSingleLinkedList<E> { public void setSize(int size) { this.size = size; } /** Reference to list head. */ private Node<E> head = null; /** The number of items in the list */ private int size = 0; /** Add an item to the front of the list. @param item The item to be added */ public void addFirst(E...
Here is the IntegerLinkedList_incomplete class: public class IntegerLinkedList { static class Node { /** The element stored at this node */ private int element; // reference to the element stored at this node /** A reference to the subsequent node in the list */ private Node next; // reference to the subsequent node in the list /** * Creates a node with the given element and next node. * * @param e the element to be stored * @param n...
public class LinkedList { // The LinkedList Node class private class Node{ int data; Node next; Node(int gdata) { this.data = gdata; this.next = null; } } // The LinkedList fields Node head; // Constructor LinkedList(int gdata) { this.head = new Node(gdata); }...