Data Structure in C++ Doubly Linked Lists of ints This is my code below: dlist.c
ID: 3849693 • Letter: D
Question
Data Structure in C++
Doubly Linked Lists of ints
This is my code below: dlist.cc
-------------------------------------------------------------------------------------------------------
#include <iostream>
#include "dlist.h"
dlist::node* dlist::at(int n){
node* c = _head;
while(n > 0 && c){
c = c->next;
n--;
}
return c;
}
void dlist::insert(node* previous, int value){
node* n = new node{value, previous->next};
previous->next = n;
}//update tail
void dlist::del(node* which){
node* nn = which->next;
which->next = nn->next;
delete nn;
}
void dlist::push_back(int value){
node* n = new node{value, nullptr};
_tail->next = n;
_tail-> n;
}
void dlist::push_front(int value){
node* n = new node(value, nullptr);
_head->prev = n;
_head-> n;
}
void dlist::pop_front(){
node* n = _head;
_head = _head->next;
delete n;
}
void dlist::pop_back(){
node* n = _tail;
_tail = _tail->prev;
delete n;
}
int dlist::size() {
node* c = _head;
int s = 0;
while(c){
s++;
c = c->next;
}
return s;
}
--------------------------------------------------------------------------------
Please fix and complete my code, and I also need whole code contatining main function(list_tests.cc) for testing dlist.cc
--------------------------------------------------------------------------------
Output I need:
In this assignment, you will implement a doubly-linked list class. together with some list operations. To make things easier, you'll implement a list of int rather than a template class pragma once dlist. h Doubly linked lists of ints include Kostream class dlist public: d list struct node int value node next node prev; node' head() const return -head; node" tail() const t return -tail Implement ALL the following methods Returns the node at a particular index (0 is the head node at(int) Insert a new value, after an existing one void insert(node *previous int value Delete the given node void del (node which)Explanation / Answer
* @author Matthew Hertz
* @param <E> variety of knowledge control during this assortment
*/
public category ArrayList<E> extends AbstractList<E> once the category is instantiated. */
non-public final static int DEFAULT_INITIAL_CAPACITY = 12;
/** initial index within the array at that parts can not be found. solely indices but this worth ar valid. */
non-public int _size;
/** Array during which the weather ar hold on. */
non-public E[] _store;
/**
* Creates associate degree empty list victimisation the default capability.
*/
public ArrayList()
/**
* Creates associate degree empty list victimisation the desired capability.
*
* @param initialCapacity the whole number worth of the scale of the array list
*/
public ArrayList(int initialCapacity)
/**
* This methodology, that is barely used among the ArrayList category, returns the instance to its initial state. This call
* ought to allot a brand new backing store with the desired capability and update any instance variables required to replicate
* that the ArrayMultiSet doesn't contain any parts.
*
* @param newCapacity once allocating a brand new (empty) backing store, this specifies the house it ought to reserve for
* later
*/
@SuppressWarnings("unchecked")
non-public void reset(int newCapacity)
/**
* Replaces the component at the desired index during this list.
*
* @param index List location whose component ought to get replaced.
* @param elem component to be hold on at the given index
* @return component antecedently hold on at the desired index during this list
*/
@Override
public E set(int index, E elem)
/**
* Removes the component found at the given index. this can be done whereas maintaining the category invariant, by shifting
* parts within the list to eliminate any "hole" that this removal may produce. Once this can be done, we are able to assign null
* to the situation that had been at the top of the ArrayList and update any instance variables to insure category
* invariant stay true.<br />
* <b>Precondition (e.g., this methodology ought to ASSUME this can be true) </b>: could be a valid index among
* AND isn't zero.
*
* @param index Index of the component to get rid of from the ArrayList.
* @return True is usually came since this methodology is named only the removal is made.
*/
non-public Boolean removeAtIndex(int index)
/**
* Removes the component within the list at the given index, if that index could be a valid one for the ArrayList. The method
* returns the component that this methodology has far from the ArrayList.Since the order of parts in an exceedingly List IS
* secure, we'd like to create changes relative to the utilization in associate degree ArrayMultiSet.
*
* @param index Index of the component to be far from the List.
* @return component far from the List.
*/
@Override
public E remove(int index) come retVal;
}
/**
* Removes one instance of the given object, if one are often found within the ArrayMultiSet. the tactic returns
* if a match was found (and removed) and if no match was found. Since the order of
* parts in an exceedingly MultiSet isn't secure, we tend to use this to modify however we tend to take away associate degree item.
*
* @param obj Object (or null) that we would like to get rid of
* @return if was found associate degreed an instance removed; if a match couldn't be found.
*/
@Override
public Boolean remove(Object obj) come removeAtIndex(indexFound);
}
come back false;
}
/**
* Returns the primary backing store index wherever is found within the ArrayMultiSet. If isn't associate degree
* component within the ArrayList, -1 is came.
*
* @param obj Object (or null) {for that|that} we tend to come back the primary index at which it's found in an exceedingly valid location in _store
* @return Index in _store at that the item is found or -1 if it's not within the ArrayList.
*/
non-public int findFirstIndex(Object obj) {
// solely scan through _size, since those ar the sole indices in _store at that parts ar found
for (int i = 0; i < _size; i++ ) ought to use the == operator to match after we ar looking for null;
// once we all know obj isn't null, we'd like to use the .equals() methodology to be sure we tend to match objects
if ((obj == _store[i]) || ((obj != null) && obj.equals(_store[i])))
// No else clause potential, since the match might be at the next index!
}
// Checked all VALID indices, therefore we tend to currently unskilled person matches. during this case, we tend to come back -1
return -1;
}
/**
* Update the list so it includes all of {the components|the weather} from before the decision and therefore the given element.
*
* @param e Item to be additional to the present assortment.
*/
@Override
public void add(int index, E e)
addElement(index, e);
}
/**
* Checks if the backing store has a part in each on the market entry.
*
* @return True once a brand new, larger, backing store is required before a part are often additional. False if we tend to may add associate degree
* component presently.
*/
non-public Boolean storeIsFull() {
Boolean retVal = _size == _store.length;
come back retVal;
}
/**
* Allocates a brand new, larger, array and copies over all of the present parts into it. This returns the larger array
* so it are often used because the backing store.
*
* @return Array with all of the ArrayList's parts and extra house during which parts might be additional.
*/
@SuppressWarnings("unchecked")
non-public E[] growBackingStore() greatest potency (and best practice), we tend to double the scale of the array every time it grows.
E[] retVal = (E[]) new Object[_store.length * 2];
// Copy all of the weather into the new array
for (int i = 0; i < _store.length; i++ )
// a neater, additional economical method (but less helpful for teaching) to allot and replica the weather writing is:
// _store = Arrays.copyOf(_store, _store.length * 2);
come back retVal;
}
/**
* once shifting parts to create house within the array, this adds the component to the backing store and updates the
* instance variables to take care of the category invariant. Returns if this addition was made (this should
* come back true).
*
* @param index Index at that the new component ought to be additional.
* @param elem component to be additional to the backing store
* @return True since this may forever with success adds the component.
*/
non-public Boolean addElement(int index, E elem) {
// Shift any parts to create house for the new one.
for (int i = _size; i > index; i-- )
// Add the component to the shop
_store[_size] = elem;
// Finally, we are able to increase _size, since this modification can not
// violate any category invariants.
_size += 1;
come back true;
}
/**
* Returns the component at the desired index during this list.
*
* @param index List location whose component ought to be came.
* @return component at the desired index during this list
*/
@Override
public E get(int index)
come back _store[index];
}
/**
* Returns true if this list is empty and false otherwise.
*
* @return true if the list is empty, false otherwise
*/
@Override
public Boolean isEmpty() {
come back _size == 0;
}
/**
* Returns the amount of parts presently during this list.
*
* @return the amount of parts within the list
*/
@Override
public int size() {
come back _size;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.