Please use this header file to build the doubly linked list. Please use a progra
ID: 3683838 • Letter: P
Question
Please use this header file to build the doubly linked list. Please use a program that someone just starting data structures is going to be able to understand
//--------------------------------------------------------------------
//
// Homework 3 ListLinked.h
//
// Class declaration for the Doubly linked implementation of the List ADT
//
//--------------------------------------------------------------------
#ifndef LISTLINKED_H
#define LISTLINKED_H
#include <iostream>
using namespace std;
template <typename DataType>
class ListNode { // doubly linked list node
public:
ListNode(const DataType& nodeData, ListNode* nextPtr, ListNode* prevPtr);
DataType dataItem;
ListNode* next;
ListNode* prev;
};
template <typename DataType>
class List { // a list implemented using doubly linked nodes
public:
List();
List(const List& other); // copy constructor
List& operator=(const List& other); // assignment operator
~List();
void insert(const DataType& newDataItem); // insert an item after cursor
void remove(); // remove the cursor node, and move the cursor to the next node. If the removed node is the last node, move the cursor to the beginning of the list
void replace(const DataType& newDataItem); // replace the cursor node value
void clear(); // clear the list, remove all nodes
bool isEmpty() const;
bool isFull() const;
void gotoBeginning(); // move cursor to the beginning of the list
void gotoEnd(); // move cursor to the end of the list
bool gotoNext(); // move cursor to the next node, return false if no next node is available
bool gotoPrior(); // move cursor to the prior node, return false if no prior node is available
DataType getCursor() const; // return the value of the cursor node
void moveToBeginning(); // move the cursor node to the beginning of the list
void insertBefore(const DataType& newDataItem); // insert a new item before the cursor
void print() const; // print the list, mark the cursor node
private: // Do not change them for this homework
ListNode<DataType>* head;
ListNode<DataType>* cursor;
};
#endif
Doubly Linked List is a data structure that holds a list of items with double links: previous and next. In this homework, you need to implement all functions in the defined ListLinked class (prototype is provided by the instructor). Please download the header file (ListLinked.h) from ecourses, read the comments and complete them using C++. You need to implement all of the ListLinked ADT functions in ListLinked.cpp file, and test them in main.cpp.
You need to make the following of the source code using the header file provided above: main.cpp, ListLinked.cpp.
Explanation / Answer
Answer for Question:
This below implementation of c++ code for double linked list may help or solved your given question.
1. Created class for double linkedlst .
2. Specified the methods for menu driven fassion .
3. Pompt the user operations based on user choice operation will performed/
Code:
#include<iostream>
using namespace std;
/* Linked list structure */
struct list {
struct list *prev;
int data;
struct list *next;
} *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL;
class linkedlist {
public:
/* Function for create/insert node at the beginning of Linked list */
void insert_beginning() {
list *addBeg = new list;
cout << "Enter value for the node:" << endl;
cin >> addBeg->data;
if(first == NULL) {
addBeg->prev = NULL;
addBeg->next = NULL;
first = addBeg;
last = addBeg;
cout << "Linked list Created!" << endl;
}
else {
addBeg->prev = NULL;
first->prev = addBeg;
addBeg->next = first;
first = addBeg;
cout << "Data Inserted at the beginning of the Linked list!" << endl;
}
}
/* Function for create/insert node at the end of Linked list */
void insert_end() {
list *addEnd = new list;
cout << "Enter value for the node:" << endl;
cin >> addEnd->data;
if(first == NULL) {
addEnd->prev = NULL;
addEnd->next = NULL;
first = addEnd;
last = addEnd;
cout << "Linked list Created!" << endl;
}
else {
addEnd->next = NULL;
last->next = addEnd;
addEnd->prev = last;
last = addEnd;
cout << "Data Inserted at the end of the Linked list!" << endl;
}
}
/* Function for Display Linked list */
void display() {
node = first;
cout << "List of data in Linked list in Ascending order!" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->next;
}
node = last;
cout << "List of data in Linked list in Descending order!" << endl;
while(node != NULL) {
cout << node->data << endl;
node = node->prev;
}
}
/* Function for delete node from Linked list */
void del() {
int count = 0, number, i;
node = node1 = node2 = first;
for(node = first; node != NULL; node = node->next)
cout << "Enter value for the node:" << endl;
count++;
display();
cout << count << " nodes available here!" << endl;
cout << "Enter the node number which you want to delete:" << endl;
cin >> number;
if(number != 1) {
if(number < count && number > 0) {
for(i = 2; i <= number; i++)
node = node->next;
for(i = 2; i <= number-1; i++)
node1 = node1->next;
for(i = 2; i <= number+1; i++)
node2 = node2->next;
node2->prev = node1;
node1->next = node2;
node->prev = NULL;
node->next = NULL;
node = NULL;
}
else if(number == count) {
node = last;
last = node->prev;
last->next = NULL;
node = NULL;
}
else
cout << "Invalid node number!" << endl;
}
else {
node = first;
first = node->next;
first->prev = NULL;
node = NULL;
}
cout << "Node has been deleted successfully!" << endl;
}
};
int main() {
int op = 0;
linkedlist llist = linkedlist();
while(op != 4) {
cout << "1. Insert at the beginning 2. Insert at the end 3. Delete 4. Display 5. Exit" << endl;
cout << "Enter your choice:" << endl;
cin >> op;
switch(op) {
case 1:
llist.insert_beginning();
break;
case 2:
llist.insert_end();
break;
case 3:
llist.del();
break;
case 4:
llist.display();
break;
case 5:
cout << "Bye Bye!" << endl;
return 0;
break;
default:
cout << "Invalid choice!" << endl;
}
}
return 0;
}