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.
can u give me an example of how to removing Double Linked List a node at...
can u give me an example of how to removing Double Linked List a node at a given location in java the method should be like this: public void removeFromLocation(int location) { }                                }
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.
Consider a linked list whose nodes are objects of the class Node: class Node {    ...
Consider a linked list whose nodes are objects of the class Node: class Node {     public int data;     public Node next; } prev references a node n1 in the list and curr references the node n2 that is right after n1 in the list. Which of the following statements is used to insert a new node, referenced by newNodebetween prev and curr? Group of answer choices newNode.next = curr; prev.next = newNode; newNode.next = head; head = newNode;...
c++ example of a double linked list of chars I need to create a double linked...
c++ example of a double linked list of chars I need to create a double linked list of chars. this should be a class that allows you to input a single character at a time, list the resulting characters, find any character and delete the first example of a character.
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.
Create a program that implements a singly linked list of Students. Each node must contain the...
Create a program that implements a singly linked list of Students. Each node must contain the following variables: Student_Name Student_ID In main(): Create the following list using addHead(). The list must be in the order shown below. Student_ID Student_Name 00235 Mohammad 00662 Ahmed 00999 Ali 00171 Fahad Print the complete list using toString() method. Create another list using AddTail(). The list must be in the order shown below. Student_ID Student_Name 00236 Salman 00663 Suliman 00998 Abdulrahman Print the complete list...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT