Question

Create an h file called list. It should have the following features:

lisis funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables s

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// list.h

#ifndef LIST_H_

#define LIST_H_

#include <iostream>

using namespace std;

template <class Type>

struct Node

{

               Type data;

               Node<Type> *next;

};

template <class Type>

class List

{

private:

               Node<Type> *head;

               Node<Type> *tail;

               int size;

public:

               List();

               List(const List<Type> &other);

               List<Type>& operator=(const List<Type> &other);

               ~List();

               void printItems() const;

               bool isEmpty() const;

               void addToFront(const Type &item);

               void addToRear(const Type &item);

               void addItem(int index, const Type&item);

};

template <class Type>

List<Type>::List()

{

               head = NULL;

               tail = NULL;

               size = 0;

}

template <class Type>

List<Type>::List(const List<Type> &other)

{

               head = NULL;

               tail = NULL;

               size = 0;

               Node<Type> *curr = other.head;

               while(curr != NULL)

               {

                              addToRear(curr);

                              curr = curr->next;

               }

}

template <class Type>

List<Type>& List<Type>::operator=(const List<Type> &other)

{

               if(this != &other)

               {

                              while(head != NULL)

                              {

                                             Node<Type> *temp = head;

                                             head = head->next;

                                             delete(temp);

                              }

                              tail = NULL;

                              size =0;

                              Node<Type> *curr = other.head;

                              while(curr != NULL)

                              {

                                             addToRear(curr);

                                             curr = curr->next;

                              }

               }

               return *this;

}

template <class Type>

List<Type>::~List()

{

               while(head != NULL)

               {

                              Node<Type> *temp = head;

                              head = head->next;

                              delete(temp);

               }

               tail = NULL;

               size =0;

}

template <class Type>

void List<Type>::printItems() const

{

               if(head != NULL)

               {

                              Node<Type> *node = head;

                              while(node != NULL)

                              {

                                             cout<<node->data<<" ";

                                             node = node->next;

                              }

                              cout<<endl;

               }else

                              cout<<"Empty List"<<endl;

}

template <class Type>

bool List<Type>::isEmpty() const

{

               return(head == NULL);

}

template <class Type>

void List<Type>::addToFront(const Type &item)

{

               Node<Type> *node = new Node<Type>();

               node->data = item;

               node->next = NULL;

               node->next = head;

               head = node;

               if(head->next == NULL)

                              tail = head;

               size++;

}

template <class Type>

void List<Type>::addToRear(const Type &item)

{

               if(isEmpty())

                              addToFront(item);

               else

               {

                              Node<Type> *node = new Node<Type>();

                              node->data = item;

                              node->next = NULL;

                              tail->next = node;

                              tail = node;

                              size++;

               }

}

template <class Type>

void List<Type>::addItem(int index, const Type &item)

{

               if(index <= 0)

                              addToFront(item);

               else if(index >= size)

                              addToRear(item);

               else

               {

                              Node<Type> *node = new Node<Type>();

                              node->data = item;

                              node->next = NULL;

                              Node<Type> *curr = head;

                              Node<Type> *prev = NULL;

                              int i=0;

                              while(i < index)

                              {

                                             prev = curr;

                                             curr = curr->next;

                                             i++;

                              }

                              prev->next = node;

                              node->next = curr;

                              size++;

               }

}

#endif /* LIST_H_ */

//end of list.h

// main.cpp

#include <iostream>

#include "list.h"

using namespace std;

int main()

{

               List<int> list;

               for(int i=0;i<5;i++)

                              list.addToRear(i);

               cout<<"List : "<<endl;

               list.printItems();

               list.addToFront(10);

               list.addItem(0,11);

               list.addItem(8,12);

               list.addItem(5,13);

               cout<<"List : "<<endl;

               list.printItems();

               return 0;

}

//end of main.cpp

Output:

List 0 1 2 34 List 11 10 0 1 2 13 3 4 12

Add a comment
Know the answer?
Add Answer to:
Create an h file called list. It should have the following features: lisi's funnc In no particular order: List(): Default constructor. This should construct an empty List, the member variables sh...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • Q1: You can find a file that defines the CircularlyLinked List class similar to what we...

    Q1: You can find a file that defines the CircularlyLinked List class similar to what we discussed in the class. Download the file and work on it. Your task is to: 1. Complete the missing methods in the file as discussed in the class. Search for the comment/" MISSING / in the file to see the methods that need to be completed. 2. Add the following methods to the class a. public Node getMin 1. Task: find the node with...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT