Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1: Write a program to implement a Stack of characters using Singly Link

ID: 670009 • Letter: E

Question

Exercise 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();

Exercise 4: Write a program to implement a Queue using an array of int type. (Look at the slides 4-7 in Linked List Slides and slides 8-14 of Queue Slides). 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

#include<iostream.h>

using namespace std;

int top = -1;

void push(int stack[], int x, int n)

{

if(top == n-1)

{

cout<<"Stack is full";

}

else

{

top = top+1;

stack[top] = x;

}

}

bool isEmpty()

{

if(top == -1)

return true;

else

return false;

}

void pop(int stack[], int n)

{

if(isEmpty())

{

cout<<"Stack is empty";

}

else

{

top = top -1;

}

}

int size()

{

return top+1;

}

int topElement()

{

return stack[top];

}

int main()

{

int stack[3];

push(stack,5,3);

cout<<"Current size of stack is"<<size()<<endl;

push(stack,10,3);

push(stack,24,3);

cout<<""Current size of stack is"<<size()<<endl;

push(stack,12,3);

cout<<"Current top element in stack is "<<topElement()<<endl;

for(int i=0;i<3;i++)

pop();

cout<<"Currnet size of stack is "<<size()<<endl;

pop();

}

This is basic program in stack like wise we can use characters also

Queue:

#include<iostream>

#include<cstdio>

using namespace std;

void enqueue(char queue[], char element, int& rear, int arraySize)

{

if(rear == arraySize)

printf("Overflow");

else

{

queue[rear]=element;

rear++;

}

}

void dequeue(char queue[], int& front, int rear)

{

if(front==rear)

printf("UnderFlow")

else

{

queue[front]=0;

front++;;

}

}

char Front(char queue[], int front)

{

return queue[front];

}

int main()

{

char queue[20]={'a','b','c','d'};

int front=0,rear=4;

int arraySize=20;

int N=3;

char ch;

for(int i=0;i<N;++i){

ch=Front(queue,front);

enqueue(queue, ch,rear,arraySize);

dequeue(queue, front, rear);

}

for(int i=front;i<=rear;++i)

priintf("%c",queue[i]);

printf(" ");

return 0;

}