Question

In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we
Sum lists: Given list A and list B of the same length, traverse them to create a new list, where the first new node on A plus
Can you make simple code of this using C++
In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we have: struct Node int data; Node 'pNext; and in main we have: Node "pHead = NULL; // pointer to the head of the list Node·pTemp: // Used to get new nodes Node "pHeadA NULL: Node "pHeadB = NULL; We will also provide for you the code that is used to create the list and the code to traverse and display the list elements. You can also assume that lists being processed already exist, and that the pointers for new lists are initialized to NULL. For the examples below, unless specified otherwise assume the list we are starting with is the list:
Sum lists: Given list A and list B of the same length, traverse them to create a new list, where the first new node on A plus the first node on B, the second new list node contains the sum of the second node on A plus the second node on B, 1. and so on. Assume list A and list B contain the following pListA is: 1->3->4-7 pListB is: 2->4->6->10 Calling combineLists/...from within main would look like the following: combineLists( Node "&pHead, Node pListA, Node "pListB) displayList( pHead); and it would result in the following output 3->7-10->17
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//C++ program

#include<iostream>
using namespace std;

struct node{
   int data;
   struct node*next;
};

struct node* addNode(struct node*list,int data){
   struct node*newnode = new(struct node);
   newnode->data = data;
   newnode->next=NULL;
  
   if(!list)return newnode;
  
   struct node*current = list;
   while(current->next)current=current->next;
  
   current->next = newnode;
   return list;
}

void print(struct node*list){
   while(list){
       cout<<list->data<<" ";
       list=list->next;
   }
   cout<<"\n";
}

void combineLists(node*&pHead,node*pHeadA,node*pHeadB){
   while(pHeadA&&pHeadB){
       pHead = addNode(pHead,pHeadA->data+pHeadB->data);
       pHeadA = pHeadA->next;
       pHeadB = pHeadB->next;
   }
   while(pHeadA){
       pHead = addNode(pHead,pHeadA->data);
       pHeadA = pHeadA->next;
   }
   while(pHeadB){
       pHead = addNode(pHead,pHeadB->data);
       pHeadB = pHeadB->next;
   }
}

int main(){
   node* pHead = NULL;
   node* pHeadA = NULL;
   node* pHeadB = NULL;
  
   pHead = addNode(pHead,2);
   pHead = addNode(pHead,3);
   pHead = addNode(pHead,5);
   pHead = addNode(pHead,6);
   pHead = addNode(pHead,8);
   pHead = addNode(pHead,9);
   pHead = addNode(pHead,11);
   cout<<"List is : \n";
   print(pHead);
  
   pHeadA = addNode(pHeadA,1);
   pHeadA = addNode(pHeadA,3);
   pHeadA = addNode(pHeadA,4);
   pHeadA = addNode(pHeadA,7);
   cout<<"\nList A is : \n";
   print(pHeadA);
  
   pHeadB = addNode(pHeadB,2);
   pHeadB = addNode(pHeadB,4);
   pHeadB = addNode(pHeadB,6);
   pHeadB = addNode(pHeadB,10);
   cout<<"\nList B is : \n";
   print(pHeadB);
  
   pHead = NULL;
   combineLists(pHead,pHeadA,pHeadB);
   cout<<"\nCombine List is : \n";
   print(pHead);
   return 0;
}

//sample output

CAUsers IshuManish\Documentsl combinelisy.exe List is : 2 3 5 6 89 11 List A is 1 3 4 7 List B is: 2 4 6 10 Combine List is 3

Add a comment
Know the answer?
Add Answer to:
Can you make simple code of this using C++ In-lab Final Exam Ideas: Following are the possible topics for the in-lab final exam for next week. These all assume that we have: struct Node int data...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the below code to fit the above requirements: struct node { char data; struct node...

    Modify the below code to fit the above requirements: struct node { char data; struct node *next; struct node *previous; } *front, *MyNode, *rear, *MyPointer, *anchor *Valuenode ; typedef struct node node; int Push(char input) { if(IsFull()==1) {   printf("The queue is full. Enter the ‘^’ character to stop.\n"); return -1; } else if (IsFull()==-1) { node *MyNode=(node*)malloc(sizeof(node)); MyNode->data=input; rear->next=MyNode; MyNode->previous=rear; MyPointer=rear=MyNode; return 1; } else { node *MyNode=(node*)malloc(sizeof(node)); node *anchor=(node*)malloc(sizeof(node)); MyNode->data=input; MyPointer=rear=front=MyNode; MyNode->previous=NULL; MyNode->next=NULL; anchor->next=MyNode; return 0; } } char...

  • Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that...

    Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignmet you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

  • Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given reference...

    Data Structures - Singly Linked Lists You will add a method swapNodes to SinglyLinkedList class (below). This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. You may need to traverse the list. package linkedlists; public class SinglyLinkedList<E> implements Cloneable {    // ---------------- nested Node class...

  • // C code // If you modify any of the given code, the return types, or...

    // C code // If you modify any of the given code, the return types, or the parameters, you risk getting compile error. // Yyou are not allowed to modify main (). // You can use string library functions. #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma warning(disable: 4996) // for Visual Studio #define MAX_NAME 30 // global linked list 'list' contains the list of patients struct patientList {    struct patient *patient;    struct patientList *next; } *list = NULL;  ...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • Project Description: In this project, you will combine the work you’ve done in previous assignments to...

    Project Description: In this project, you will combine the work you’ve done in previous assignments to create a separate chaining hash table. Overview of Separate Chaining Hash Tables The purpose of a hash table is to store and retrieve an unordered set of items. A separate chaining hash table is an array of linked lists. The hash function for this project is the modulus operator item%tablesize. This is similar to the simple array hash table from lab 5. However, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT