In: Computer Science
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.
};
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