Question

In: Computer Science

PROGAMMING LANGUAGE: C++ Can anyone solve this assignment? Especially the extra credit part? Thanks. Please study...

PROGAMMING LANGUAGE: C++

Can anyone solve this assignment? Especially the extra credit part? Thanks.

Please study the code 'Stack as Array example ' posted below.

You need to implement a stack template, but this time implement the stack as a single linked list. The test of the class should be done with the same main function, slightly modified to declare a stack object and properly call the class functions.

There is a helper file in the module that shows you a sample definition of a template class. This is just an example, you do not need to implement all functionality. You could implement all functions for 5 points extra credit. If you chose to implement the additional functions, you should test them all in the driver ( main ) function.

***************************************************************************************************************

/**
* Stack implementation using array in C/procedural language.
*/
#include
#include
#include
//#include // For INT_MIN

#define SIZE 100

using namespace std;

/// Create a stack with capacity of 100 elements
int stack[SIZE];

/// Initially stack is empty
int top = -1;


/** Function declaration to perform push and pop on stack */
void push(int element);
int pop();
void display();


int main()
{
int choice, data;

while(1)
{
/* Menu */
cout <<"------------------------------------\n";
cout <<" STACK IMPLEMENTATION PROGRAM \n";
cout <<"------------------------------------\n";
cout <<"1. Push\n";
cout <<"2. Pop\n";
cout <<"3. Size\n";
cout <<"4. Print Stack\n";
cout <<"5. Exit\n";
cout <<"------------------------------------\n";
cout <<"Enter your choice: ";

cin >>choice;

switch(choice)
{
case 1:
cout <<"Enter data to push into stack: ";
cin >> data;

// Push element to stack
push(data);
break;

case 2:
data = pop();

/// If stack is not empty
if (data != INT_MIN)
cout <<"Data => " << data << endl;
break;

case 3:
cout <<"Stack size: " << top + 1 << endl;
break;
case 4:
display();
break;


case 5:
cout <<"Exiting from app.\n";
exit(0);
break;

default:
cout <<"Invalid choice, please try again.\n";
}

cout <<"\n\n";
}


return 0;
}

/**
* Function to push a new element in stack.
*/
void push(int element)
{
/// Check stack overflow
if (top >= SIZE)
{
cout <<"Stack Overflow, can't add more element element to stack.\n";
return;
}

/// Increase element count in stack
top++;

/// Push element in stack
stack[top] = element;

cout <<"Data pushed to stack.\n";
}


/**
* Function to pop element from top of stack.
*/
int pop()
{
/// Check stack underflow
if (top < 0)
{
cout <<"Stack is empty.\n";

/// Throw empty stack error/exception
/// Since C does not have concept of exception
/// Hence return minimum integer value as error value
/// Later in code check if return value is INT_MIN, then
/// stack is empty
return INT_MIN;
}


/// Return stack top and decrease element count in stack
return stack[top--];
}
void display()
{
if ( top >=0)
{
for(int i = 0; i <= top ; i++ )
cout << stack[i] << " ";
cout << endl;
}

else
cout << "stack is empty\n\n";
}

And here is the "helper file in the module" the teacher gave us:

Assignmenmt 06 HELP -- Stack Class Template Definition

Assignment help -- linked list stack sample header file

//Header File: linkedStack.h

#ifndef H_StackType
#define H_StackType

#include
#include


using namespace std;

//Definition of the node
template
struct nodeType
{
Type info;
nodeType *link;
};

template
class linkedStackType:
{
public:

//Overload the assignment operator.
const linkedStackType& operator=
(const linkedStackType&);

//Function to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
// otherwise returns false.

bool isEmptyStack() const;

//Function to determine whether the stack is full.
//Postcondition: Returns false.

bool isFullStack() const;

//Function to initialize the stack to an empty state.
//Postcondition: The stack elements are removed;
// stackTop = nullptr;

void initializeStack();

//Function to add newItem to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: The stack is changed and newItem
// is added to the top of the stack.

void push(const Type& newItem);

//Function to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the program
// terminates; otherwise, the top
// element of the stack is returned.

Type top() const;

//Function to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.

void pop();

//Default constructor
//Postcondition: stackTop = nullptr;

linkedStackType();

//Copy constructor

linkedStackType(const linkedStackType& otherStack);

~linkedStackType();
//Destructor
//Postcondition: All the elements of the stack are
// removed from the stack.

private:
nodeType *stackTop; //pointer to the stack

void copyStack(const linkedStackType& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is created and
// assigned to this stack.
};

Solutions

Expert Solution

C++ program to implement a stack template using single linked list


#include <iostream>

using namespace std;

template <class Type>

class n { //node class

public:

Type info;

n<Type>* next;

};

template <class Type>

class linkedStackType {

private:

n<Type>* top;

public:

linkedStackType () {

top = nullptr;

}

bool isEmptyStack() {

if (top == nullptr)

return false;

else

return true;

}

Type TopEle() {

if (top)

return top->info;

else

cout << "Stack empty" << endl;

}

void push(const Type & value) {

if (top == nullptr) {

top = new n<Type>;

top->next = nullptr;

top->info = value;

}

else {

n<Type>* temp = new n<Type>;

temp->info = value;

temp->next = top;

top = temp;

}

}

void pop() {

if (top == nullptr) {

cout << "Stack empty" << endl;

return;

}

else {

cout << top->info << " is popped out" << endl;

n<Type>* temp = top;

top = top->next;

delete temp;

}

}

void display() {

n<Type>* temp = top;

while (temp != nullptr)

{

cout << temp->info << " ";

temp = temp->next;

}

}

};

int main()

{ int size=0;

linkedStackType<int> s;

while (1)

{

/* Menu */

cout <<"------------------------------------\n";

cout <<" STACK IMPLEMENTATION PROGRAM \n";

cout <<"------------------------------------\n";

cout <<"1. Push\n";

cout <<"2. Pop\n";

cout <<"3. Size\n";

cout <<"4. Print Stack\n";

cout <<"5. Exit\n";

cout <<"------------------------------------\n";

cout <<"Enter your choice: ";

int choice;

cin >> choice;

switch (choice) {

case(1):

cout << "Enter data to push into stack:";

int n;

cin >> n;

s.push(n);

size++;

break;

case(2):

s.pop();

size--;

break;

case(3):

cout<<"Stack size is "<<size;

break;

case(4):

cout<<"Elements are ";

s.display();

break;

case(5):

cout<<"Exiting from app.\n";

exit(0);

default:

cout << "Invalid choice" << endl;

break;

}

}

return 0;

}

Output

----------------------------------- STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 1 Enter data to push into stack:5 ------------------------------------ STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 1 Enter data to push into stack:8 ------------------------------------ STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 1 Enter data to push into stack:6 ------------------------------------ STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 4 Elements are 6 8 5 ------------------------------------ STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 3 Stack size is 3------------------------------------ STACK IMPLEMENTATION PROGRAM ------------------------------------ 1. Push 2. Pop 3. Size 4. Print Stack 5. Exit ------------------------------------ Enter your choice: 5 Exiting from app.

This is the output but when it copied to it,it not in correct representation.There will be proper spacing and newlines if we run it on a compiler.

if this code helps you,then please give positive rating,thankyou


Related Solutions

(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an...
(Please solve the question using C Language. Thanks). Write a function called is_perfect which takes an integer n and returns 1 if n is a perfect number, otherwise it will return 0. If the sum of a number’s proper divisors are equal to the number, than the number is called a perfect number. For example, 6 is a perfect number: 6=1+2+3.
Can anyone please explain this table for me please. I need it very badly. thanks The...
Can anyone please explain this table for me please. I need it very badly. thanks The 95% confidence intervals for the student using cannabis in the last 12 months is as follows: Point Estimate Upper Limit Lower Limit Male 0.27 0.05 Female 0.35 0.09 Total 0.28 0.10
professor instructions for the assignment: You can earn extra credit if you read an article or...
professor instructions for the assignment: You can earn extra credit if you read an article or news item about a project we discussed in class and write a 2 page reaction paper about it. Your paper must tell me about the project you read about, what makes it a sustainable or resilient project, what are the impacts, what did you like about the article, what did you not like, etc.. I would like to write about this new project that...
Can anyone use python to solve this problem: Part B: List of lists The aim of...
Can anyone use python to solve this problem: Part B: List of lists The aim of Part B is to learn to work with list of lists. You may want to open the file lab05b_prelim.py in the editor so that you can follow the task description. The first part of the file lab05b_prelim.py defines a variable called datasets. The variable datasets is a list of 4 lists. This part consists of 2 tasks. Task 1: We ask you to type...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks 1. A driver class with a main method, which creates instances of various objects (two of each subclass) and adds them as items to an ArrayList named "inventory". A foreach loop should loop through the ArrayList and call the use() method of each item. Define the ArrayList in a way that it only holds elements of the GameItem class and its subclasses. Make proper...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of Comparable objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value. (Analysis of Algorithms question) Determine the growth function and time complexity (in Big-Oh notation) of the...
Please use java language in an easy way and comment as much as you can! Thanks...
Please use java language in an easy way and comment as much as you can! Thanks (Write a code question) Write a generic method called "findMin" that takes an array of String objects as a parameter. (Use proper syntax, so that no type checking warnings are generated by the compiler.) The method should search the array and return the index of the smallest value.
There is no extra information needed to solve this question so please don’t ask for extra...
There is no extra information needed to solve this question so please don’t ask for extra information. Thanks
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite...
PLEASE WRITE IN C++ PROGRAM THANKS - QUEUES Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same /** * Queue implementation using linked list C style implementation ( no OOP). */ #include <cstdio> #include <cstdlib> #include <climits> #include <iostream> #define CAPACITY 100 // Queue max capacity using namespace std; /** Queue structure definition */ struct QueueType { int data; struct...
Can you please not solve this on excel. can you show clearly steps you took. Thanks...
Can you please not solve this on excel. can you show clearly steps you took. Thanks An office uses 1,000 photocopies per working day and there are 200 working days per year. Brand A copier costs $3,000 and will produce a total of one million copies before it wears out. Brand B’s copier costs $5,000 and will produce 2 million copies over its life. Maintenance and materials cost 3 cents pr copy with either machine, and neither machine will have...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT