Questions
Problem Description Objective This practical will test your capability of implementing a linked list. Design Think...

Problem Description

Objective

This practical will test your capability of implementing a linked list.

Design

Think of how you are going to solve the problem and test your implementation with the test cases you designed based on the stages below.

Testing Hint: it’s easier if you test things as a small piece of code, rather than building a giant lump of code that doesn’t compile or run correctly. As part of your design, you should also sketch out how you are going to build and test the code.

Problem

Linked lists are dynamic data structures that allow storing a list of items in separate places in the memory. Each item of the list, together with the address of the next item, is placed in one object of type Node (find the description below). The last node of the list should include the last item and a null pointer (NULL or nullptr).

Lists, as abstract data types, can be implemented by different structures, including arrays, vectors and linked lists. Please do not use arrays or vectors for this practical. In this practical, you should write the code for two classes named LinkedList, and Node.

You must include separate header and implementation files for both classes. The class Node should consist of two member variables, an integer data and a pointer to Node next, and 5 functions, a constructor, and getter and setter for the two member variables.

The class LinkedList must have only one member variable: head, which is of type pointer to Node. If the list is empty, head should contain NULL or nullptr. It should also have at least the following member functions. Note that add functions should construct nodes from the heap, and delete functions should manually delete the one that is removed from the list. Don’t forget to write test cases for each function and ensure that the tests pass before progressing to the next function.

  • void addFront(int newItem) : The function inserts a new node, containing the newItem, at the beginning of the list.
  • void addEnd(int newItem) : The function inserts a new node, containing the newItem, at the end of the list.
  • void addAtPosition(int position, int newItem) : The function inserts a new node, containing the newItem, such that it is the position-th member of the list. i.e. we assume the first element of the list is in position 1. If position is larger than the size of the list, the new item is added to the end of the list. If position < 1, the new item is added at the beginning of the list.
  • int search(int item) : The function searches the list for the first instance of the item, and if found, both prints the position of the of the item (followed by a space) and returns the position of the item in the list (positions start from 1). If not found, both prints 0 (followed by a space) and returns 0. Note that the returning type is different from what was explained in the search function in the lecture.
  • void deleteFront() : The function deletes the first element of the list.
  • void deleteEnd() : The function deletes the last element of the list.
  • void deletePosition(int position) : The function deletes the element at the given position of the list. If the position < 1 or it is larger than the size of the list, only print ”outside range”.
  • int getItem(int position) : The function both prints the value of the item (followed by a space) and returns the value of the item at the given position of the list, If beyond the size of the array, both prints std::numeric_limits < int >::max() (followed by a space) and returns std::numeric_limits< int >::max(). You should include <limits> for this. Take a look at
    http://www.cplusplus.com/reference/limits/numeric_limits/ if you need.
  • void printItems() : The function prints the value of the items of the list from head to tail. In case of an empty list, it does not print anything
  • A constructor with no parameters, which makes an empty list.
  • A constructor that takes an array of integers and makes a linked list, containing all the elements of the array, in the same order. As the second parameter, it takes the size of the array.
  • A destructor that manually deletes all the elements that are still in the list.

Note that the printing in the functions search and getItem is for the purpose of easy testing.

Main function

The test script will compile your code using

g++ -o main.out -std=c++11 -O2 -Wall *.cpp

  

It is your responsibility to ensure that your code compiles on the university system. g++ has too many versions, so being able to compile on your laptop does not guarantee that it compiles on the university system. You are encouraged to debug your code on a lab computer (or use SSH).

You are asked to create a main function (main.cpp). It takes in one line of input.
  

int1 int2 ... intn FUNCTIONINITIAL param1 param2

int1, until intn are integers, separated by space, which should be placed in an integer array, and passed to the linked list constructor. For simplicity, we assume that the size of this array never exceeds 100; therefore, you can take an static array with the size of 100.

After the elements of the list, the input consists of an string, denoting a function, followed by its parameters. The string is one of these:

  • AF standing for addFront
  • AE standing for addEnd
  • AP standing for addAtPosition
  • S standing for search
  • DF standing for deleteFront
  • DE standing for deleteEnd
  • DP standing for deletePosition
  • GI standing for getItem

Call the indicated function with its parameter. If the function has only one parameter, then the last integer value from the input is not used. At the end, call the function printItems, to produce the required output.

  

Sample input: 5 2 7 10 AP 3 9

expected output: 5 2 9 7 10

Sample input: 3 4 2 1 DP 3 0

expected output: 3 4 1

Sample input: 45 20 2 10 GI 3 0

expected output: 2 45 20 2 10

In: Computer Science

List and describe the parts of the female gametophore of Marchantia. What is the structure in...

List and describe the parts of the female gametophore of Marchantia. What is the structure in which the eggs are produced? List and describe its parts.

In: Biology

in the context of access control , explain the concepts of access control matrix, access control...

in the context of access control , explain the concepts of access control matrix, access control list ,privilege control list and capability

In: Computer Science

Overview: You will write a python program that will work with lists and their methods allowing...

Overview: You will write a python program that will work with lists and their methods allowing you to manipulate the data in the list. Additionally you will get to work with randomization.

Instructions:

Please carefully read the Instructions and Grading Criteria.

Write a python program that will use looping constructs as noted in the assignment requirements.

Name your program yourlastname_asgn5.py

Assignment Requirements

IMPORTANT: Follow the instructions below explicitly.
Your program must be structured as I describe below.


Write a Python program that ...

1. Creates a list that has these values in this order, 'Python', 'JavaScript', and 'PHP'


2. Define a displayMyClasses function that sorts the list and then displays each item on the list, one per line, in sorted order. Also, number the displayed list as shown in the "Running a Sample Program" shown below.

3. Define a guessNext function that selects a random element from the list and returns it to the call of the function.

4. When the program begins, the main function should display the "Assignment 5" title as the first line and then call the displayMyClasses function to display the current contents of the list.

5. Next, prompt the user to enter an "A" to add a new class to the list or enter an "R" to remove a class from the list.

If the user enters a blank, exit the loop.

If the user enters an "A", then prompt them to enter the name of the class they want to add to the end of the list and, after they answer, add the class to the end of the list.

If the user enters an "R", then prompt them to enter the name of the class they want to remove from the list and, after they answer, remove the class from the list.

If the user has not entered an "A" or and "R" display a message that says "You must choose an 'A' to Add or an 'R' to Remove a class" and start the code back at the top of this #5 step so that they get re-prompted for the correct information.

6. Once the loop is exited, call the displayMyClasses to display the current contents of the list.

7. Now call the guessNext function and receive the random class value returned.

Display "The next class you should teach is: (the random class)"

8. Display "END OF ASSIGNMENT 5"

Running a Sample Program

Below is a example of how your program might run if you used the same answers to the prompts I used below. Your program should run in a similar manner... no matter what names (data) are entered.

NOTE: The data I entered appear in maroon bold

Sample Run...


Assignment 5

List of Classes I Teach:

1. JavaScript
2. PHP
3. Python

Do you need to Add or Remove a class? (A/R) A

Enter the name of the class you wish to add: HTML

Do you need to Add or Remove a class? (A/R) R

Enter the name of the class you wish to remove: PHP

Do you need to Add or Remove a class? (A/R) A

Enter the name of the class you wish to add: PHP with MySQL

Do you need to Add or Remove a class? (A/R) d

You must choose an 'A' to Add or an 'R' to Remove a class

Do you need to Add or Remove a class? (A/R)

List of Classes I Teach:
1. HTML
2. JavaScript
3. PHP with MySQL
4. Python

The next class you should teach is: JavaScript

END OF ASSIGNMENT 5

In: Computer Science

Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...

Define empty methods in Queue class using LinkedList class in Java

-------------------------------------------------------------------------------

//Queue class

public class Queue{

public Queue(){

// use the linked list

}

public void enqueue(int item){

// add item to end of queue

}

public int dequeue(){

// remove & return item from the front of the queue

}

public int peek(){

// return item from front of queue without removing it

}

public boolean isEmpty(){

// return true if the Queue is empty, otherwise false

}

public int getElementCount(){

// return current total number of elements in Queue

}

public static void main(String[] args){

Queue s = new Queue();

int[] inputArr = {1, 2, 3, 4, 5, 6};

System.out.println("\nTrying to dequeue and peek on empty Queue:");

int test = s.dequeue();

test = s.peek();

System.out.println("\nPopulating the Queue:");

for (int v : inputArr){

System.out.println("Enqueuing: " + v);

s.enqueue(v);

}

System.out.println("\nRemoving from the Queue:");

while (!s.isEmpty()){

System.out.println("Dequeuing: " + s.dequeue());

}

System.out.println("\nTrying to dequeue and peek on empty Queue:");

test = s.dequeue();

test = s.peek();

}

}
------------------------------------

//Node class

public class Node{

private int value; // Stores the actual value

private Node nextNode; // Stores the link to the next Node object

public Node(int value){

setValue(value);

setNextNode(null);

}

public Node(int value, Node nextNode){

setValue(value);

setNextNode(nextNode);

}

// getters

public int getValue(){

return value;

}

public Node getNextNode(){

return nextNode;

}

// setters

public void setValue(int value){

this.value = value;

}

public void setNextNode(Node nextNode){

this.nextNode = nextNode;

}

@Override

public String toString(){

return String.valueOf(value);

}

}

-------------------------------------------------------------------------------

//LinkedList class

public class LinkedList implements LinkedListBehavior{

private int length;

private Node head;

private Node tail;

public LinkedList(){

length = 0;

head = null;

tail = null;

}

public boolean isEmpty(){

return head == null;

}

public int getLength(){

return length;

}

public Node getHead(){

return head;

}

public Node getTail(){

return tail;

}

public void append(Node newNode){

Node oldTail = getTail();

tail = newNode;

if (isEmpty()){

head = newNode;

}

else{

oldTail.setNextNode(tail);

}

length++; // update the current number of Node's in the LinkedList

}

public Node removeHead(){

if (isEmpty()){

System.out.println("\nLinkedList is empty. Can not remove head Node.");

return null;

}

Node oldHead = getHead();

head = oldHead.getNextNode();

oldHead.setNextNode(null);

if (isEmpty()){

tail = null;

}

length--;

return oldHead;

}

public Node removeTail(){

if (isEmpty()){

System.out.println("\nLinkedList is empty. Can not remove tail Node.");

return null;

}

Node oldTail = tail;

if (length == 1){

head = null;

tail = null;

length = 0;

return oldTail;

}

Node curNode = head;

while (curNode.getNextNode() != oldTail){

curNode = curNode.getNextNode();

}

curNode.setNextNode(null);

tail = curNode;

length--;  

return oldTail;

}

@Override

public String toString(){

String output = "";

Node curNode = head;

while (curNode != null){

output += curNode + " ---> ";

curNode = curNode.getNextNode();

}

return output;

}

}

-------------------------------------------------------------------------------

public interface LinkedListBehavior{

public void append(Node newNode);

public Node getHead();

public Node getTail();

public Node removeHead();

public Node removeTail();

public boolean isEmpty();

public int getLength();

}

In: Computer Science

A provider order is written for prochlorperazine (Compazine®) 7.5 mg IV q 4 hrs prn N/V....

A provider order is written for prochlorperazine (Compazine®) 7.5 mg IV q 4 hrs prn N/V. Prochlorperazine (Compazine®) is available in a prefilled syringe labeled 10 mg/2 mL. How many milliliters should be given?

In: Nursing

What current trend in the sport or entertainment area do you think will fall into disfavor...

What current trend in the sport or entertainment area do you think will fall into disfavor with the consumer population? What supports your argument, and what would you do to possibly reverse that downward trend?

Case Study V Tracking Industry Changes

In: Finance

In preparing an IV infusion containing sodium bicarbonate, 50 mL of a 75% (w/v) sodium bicarbonate...

In preparing an IV infusion containing sodium bicarbonate, 50 mL of a 75% (w/v) sodium bicarbonate (z=84) injection were added to 500 mL of D5W. How many mEq of sodium are represented in the total infusion (round to one decimal)

In: Chemistry

Q2 Describe the following tools as used in directional drilling. I. Bent Sub and Positive Displacement...

Q2 Describe the following tools as used in
directional drilling.
I. Bent Sub and Positive Displacement Motor
II. Non‐Rotating Steerable Drilling Systems
III. Rotary Steering System
IV. Directional Bottom Hole Assemblies (BHA)
V. Whipstocks

In: Mechanical Engineering

F is a position dependent force given by F(x) = -e^-x. Sketch the graphs showing F(t),...

F is a position dependent force given by F(x) = -e^-x. Sketch the graphs showing F(t), v(t), and x(t) for initial velocity of 10m/s, initial position of 100m, and mass = 1kg. Mention all salient points.

In: Physics