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);
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...
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...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from...
This is a C programming problem: Construct a doubly linked list: • Read non-zero integers from the input and insert them into the linked list. • If an input integer is 0, the program should print all the integers in the list(from tail to head) and then exit.
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT