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...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
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 ...
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; }...
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...
Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 { private Node head, tail; private int size; public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...
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()) { ...
Write a method with signature "concatenate(LinkedQueue<E> Q2)" for the LinkedQueue<E> class that takes all elements of Q2 and appends them to the end of the original queue. The operation should run in O(1) time and should result in Q2 being an empty queue. Write the necessary code to test the method.You may just modify the SinglyLinkedList class to add necessary support. LinkedQueue Class public class LinkedQueue<E> implements Queue<E> { /** The primary storage for elements of the queue */ private...