Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

WORKSHEETS

in
DATA STRUCTURES AND ALGORITHM

STACKS
TOPIC

Prepared by:
Jonathan C. Ilao
Jasmin Marie Layag
BSIT II-F2
STRATEGY IN TEACHING: PEER EVALUATION

DIRECTION: In this Pair Activity, you are going to create an idea or understanding
of the terms/ on the problem scenario below. Then you have to peek other 1 pair
from your classmates and ask them to listen and evaluate your reporting (about the
problem scenario below). Let them write/send a note to you regarding your
reporting. Take a screenshot/paste their comments/names in your answer sheet
then submit it to your Instructor.

NOTE: Don’t forget to take a screenshot of the collaboration together with your
Report.

Problem Scenario: Together with your pair, you are going to make a report and
choose a topic below.
TOPIC 1: What is STACK?
What are the two primary Operation in STACK?
What is peek(), isFull(), isEmpty()?

TOPIC 2:
Infix Notation
Prefix Notation
Postfix Notation

TOPIC 3:
Parsing Expression
Precedence
Associativity
What is STACK?

A stack is an Abstract Data Type (ADT), commonly used in most programming


languages. It is named stack as it behaves like a real-world stack, for example, a
deck of cards or a pile of plates, etc.

What are the two primary Operations in STACK?


two primary operations:
1. push() − Pushing (storing) an element on the stack.
2. pop() − Removing (accessing) an element from the stack.

What is LIFO?
LIFO stands for Last-in-first-out.

• peek() − get the top data element of the stack, without removing it.
• isFull() − check if stack is full.
• isEmpty() − check if stack is empty.

6. Infix Notation – Operators are written between the operands. This is the
standard way of writing expressions. For example, A * (B + C) / D

• PostfixNotation/Reverse Polish Notation – Operators are written after the


operands, hence the name. For instance, A B C + * D /
• Prefix Notation/Polish Notation – Operators are written before the
operands. / * A + B C D is the prefix notation equivalent of the
aforementioned postfix notation example.
Parsing Expression

Expression Parsing in Data Structure means the evaluation of arithmetic and


logical expressions.

Precedence

When an operand is in between two different operators, which operator will


take the operand first, is decided by the precedence of an operator over
others.

For example:

a + b *c -> a +(b*c)

Associativity

Associativity describes the rule where operators with the same precedence
appear in an expression. For example:

in expression a + b – c
STRATEGY IN TEACHING: MOTIVATED LEARNING

A. Multiple Choice
Direction : Choose the letter of the correct answer.
B. Enumeration
Direction : Answer the following question and explain briefly

1.Stack is a data structure that follows order.


