Question

In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...

In def c++ IDE correct mistake and screenshot output

------------------------------------------------------------------------------

#define NULL 0
#include <iostream>

/*
Name: Rabia Saad Al-wethnani
ID: 43503535
Section: 2486
*/

using namespace std;

struct Node
{
int item; //data
Node *next; //pointer to the next node in the list
};

Node* createNode(int);
int isPresent(Node*, int);
void appendNode(Node*&, int);
void displayList(Node*);
void printLists(Node*, Node*);
void swapNodes(Node* &head, int s);
void findIntersection(Node* first, Node* P);
void findUnion(Node* first, Node* P);


int main()
{
Node* L = NULL, *P = NULL;

appendNode(L, 1);
appendNode(L, 2);
appendNode(L, 3);
appendNode(L, 4);
appendNode(L, 6);
appendNode(L, 8);
displayList(L);

appendNode(P, 1);
appendNode(P, 2);
appendNode(P, 4);
appendNode(P, 6);
displayList(P);

printLists(L, P); //complete this function
swapNodes(L, 30); //It should swap the links of nodes 30 and 40 in L.
findIntersection(L, P); //This method should print 1, 2, 4 ,6
findUnion(L, P); //It should print 1,2,3,4,6,8
return 0;
}

int isPresent(Node* P, int d)
{
Node* currNode = P;
while(currNode != NULL){
if(currNode->item == d)
return 1;
currNode = currNode->next;
}
return 0;
}

void printLists(Node* L, Node* P)
{
Node* newList = NULL;
Node* currNode = L;
while(currNode != NULL){
if(isPresent(P, currNode->item))
appendNode(newList, currNode->item);
currNode = currNode->next;
}

displayList(newList);
//The output of the function should be:
// 1, 2, 4, 8
}

//swap the node containing item s with the next node in the list
void swapNodes(Node* &head, int s)
{
//write your code here
}


void findIntersection(Node* first, Node* P)
{
Node* newList = NULL; //this list should contain intersection of L and P

Node* currNode = first;
while(currNode != NULL){
if(isPresent(P, currNode->item))
appendNode(newList, currNode->item);
// cout << currNode->item;
currNode = currNode->next;
}
//display the new list after union
displayList(newList);
}


void findUnion(Node* first, Node* P)
{
Node* newList = NULL; //this list should contain Union of L and P

Node* currNode = first;
while(currNode != NULL){
appendNode(newList, currNode->item);
currNode = currNode->next;
}

currNode = P;
while(currNode != NULL){
if (!isPresent(newList, currNode->item))
appendNode(newList, currNode->item);
currNode = currNode->next;
}
//display the new list after union
displayList(newList);
}


Node* createNode(int value)
{
Node *node = new Node;
node->item = value;
node->next = NULL;
return node;
}

void appendNode(Node* &head, int value)
{
Node *newNode, *currNode;
newNode = createNode(value);
if(head == NULL) //the list is empty
head = newNode; //make a new list
else
{ //list is not empty
currNode = head; //current node points to the list head

while (currNode->next != NULL) //move current node in the list
currNode = currNode->next;

currNode->next = newNode; //add new node at the end
}
}

