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 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
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
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.
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
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,...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
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...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
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