Question

Build the following data structures in c++: STACK (Do not use the STL library for your...

Build the following data structures in c++: STACK (Do not use the STL library for your containers)

C++

Stack and Queue should contain

  • Insertion (Push/Enqueue)

  • Deletion (Pop/Dequeue)

  • Print/Display

  • Sort (Stacks: Value or Color, Queue: Alphabetical)

  • Search (Contains, position, and how many instances as three separate functions)

  • Clear/empty

  • Size (if empty, print that it’s empty)

  1. Each data structure should contain a set of overloaded operators (Respect innate object behavior):
    • ‘+’ (addition) that allows you to add two objects of the same type together.

    • ‘++’ (increment) that adds a new node to the object and then fills it with a random value.

    • ‘—’ (decrement) that deletes a node from the object.

  2. Fill the stack with 52 values representing cards in a deck, then print a 7-card hand.

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

#include "matrix.h"
using namespace std;
Matrix::Matrix()
{
row=2;
col=2;
init();
}

Matrix::Matrix(int r,int c)
{
row=r;
col=c;
init();
}

//will initialize 2d matrix
void Matrix::init()
{
mat = new double*[row];
for(int i = 0; i < row; ++i)
mat[i] = new double[col];
//setting all values to zero
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
mat[i][j]=0.1;
}

//copy constructor
Matrix::Matrix(const Matrix &mat2)
{
row=mat2.row;
col=mat2.col;
init();
}

//destructor
Matrix::~Matrix()
{
for(int i=0;i<row;i++)
{
delete mat[i];
}
delete mat;
}

// = operator overloading

void Matrix::operator = (const Matrix &mat2)
{
//delete current mat data
cout<<"enter";
for(int i=0;i<row;i++)
{
delete mat[i];
}
delete mat;
  
//assign new values;
row=mat2.row;
col=mat2.col;
//create new **mat using current value of row and col
init();
  
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{ mat[i][j]=mat2.mat[i][j];
cout<<mat2.mat[i][j];
}
}

//overloading + operator
Matrix Matrix::operator + (const Matrix &obj)
{
Matrix result=;
if(row!=obj.row or col!=obj.col)
{
return Matrix(0,0);
}
else
{

for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{ result.mat[i][j]=mat[i][j]+obj.mat[i][j];
  
// cout<<result.mat[i][j];
}
  
}
result.printValue();
return result;
}

//return row and columns
int* Matrix::getDimensions()
{
int* dimensions=new int[2];
dimensions[0]=row;
dimensions[1]=col;
}

bool Matrix::setVaule(double val,int rowNo,int colNo)
{
if(rowNo<row && colNo<col)
{
mat[rowNo][colNo]=val;
return true;
}
else
{
cout<<"dimensions out of range."<<endl;
return false;
}
}

//return value of matrix a given row and col no
double Matrix::getValue(int rowNo,int colNo)
{
if(rowNo<row && colNo<col)
{
return mat[rowNo][colNo];
}
else
{
cout<<"dimensions out of range."<<endl;
return -1.0;
}
return -1.0;
}

//search for values and return [row,col]

int * Matrix::search(double val)
{
int* dimensions=new int[2];
dimensions[0]=-1;
dimensions[1]=-1;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
if(mat[i][j]==val)
{
dimensions[0]=i;
dimensions[1]=j;
}
}
return dimensions;
}


void Matrix::printValue()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<mat[i][j]<<" ";
}
cout<<endl;
}
}

Add a comment
Know the answer?
Add Answer to:
Build the following data structures in c++: STACK (Do not use the STL library for your...
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
  • Build the following data structures in c++: STACK (Do not use the STL library for your...

    Build the following data structures in c++: STACK (Do not use the STL library for your containers, PREFERRED IF YOU USED LINKED LIST OR ARRAYS ) C++ Stack should contain Insertion (Push) Deletion (Pop) Print/Display Clear/empty Size (if empty, print that it’s empty) Fill the stack with 52 values representing cards in a deck, then print a 7-card hand.

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int heig...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect;...

    CODE IN C Objectives: Queue operations. Data structure: typedef struct { int width; int height; }Rect; typedef struct node { Rect* r; struct node* next; }Node; typedef struct { Node* head; Node* tail; }Queue; Specification: In this lab six Queue-related operation functions need to be implemented by using the given function prototypes and data structures. 1. List* createQueue(void); This function initializes an empty “Queue” with the “Queue” data structure and returns an empty queue. 2. int enQueue(List*); This function receives...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap...

    Using C++, data structures, C++ STL, inputs and expected outputs are shown below. Max Heap Heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either > (in a max heap) or s (in a min heap) the key of C. The node at the "top" of the heap (with no parents) is called the root node. In binary-tree based heap, it...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • 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...

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