Question

Using a class template, write a linked list with 5 nodes and print the linked list....

Using a class template, write a linked list with 5 nodes and print the linked list. Then make a second linked list with the values from the first linked list and reverse the values and print. (C++)

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

// do comment if any problem arises

// code

#include <iostream>

using namespace std;

template <class T>

struct node

{

    T data;

    node *next;

};

template <class T>

class LinkedList

{

public:

    // head of linked list

    node<T> *head;

    // default constructor

    LinkedList()

    {

        head = NULL;

    }

    // insert at top of list

    void insert(T item)

    {

        node<T> *temp = head;

        head = new node<T>;

        head->data = item;

        head->next = temp;

    }

    // print list

    void print()

    {

        node<T> *temp = head;

        while (temp != NULL)

        {

            cout << temp->data << " ";

            temp = temp->next;

        }

        cout << endl;

    }

};

template <class T>

// this function inserts element in given list in reverse order

void reverse(LinkedList<T> from, LinkedList<T> &to)

{

    node<T> *temp = from.head;

    while (temp != NULL)

    {

        to.insert(temp->data);

        temp = temp->next;

    }

}

int main()

{

    // create linkel list with 5 elements

    LinkedList<int> testing;

    testing.insert(4);

    testing.insert(1);

    testing.insert(34);

    testing.insert(98);

    testing.print();

    // create list with reverse

    LinkedList<int> reverse_list;

    reverse(testing, reverse_list);

    cout << "After reverse: ";

    reverse_list.print();

}

Output:

98 34 1 4 After reverse: 4 1 34 98 ns. I fiyon

Add a comment
Know the answer?
Add Answer to:
Using a class template, write a linked list with 5 nodes and print the linked list....
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
  • Write a C++implementation of a doubly linked list class using a template class representation for a...

    Write a C++implementation of a doubly linked list class using a template class representation for a node and using pointers.In its public API provide functions to insert,find, delete, get size and get position of an element. Write C++ code to show those functions work as expected. (MUST COMPILE AND RUN)

  • a. Using C++, define a node structure of the linked list (e.g. value is an integer,...

    a. Using C++, define a node structure of the linked list (e.g. value is an integer, next is a node type pointer), construct a linked list of 10 nodes and assign random numbers as the nodes’ values. Use loop to track and print from the first node to the last and output all nodes’ values. Finally, free all memories of the linked list.

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • use python In class, we've studied Singly-Linked Lists which are made of nodes that point at...

    use python In class, we've studied Singly-Linked Lists which are made of nodes that point at subsequent nodes. One of the biggest annoyances with Linked Lists is the difficulty of going backwards through a list (such as getting the previous node or traversing the list backwards). An intuitive solution to this inefficiency is the doubly-linked list, which adds pointers to previ- ous nodes. Doubly-Linked Lists are not very different from Singly-Linked Lists, but are far more common because they are...

  • Using C++ language, Design and implement a class representing a doubly linked list. The class must...

    Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...

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

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Define a C++ class managing objects using a Linked List. (10 points) Define a C++ class...

    Define a C++ class managing objects using a Linked List. (10 points) Define a C++ class named "BirthdayList" that manages a list of Birthday objects as a linked list maintaining the order of entry (first birthday should be displayed first). Write a main function to show that you can add a list of birthdays and print out that list in the order that you have entered. (5 points) Add a "search" method into the "BirthdayList" class that accepts a Birthday...

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