Question

In: Computer Science

Using C++, you will create a program, where you will create two doubly linked lists. These...

Using C++,

you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list.

the input can be from the user or you just write the input.

for example, if one number in the doubly linked list is 817 and in the other linked list has 43, you would add these two numbers, 860 and insert this number into the single linked list

(If you want some ideas: You can traverse the list backwards to add the numbers. the void add function can be: void add(node* tail of linked list1, node* tail of linked list 2, node*head of singly linked list)

Solutions

Expert Solution

Please find the code:

#include <iostream>
using namespace std;
//define a struct node for Double Linked List(DLL) 1
struct Node { 
   int data; 
   struct Node *prev; 
   struct Node *next; 
}; 
struct Node* head = NULL; //points to first node of DLL
struct Node* tail = NULL; //points to last node of DLL
//define a struct node2 for Double Linked List(DLL2) 2
struct Node2 { 
   int data2; 
   struct Node2 *prev2; 
   struct Node2 *next2; 
}; 
struct Node2* head2 = NULL;//points to first node of DLL2 
struct Node2* tail2 = NULL; //points to last node of DLL2
//define a struct Node_LL for Single Linked List(SLL) 
struct Node_LL { 
   int data_LL; 
   struct Node_LL *next_LL; 
}; 
struct Node_LL* head_LL= NULL;//points to first node of SLL   
//insert data into DLL
void insert(int newdata) { 
   struct Node* newnode = (struct Node*) malloc(sizeof(struct Node)); //create a new node
   newnode->data  = newdata; //set newdata as the data of newly created node
   newnode->prev = NULL; // set previous pointer as null
   newnode->next = head; //make next point to head    
   if(head !=  NULL) 
      head->prev = newnode ; //set prev for head as the newnode
   else
     tail=newnode; //make tail point to the last node(only when head=null)
   head = newnode; 
}  
//insert data into DLL2
void insert2(int newdata2) { 
   struct Node2* newnode2 = (struct Node2*) malloc(sizeof(struct Node2)); 
   newnode2->data2 = newdata2; 
   newnode2->prev2 = NULL; 
   newnode2->next2 = head2;     
   if(head2 !=  NULL) 
      head2->prev2 = newnode2 ; 
   else
    tail2=newnode2;
      head2 = newnode2; 
}
//insert data into SLL
void insert_LL(int new_data_LL) { 
   struct Node_LL* new_node_LL = (struct Node_LL*) malloc(sizeof(struct Node_LL)); 
   new_node_LL->data_LL= new_data_LL; 
   new_node_LL->next_LL = head_LL; 
   head_LL = new_node_LL; 
   
} 
//function to add the values in double linked lists and store it into SLL
void add(struct Node* ptr,struct Node2* ptr2,struct Node_LL* ptr_LL) { 
  
  
  while(ptr!=NULL){
      insert_LL(ptr->data+ptr2->data2);//add and insert data
      ptr = ptr->prev; //increment the pointers
      ptr2 = ptr2->prev2; 
  }
  
} 
//display contents of both DLL
void display_DLL() { 
     struct Node* ptr;
     ptr=head;
    cout<<"The doubly linked list is: ";
   while(ptr != NULL) { 
       
      cout<< ptr->data <<" "; 
      ptr = ptr->next; 
  } 
  struct Node2* ptr2;
     ptr2=head2;
  cout<<"\nThe second doubly linked list is: ";
   while(ptr2 != NULL) { 
      cout<< ptr2->data2 <<" "; 
      ptr2 = ptr2->next2; 
  } 
}
//display contents of SLL(Result)
void display_SLL()
{  cout<<" \n";
   cout<<"Resut(Single Linked List): "; 

     struct Node_LL* ptr_LL;
     ptr_LL = head_LL;
    while (ptr_LL != NULL) { 
       
      cout<< ptr_LL->data_LL <<" "; 
      ptr_LL = ptr_LL->next_LL; 
   } 
}
int main() { 
   insert(3);
   insert(1);
   insert(7);
   insert(2);
   insert(817);
  
   insert2(8);
   insert2(7);
   insert2(66);
   insert2(20);
   insert2(43);
 
   struct Node* tail_DLL1;
   tail_DLL1 = tail; 
   struct Node2* tail_DLL2;
   tail_DLL2 = tail2;  
   struct Node_LL* head_SLL;
   head_SLL = head_LL;
   display_DLL();
   add(tail_DLL1,tail_DLL2,head_SLL);//pass tail of DLL,DLL2 and head of SLL for adding and inserting.
   display_SLL();
   return 0; 
}


Related Solutions

In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
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...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
Can you write a program for the card game WAR using linked lists in c++!
Can you write a program for the card game WAR using linked lists in c++!
This is c++ Create a program where you create, display, search, delete elements within a linked...
This is c++ Create a program where you create, display, search, delete elements within a linked list. Watch your function arguments. Pointer parameters are passed by reference to some functions and by value to others. Functions to make: copy : copies element in linked list destroy all: destroys all elements in linked list wherethisgoes:user  enters element and returns where the element is located insert sorted: inserts element create using linked lists with a head pointer and so forth
Can you write a program for snakes and ladder board game using linked lists in c++
Can you write a program for snakes and ladder board game using linked lists in c++
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...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game...
Can you program Exploding kittens card game in c++ using linked lists and classes! The game is played with between 2 and 4 players. You'll have a deck of cards containing some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten. When that happens, that person explodes. They are now dead and out of the game. This process continues until there's only one player left, who...
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.
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly...
Write a pseudocode function that interchanges two adjacent items of: (a) singly linked lists (b) doubly linked lists if you can make it clean and short that be nice
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT