Question

In: Computer Science

class nodeType                    // class used to implement a node { public:         int data;   &n

class nodeType                    // class used to implement a node

{

public:

        int data;                        // data member of node

        nodeType * next;        // pointer member of node

};

int main()

{

        int x;

        nodeType * head =________ ;                     // initialize head pointer

        nodeType * tail = _______ ;                        // initialize tail pointer

_______ * p;                                                 // create an auxiliary pointer to a node

        for (x = 0; x < 10; ++x)

        {

                p =   _________ nodeType; // create a node

___________ = x + 10;                                // store a number in the node's data member

                                                           

                p->next = ______ ;   // connect the node to the list

                if (x == 0)

                        tail = _______ ;               

                head = ______ ;

        }

        return 0;

}

Solutions

Expert Solution

In case of any query do comment. Please rate answer. Thanks

Modified Code:

class nodeType                    // class used to implement a node
{

public:
        int data;               // data member of node
        nodeType * next;        // pointer member of node
};

int main()
{
        int x;
        nodeType * head =new nodeType ;                     // initialize head pointer
        nodeType * tail = new nodeType ;                    // initialize tail pointer
        nodeType * p;                                       // create an auxiliary pointer to a node
        for (x = 0; x < 10; ++x)
        {
                p =   new nodeType; // create a node
                p->data = x + 10;   // store a number in the node's data member
                p->next = head ;   // connect the node to the list with head pointer
                if (x == 0)
                    tail = p ; //make tail as first node
                head = p; //add newly added node as head node
        }
      
        return 0;

}

Explanation:

initialize head & tail pointer with new node object

nodeType * head =new nodeType ;

nodeType * tail = new nodeType ;

For auxiliary pointer:

nodeType * p;

To create new node object use new keyword as below:

p = new nodeType;

store number in data member using -> notation of auxiliary pointer p

p->data = x + 10;

now connect the node p with head of the list:

p->next = head ;

if it is first node then set the tail as first node:

if (x == 0)
tail = p ; //make tail as first node

then make newly added node as head node

head = p;

====================example code to display the functionality=====================

#include<iostream>
using namespace std;

class nodeType                    // class used to implement a node
{

public:
        int data;               // data member of node
        nodeType * next;        // pointer member of node
};

int main()
{
        int x;
        nodeType * head =new nodeType ;                     // initialize head pointer
        nodeType * tail = new nodeType ;                    // initialize tail pointer
        nodeType * p;                                       // create an auxiliary pointer to a node
        for (x = 0; x < 10; ++x)
        {
                p =   new nodeType; // create a node
                p->data = x + 10;   // store a number in the node's data member
                p->next = head ;   // connect the node to the list with head pointer
                if (x == 0)
                    tail = p ; //make tail as first node
                head = p; //add newly added node as head node
        }
        
        while(p->next != NULL){
            cout << p->data << endl;
            p = p->next;
        }
        return 0;

}

output:


Related Solutions

public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor   ...
import java.io.*; import java.util.Scanner; class Node { int data; Node next; Node(int d){ // Constructor    data = d;    next = null; } } class ACOLinkedList {// a Singly Linked List    Node head; // head of list    public void insert(int data){ // Method to insert a new node        Node new_node = new Node(data); // Create a new node with given data        new_node.next = null;        if (head == null) // If the...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int...
Java program to implement circular linked list. public class CircularLinkedList { private Node tail; private int size; public CircularLinkedList() { tail= null; size = 0; } public int size(){ return size; } public boolean isEmpty() { return size==0; } //if list is not empty return the first element public E first() { if (isEmpty()) return null; //code here return 0; } //if list not empty return last element public E last() { if (isEmpty()) return null; return tail.getElement(); } /*...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next;...
Use this implementation of Integer node, public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain ordered. Your code...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item;...
Using the textbook implementation of integer node given below; public class IntegerNode { public int item; public IntegerNode next; public IntegerNode(int newItem) { item = newItem; next = null; } // end constructor public IntegerNode(int newItem, IntegerNode nextNode) { item = newItem; next = nextNode; } // end constructor } // end class IntegerNode You need to implement add( ), delete( ), traverse( ) methods for an ordered linked list. And after insertion and deletion, your linked list will remain...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT