These are different problems, please do them seperately 1) Write a program to im
ID: 670683 • Letter: T
Question
These are different problems, please do them seperately
1) Write a program to implement a Stack of characters using Singly Linked List. Name your Stack class charSLLStack. To do so, create a class charSLLNode (similar to intSLLNode presented in slide 13 in LinkedList slides but replace int data type with char data type) and use the nodes to store your data in the stack. Your Stack class needs to have the following functions: (a) Constructor to initialize the Stack (b) void Push (char d); (c) char pop(); (d) bool isEmpty();
2) Use your program in Problem 1 and write a function TransferStack to transfer a stack as is shown in the following picture to another stack while keeping the order of items the same. In your function you need to only use stacks and stack operations
3) Write a program to implement a Queue using an array of int type. Your Queue class needs to have the following functions: (a) Constructor to initialize the Queue (b) void Enqueue (int d); (c) int Dequeue(); (d) bool isEmpty();
Explanation / Answer
Implemented stack programme. please post the diagra for TransferSTack. Pls create seperate request for 3rd program.
#ifndef LINKEDLIST_NODE_H
#define LINKEDLIST_NODE_H
#include<iostream>
class CharSLLNode {
public:
CharSLLNode ();
CharSLLNode (char);
char data;
CharSLLNode * next;
};
#endif
#include "CharSLLNode.h"
using namespace std;
CharSLLNode ::CharSLLNode (){
data = {};
next = NULL;
}
CharSLLNode ::CharSLLNode (char new_data){
data = new_data;
next = NULL;
}
/*
* charSLLStack.h
*
* Created on: 29-Sep-2015
* Author: Ravi
*/
#ifndef CHARSLLSTACK_H_
#define CHARSLLSTACK_H_
using namespace std;
#include "CharSLLNode.h"
class CharSLLStack {
public:
CharSLLStack();
void Push (char d);
char pop();
bool isEmpty();
virtual ~CharSLLStack();
void PrintList();
private:
CharSLLNode * head;
};
#endif /* CHARSLLSTACK_H_ */
/*
* charSLLStack.cpp
*
* Created on: 29-Sep-2015
* Author: Ravi
*/
#include "CharSLLStack.h"
CharSLLStack::CharSLLStack() {
// TODO Auto-generated constructor stub
head = NULL;
}
CharSLLStack::~CharSLLStack() {
// TODO Auto-generated destructor stub
}
void CharSLLStack:: Push (char d) {
if(head == NULL){
head = new CharSLLNode(d);
}
else{
CharSLLNode* temp = new CharSLLNode(head->data);
temp->next = head->next;
head->data = d;
head->next = temp;
}
}
char CharSLLStack:: pop() {
char d = ' ';
if(isEmpty())
cout << "Stack is empty" <<endl;
else {
// cout << "Stack is not empty" <<endl;
d = head->data;
head = head->next;
//TODO: Clean the first node from memor y
return d;
}
return d;
}
bool CharSLLStack:: isEmpty() {
bool empty = true;
CharSLLNode* trav = head;
if(trav != NULL){
empty = false;
}
return empty;
}
void CharSLLStack::PrintList(){
CharSLLNode* trav = head;
while(trav != NULL){
cout << trav->data << " ";
trav = trav->next;
}
cout <<endl;
}
int main () {
CharSLLStack stack;
cout << " Is stack empty: " << stack.isEmpty() << endl;
stack.Push('a');
stack.Push('b');
stack.Push('c');
stack.Push('d');
stack.PrintList();
cout<< "Removed Element: "<< stack.pop()<<endl;
stack.PrintList();
cout << " Is stack empty: " << stack.isEmpty() << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.