Question

Using C++ • Implement the following in both string linked list and in the template version...

Using C++

• Implement the following in both string linked list and in the template version of the class:

• Add to the back and remove from back of the list :

- void addBack(….);

- string removeBack(); E removeBack(…);

• Insert elements in a sorted list i.e., find the position where an element fits and add it to the list. Note you must call the addBack and addFront functions if you insert in the front or rare of the list (same for remove functions) :

- void insert(string value); void insert(E value)

- void delete(string value); void delete(E value);

//SNode.h

#pragma once
#include
#include

template
class SNode {

private:
   E elem;
   SNode* next;
   friend class SLinkedList;
};

//SLinkedList.h

#pragma once
#include
#include

template
class SLinkedList {

public:
   SLinkedList();
   ~SLinkedList();
   bool empty() const;
   const E& front() const;
   void addFront(const E& e);
   void removeFront();
private:
   SNode* head;

};


template
SLinkedList::SLinkedList() : head(NULL) {}

template
bool SLinkedList::empty() const {
   return head == NULL;
}

template
const E& SLinkedList::front() const {
   return head->elem;
}

template
SLinkedList::~SLinkedList() {
   while (!empty()) removeFront();
}

template
void SLinkedList::addFront(const E& e) {
   SNode* v = new SNode;
   v->elem = e;
   v->next = head;
   head = v;
}

template
void SLinkedList::removeFront() {
   SNode* old = head;
   head = old->next;
   delete old;
}

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

//SNode.h
template<class E>
class SNode {
public:
E elem;
SNode<E>* next;
};

//SLinkedList.h

#include"SNode.h"
#include<iostream>
using namespace std;

template<class E>
class SLinkedList {

public:
SLinkedList();
~SLinkedList();
bool empty() const;
const E& front() const;
void addFront(const E& e);
void removeFront();
void addBack(const E& e);
void removeBack();
void insert(E value);
void Delete(E value);
void display();
private:
SNode<E>* head;

};


template<typename E>
SLinkedList<E>::SLinkedList() {
   head = NULL;
}

template<typename E>
bool SLinkedList<E>::empty() const {
return head == NULL;
}

template<typename E>
const E& SLinkedList<E>::front() const {
return head->elem;
}

template<typename E>
SLinkedList<E>::~SLinkedList() {
while (!empty()) removeFront();
}

template<typename E>
void SLinkedList<E>::addFront(const E& e) {
SNode<E>* v = new SNode<E>;
v->elem = e;
v->next = head;
head = v;
}

template<typename E>
void SLinkedList<E>::removeFront() {
   if(!head)return;
SNode<E>* old = head;
head = old->next;
delete old;
}

template<typename E>
void SLinkedList<E>::addBack(const E& e) {
SNode<E>* v = new SNode<E>;
v->elem = e;
v->next = NULL;
if(head==NULL)head = v;
SNode<E>*current = head;
while(current->next)current = current->next;
current->next = v;
}

template<typename E>
void SLinkedList<E>::removeBack() {
if(!head)return;
SNode<E>* current = head,*prev=head;
if(head->next==NULL){
    head = current->next;
delete current;
return;
}
while(current->next){
    prev=current;
    current = current->next;
}

prev ->next = current->next;
delete current;
return ;
}

template<typename E>
void SLinkedList<E>::insert(E value){
if(head==NULL || head->elem>value){
    addFront(value);
    return;
}
SNode<E>* v = new SNode<E>;
v->elem = value;
v->next = NULL;
SNode<E>* previous = head;
SNode<E>* current = head->next;
       while (current != NULL && value > current->elem)
       {
       previous = current;
       current = current->next;
       }
   previous->next = v;
        v->next = current;
}
template<typename E>
void SLinkedList<E>::Delete(E value){
   if(head==NULL)return;
   if(head->elem==value){
       removeFront();
       return;
   }
   SNode<E>* previous = head;
SNode<E>* current = head->next;
  
while(current && current->elem!=value){
   previous=current;
   current=current->next;
   }
if(!current){
   cout<<"No found in list\n";
   return;
   }
  
   if(current->next==NULL){
       removeBack();
       return;
   }
   previous->next = current->next;
   delete current;
}
template<typename E>
void SLinkedList<E>::display(){
   SNode<E>*current = head;
   cout<<"List is : ";
   while(current){
       cout<<current->elem<<" ";
       current= current->next;
   }
   cout<<"\n\n";
}

//Main.cpp

#include"SLinkedList.h"

int main(){
  
   SLinkedList<int>list;
  
   list.insert(5);
   list.insert(3);
   list.insert(2);
   list.insert(1);
   list.insert(4);
  
   list.display();
  
   list.Delete(1);
  
   list.display();
  
   list.Delete(5);
  
   list.display();
  
  
   SLinkedList<string>list1;
  
   list1.insert("Dog");
   list1.insert("Mango");
   list1.insert("Apple");
   list1.insert("Cat");
   list1.insert("Hello");
  
   list1.display();
  
   list1.Delete("Mango");
  
   list1.display();
  
   list1.Delete("Cat");
  
   list1.display();
   return 0;
}

//sample output

C\Users\IshuManish\Documents SLinkedList.exe List is : 1 2 3 4 5 List is 2 3 4 5 List is :2 34 IList is Apple Cat Dog Hello M

Add a comment
Know the answer?
Add Answer to:
Using C++ • Implement the following in both string linked list and in the template version...
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
  • In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the...

    In this assignment, you will implement a sort method on singly-linked and doubly-linked lists. Implement the following sort member function on a singly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); Implement the following sort member function on a doubly-linked list: void sort(bool(*comp)(const T &, const T &) = defaultCompare); The sort(…) methods take as a parameter a comparator function, having a default assignment of defaultCompare, a static function defined as follows: template <typename T> static bool defaultCompare(const...

  • Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using...

    Below is the given code of implementation: #include <string> #include <iostream> #include <list> #include <cassert> using namespace std; class List; class Iterator; class Node { public: /* Constructs a node with a given data value. @param s the data to store in this node */ Node(string s); /* Destructor */ ~Node() {} private: string data; Node* previous; Node* next; friend class List; friend class Iterator; }; class List { public: /** Constructs an empty list. */ List(); /* Destructor. Deletes...

  • I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding...

    I have the following c++ data structures assignment: Copy Constructors, Destructors, and Assignment Operators An understanding of how to implement copy constructors, destructors, and assignment operators is essential when working with data structures using dynamic memory allocation. Failure to implement these methods correctly can and probably will result in memory leaks. In this project, you are provided with a working implementation of a doubly-linked list in which the copy constructor, destructor, and assignment operator methods are not complete. To complete...

  • C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr...

    C++ Implement a class template for a LinkedList.(doubly linked). Also, your class will have a tailPtr in addition to a headPtr along with methods to get, set, insert and remove values at either end of the list. Call these getFirst, getLast, setFirst, setLast, insertFirst, insertLast, removeFirst, removeLast. Don't forget, you also need a copy constructor and destructor plus getLength, isEmpty and clear methods. Overload the stream insertion operator as a friend function which outputs the list in format { 1,...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • v. 15 points) implementing data structure operations C++ code for a new public member function to...

    v. 15 points) implementing data structure operations C++ code for a new public member function to be added to the doubly linked list data structure used in class. Return the average value of all elements in the list ple a list of integers) Throws a length error exception if the list is empty. template «typename E> E& 0; average You may use loops or recursion as needed. If necessary, you may add private helper function0s) The definition of the DLinkedList...

  • (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and...

    (The SortedLinkedList class template) Complete the SortedLinkedList class template which is a doubly linked list and is implemented with a header node and a tail node. // SortedLinkedList.h // SortedLinkedList.h // A collection of data are stored in the list by ascending order #ifndef SORTEDLIST_H #define SORTEDLIST_H using namespace std; template <typename T> class SortedList { private: // The basic single linked list node type. // Nested inside of SortedList. struct NodeType { T data; NodeType* next; NodeType* prev; NodeType(const...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

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