In C++
Create a data structure doubly linked list, implement the following operations for the doubly linked list:
addFirst
addLast
insertBefore
insertAfter
delete
printList
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <iomanip>
using namespace std;
//Node Class
class Node{
public:
double v;
Node *next;
Node *prev;
};
//LinkedList class
class LList{
//Head pointer
Node *head=nullptr;
Node *tail = nullptr;
public:
//This adds a new node at the end of linked
list
void addFirst(double val){
Node *nnode = new
Node();
nnode->v = val;
if(head==nullptr){
head = nnode;
tail = nnode;
return;
}
nnode->next =
head;
head->prev =
nnode;
head = nnode;
}
//This adds a new node at the end of linked
list
void addLast(double val){
Node *nnode = new
Node();
nnode->v = val;
if(head==nullptr){
head = nnode;
tail = nnode;
return;
}
tail->next =
nnode;
nnode->prev =
tail;
nnode->next =
nullptr;
tail = nnode;
}
void insertBefore(double target, double
val){
Node *nnode = new
Node();
nnode->v = val;
if(head->v==target){
nnode->next = head;
head->prev = nnode;
head = nnode;
return;
}
Node *temp = head;
while(temp!=nullptr){
if(temp->v==target){
Node *prev = temp->prev;
prev->next = nnode;
nnode->prev = prev;
nnode->next = temp;
temp->prev = nnode;
break;
}
temp = temp->next;
}
}
void insertAfter(double target, double
val){
Node *nnode = new
Node();
nnode->v = val;
if(target==tail->v){
tail->next = nnode;
nnode->prev = tail;
nnode->next = nullptr;
tail = nnode;
return;
}
Node *temp = head;
while(temp!=nullptr){
if(temp->v==target){
Node *nxt = temp->next;
temp->next = nnode;
nnode->prev = temp;
nnode->next = nxt;
nxt->prev = nnode;
break;
}
temp = temp->next;
}
}
//This deletes head node
void deleteNode(double val){
if(head->v==val){
Node *temp = head;
head = head->next;
head->prev = nullptr;
free(temp);
return;
}
if(tail->v==val){
Node *temp = tail;
tail = tail->prev;
tail->next = nullptr;
free(temp);
return;
}
Node *temp = head;
while(temp!=nullptr){
if(temp->v==val){
Node *prev = temp->prev;
Node *nxt = temp->next;
prev->next = nxt;
nxt->prev = prev;
free(temp);
break;
}
temp = temp->next;
}
}
//This prints the linked list
void print(){
Node *temp = head;
while(temp!=nullptr){
cout<<temp->v<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
};
int main(){
LList a;
a.addFirst(3);
a.addLast(4);
a.addFirst(2);
a.addLast(5);
a.print();
a.insertAfter(4,6);
a.insertBefore(3,7);
a.insertBefore(2,1);
a.print();
a.deleteNode(4);
a.deleteNode(1);
a.print();
return 0;
}
OUTPUT :
In C++ Create a data structure doubly linked list, implement the following operations for the doubly...
3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks) 3. How can you implement a queue data structure using a doubly linked list? Do you think it is necessary to use a doubly linked list rather than a singly linked list or not?(3 marks)
C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.
write any 2 c functions for a linked list and a doubly-linked sorted list create at least two data structures
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...
In c++ language Design and implement a Queue data structure using linked list. Support the following usual operations: default constructor parameterized constructor to create a queue of user-specified capacity enqueue dequeue is_full is_empty display destructor that deallocates all the nodes copy constructor overloaded assignment operator Demonstrate using a main function.
In python - Implement a doubly linked circular linked list of Node objects called CircularDoublyLinkedList. The data of each Node in the list is an integer. You will collect measurements for: An already sorted linked list An already sorted linked list in descending order A linked list containing random data
Implement a simple Doubly Linked List using nodes. Functions to include: an insert in a sorted list, and a delete function, including constructor. Please include comments to make the code traceable and understandable
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
Implement the following operation for doubly Linked List: public void insertAt(int position, E data){ } public void insertEnd(E data){ } public void deleteAt(int position) { } public void deleteEnd(){ }
implement delete node function in c++ language I just need a basic doubly linked list code for the delete node portion of the code // Delete node containing word from list if it is present void delNode (DLList list, char *str) ( // Delete node containing word from list if it is present void delNode (DLList list, char *str) (