Question

2. Write a function definition to create a new list. sub, that is a subset of M. Return the new list to the calling function. in c++ only. second part of the first question.

M is a linked list. this question as well as 3 and 4 are built off of question 1.
0 0
Add a comment Improve this question Transcribed image text
Answer #1


/*
 *  List.h file
 */

#include <iostream>
using namespace std;

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

class List
{
  private:

    NodeType *front, *rear;

  public:

    List();

    void getList();

    void addNodeLast(int val);

    NodeType* getFront()
    {
      return front;
    }

    NodeType* getRear()
    {
      return rear;
    }

};
/*  List.h file ends here */




/*
 *  List.cpp file
 */

#include "List.h"

List :: List()
{
  front = NULL;
  rear = NULL;
}

void List :: getList()
{
  NodeType *ptr = new NodeType;
  ptr = front;

  while (ptr != NULL)
  {
    cout << ptr->data << " ";
    ptr = ptr->next;
  }
}

void List :: addNodeLast(int val)
{
  NodeType *newNode = new NodeType;
  newNode->data = val;
  newNode->next = NULL;
  if (front == NULL)
  {
    front = newNode;
    rear = newNode;
    newNode = NULL;
  }
  else
  {
    rear->next = newNode;
    rear = newNode;
  }
}

/*  List.cpp file ends here */





/*
 *  Main.cpp file
 */

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

#include "List.h"


List getSubset(List M, int N)
{
  List sub;

  NodeType *ptr = new NodeType;
  ptr = M.getFront();
  
  while (N--)
  {
    sub.addNodeLast(ptr->data);
    //cout << ptr->data << " ";
    ptr = ptr->next;
  }
  return sub;
}


int main()
{
  List M, N, subset;
  
  srand(time(NULL));

  for (int i = 0; i < 10; i++)
  {
    M.addNodeLast(rand()%25 + 18);
  }
  cout << "Linked List M: ";
  M.getList();
  cout << endl;

  for (int i = 0; i < 6; i++)
  {
    N.addNodeLast(rand()%25 + 18);
  }
  cout << "Linked List N: ";
  N.getList();
  cout << endl;

  //  calling functions from question 2
  int subLen = 4;
  subset = getSubset(M, subLen);
  cout << "Subset of M with subset length " << subLen << ": ";subset.getList();

  
  //  calling functions from question 3

  return 0;
}
/*  Main.cpp file ends here */

saved - clang veraion 7.0.0-3-ubuntuo.18.04.1 (taga/RELEASE_700/all clang++-7-pthread-o main List.cpp main.cpp ./main Linked

Note: This part is done, with all the necessary parts (1, 2, and 4). Please upload the 3rd question. I will do it for you.

Add a comment
Know the answer?
Add Answer to:
in c++ only. second part of the first question. M is a linked list. this question as well as 3 and 4 are built off of question 1. 2. Write a function definition to create a new list. sub, that i...
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
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
ADVERTISEMENT