Question

In: Computer Science

In a double linked chain, each node can point to the previous node as well as...

In a double linked chain, each node can point to the previous node as well as the next node. In a double linked chain, each node can point to the previous node as well as the next node.

Solutions

Expert Solution

Here I am implementing doubly linked list in cpp.

If any issue plzz comment below

#include <iostream>
using namespace std;

//Here structure of a doubly linked list.
struct Node {
   int data;   //point to the data in the linked list
   struct Node *prev; //point to left node 
   struct Node *next; //point to right node
};

//Header node initialization
struct Node* head = NULL;

//function to add new Data
void insert(int newdata) {
    
    //create a new node for doubly linked list
   struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
   
   //insert data
   newnode->data = newdata;
   
   //first point the left node to null
   newnode->prev = NULL;
   
   //put the address of current head node to right of new node
   newnode->next = head;
   
   //if head is not null
   if(head != NULL)
   
   //put the address of new node to left of head node
   head->prev = newnode ;
   
   //make the current node as head node
   head = newnode;
}

//To display the doubly linked list
void display() {
   struct Node* ptr;
   ptr = head;
   
   //iterate until we get null address
   while(ptr != NULL) {
      cout<< ptr->data <<" ";
      ptr = ptr->next;
   }
}

//main function
int main() {
   insert(9);
   insert(10);
   insert(7);
   insert(4);
   insert(9);
   insert(54);
   cout<<"The doubly linked list is: ";
   display();
   return 0;
}

Output:


Related Solutions

In a double linked chain, each node can point to the previous node as well as...
In a double linked chain, each node can point to the previous node as well as the next node. Illustrate and list the steps necessary to add a node to the end of the doubly linked chain.
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class in C++ that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value...
Assume that a singly linked list is implemented with a header node, but no tail node,...
Assume that a singly linked list is implemented with a header node, but no tail node, and that it maintains only a pointer to the header node. Write a class that includes methods to a. return the size of the linked list b. print the linked list c. test if a value x is contained in the linked list d. add a value x if it is not already contained in the linked list e. remove a value x if...
What is a linked data structure? What is a node? What are the benefits of linked...
What is a linked data structure? What is a node? What are the benefits of linked structure? What are the drawbacks of linked structure? What are the differences between singly linked and doubly linked structures? Give examples of when a linked structure could be used.
C++ Write a C++ program that implements a tree using a linked representation Each node will...
C++ Write a C++ program that implements a tree using a linked representation Each node will contain a single integer data element. Initialize the tree to contain 10 nodes. The program should allow for the insertion and deletion of data. The program should allow the user to output data in Preorder, Inorder and Postorder.
I've provided a Node class that implements a node of a simple singly-linked list (with .value...
I've provided a Node class that implements a node of a simple singly-linked list (with .value and .next fields), and an empty LinkedList class. Your task is to implement LinkedList.sort(l), where given the node l as the head of a singly-linked list, LinkedList.sort(l) sorts the nodes in the list into ascending order according to the values in the .value field of each node. Your implementation should do an in-place update of the list. It is ok to use a simple...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;   ...
JAVA DATA STRUCTURE (Linked Lists/Queue) public class Node {    int value;    Node nextNode;    Node(int v, Node n){        value = v;        nextNode = n;    }    Node (int v){        this(v,null);    } } public class Stack {    protected Node top;    Stack(){        top = null;    }    boolean isEmpty(){        return( top == null);    }    void push(int v){        Node tempPointer;       ...
Classify (if possible) each critical point of the given plane autonomous system as a stable node,...
Classify (if possible) each critical point of the given plane autonomous system as a stable node, a stable spiral point, an unstable spiral point, an unstable node, or a saddle point. (Order your answers from smallest to largest x, then from smallest to largest y.) x' = x(1 − x2 − 5y2) y' = y(5 − x2 − 5y2)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT