Question

#1 [4 points] a) Draw a sketch of a doubly-linked list, specifically of class DLinkedList in DLinkedList.h, containing the courses, including the section number, you are taking this semester; for example, CPSC 131.01 will be CPSC 131.01. Make sure to show the DLinkedList object, all node objects, and all objects data members and pointers. b) Complete the following main function to create your linked list above (Your code should go in only the indicated space - do not change any other code). #include <string> #include DLinkedList.h using namespace std; int mainO // YOUR CODE TO CREATE THE LIST GOES HERE
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string>

#include "DLinkedList.h"

using namespace std;

class Student {

private:

string name;

string course;

int section_number;

public:

void display()

{

cout << "Student Name:" << name << "\t Course Name: " << course << "\t Section Number: " << section_number << endl;

}

void setName(string n)

{

name = n;

}

void setCourse(string c)

{

course = c;

}

void setSection(int sn)

{

section_number = sn;

}

};

int main()

{

DLinkedList<Student> stud;

Student s;

int i;

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

{

s.setName("Student" + to_string(i+10));

s.setCourse("CSE");

s.setSection(4);

stud.add_Front(s);

}

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

{

s.setName("Student" + to_string(i + 20));

s.setCourse("ME");

s.setSection(5);

stud.add_Back(s); // can also insert at the end

}

while (!stud.check_empty())

{

stud.back_element().display();

stud.remove_Back();

}

}

DLinkedList.h

#include <iostream>

using namespace std;

template <typename T> class DLinkedList; // forward declaration

template <typename T>

// doubly linked list

class Double_Node

{   

private:

T element; // node element

Double_Node<T> *prev; // previous node in list

Double_Node<T> *nxt; // next node in list

friend class DLinkedList<T>; // provide SingleLinkedList access

};

template <typename T>

// a doubly linked list

class DLinkedList

{   

public:

DLinkedList(); // constructor

~DLinkedList(); // destructor

bool check_empty() const; // check list is empty

T& front_element(); // to get front element

T& back_element(); // to get back element

void add_Front(const T& tt); // add to in front

void add_Back(const T& tt); // add to back

void remove_Front(); // remove from front

void remove_Back(); // remove from back

int size() const; // size of list

private: // local type definitions

int n; // number of items in list

Double_Node<T>* head; // header sentinel

Double_Node<T>* trail; // trailer sentinel

protected:

void insert(Double_Node<T>* v, const T& tt); // insert new node before v

void remove(Double_Node<T>* v); // remove node v

};

template <typename T>

DLinkedList<T>::DLinkedList()

{

// initially list is empty

n = 0;   

// create sentinels

head = new Double_Node<T>;   

trail = new Double_Node<T>;

head->nxt = trail;

trail->prev = head;

}

template <typename T>

bool DLinkedList<T>::check_empty() const

{

return (head->nxt == trail);

}

template <typename T>

T& DLinkedList<T>::front_element()

{

if (check_empty()) throw length_error("ERROR: List is empty!!!");

return head->nxt->element;

}

template <typename T>

T& DLinkedList<T>::back_element()

{

if (check_empty()) throw length_error("ERROR: List is Empty!!!");

return trail->prev->element;

}

template <typename T>

DLinkedList<T>::~DLinkedList()

{

// remove the sentinels

while (!check_empty()) remove_Front();

delete head;   

delete trail;

}

template <typename T>

void DLinkedList<T>::insert(Double_Node<T>* v, const T& tt)

{

Double_Node<T>* u = new Double_Node<T>; // create a new node

u->element = tt;

u->nxt = v;   

u->prev = v->prev;

v->prev->nxt = u;

v->prev = u;

n++;

}

template <typename T>

void DLinkedList<T>::add_Front(const T& tt)   

{

insert(head->nxt, tt);

}

template <typename T>

void DLinkedList<T>::add_Back(const T& tt)

{

insert(trail, tt);

}

template <typename T>

// remove node v

void DLinkedList<T>::remove(Double_Node<T>* v)

{

Double_Node<T>* u = v->prev; // predecessor

Double_Node<T>* w = v->nxt; // successor

u->nxt = w;   

w->prev = u;

delete v;

n--;

}

template <typename T>

void DLinkedList<T>::remove_Front()

{

if (check_empty()) throw length_error("ERROR: List is empty!!!");

remove(head->nxt);

}

template <typename T>

void DLinkedList<T>::remove_Back()

{

if (check_empty()) throw length_error("ERROR: List is empty!!!");

remove(trail->prev);

}

template <typename T>

int DLinkedList<T>::size() const

{

return n;

}

Student Name:Student24 Course Name: ME Student Name: Student23 Course Name: ME Student Name: Student22 Course Name: ME Studen

Add a comment
Know the answer?
Add Answer to:
#1 [4 points] a) Draw a sketch of a doubly-linked list, specifically of class DLinkedList in...
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
  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that...

    Linked Lists: Suppose you have a doubly linked list with both head and tail pointers, that stores integers. Implement a non-recursive function that takes a linked list, searches for an integer, and removes the node with the first occurrence of that integer and also removes the node directly after it regardless of value . This function will return to address of the resulting list. You ca n assume that there will be at least three nodes, and if there is...

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

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

  • Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype...

    Consider a Linked List program with the following class: typedef int datatype; struct node {   datatype data; node *tail; }; class LinkedList{ private: node *head; node *current;public: //constructors LinkedList(); LinkedList(int i); //destructor ~LinkedList(); bool start(); //sets list postion to header bool nextNode(); //increments to next node in list int getCurrent(); //returns data from current node void insertNode(int i); //inserts node after current node    //then sets current node to new node bool deleteNode();//deletes currentnode void deleteAll(); //deletes all nodes };...

  • PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use...

    PLEASE USE C++ Source Code Attached is a linked list with 2 nodes. You can use this or write a similar one. The assignment is to write 2 functions. One function will add another node at the end of the list. The other function will delete a node. Don't forget - No dangling pointers ! Example linked source code attached below #include<iostream> using namespace std; class Node { int data; Node *next; public: void setdata(int d) {data = d;} void...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface:...

    Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> {      /**       * Append an item to the end of the list       *       * @param item – item to be appended       */ public void append(E item);      /**       * Insert an item at a specified index position       *       * @param item – item to be...

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

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

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