Question

In: Computer Science

inserting a node after given node. DLL::DLL(int x){ // constructor, initializes a list with one new...

inserting a node after given node.

DLL::DLL(int x){ // constructor, initializes a list with one new node with data x
   DNode *n = new DNode (x);
   first = n;
   last = n;
   size=1;
}

DNode::DNode( int x){
   data = x;
   next = NULL;
   prev = NULL;
}

void DLL::addFirst(int x) {
   DNode* tmp = new DNode(x);
   first = tmp;
   last = first;
}

void DLL::insertAt(int ind, int x) {
   int i = 0;
   DNode *tmp = first;
   DNode *tmp2;
   while(i < ind && tmp != NULL) {
       i += 1;
       tmp2 = tmp;
       tmp = tmp->next;
   }

   if (tmp != NULL) {
       DNode *tmp3 = new DNode(x);
       tmp3->next = tmp;
       tmp->prev = tmp3;
       tmp2->next = tmp3;
       tmp3->prev = tmp2;
   }

}

command to get out put:

codelist.addFirst(0);
    codelist.printList();
    codelist.insertAt(1,1);
    codelist.printList();
    codelist.insertAt(2,3);
    codelist.printList();
    codelist.insertAt(2,2);
    codelist.printList();
    codelist.push(4);
    codelist.printList();
    codelist.insertAt(2,42);
    codelist.printList();

desired output:

0,

0, 1,

0, 1, 3,

0, 1, 2, 3,

0, 1, 2, 3, 4,

0, 1, 42, 2, 3, 4,

actual output:

0,
0,
0,
0,
0, 4,
0, 4,

Solutions

Expert Solution

Please find the following answer for the given program in CPP.

Note:

1. The reason for your incorrect output is, you are inserting the value in index 1 but the index is not a valid element in the double linked list because you have insert 1 first element as 0, then inserting the element 1 in index 1 but the DLL doesnt have index 1. Hence it is added as part of codelist.

2. But when you push the element it always added as last element that why you have got element 4 added as part of DLL and rest of the elements are not added.

3. I have added screen shots and comments inline for better understanding.

Fix description: We have added a else part to handle if there is no element in that position and hence it will added as part of last element.

Screen Shot of corrected function:

Program:

#include <iostream>

using namespace std;

class DNode
{
public:
int data;
DNode *next, *prev;
DNode(int x);
};

class DLL
{
private:
DNode *first;
DNode *last;
int size;
  
public:
DLL(int x);
DLL();
void addFirst(int x);
void printList();
void insertAt(int ind, int x);
void push(int x);
};


DNode::DNode( int x)
{
data = x;
next = NULL;
prev = NULL;
}

// default constructor, initializes a list with NULL values
DLL::DLL()
{
first = NULL;
last = NULL;
size = 0;
}

DLL::DLL(int x)
{ // constructor, initializes a list with one new node with data x
DNode *n = new DNode (x);
first = n;
last = n;
size=size+1;
}

void DLL::addFirst(int x)
{
DNode* tmp = new DNode(x);
first = tmp;
last = first;
}

void DLL::printList()
{
DNode* temp = this->first;
  
   while(temp)
   {
       cout << temp->data<< ", ";
       temp = temp->next;
   }
   cout<<endl;
}

void DLL::insertAt(int ind, int x)
{
int i = 0;
DNode *tmp = first;
DNode *tmp2;
while(i < ind && tmp != NULL)
{
i += 1;
tmp2 = tmp;
tmp = tmp->next;
}

if (tmp != NULL)
{
DNode *tmp3 = new DNode(x);
tmp3->next = tmp;
tmp->prev = tmp3;
tmp2->next = tmp3;
tmp3->prev = tmp2;
}
//added the else part because if a user enter a value to insert at position 1 and if there is not element in
//position 1, then we add at the last.
//This is the reason for your incorrect output
else
{
//uncomment this line for debugging
//cout << "we have reached the end of list, hence adding the element at the end of DLL"<<endl;
DNode *tmp3 = new DNode(x);
last->next=tmp3;
tmp3->prev=last;
last=tmp3;
}
}

void DLL::push(int x)
{
if (first==NULL)
{
cout <<"List is empty, allocate memory to it\n";
}
else
{
DNode *tmp = new DNode(x);
last->next=tmp;
tmp->prev=last;
last = tmp;
}
}


int main()
{
DLL codelist;
  
codelist.addFirst(0);
codelist.printList();
codelist.insertAt(1,1);
codelist.printList();
codelist.insertAt(2,3);
codelist.printList();
codelist.insertAt(2,2);
codelist.printList();
codelist.push(4);
codelist.printList();
codelist.insertAt(2,42);
codelist.printList();
return 0;
}

Output:

0,
0, 1,
0, 1, 3,
0, 1, 2, 3,
0, 1, 2, 3, 4,
0, 1, 42, 2, 3, 4,


Screen Shot:


Output ScreenShot:


Related Solutions

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...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age...
Complete the PoundDog code by adding a constructor having a constructor initializer list that initializes age with 1, id with -1, and name with "NoName". Notice that MyString's default constructor does not get called. Note: If you instead create a traditional default constructor as below, MyString's default constructor will be called, which prints output and thus causes this activity's test to fail. Try it! // A wrong solution to this activity... PoundDog::PoundDog() { age = 1; id = -1; name.SetString("NoName");...
Identify the define node, usage node, du-paths and dc-paths for all the variables. int binsearch(int x,int...
Identify the define node, usage node, du-paths and dc-paths for all the variables. int binsearch(int x,int v[],int n) { int low,high,mid; low=0; high=n-1; while(low<high) { mid = ( low + high ) / 2; if( x < v[mid]) high = mid - 1; else if ( x > v[mid]) low = mid + 1; else return mid; } return -1; }
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions...
IN C++ Given a struct Node { int value; Node *left, *right;}; , implement the functions below. a) int getSmallest(Node * r); // return smallest value in the BST with root r. Assume r not null. b) int getSecondSmallest(Node * r); // return 2nd smallest value in BST with root r. Assume r not null and r has a nonnull left or right child. c) void removeSecondSmallest(Node * r); // remove 2nd smallest value in BST with root r. Assume...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the...
Given the method static int test(int x, int y, int z){ return(x=y+z); } Which of the follwing is true: public static void main(String[] args) { A System.out.println(test ( 7, 14, 23)) ; B System.out.println(test ( test ( 7,9, 14) , 23 ); C System.out.println( test ( 14, 23 ) ; D System.out.println(test(1,2,4), 14, 23)) ;
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_);...
How do I remove a node from a linked list C++? void LinkedList::Remove(int offset){ shared_ptr<node> cursor(top_ptr_); shared_ptr<node> temp(new node); if(cursor == NULL) { temp = cursor-> next; cursor= temp; if (temp = NULL) { temp->next = NULL; } } else if (cursor-> next != NULL) { temp = cursor->next->next; cursor-> next = temp; if (temp != NULL) { temp->next = cursor; } } }
You are given a reference to the head node of a linked list that stores integers....
You are given a reference to the head node of a linked list that stores integers. Please print the minimum element in this linked list. The class ListNode.java contains the description of a single node in the linked list. It has a num field to store the integer number and a reference next that points to the next element in the list. The file MyList.class is a pre-defined java code, that creates a linked list. The file ListSmallest.java creates an...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT