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 ()...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • 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...

  • Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that...

    Question: SWAPPING NODES IN A SINGULARLY LINKED LIST: I am attempting to create a program that swaps 2 nodes (no matter where they are in the list) and am having some difficulty. I can't seem to figure out why this swap method is throwing an error. Code: (SWAPPING METHOD AND TEST LINE BOLDED) package linkedlists; public class SinglyLinkedList<E> implements Cloneable { //---------------- nested Node class ---------------- /** * Node of a singly linked list, which stores a reference to its...

  • 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() //...

  • 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...

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