Question

In: Computer Science

!Must be written in C++! Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30);...

!Must be written in C++!

Build a doubly linked list with these operations:
AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40);
Build a sorted doubly linked list with these operations:
Add(30), Add(20), Add(40), Add(15), Add(35);

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;

// Node structure
struct Node {
  int data;
  Node *next, *prev;
  Node(int _data, Node *_next = NULL, Node *_prev = NULL) {
    data = _data;
    next = _next;
    prev = _prev;
  }
};

// Doubly linked list with head and tail
class DoublyLinkedList {
  Node *head, *tail;
  public:
    DoublyLinkedList() {
      head = tail = NULL;
    }
    void print() {
      cout << "\nList: [ ";
      Node *p = head;
      while (p != NULL) {
        cout << p -> data << " ";
        p = p -> next;
      }
      cout << "]\n";
    }
    // Add new node at head
    void AddToHead(int data) {
      Node *newNode = new Node(data);
      if (head == NULL) {
        // If the list is empty
        head = tail = newNode;
      } else {
        // Else, insert before head
        newNode -> next = head;
        head -> prev = newNode;
        head = newNode;
      }
    }
    // Add new node at tail
    void AddToTail(int data) {
      Node *newNode = new Node(data);
      if (head == NULL) {
        // If the list is empty
        head = tail = newNode;
      } else {
        // Else, insert after tail
        newNode -> prev = tail;
        tail -> next = newNode;
        tail = newNode;
      }
    }
    // Add new node in its sorted position (ascending order)
    void Add(int data) {
      Node *newNode = new Node(data);
      if (head == NULL) {
        // If the list is empty
        head = tail = newNode;
      } else if (head -> data >= data) {
        // If data is less than (or equal to) head's data, insert at head.
        AddToHead(data);
      } else if (tail -> data <= data) {
        // If data is more than (or equal to) tail's data, insert at tail
        AddToTail(data);
      } else {
        // Else, we have the case where new data needs to be inserted
        // somewhere in the list and not corners (head or tail).
        // We find the first node whose data is more than the new node's data
        // and insert new node just before that node.
        Node *p = head;
        while (p -> data < data) {
          p = p -> next;
        }
        // Insert new node before p
        newNode -> next = p;
        newNode -> prev = p -> prev;
        p -> prev -> next = newNode;
        p -> prev = newNode;
      }
    }
};

int main() {
  DoublyLinkedList simple, sorted;
  simple.AddToHead(10);
  simple.AddToHead(20);
  simple.AddToTail(30);
  simple.AddToTail(40);
  simple.print();
  sorted.Add(30);
  sorted.Add(20);
  sorted.Add(40);
  sorted.Add(15);
  sorted.Add(35);
  sorted.print();
  return 0;
}

Output


Related Solutions

Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly...
Build a doubly linked list with these operations: AddToHead(10); AddToHead(20); AddToTail(30); AddToTail(40); Build a sorted doubly linked list with these operations: Add(30), Add(20), Add(40), Add(15), Add(35);
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end;...
A circular doubly-linked list .(a) In a circular doubly-linked list, there is no front or end; the nodes form a full circle. Instead of keeping track of the node at the front, we keep track of a current node instead. Write a class for a circular doubly-linked list using the attached Job class as your node objects. It should have: • A private instance variable for the current node • A getCurrent() method that returns a reference to the current...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
I was supposed to conver a singly linked list to a doubly linked list and everytime...
I was supposed to conver a singly linked list to a doubly linked list and everytime I run my program the output prints a bunch of random numbers constantly until I close the console. Here is the code. #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct node { int data; struct node *next; struct node *prev; }; //this always points to first link struct node *head = NULL; //this always points to last link struct node *tail = NULL;...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Develop a C++ "doubly" linked list class of your own that can hold a series of...
Develop a C++ "doubly" linked list class of your own that can hold a series of signed shorts Develop the following functionality: Develop a linked list node struct/class You can use it as a subclass like in the book (Class contained inside a class) You can use it as its own separate class Your choice Maintain a private pointer to a node class pointer (head) Constructor Initialize head pointer to null Destructor Make sure to properly delete every node in...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev...
Using doubly linked list in c++ with class constructor: DNode(){    song = Song();    prev = NULL;    next = NULL; } DNode(string s, string a, int lenmin, int lensec){    song = Song(s,a,lenmin,lensec);    prev = NULL;    next = NULL; } for each node. Write the method: void moveUp(string t); This method moves a song up one in the playlist. For instance, if you had the following: Punching in a Dream, The Naked And Famous................3:58 Harder To...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT