Question

c++ just one file for all of it. You are to implement a 'list' class to...

c++ just one file for all of it.

You are to implement a 'list' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. your list will be an array. the list has no order, except for the order you insert or delete. The methods you are to implement are as given:

constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'list listA(listB)')

empty returns true or false if list is empty or not.

first makes current position at the beginning of the list

last makes current position at the end of a list

prev places current position at the previous element in the list

getpos returns current position or where you are in the list

setpos(int)places current position in a certain position in the list

insertbefore inserts a new element before the current position

insertafter inserts a new element after the current position

getelement returns the one element that current position is pointing to

size returns the size of the list (number of elements in list)

replace(int) replace the current element with a new value

erase deletes the current element

clear makes the list an empty list

overload the operators: <<output, ==, !=, +, +=, =(assignment)

you are to implement the list class using an array. the array can be 20 in size.

you will need to use the 'element type' for 'typing' your data

you will need to use CAPACITY constant for the size of the array, in case we need to change the array size

invariants: the elements in the list are to be left justified with in the array. 'pos' will point to a valid location in the array (ie where data is located.

on insert, 'pos' should point to the element just inserted on insert operations. 'pos' should never be outside of element list.

test the code with a main program.

Homework Answers

Answer #1

PROGRAM

#include<iostream>
#include<stdlib.h>
#include <iomanip>

using namespace std;

const int CAPACITY = 20;

template<class T>
class List
{
//private members
private:
   T arr[CAPACITY];
   int num_used;
   int cur_pos;
public:

   //default constructor
   List()
   {
       num_used = 0;
       cur_pos = -1;
   }

   //copy constructor
   List(const List& list)
   {
       num_used = list.num_used;
       for(int i=0; i<num_used; i++)
       {
           arr[i] = list.arr[i];
       }
   }
  
   //returns true or false if list is empty or not.
   bool empty()
   {
       return ((num_used==0)?true:false);
   }
  
   //makes current position at the beginning of the list
   void first()
   {
       cur_pos = 0;
   }
  
   //makes current position at the end of a list
   void last()
   {
       cur_pos = num_used - 1;
   }
  
   //places current position at the previous element in the list
   void prev()
   {
       cur_pos--;
   }
  
   //returns current position or where you are in the list
   int getpos()
   {
       return cur_pos;
   }
  
   //places current position in a certain position in the list
   void setpos(int i)
   {
       if(i>=0 && i<num_used)
           cur_pos = i;
   }
  
   //inserts a new element before the current position
   void insertbefore(int item)
   {      
       for(int i=num_used; i>=cur_pos; i--)
           arr[i+1] = arr[i];
       arr[cur_pos] = item;
       num_used++;
   }
  
   //inserts a new element after the current position
   void insertafter(int item)
   {
       cur_pos++;
       for(int i=num_used; i>=cur_pos; i--)
           arr[i+1] = arr[i];
       arr[cur_pos] = item;
       num_used++;
   }
  
   //returns the one element that current position is pointing to
   T getelement()
   {
       return arr[cur_pos];
   }
  
   //returns the size of the list (number of elements in list)
   int size()
   {
       return num_used;
   }
  
   //replace the current element with a new value
   void replace(T item)
   {
       arr[cur_pos] = item;
   }
  
   //deletes the current element
   T erase()
   {
       T item = arr[cur_pos];
       for(int i=cur_pos; i<num_used-1; i++)
           arr[i] = arr[i+1];
       num_used--;
       return item;
   }
  
   //makes the list an empty list
   void clear()
   {
       num_used = 0;
       cur_pos = -1;
   }
  
   // << operator overloading
   friend ostream& operator<<(ostream &out, const List &list)
   {
       out<<endl<<"[";
       for(int i=0; i<list.num_used; i++)
           out<<list.arr[i]<<" ";
       out<<"]"<<endl;
      
       return out;
   }
  
   // == operator overloading
   bool operator==(const List &list)
   {
       if(num_used!=list.num_used)
           return false;
       for(int i=0; i<num_used; i++)
       {
           if(arr[i]!=list.arr[i])
               return false;
       }
       return true;
   }
  
   // != operator overloading
   bool operator!=(const List &list)
   {
       if(num_used!=list.num_used)
           return true;
       for(int i=0; i<num_used; i++)
       {
           if(arr[i]!=list.arr[i])
               return true;
       }
       return false;
   }
  
   // = operator overloading
   List& operator=(const List &list)
   {
       num_used = list.num_used;
       for(int i=0; i<num_used; i++)
       {
           arr[i] = list.arr[i];
       }
       return *this;
   }
  
   // + operator overloading
   List operator+(const List &b)
   {
       List a;
      
       a.num_used = num_used + b.num_used;
       for(int i=0; i<num_used; i++)
       {
           a.arr[i] = arr[i];
       }
      
       for(int k=0; k<b.num_used; k++)
       {
           a.arr[num_used + k] = b.arr[k];
       }
      
       return a;
   }
  
   // += operator overloading
   List& operator+=(const List &b)
   {
       for(int k=0; k<b.num_used; k++)
       {
           arr[num_used + k] = b.arr[k];
       }
       num_used = num_used + b.num_used;
      
       return *this;
   }
};


int main()
{
   //create objects
   List<int>a;
   List<int> b;
   List<int> c;
  
   //insert elements
   a.insertafter(10);
   a.insertafter(20);
   a.insertafter(30);
   a.insertafter(40);
   a.insertafter(50);
  
   //print a
   cout<<a;
  
   //insert elements
   b.insertafter(60);
   b.insertafter(70);
   b.insertafter(80);
  
   //print b
   cout<<b;
  
   //add two list
   c = a + b;
  
   //print c
   cout<<c;
  
   //clear c
   c.clear();
  
   //assign a to c
   c = a;
  
   //print c
   cout<<c;
  
   //set current position
   c.setpos(2);
  
   //delete an element
   c.erase();
  
   //print c
   cout<<c;
  
   /* Testing */
   List<int> d(a);
  
   cout<<d;
   cout<<boolalpha<<d.empty()<<endl;
  
   d.first();
   cout<<d.getpos()<<endl;
   cout<<d.getelement()<<endl;
  
   d.last();
   cout<<d.getpos()<<endl;
   cout<<d.getelement()<<endl;
  
   d.prev();
   cout<<d.getpos()<<endl;
   cout<<d.getelement()<<endl;
  
   d.insertbefore(90);
   cout<<d;
  
   d.replace(55);
   cout<<d;
  
   cout<<d.erase()<<endl;
   cout<<d;
  
   cout<<(a==d)<<endl;
   cout<<(a!=d)<<endl;
   d+=c;
   cout<<d;
  
   return 0;
}

Sample Output:


[10 20 30 40 50 ]

[60 70 80 ]

[10 20 30 40 50 60 70 80 ]

[10 20 30 40 50 ]

[10 20 40 50 ]

[10 20 30 40 50 ]
false
0
10
4
50
3
40

[10 20 30 90 40 50 ]

[10 20 30 55 40 50 ]
55

[10 20 30 40 50 ]
true
false

[10 20 30 40 50 10 20 40 50 ]

--------------------------------
Process exited after 0.248 seconds with return value 0
Press any key to continue . . .

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
In c++ Implement the following recursive function, that returns true if all elements in the given...
In c++ Implement the following recursive function, that returns true if all elements in the given array are smaller than the given value, otherwise false. Return false for the empty array and NULL array cases. bool all_smaller_r(int *a1, int size, int value) { }
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
In the attached FlexArray Java class, implement a method public int delete (int location) { }...
In the attached FlexArray Java class, implement a method public int delete (int location) { } that deletes the integer value stored at location in the array, returns it, and ensures that the array values are contiguous.  Make sure to handle the array empty situation.  What is the time-complexity of the method, if the array size is n. ***************************************************************************************************************************** public class FlexArray { int [] array; private int size; private int capacity; public FlexArray() { capacity=10; size=0; array=new int[10]; } public FlexArray(int...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next...
1. Design and implement a CircularLinkedList, which is essentially a linked list in which the next reference of the tail node is set to refer back to the head of the list (rather than null) . Start from the SinglyLinkedList provided in the Lecture (not the built-in Java LinkedList class). 2. Starting from  SinglyLinkedList you will need to modify or complete methods: first(), last(), addFirst(), addLast(), removeFirst(), toString(), and also create a new rotate() method which will move the tail to...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...