Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Data Structure Lab Report Week 3

Data structures

Uploaded by

mianabdullah8856
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Data Structure Lab Report Week 3

Data structures

Uploaded by

mianabdullah8856
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Name: Mudassar Ali

Roll no: 2831

Course: Data Structure

Class: BSCS - 3A (Afternoon)

Topic: Stack and Conversion of Boolean Expression

Due Date: ____________

Submission Date: ______________

WEEK 03
Previous Class Discussion:

In Previous class we have discussion on following points:


 Firstly we execute binary search algorithm which we have
discussed in previous class.
 After that we start a new topic which is sorting algorithms in which
we sorting random data in ascending or descending order with the
help of different methods or algorithms.
 There are three types of sorting algorithms: 1.Bubble sorting
2. Selection sorting 3. Insertion sorting
 Bubble Sort is the simplest sorting algorithm that works by
repeatedly swapping the adjacent elements if they are in the wrong
order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
 Selection sort is a simple and efficient sorting algorithm that
works by repeatedly selecting the smallest (or largest) element
from the unsorted portion of the list and moving it to the sorted
portion of the list
 Insertion sort is a simple sorting algorithm that works similar to
the way you sort playing cards in your hands. The array is virtually
split into a sorted and an unsorted part. Values from the unsorted
part are picked and placed at the correct position in the sorted part.
 Insertion sort is an efficient sorting algorithm than selection and
bubble sort. The average case time complexity of the insertion sort
is closer to the worst-case time complexity.
Current Class Discussion:

In current class we have discussion on following points:


 Firstly we talk about stack that Stack is a linear data structure that
follows a particular order in which the operations are performed.
 The order may be LIFO(Last In First Out) or FILO(First In Last
Out). LIFO implies that the element that is inserted last, comes out
first and FILO implies that the element that is inserted first, comes
out last.
 In stack we use functions to insert, delete, and display data.
 Then we take a look on code of stack that how to create stack and
we came to know that to insert data in stack we use PUSH and to
delete data we use POP.
 Also we can display the stack and erase the stack with Null.
 After that we start a new topic which is conversion of Boolean
Expressions.
 In this topic we learn three types of Boolean expression: 1. Infix 2.
Prefix 3. Postfix
 Boolean expression is consist of operators (+,-,*, ^, /) and operands
(A, B, C).
 In infix the operator is between two operands like A+B.
 In Prefix the operator is left side of operands like +AB.
 In postfix the operator is on right side of operands like AB+.
 We learn some rules about conversion of Boolean expression from
infix form to prefix and postfix form.
Lab Task
Q: Create a stack and use menu for insert, delete, display,
null and exit functions.

Header File Code:


#include<iostream>
using namespace std;
class IntStack
{
private:
int *stackarray;
int stacksize;
int top;
public:
IntStack(int size){
stackarray = new int[size];
stacksize = size;
top = -1;
}
~IntStack();
bool isfull();
bool isempty();
void push(int val);
void pop();
void makenull();
void display();
};
bool IntStack :: isfull(){
if(top == stacksize-1){
return true;
}
else{
return false;
}
}
bool IntStack :: isempty(){
if(top == -1){
return true;
}
else{
return false;
}
}
IntStack :: ~IntStack(){
delete [] stackarray;
}
void IntStack :: makenull(){
top = -1;
}
void IntStack :: push(int val){
if(isfull()){
cout<<"Stack is overflow";
}
else{
top++;
stackarray[top]=val;
}
}
void IntStack :: pop(){
if(isempty()){
cout<<"Stack is underflow";
}
else{
top--;
}
}
void IntStack :: display(){
cout<<"Values in Stack are :"<<endl;
for(int i=top; i>=0; i--){
cout<<stackarray[i]<<"\t"<<endl;
}
}
Source File Code:
#include<iostream>
#include "stack_header.h";
using namespace std;
int main(){
int size;
IntStack obj(size);
int choice;
cout<<"Select an operation to perform:"<<endl;
cout<<"1. Enter Number"<<endl;
cout<<"2. Enter Multiple Numbers"<<endl;
cout<<"3. Display"<<endl;
cout<<"4. Delete Element"<<endl;
cout<<"5. Exit\n"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch (choice){
case 1:
int n;
cin>>n;
obj.push(n);
cout<<"Number is entered"<<endl;
break;
case 2:
int numbers;
cout<<"Enter the number of elements to push: ";
cin>>numbers;
for(int i=0; i<numbers; i++){
int n;
cout<<"Enter element "<<i+1<<": ";
cin>>n;
obj.push(n); }
break;
case 3:
obj.display();
break;
case 4:
obj.pop();
break;
case 5:
cout<<"Exit program";
break;
default:
cout<<"Do not Valid";
return 0;
}}

You might also like