Can you create a \"rudimentary templated singly-LinkedList ADT class\" from the
ID: 3582875 • Letter: C
Question
Can you create a "rudimentary templated singly-LinkedList ADT class" from the file below? I think there might be an error with this file. If it is, can you fix the file and still help with the linked list?
Thank you.
#ifndef _LIST_INTERFACE
#define _LIST_INTERFACE
template<class ItemType>
class ListInterface
{
public:
/** Sees whether this list is empty.
@return True if the list is empty; otherwise returns false. */
virtual bool isEmpty() const = 0;
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtual int getLength() const = 0;
/** Inserts an entry into this list at a given position.
@pre None.
@post If 1 <= position <= getLength() + 1 and the insertion is
successful, newEntry is at the given position in the list,
other entries are renumbered accordingly, and the returned
value is true.
@param newPosition The list position at which to insert newEntry.
@param newEntry The entry to insert into the list.
@return True if insertion is successful, or false if not. */
virtual bool insert(int newPosition, const ItemType& newEntry) = 0;
/** Removes the entry at a given position from this list.
@pre None.
@post If 1 <= position <= getLength() and the removal is successful,
the entry at the given position in the list is removed, other
items are renumbered accordingly, and the returned value is true.
@param position The list position of the entry to remove.
@return True if removal is successful, or false if not. */
virtual bool remove(int position) = 0;
/** Removes all entries from this list.
@post List contains no entries and the count of items is 0. */
virtual void clear() = 0;
/** Gets the entry at the given position in this list.
@pre 1 <= position <= getLength().
@post The desired entry has been returned.
@param position The list position of the desired entry.
@return The entry at the given position. */
virtual ItemType getEntry(int position) const = 0;
/** Replaces the entry at the given position in this list.
@pre 1 <= position <= getLength().
@post The entry at the given position is newEntry.
@param position The list position of the entry to replace.
@param newEntry The replacement entry. */
virtual void replace(int position, const ItemType& newEntry) = 0;
}; // end ListInterface
#endif
Explanation / Answer
#ifndef _LIST_INTERFACE
#define _LIST_INTERFACE
int getLength();
bool insert(int newPosition int newEntry);
void clear();
bool remove();
int getEntry();
}
bool SinglyLinkedList<T>::insert(struct node *numberofnodes int newPosition, int newEntry)
{
int i;
if(newPosition<=0 || newPosition>getLength(numberofnodes))
{
return false;
} else {
int temp;
struct node *temp1
temp = p ;
i = 1;
while ( i < newPosition )
{
i = i+1;
temp = temp-> link ;
}
temp1 = ( struct node * )malloc ( sizeof(struct node));
if ( temp == NULL )
{
return false;
}
temp1 -> data = value ;
temp1 -> link = temp -> link;
temp -> link = temp1;
return true;
}
}
template <class T>
void Singly_Linked_List<T>::remove(int node_number)
{
//first node
if(node_number == 1)
{
node * temp = first_node;
first_node = first_node->link;
delete temp;
number_of_nodes -= 1;
return true;
}
//last node
else if(node_number != 1 && node_number == number_of_nodes)
{
node * iterator = first_node;
for(int i = 1;i < number_of_nodes;i++)
{
iterator = iterator->link;
}
node * temp = iterator->link;
last_node = iterator;
iterator->link = NULL;
iterator = NULL;
delete temp;
delete iterator;
number_of_nodes -= 1;
return true;
}
//middle nodes
else if(node_number != 1 && node_number != number_of_nodes && node_number < number_of_nodes)
{
node * iterator = first_node;
for(int i = 1; i < node_number - 1;i++)
{
iterator = iterator->link;
}
node * temp = iterator->link;
iterator->link = iterator->link->link;
delete temp;
iterator = NULL;
delete iterator;
number_of_nodes -= 1;
return true;
}
else {
return false;
}
template <class T>
void Singly_Linked_List<T>::getEntry(int position)
if (getLength(numberofnodes)<postion)
{
return;
}
temp=head;
for (i = 1; i <getLength(numberofnodes)-position+1; i++)
{
temp = temp->next;
return temp->data;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.