Questions
C++ constructor initializer list Constructor initializer list:         - syntax         - when it must be...

C++ constructor initializer list

Constructor initializer list:

        - syntax

        - when it must be used

        - how to instantiate and initialize data members that are user-defined types at the same time

In: Civil Engineering

reverse_number_in_list(number_list:list)-> list This function will be given a list of numbers your job is to reverse...

reverse_number_in_list(number_list:list)-> list


This function will be given a list of numbers your job is to reverse all the numbers in the list and return a list with the reversed numbers. If a number ends with 0 you need to remove all the trailing zeros before reversing the number. An example of reversing numbers with trailing zeros: 10 -> 1, 590 -> 95. None of the numbers in the number_list will be less than 1.

Example:

number_list = [13, 45, 690, 57]

output = [31, 54, 96, 75]

In: Computer Science

I was supposed to conver a singly linked list to a doubly linked list and everytime...

I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console.

Here is the code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

struct node {
int data;

struct node *next;
struct node *prev;
};

//this always points to first link
struct node *head = NULL;

//this always points to last link
struct node *tail = NULL;

//return length of list
int sizeList (struct node *pointer)
{
int length = 0;
struct node *temp;

if(pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
length++;
}
else
{
for (temp = tail; temp != NULL; temp = temp-> prev)
length++;
}
return length;
}

//insert element from last node
struct Node* insertLast (struct node *tail, int newelement)
{
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp-> data = newelement;

if (tail == NULL){
head = temp;
tail = temp;
}
else
{
tail-> next = temp;
temp-> prev = tail;
}
//point last to new last node
tail = temp;

return tail;
};

void printList(struct node *pointer)
{
int length = 0;
struct node *temp;

//check if pointer is head and print head of node
if (pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
printf("%d ", temp-> data);
}
//check if pointer is tail and print the list from tail node using previous pointer
else
{
for (temp = tail; temp != NULL; temp = temp-> prev)
printf("%d ", temp-> data);
}

printf("\n");
}

int get (struct node *pointer, int position)
{
struct node *temp;
int length = sizeList(pointer);
int tempPosition = 0;

if (length < position)
{
printf("Enter the position with in %d.", length);
return -1;
}
//check if pointer is head and return the element from the position
if (pointer == head)
{
for (temp = head; temp != NULL; temp = temp-> next)
{
if(++tempPosition == position)
{
return temp-> data;
}
}
}
//check if pointer is tail and return the element from the position
else
{
for (temp = tail; temp != NULL; temp = tail-> prev)
{
if (++tempPosition == position)
{
return temp-> data;
}
}
}
return 0;
}

//delete node from position
struct node* removeEle(struct node *head, int position)
{
struct node *temp = head, *deleteNode = NULL;
int i;

//traverse list until we get each position
for (i=1; i<position && temp != NULL; i++)
{
temp = temp-> next;
}

//if position is 1, move the head pointer to next and free node and return to head
if (position ==1)
{
deleteNode = temp;
temp = temp-> next;
temp-> prev = NULL;
head = temp;

free (deleteNode);
return head;
}

//if deleteNode is last position move the tail to previous node and free last node
else if (temp == tail)
{
deleteNode = tail;
tail = tail-> prev;
tail-> next = NULL;

free(deleteNode);
return head;
}
//swap temp pointers to skip node
else if (temp != NULL)
{
temp-> prev-> next = temp-> next;
temp-> next-> prev = temp-> prev;

free(temp); //delete n node
return head;
}
else
{
printf("It's an invalid position..\n");
return head;
}
};

void reverseList(struct node *node)
{
//stop and return if node is null
if (!node)
return;

//swap next and prev pointer of node
struct node* temp = node-> next;
node-> next = node-> prev;
node-> prev = temp;

//node of prev is null stop and update head and tail pointer
if(!node-> prev)
{
head = node;
if(head-> next == NULL)
{
tail = head;
}
struct node *temp = head;
while(temp-> next != NULL)
{
temp = temp -> next;
}
tail = temp;
return;
}
//invoke recursive api
reverseList(node-> prev);
}

int main()
{
struct Node *tail = NULL;

//insert element at last
tail = insertLast(tail, 10);
tail = insertLast(tail, 20);
tail = insertLast(tail, 30);
tail = insertLast(tail, 40);
tail = insertLast(tail, 50);

printf("Printing from Head: ");
printList(head);
printf("\n");

printf("Printing from Tail: ");
printList(tail);
printf("\n");

printf("Size of list from Head: %d\n", sizeList(head));
printf("Size of list from Tail: %d\n", sizeList(tail));

//remove element from position 4
printf("Get the element from head at position 4: ");
int element = get(head, 4);

if(element != -1)
{
printf("%d\n", element);
}

//remove element from position 2
printf("Get the element from tail at position 2: ");
element = get(tail, 2);

if(element != -1)
{
printf("%d\n", element);
}

printf("Remove element from position 3\n");
head = removeEle(head, 3);

printf("Printing from Head after removal: ");
printList(head);
printf("\n");

//invoke reverse list
reverseList(head);

printf("Printing from Head after reverseList: ");
printList(head);
printf("\n");

printf("Printing from tail after reverseList: ");
printList(tail);
printf("\n");

return 0;
}

In: Computer Science

A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...

A circular doubly-linked list

.(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have:

• A private instance variable for the current node

• A getCurrent() method that returns a reference to the current node • A method to insert a node before the current node

• A method to delete the current node

• A method to advance the current node to the next node in the list • A method to check whether the list is empty

• A method to print the list, starting with the current node

.(b) Write tests for these methods. Why did you choose these test cases? (Think about any edge cases as you do this.) Did you get what you expected?

In: Computer Science

Overview: implement the ADT List in Java. This program is meant to the ADT List from...

Overview: implement the ADT List in Java. This program is meant to the ADT List from the ground up

In the lecture, we learned how to implement an ADT like the ArrayList you have used in Project 1. With this project, you have the chance to implement an ADT called MyList, which is a simplified replacement for the full-blown ArrayList.

Requirements

You will implement the MyList ADT according to the following:

1. MyList must implement the List interface. It will look something like below:

public class MyList implements List {

... } Note: As said in the class, when implementing an interface, you have to implement each method in it. There are a lot of methods in the List interface. We are going to address this issue below.

2. The initial capacity of MyList must be 2. This is not an ideal initial capacity. A smaller initial

capacity allows us to test out our code more quickly.

3. You must implement the following methods:

@SuppressWarnings("unchecked") public MyList() {

... } public int size() {

... } public boolean isEmpty() {

... } public boolean add(E e) {

... } public void add(int index, E element) {

... } public E get(int index) {

... } public E set(int index, E element) {

... } public E remove(int index) {

... } You can choose to implement any other methods as you need – private or public. For any method you don’t need but is required by the List interface, you can stub them out like below:

public void clear() {

StackTraceElement[] stackTrace =

new Throwable().getStackTrace(); String methodName = stackTrace[0].getMethodName(); throw new UnsupportedOperationException("Method '"

+ methodName + "' not implemented!"); }

What each method will do:

void add(E item) add item to the end of the List
void add(int pos, E item) add item at position pos in the List, moving the items originally in positions pos through size()-1 one place to the right to make room (error if pos is less than 0 or greater than size())
int size() return the number of items in the List
boolean isEmpty() return true iff the List is empty
E get(int pos) return the item at position pos in the List (error if pos is less than 0 or greater than or equal to size())
E remove(int pos) remove and return the item at position pos in the List, moving the items originally in positions pos+1 through size()-1 one place to the left to fill in the gap (error if pos is less than 0 or greater than or equal to size())

4. There is at least one place where you will encounter an avoidable “unchecked” compiler warning. To get a clean compilation, use: @SuppressWarnings("unchecked").

But don’t abuse it for any other places where a warning is avoidable but due to your questionable coding skills.

In: Computer Science

JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...

JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You can set up a menu or hard code some test cases to test your implementations.

I need JAVA code that's able to run.

In: Computer Science

List.h template class List { // List class ADT              public:    virtual void...

List.h
template
class List { // List class ADT
             public:
   virtual void clear() = 0;

// Inserts an item into the list at position index
   virtual bool insert(const ListItemType& newItem) = 0;

   // Append "it" at the end of the list
   // The client must ensure that the list's capacity is not exceeded
   virtual bool append(const ListItemType& newItem) = 0;

   // Deletes an item from the list at a given position
   virtual ListItemType remove() = 0;

   // Set the current position to the start of the list
   virtual void moveToStart() = 0;

   // Set the current position to the end of the list
   virtual void moveToEnd() = 0;

   // Move the current position one step left, no change if already at beginning
   virtual void prev() = 0;

   // Move the current position one step right, no change if already at end
   virtual void next() = 0;

   //Return the number of items stored in the list
   virtual int length() const = 0;

   // Return the position of the current element
   virtual int currPos() const = 0;

   // Set the current position to "pos"
   virtual bool moveToPos(int pos) = 0;

   // Return true if current position is at end of the list
   virtual bool isAtEnd() const = 0;

   // Return the current element
   virtual ListItemType getValue() const = 0;

   // Note: The fololwing methods assume that ListItemType is comparable: that is to say the relational operators such as == exist.

   //search for the first occurance in the list of an item. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   virtual bool find(const ListItemType& newItem) = 0;

   //search for the next occurance in the list of an item. Start with the next position after curr. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   virtual bool findNext(const ListItemType& newItem) = 0;

   // count the number of times a value is found in the list
   // curr should be set back to its current location after the count.
   virtual int count(const ListItemType& newItem) = 0;

};

LList.h

#include "list.h"

// Linked list implementation

template
class Node
{
public:
   ListItemType element;
   Node* next;

   Node()
   {
       next = nullptr;
   }

   Node(const ListItemType& e)
   {
       this->element = e;
       next = nullptr;
   }
};


template
class LList : public List {

private:
   Node* head;   
   Node* tail;   
   Node* curr;   
   int listSize;

public:
   // Constructor
   LList() {
       head = tail = curr = nullptr;
       listSize = 0;
   }

   //Destructor
   ~LList() {
       clear();
   }

   // Remove all elements
   void clear() {
       if (listSize > 0) {
           Node* temp;
           curr = head;
           do {
               temp = curr -> next;
               delete curr;
               curr = temp;
           } while (curr != nullptr);
       }
       curr = tail = head = nullptr;
       listSize = 0;
   }

  
   bool insert(const ListItemType& newItem) {
       if (listSize == 0) {
           head = new Node(newItem);
           tail = head;
           curr = head;
       }
       else if (curr == head) {
           Node* temp = new Node(newItem);
           temp->next = head;
           head = temp;
           curr = head;
       }
       else if (curr == nullptr) {
           return false;
       }
       else {
           Node* temp = head;
           while (temp->next != curr)
               temp = temp->next;
           temp->next = new Node(newItem);
           temp->next->next = curr;
           curr = temp->next;
       }
       listSize++;
       return true;
   }

   // Append "it" to list
   bool append(const ListItemType& newItem) {
      
       // hint: handle an empty list and a non-empty list separately
       cout << "***** complete this method ...\n";
       return false;
   }

   // Remove and return current element
   ListItemType remove() {
       ListItemType it = curr->element;
       if (curr == head) {
           head = head->next;
           delete curr;
           curr = head;
           listSize--;
           if (head == nullptr)
               tail = nullptr;
       }
       else if (curr == tail) {
           moveToPos(listSize - 2);
           delete curr->next;
           tail = curr;
           tail->next = nullptr;
           curr = nullptr;
           listSize--;
       }
       else {
           Node* temp = head;
           while (temp->next != curr)
               temp = temp->next;
           temp->next = curr->next;
           delete curr;
           curr = temp->next;
           listSize--;
       }
       return it;
   }

   void moveToStart() { curr = head; }
   void moveToEnd() { curr = nullptr; }

                                                  
   void prev() {
       if (head == curr) return;
       Node* temp = head;
      
       while (temp -> next != curr) temp = temp -> next;
       curr = temp;
   }

  
   void next() { if (curr != nullptr) curr = curr -> next; }

   int length() const { return listSize; }


                                             
   int currPos() const {
       if (curr == nullptr) return listSize;
       Node* temp = head;
       int i;
       for (i = 0; curr != temp; i++)
           temp = temp -> next;
       return i;
   }

   // Move down list to "pos" position
   bool moveToPos(int pos) {
       if ((pos < 0) || (pos > listSize)) return false;
       curr = head;
       for (int i = 0; i next;
       return true;
   }

   // Return true if current position is at end of the list
   bool isAtEnd() const { return curr == nullptr; }

   // Return current element value.
   ListItemType getValue() const { return curr -> element; }

   // Check if the list is empty
   bool isEmpty() const { return listSize == 0; }

   bool find(const ListItemType& it) {
       cout << "***** complete this method ...\n";
       // hint: if list is empty return false
       // set curr to head and traverse list until curr becomes nullptr or "it"is found
       return false;
   }

   //search for the next occurance in the list of an item. Start with the next position after curr. If found return true and set curr to the location.
   // if not found, return false and set curr to the end of the list.
   bool findNext(const ListItemType& it) {
       cout << "***** complete this method ..\n";
       // hint: if curr is nullptr return false
       // set curr to curr->next and traverse list until curr becomes nullptr or "it" is found

       return false;
   }
   // count the number of times a value is found in the list
   // curr should remain where it is
   int count(const ListItemType& it) {
       cout << "***** complete this method ...\n";
       // hint: do not use curr, but a temp pointer to traverse the list.
       // set temp to head, and traverse list until temp is nullptr counting elements that are equal
       int count = 0;

       return count;
   }

};
1- Complete the append method in LList.h( Bold )

2- Complete the find, findNext and count in LList.h(Bold)

In: Computer Science

Use Python to Load a file containing a list of words as a python list :param...

Use Python to Load a file containing a list of words as a python list
:param str filename: path/name to file to load
:rtype: list

In: Computer Science

write a java code to implement a linked list, called CupList, to hold a list of...

write a java code to implement a linked list, called CupList, to hold a list of Cups.

1.Define and write a Cup node class, called CupNode, to hold the following information about a cup:

•number (cup number)

•capacity (cup capacity in ml)

•Write a method size() that returns the number of elements in the linkedlist CupList.

•Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method.

•Write a method, insertPos(),  to insert a new CupNode object at a specific position given as a parameter of the method.

•Write a method, deletePos(),  to remove the CupNode object at a specific position given as a parameter of the method.

•Write a method, swapNodes,  to swap two nodes at two positions pos1 and pos2 given as parameters of the method.

Implement the Cup list using double linked list.

•Update CupNode class by adding a link to the previous node in the list. Then update the constructor of the class

•Update the CupList by adding a last attribute, to hold the reference to the last node of the list.

•Implement the following methods in CupList isEmpty(), size(), insertAtFront(), insertAtRear(), insertAt(), removeFirst(), removeLast(), removeAt().

  CupNode should have a constructor and methods to manage the above information as well as the link to next node in the list.

2.Define and write the CupList class to hold objects of the class CupNode. This class should define:

a)a constructor,

b)a method isEmpty() to check if the list is empty or not,

c)a method insert() that will allow to insert a new cup node at the end of the list,

d)a method print() that will allow to print the content of the list,

e)A method getCupCapacity() that will return the capacity of a cup given its number. The method should first find out if a cup with that number is in the list.

3.Write a TestCupList class to test the class CupList. This class should have a main method in which you perform the following actions :

a)Create a CupList object,

b)Insert five to six CupNode objects into the created Cup List,

c)Print the content of your cup list,

d)Search for a given cup number in the list and print out its capacity.

In: Computer Science

InPython, Create a list of strings, don’t ask from the user, and return a list with...

InPython,

Create a list of strings, don’t ask from the user, and return a list with the strings in sorted order, except group all the strings that begin with 'x' first.

e.g. ['mix', 'xyz', 'apple', 'xanadu', 'aardvark'] yields

['xanadu', 'xyz', 'aardvark', 'apple', 'mix']

# Hint: this can be done by making 2 lists and sorting each of them before combining them.

In: Computer Science