void displayList(Node* head)
{
cout<< "List:";
Node* currNode = head;

while (currNode != NULL)
{
cout<<currNode->item<<", ";
currNode = currNode->next;

}
cout<<endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM CODE:

#define NULL 0
#include <iostream>
/*
Name: Rabia Saad Al-wethnani
ID: 43503535
Section: 2486
*/
using namespace std;
struct Node
{
int item; //data
Node *next; //pointer to the next node in the list
};
Node* createNode(int);
int isPresent(Node*, int);
void appendNode(Node*&, int);
void displayList(Node*);
void printLists(Node*, Node*);
void swapNodes(Node* &head, int s);
void findIntersection(Node* first, Node* P);
void findUnion(Node* first, Node* P);

int main()
{
Node* L = NULL, *P = NULL;
appendNode(L, 1);
appendNode(L, 2);
appendNode(L, 3);
appendNode(L, 4);
appendNode(L, 6);
appendNode(L, 8);
displayList(L);
appendNode(P, 1);
appendNode(P, 2);
appendNode(P, 4);
appendNode(P, 6);
displayList(P);
printLists(L, P); //complete this function
swapNodes(L, 3); //It should swap the links of nodes 30 and 40 in L.
displayList(L); // the value of 30 was not available in the list. Hence changed it to 3.
//have a check on the output. Swap is now happening for 3 and 4.
findIntersection(L, P); //This method should print 1, 2, 4 ,6
findUnion(L, P); //It should print 1,2,3,4,6,8
return 0;
}
int isPresent(Node* P, int d)
{
Node* currNode = P;
while(currNode != NULL){
if(currNode->item == d)
return 1;
currNode = currNode->next;
}
return 0;
}
void printLists(Node* L, Node* P)
{
Node* newList = NULL;
Node* currNode = L;
while(currNode != NULL){
if(isPresent(P, currNode->item))
appendNode(newList, currNode->item);
currNode = currNode->next;
}
displayList(newList);
//The output of the function should be:
// 1, 2, 4, 8
}
//swap the node containing item s with the next node in the list
void swapNodes(Node* &head, int s)
{
   Node * newNode = head;
   while(newNode != NULL)
   {
       if(newNode->item == s)
       {
           int nextItem = newNode->next->item;
           newNode->item = nextItem;
           newNode->next->item = s;
           break;
       }
       newNode = newNode->next;
   }
}

void findIntersection(Node* first, Node* P)
{
Node* newList = NULL; //this list should contain intersection of L and P
Node* currNode = first;
while(currNode != NULL){
if(isPresent(P, currNode->item))
appendNode(newList, currNode->item);
// cout << currNode->item;
currNode = currNode->next;
}
//display the new list after union
displayList(newList);
}

void findUnion(Node* first, Node* P)
{
Node* newList = NULL; //this list should contain Union of L and P
Node* currNode = first;
while(currNode != NULL){
appendNode(newList, currNode->item);
currNode = currNode->next;
}
currNode = P;
while(currNode != NULL){
if (!isPresent(newList, currNode->item))
appendNode(newList, currNode->item);
currNode = currNode->next;
}
//display the new list after union
displayList(newList);
}

Node* createNode(int value)
{
Node *node = new Node;
node->item = value;
node->next = NULL;
return node;
}
void appendNode(Node* &head, int value)
{
Node *newNode, *currNode;
newNode = createNode(value);
if(head == NULL) //the list is empty
head = newNode; //make a new list
else
{ //list is not empty
currNode = head; //current node points to the list head
while (currNode->next != NULL) //move current node in the list
currNode = currNode->next;
currNode->next = newNode; //add new node at the end
}
}
void displayList(Node* head)
{
cout<< "List:";
Node* currNode = head;
while (currNode != NULL)
{
cout<<currNode->item<<", ";
currNode = currNode->next;
}
cout<<endl;
}

OUTPUT:

List:1, 2, 3, 4, 6, 8, 
List:1, 2, 4, 6, 
List:1, 2, 4, 6, 
List:1, 2, 4, 3, 6, 8, 
List:1, 2, 4, 6, 
List:1, 2, 4, 3, 6, 8, 

Success time: 0 memory: 3468 signal:o List:1, 2, 3, 4, 6, 8, List:1, 2, 4, 6, List:1, 2, 4, 6, List:1, 2, 4, 3, 6, 8, List:1,

Add a comment
Know the answer?
Add Answer to:
In def c++ IDE correct mistake and screenshot output ------------------------------------------------------------------------------ #define NULL 0 #include <iostream> /*...
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
  • C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int...

    C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

  • CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare...

    CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node {    char c;    Node *next; }; class LinkedString {    Node *head; public:    LinkedString();    LinkedString(char[]);    LinkedString(string);    char charAt(int) const;    string concat(const LinkedString &obj) const;    bool isEmpty() const;    int length() const;    LinkedString substring(int, int) const;    //added helper function to add to linekd list private:    void add(char c); };...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • Using the provided Linked List template, add the following recursive functions and demonstrate them in a...

    Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...

  • Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should h...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Design and implement your own linked list class to hold a sorted list of integers in...

    Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to display the list, check if...

  • Your task is to complete the following function/functions: 1. Given a position in the linked list,...

    Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

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