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

Write 2 classes that implement a stack with push, pop and peek operations. One s

ID: 3739744 • Letter: W

Question

Write 2 classes that implement a stack with push, pop and peek operations. One should be called StackL and internally should store the data in a series of Nodes as a singly linked list. The other, StackO, can use a different data structure of your choice. Write 2 classes that implement a queue with enqueue and dequeue operatons. One should be called QueueL and internally should store the data in a C# linked list. The other, QueueO, can use a different data structure of your choice.

Write a console application to determine the time required to perform the push/pop or queue/dequeue operations on your stacks and queues. Make an instance of a C# stack and a C# queue. Get the time required to push 10,000,000 values onto each stack and then get the time required to pop 10,000,000 values from each stack. Repeat the process using the queue and dequeue operations with the queues.

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

struct StackL
{
int info;
struct StackL *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main()
{
int no, ch, e;

printf(" 1 - Push");
printf(" 2 - Pop");
printf(" 3 - Top");
printf(" 4 - Empty");
printf(" 5 - Exit");
printf(" 6 - Dipslay");
printf(" 7 - Stack Count");
printf(" 8 - Destroy stack");

create();

while (1)
{
printf(" Enter choice : ");
scanf("%d", &ch);

switch (ch)
{
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf(" Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create empty stack */
void create()
{
top = NULL;
}

/* Count stack elements */
void stack_count()
{
printf(" No. of elements in stack : %d", count);
}

/* Push data into stack */
void push(int data)
{
if (top == NULL)
{
top =(struct StackL *)malloc(1*sizeof(struct StackL));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct StackL *)malloc(1*sizeof(struct StackL));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

/* Display stack elements */
void display()
{
top1 = top;

if (top1 == NULL)
{
printf("Stack is empty");
return;
}

while (top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */
void pop()
{
top1 = top;

if (top1 == NULL)
{
printf(" Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf(" Popped value : %d", top->info);
free(top);
top = top1;
count--;
}


//This method will give you peek operations
/* Return top element */
int topelement()
{
return(top->info);
}

/* Check if stack is empty or not */
void empty()
{
if (top == NULL)
printf(" Stack is empty");
else
printf(" Stack is not empty with %d elements", count);
}

/* Destroy entire stack */
void destroy()
{
top1 = top;

while (top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf(" All stack elements destroyed");
count = 0;
}

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

Output

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

2nd way of push pop and peek operations

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class StackOperations
{
static void Main(string[] args)
{   
StackL st = new StackL();
while (true)
{
Console.Clear();
Console.WriteLine(" StackL MENU(size -- 10)");
Console.WriteLine("1. Add an element");
Console.WriteLine("2. See the Top element.");
Console.WriteLine("3. Remove top element.");
Console.WriteLine("4. Display StackL elements.");
Console.WriteLine("5. Exit");
Console.Write("Select your choice: ");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Enter an Element : ");
st.Push(Console.ReadLine());
break;

case 2: Console.WriteLine("Top element is: {0}", st.Peek());
break;

case 3: Console.WriteLine("Element removed: {0}", st.Pop());
break;

case 4: st.Display();
break;

case 5: System.Environment.Exit(1);
break;
}
Console.ReadKey();
}
}
}

interface StackLADT
{
Boolean isEmpty();
void Push(Object element);
Object Pop();
Object Peek();
void Display();
}
class StackL : StackADT
{
private int StackSize;
public int StackSizeSet
{
get { return StackSize; }
set { StackSize = value; }
}
public int top;
Object[] item;
public stack()
{
StackSizeSet = 10;
item = new Object[StackSizeSet];
top = -1;
}
public stack(int capacity)
{
StackSizeSet = capacity;
item = new Object[StackSizeSet];
top = -1;
}
public bool isEmpty()
{
if (top == -1) return true;

return false;
}
public void Push(object element)
{
if (top == (StackSize - 1))
{
Console.WriteLine("Stack is full!");
}

else
{

item[++top] = element;
Console.WriteLine("Item pushed successfully!");
}
}
public object Pop()
{
if (isEmpty())
{
Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{

return item[top--];
}
}
public object Peek()
{
if (isEmpty())
{

Console.WriteLine("Stack is empty!");
return "No elements";
}
else
{
return item[top];
}
}


public void Display()
{
for (int i = top; i > -1; i--)
{

Console.WriteLine("Item {0}: {1}", (i + 1), item[i]);
}
}
}
}

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

Ouput:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote