IMPLEMENT FUNCTIONS C++ #include "node.h" #include "list.h" #include #include using namespace std; class DoublyLinkedList : public List { node* head; node* tail; int index; node* create_node(int num, node* predecessor, node* successor) { node* n = (
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
IMPLEMENT FUNCTIONS C++
#include "node.h"
#include "list.h"
#include <cstdlib>
#include <iostream>
using namespace std;
class DoublyLinkedList : public List {
node* head;
node* tail;
int index;
node* create_node(int num, node* predecessor, node* successor) {
node* n = (node*) malloc( sizeof(node) );
n->element = num;
n->next = successor;
n->prev = predecessor;
return n;
}
void add_between(int num, node* predecessor, node* successor) {
node* newest = create_node(num, predecessor, successor);
predecessor->next = newest;
successor->prev = newest;
index++;
}
public:
DoublyLinkedList() {
index = 0;
head = create_node(0, NULL, NULL);
tail = create_node(0, head, NULL);
head->next = tail;
}
~DoublyLinkedList() {
clear();
}
void addHead(int num) {
add_between(num, head, head->next);
}
void addTail(int num) {
add_between(num, tail->prev, tail);
}
int removeHead() {
return 0;
}
int removeTail() {
return 0;
}
int add(int num) {
return 0;
}
int remove(int num) {
return 0;
}
int get(int pos) {
node* currnode;
int count;
bool start_from_head = true;
if (pos > index/2) {
start_from_head = false;
}
if (start_from_head) {
// START FROM HEAD
currnode = head->next;
count = 0;
while (currnode != NULL) {
count++;
if (count == pos) {
return currnode->element;
} else {
currnode = currnode->next;
}
}
} else {
// START FROM TAIL
currnode = tail->prev;
count = index + 1;
while (currnode != NULL) {
count--;
if (count == pos) {
return currnode->element;
} else {
currnode = currnode->prev;
}
}
}
return -1;
}
int size() {
return index;
}
bool addAt(int num, int pos) {
return false;
}
int removeAt(int pos) {
return 0;
}
int removeAll(int num) {
return 0;
}
int contains(int num) {
return 0;
}
int count(int num) {
return 0;
}
bool move(int pos1, int pos2) {
return false;
}
bool isEmpty() {
return false;
}
void clear() {
}
// WARNING! Do not modify this method below!
// Doing so will nullify your score for this activity.
void print() {
node* currnode = head;
cout << "HEAD: ";
while (true) {
cout << currnode->element;
if (currnode == tail) {
cout << endl;
break;
}
cout << " -> ";
currnode = currnode->next;
}
currnode = tail;
cout << "TAIL: ";
while (true) {
cout << currnode->element;
if (currnode == head) {
cout << endl;
break;
}
cout << " -> ";
currnode = currnode->prev;
}
}
};
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 14 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)