A. FIFO
B. LIFO
C. LILO
D. FILO
2.Queue is a data structure that follows order.
A. FIFO
B. LIFO
C. LILO
D. FILO
3.Identify the error in following code:
➢ List 1 = { 10,20,30,40
List 1 [4] = 100
➢ Name = ”Michael Jackson”
Name[2] = ”k”
4.Write down the status of stack after each operation:
➢ Push 70
➢ Push 70
➢ Pop an item from Stack
➢ Peek the Stack
5. Identify the Data Structure ( Stack/Queue) used for following operations in
computer?
➢ Function calling
➢ Printer spooling
➢ Undo operation
➢ Solving expression
➢ Keyboard buffering

• Explain the postfix expression.


• What is a queue? How is it different from a stack?
• How can you write a C program to insert a node in a circular singly list
at the beginning?
• Based on your understanding what is Infix, Prefix, and Postfix notations?
• Define stack and give some of its important application
WORKSHEET NO. 1 (MOTIVATED LEARNING :ANSWER)

1. B. LIFO
2. B. FIFO
3. Index 4 is out of range,because List 1 contain 4 items(Index 0 to 3 Only).
String is immutable type so we can’t change any character of string in place.
4. [10,20,30,40,70]
[10,20,30,40,70,100]
[10,20,30,40,70]
70
5. Stack
Queue
Stack
Stack
Queue
6. In a postfix expression, the operator is fixed after the operands. Some examples
are:
➢ B++ (i.e. B+B)
➢ AB+ (i.e. A+B)
➢ ABC*+ (i.e. A+B*C)
➢ AB*CD*+ (i.e. A*B + C*D)
7. A queue is a form of linear structure that follows the FIFO (First In First Out)
approach for accessing elements. Dequeue, enqueue, front, and rear are basic
operations on a queue. Like a stack, a queue can be implemented using arrays and
linked lists.
8.In a circular LinkedList, the last pointer points to the head (first node). For
this, we use an external pointer that points to the last node, and the last next
points to the first node. We take the last node pointer because it saves us from
traversing the entire list while inserting a node in the beginning or end.

6. Infix Notation – Operators are written between the operands. This is the
standard way of writing expressions. For example, A * (B + C) / D

• PostfixNotation/Reverse Polish Notation – Operators are written after


the operands, hence the name. For instance, A B C + * D /
• Prefix Notation/Polish Notation – Operators are written before the
operands. / * A + B C D is the prefix notation equivalent of the
aforementioned postfix notation example

7. Stack is a linear data structure that follows either LIFO (Last In First Out) or
FILO (First In Last Out) approach for accessing elements. Push, pop, and peek
are the basic operations of a stack.
WORKSHEETS
in
DATA STRUCTURES AND
ALGORITHM

ARRAY
TOPIC

Prepared by:
Jonathan C. Ilao
Jasmin Marie Layag
BSIT IIF2
WORKSHEET NO. 2

STRATEGY IN TEACHING: LABORATORY EXERCISES

Direction : Create a simple program with class and method. .Do not forget the
sample output (witten or coded)

Note: Create a Class called Bank using array implementation.

Note :

 Create a class called Course with the following attributes: Course


Code, Course Description, Number of Units, and Amount per Unit.
 Use the constructor to collect the course information (Subject Code,
Subject Description, Number of Units, and Amount per Unit).
 Create a method that will compute Total Tuition Fee of the course.
 Create a method that will display the result.

Note : ( Class and Object with Encapsulation )

1. Create a class called Product with the following attributes: Product


Code, Product Description, Quantity, Unit Price.
2. Use accessor methods (getter and setter) to collect, and retrieve the
product information (Product Code, Product Description, Quantity, Unit
Price).
3. Create a method that will compute Total Amount of the product.
4. Display the result in main program.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Worksheet1
{
class bank
{
private double balance = 100000;

public double bal


{
get { return balance;}
set { balance = value; }
}
}
class fuctions
{
bank i = new bank();
string name;
int account;
double withdraw, dep,tobal;

public void fun1()


{
Console.WriteLine("Enter Name of the depositor :");
name = Console.ReadLine();
Console.WriteLine("Enter Account Number :");
account = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Deposit Amount :");
dep = Convert.ToDouble(Console.ReadLine());
tobal = i.bal + dep;

Console.WriteLine("------------------------------\n");
Console.WriteLine("Name of the depositor : " + name);
Console.WriteLine("Account Number: " + account);
Console.WriteLine("Total Balance amount in the account : " +tobal);

}
public void fun2()
{
Console.WriteLine("Enter Account Name :");
name = Console.ReadLine();
Console.WriteLine("Enter Account Number :");
account = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Withdraw Amount :");
withdraw = Convert.ToDouble(Console.ReadLine());
if (withdraw <= i.bal)
{
tobal = i.bal - withdraw;
Console.WriteLine("------------------------------\n");
Console.WriteLine("Account Name : " + name);
Console.WriteLine("Account Number: " + account);
Console.WriteLine("After Withdraw Amount balnace is : " + tobal);
}
else
Console.WriteLine("\n\nWithdraw Ammount does not Exist your Account.");
}

}
class Program
{
static void Main(string[] args)
{
char agn;
do
{
fuctions k = new fuctions();
int num;
Console.WriteLine("Please Select Any Function.");
Console.WriteLine("\nPress 1 for Deposit an Amount. \nPress 2 for Withdraw an
Amount.");
num = Convert.ToInt32(Console.ReadLine());
switch (num)
{
case 1:
k.fun1();
break;
case 2:
k.fun2();
break;
default:
Console.WriteLine("Invalid Selection!!!");
break;
}
Console.WriteLine("\nDo you want to continue this program? (y/n)");
agn =Convert.ToChar(Console.ReadLine());

} while (agn == 'y');

Console.ReadKey();
}
}
}

OUTPUT:
WORKSHEET NO. 2 (LABORATORY :ANSWER)
using System;

namespace Worksheet2

class Course

private string coursecode;

private string courseDes;

private int numUnit;

private double apUnit;

public Course(string code, string des, int num, double apu)

coursecode = code;

courseDes = des;

numUnit = num;

apUnit = apu;

public double Compute()

return numUnit * apUnit;

public void Display()

Console.WriteLine("\n---------Enrollment Receipt---------");

Console.WriteLine("Course Code: " + coursecode);

Console.WriteLine("Course Description: " + courseDes);

Console.WriteLine("Number of Units: " + numUnit);

Console.WriteLine("Amount per Unit: " + apUnit + " pesos");

Console.WriteLine("Total Tuition Fee: " + Compute() + " pesos");

}
class Program

static void Main()

Console.Write("Course Code: ");

string code = Console.ReadLine();

Console.Write("Course Description: ");

string des = Console.ReadLine();

Console.Write("Number of Units: ");

int num = Convert.ToInt32(Console.ReadLine());

Console.Write("Amount per Unit (in pesos): ");

double apu = Convert.ToDouble(Console.ReadLine());

Course student = new Course(code, des, num, apu);

student.Display();

Console.ReadKey();

OUTPUT
WORKSHEET NO. 2 (LABORATORY :ANSWER)
zusing Encapsulation;

using System;

namespace Encapsulation

class PRODUCT

public string Code;

public string Description;

public int Quantity;

public double UnitPrice;

public string ProductCode

get

return Code;

set

Code = value;

public string ProductDescription

get

return Description;

set

Description = value;

}
public int ProductQuantity

get

return Quantity;

set

Quantity = value;

public double ProductUnitPrice

get

return UnitPrice;

set

UnitPrice = value;

public double TotalAmount()

return this.Quantity * this.UnitPrice;

public void Display()

Console.WriteLine("============================================");

Console.WriteLine("================OFFICIAL RECEIPT============");

Console.WriteLine("Product Code: " + this.Code);

Console.WriteLine("Product Description: " + this.Description);


Console.WriteLine("Quantity: " + this.Quantity);

Console.WriteLine("Unit Price: {0:C}", this.UnitPrice);

Console.WriteLine("Total Amount: {0:C}", this.TotalAmount());

public static class Program

public static void Main()

PRODUCT prod = new PRODUCT();

Console.Write("Product Code: ");

prod.Code = Console.ReadLine();

Console.Write("Product Description: ");

prod.Description = Console.ReadLine();

Console.Write("Quantity: ");

prod.Quantity = Convert.ToInt32(Console.ReadLine());

Console.Write("Unit Price: ");

prod.UnitPrice = Convert.ToDouble(Console.ReadLine());

prod.Display();

Console.ReadKey();

OUTPUT :

You might also like