Question

In: Computer Science

PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

PART A - STACKS

To complete this assignment:

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

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

/**
* Stack implementation using array in C/procedural language.
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
//#include <climits> // 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";
}

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

PART B - 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 QueueType * next;
};
/** Queue size */
unsigned int size = 0;


int enqueue(QueueType * &rear, QueueType * &front, int data);
int dequeue(QueueType * &front);
int getRear(QueueType * &rear);
int getFront(QueueType * &front);
void display(QueueType * front);
int isEmpty();
int isFull();
string prepMenu();


int main()
{
int option, data;

QueueType *rear, *front;

rear = NULL;
front = NULL;
string menu = prepMenu();
cout << menu << endl;
cin >> option;
while (option !=7)
{

switch (option)
{
case 1:
cout << "\nEnter data to enqueue (-99 to stop): ";
cin >> data;
while ( data != -99)
{
/// Enqueue function returns 1 on success
/// otherwise 0
if (enqueue(rear, front, data))
cout << "Element added to queue.";
else
cout << "Queue is full." << endl;
cout << "\nEnter data to enqueue (-99 to stop): ";
cin >> data;
}


break;

case 2:
data = dequeue(front);

/// on success dequeue returns element removed
/// otherwise returns INT_MIN
if (data == INT_MIN)
cout << "Queue is empty."<< endl;
else
cout << "Data => " << data << endl;

break;

case 3:

/// isEmpty() function returns 1 if queue is emtpy
/// otherwise returns 0
if (isEmpty())
cout << "Queue is empty."<< endl;
else
cout << "Queue size => "<< size << endl;

break;

case 4:
data = getRear(rear);

if (data == INT_MIN)
cout << "Queue is empty." << endl;
else
cout << "Rear => " << data << endl;

break;

case 5:

data = getFront(front);

if (data == INT_MIN)
cout <<"Queue is empty."<< endl;
else
cout <<"Front => " << data << endl;

break;

case 6:
display(front);
break;

default:
cout <<"Invalid choice, please input number between (0-5).\n";
break;
}

cout <<"\n\n";
cout << menu<< endl;
cin >> option;
}
}

/**
* Enqueues/Insert an element at the rear of a queue.
* Function returns 1 on success otherwise returns 0.
*/
int enqueue(QueueType * &rear, QueueType * &front, int data)
{
QueueType * newNode = NULL;

/// Check queue out of capacity error
if (isFull())
{
return 0;
}

/// Create a new node of queue type
newNode = new QueueType;

/// Assign data to new node
newNode->data = data;

/// Initially new node does not point anything
newNode->next = NULL;

/// Link new node with existing last node
if ( (rear) )
{
rear->next = newNode;
}


/// Make sure newly created node is at rear
rear = newNode;

/// Link first node to front if its NULL
if ( !( front) )
{
front = rear;
}

/// Increment quque size
size++;

return 1;
}


/**
* Dequeues/Removes an element from front of the queue.
* It returns the element on success otherwise returns
* INT_MIN as error code.
*/
int dequeue(QueueType * &front)
{
QueueType *toDequque = NULL;
int data = INT_MIN;

// Queue empty error
if (isEmpty())
{
return INT_MIN;
}

/// Get element and data to dequeue
toDequque = front;
data = toDequque->data;

/// Move front ahead
front = (front)->next;

/// Decrement size
size--;

/// Clear dequeued element from memory
free(toDequque);

return data;
}


/**
* Gets, element at rear of the queue. It returns the element
* at rear of the queue on success otherwise return INT_MIN as
* error code.
*/
int getRear(QueueType * & rear)
{
// Return INT_MIN if queue is empty otherwise rear.
return (isEmpty())
? INT_MIN
: rear->data;
}


/**
* Gets, element at front of the queue. It returns the element
* at front of the queue on success otherwise return INT_MIN as
* error code.
*/
int getFront(QueueType * &front)
{
// Return INT_MIN if queue is empty otherwise front.
return (isEmpty())
? INT_MIN
: front->data;
}


/**
* Checks, if queue is empty or not.
*/
int isEmpty()
{
return (size <= 0);
}


/**
* Checks, if queue is within the maximum queue capacity.
*/
int isFull()
{
return (size > CAPACITY);
}
string prepMenu()
{

string menu = "";

menu+= " \n-------------------------------------------------------------------\n";
menu+= "1.Enqueue 2.Dequeue 3.Size 4.Get Rear 5.Get Front 6.Display 7.Exit\n";
menu+= "----------------------------------------------------------------------\n";
menu+= "Select an option: ";
return menu;
}
void display(QueueType * front)
{
for ( QueueType *t = front; t !=NULL; t = t->next)
cout <<t->data << " ";
cout << endl << endl;
}

Solutions

Expert Solution

Stack implementation using Linked List

Source Code:


#include <iostream>

using namespace std;
struct Node
{
int data;
struct Node *next;
  
};

struct Node* top = NULL;
struct Node* head;
//struct Node* display;

void pushOperation(int x){
struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->data = x;
newnode->next = top;
top = newnode;
}

void popOperation(){
if(top == NULL)cout<<"Stack UnderFlow"<<endl;
else{
cout<<"Element "<<top->data<<" is popped"<<endl;
top = top->next;
}
}

void display() {
struct Node* ptr;
if(top==NULL)
cout<<"stack is empty";
else {
ptr = top;
cout<<"Stack elements are: ";
while (ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
cout<<endl;
}

int main(){
int choice,value;
cout<<"1. Push Operation\n2.Pop Operation\n";
cout<<"3.Display\n4.Exit\n";
do{
cout<<"Enter Your Choice:";
cin>>choice;
switch(choice){
case 1:
cout<<"\nEnter value to be pushed:";
cin>>value;
pushOperation(value);
break;
case 2:
popOperation();
break;
case 3:
display();
break;
case 4:
cout<<"\nEXIT"<<endl;
default:
cout<<"Invalid Choice";
}
}while(choice!=4);
return 0;
}

OUTPUT:

We try our best But in given time limit we will able to solve one part.

If you have any error or doubt then please explain in comment section

Click on Thumbs up if you are satisfy.

Thankyou


Related Solutions

USING C++ ONLY. Please study the code posted below. the goal is to rewrite the code...
USING C++ ONLY. Please study the code posted below. the goal is to 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 QueueType *...
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...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that....
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that. (Don't edit main.c or challenge.h, only edit challenge.c) The instructions are in the comments. Hint: the_person is declared in main, so you need to define it in challenge.c using extern challenge.c #include "challenge.h" //return: struct //param: (struct person p1, struct person p2) //TODO: create a function that returns the person who has a higher GPA. // 1. if GPAs are equal, then return the...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code...
This is a programming assignment!!! Start with the Assignment3.java source code posted to Canvas. This code outputs the programmer’s name , prompts the user to enter two numbers (integers) and outputs their sum. Note: When you run the program, it expects you to enter two integer values (a counting or whole number like 1, 216, -35, or 0) Make the following changes/additions to the code: On line 11, change the String myName from “your full name goes here!!!” to your...
QUESTIONS POSTED BELOW, BASED ON THIS CASE STUDY: "Case Studies for Part III The Four Zones...
QUESTIONS POSTED BELOW, BASED ON THIS CASE STUDY: "Case Studies for Part III The Four Zones of Social Media: Case Study 5—Native Advertising: Novel or Deceptive? Jennifer Zarzosa, Henderson State University and Sarah Fischbach, California Lutheran University Lisa attentively pays attention to the posts her network of friends and family have posted today on Facebook. After all, this is the best way of getting her news and connecting with what’s going on in the world. In fact, Lisa usually only...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in...
Skills needed to complete this assignment: linked lists, stacks. Postfix notation, is a mathematical notation in which operators follow their operands; for instance, to add 3 and 4, one would write 3 4 + rather than 3 + 4 (infix notation). If there are multiple operations, operators are given immediately after their second operands; so, the expression written 3 − 4 + 5 in conventional notation would be written 3 4 − 5 + in postfix notation: 4 is first...
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
In this paper, please discuss the following case study. For you to complete this assignment you...
In this paper, please discuss the following case study. For you to complete this assignment you must: Explain your approach to the problem. Support your approach with references, and execute your approach. Provide an answer to the case study’s questions with a recommendation. This case continues following the new project of the WePROMOTE Company, that you and your partner own. WePROMOTE is in the promotional materials business. The project being considered is to manufacture a very unique case for smartphones....
I have posted this earlier, but it seems that the code part was cutoff, therefore, this...
I have posted this earlier, but it seems that the code part was cutoff, therefore, this is re post with full code. In the program the "case: 'C'" doesn't seem to function correctly, We were told to write a book tracking program, and it takes inputs and once all the book info such as ISBN, date, author, course number, etc are put in then input, then we can print them out, by using the print functions that are defined in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT