Hey, it is my assignment; I need someone helps to complete this code by C#. than
ID: 3883162 • Letter: H
Question
Hey,
it is my assignment; I need someone helps to complete this code by C#. thank you
Complete the attached MyLinkedStack.cs. (50 points)
Part1. Complete the MyListStack. There are four methods to be completed: Push(), Pop(), IsEmpty() and Size() .
Part2. Complete a method to evaluate postfix expressions using MyListStack. (Don't know what is postfix! Refer to my slides on Stacks in Course Documents.)
------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackExperiment
{
//we use TestStackProgram to test our program
class TestStackProgram
{
static void Main(string[] args)
{
MyListStack myStack = new MyListStack();
string testString = "78+2*5-";
int result = EvaluatePostFix(testString, myStack);
Console.WriteLine("The expression produces: "+ result);
Console.ReadKey();
}
//Part 2: Complete this method to evaluate Postfix Expressions using MyListStack
static int EvaluatePostFix(string testString, MyListStack stack)
{
}
}
//definition of each Node of Stack
public class Node
{
public int data;
public Node next;
public Node(int item)
{
data = item;
next = null;
}
}
//Part 1: Complete class MyListStack
public class MyListStack
{
//top element
private Node top = null;
// number of elements stored
private int numberOfNodes = 0;
//indicates whether no elements are stored
public bool IsEmpty()
{
}
//returns the number of elements stored
public int Size()
{
}
//inserts an element
public void Push(int item)
{
}
//removes and returns the last inserted element
public int Pop()
{
}
}
}
Explanation / Answer
Complete C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StackExperiment
{
//we use TestStackProgram to test our program
class TestStackProgram
{
static void Main(string[] args)
{
MyListStack myStack = new MyListStack();
Console.WriteLine("IsEmpty:"+myStack.IsEmpty());
Console.WriteLine("Size:"+myStack.Size());
myStack.Push(10);
myStack.Push(20);
myStack.Push(30);
myStack.Push(40);
Console.WriteLine("Size:"+myStack.Size());
Console.WriteLine("Pop:"+myStack.Pop());
Console.WriteLine("Pop:"+myStack.Pop());
Console.WriteLine("Size:"+myStack.Size());
string testString = "78+2*5-";
int result = EvaluatePostFix(testString, myStack);
Console.WriteLine("The expression produces: "+ result);
Console.ReadKey();
}
//Part 2: Complete this method to evaluate Postfix Expressions using MyListStack
static int EvaluatePostFix(string testString, MyListStack stack)
{
int num1=0;
int num2=0;
foreach (char c in testString.ToCharArray()){
switch(c){
case '+':
num2 = stack.Pop();
num1 = stack.Pop();
stack.Push(num1+num2);
break;
case '-':
num2 = stack.Pop();
num1 = stack.Pop();
stack.Push(num1-num2);
break;
case '*':
num2 = stack.Pop();
num1 = stack.Pop();
stack.Push(num1*num2);
break;
case '/':
num2 = stack.Pop();
num1 = stack.Pop();
stack.Push(num1/num2);
break;
default:
int item=int.Parse(""+c);
stack.Push(item);
break;
}
}
return stack.Pop();
}
}
//definition of each Node of Stack
public class Node
{
public int data;
public Node next;
public Node(int item)
{
data = item;
next = null;
}
}
//Part 1: Complete class MyListStack
public class MyListStack
{
//top element
private Node top = null;
// number of elements stored
private int numberOfNodes = 0;
//indicates whether no elements are stored
public bool IsEmpty()
{
if(numberOfNodes==0){
return true;
}else{
return false;
}
}
//returns the number of elements stored
public int Size()
{
return numberOfNodes;
}
//inserts an element
public void Push(int item)
{
if(top == null){
top=new Node(item);
}else{
Node newNode=new Node(item);
newNode.next=top;
top=newNode;
}
numberOfNodes++;
}
//removes and returns the last inserted element
public int Pop()
{
if(IsEmpty()){
Console.WriteLine("Stack is empty!");
}
int item=top.data;
top=top.next;
numberOfNodes--;
return item;
}
}
}
OUTPUT: