مواضيع المحاضرة: Stack .. Structure .. Queue .. MATLAB Operators .. Flow Control
background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Stack

  

A stack is a list of data that follows 2 rules: 
1. Data can only be accessed by removing the top 

(pop)

2. A new data element can only be added to the top of the stack 

(push)

This means that all data in the middle of the stack is hidden. 
Note that the last item pushed onto a stack is always the first that will be popped 
from the stack. This property is called 

last in, first out

or 

LIFO.

 

In a stack data structure, all insertions and deletions of entries are made at one 
end, called the 

top 

of the stack. 

stack 

is particularly useful in applications involving reversing 

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

Figure: Stack Operation.

 

 

 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Program #7: write a program to implement enter 5-element and print the reverse 
using stack. 
 
#include<iostream> 
#include<conio.h> 
#define size 5 
int top=-1; 
int a[size]; 
 
///EMPTY FUNCTION 
int empty() 

 

if(top==-1) 

 

 

return(1); 

 

else 

 

 

return(0); 


 
// FULL FUNCTION 
int full() 

 

if(top==size-1) 

 

 

return (1); 

 

else 

 

 

return(0); 


 
// PUSH FUNCTION 
void push(int k) 

if(full()) 

 

 

cout<<" FULL STACK"; 

else 

 

 

top++; 
a[top]=k; 


 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

//  POP FUNCTION 
int pop() 

if(empty()) 

 

 

cout<<"EMPTE STACK"; 

else 

int c=a[top]; 
top--; 
return c; 


 
// For Main 
void main() 

int i,k,p; 
for(i=0;i<size;i++) 

cout << "Enter a Number to push: "; 
cin>>k; 

 

 

push(k); 


for(i=0;i<size;i++) 

 

 

p=pop(); 

 

 

cout<<p<<endl; 


getch(); 

 
 
 
 
 
 
 
 
 
 
 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Program #8: write a program to implement a stack of 5-elements with  the  choice 
to push or pop the stack. 
#include<iostream> 
#include<conio.h> 
#define size 5 
int top=-1; 
int a[size]; 
///EMPTY FUNCTION 
int empty() 

 

if(top==-1) 

 

 

return(1); 

 

else 

 

 

return(0); 


 
//  FULL FUNCTION 
int full() 

 

if(top==size-1) 

 

 

return (1); 

 

else 

 

 

return(0); 


 
// PUSH FUNCTION 
void push(int k) 

if(full()) 
 

cout<<"FULL STACK"; 

else 

 

top++; 

   a[top]=k; 


 
//  POP FUNCTION 
int pop() 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 


if(empty()) 
 

cout<<"EMPTEY STACK"; 

else 

int c=a[top]; 
top--; 
return c; 


// For Main 
void main() 

int i,k,p,d=1; 
cout<<"1:Push\n2:Pop\n3:Exit\n"; 
while(d<3) 

   cout<<"\nChoose a number: "; 
 

cin>>d; 

 

if(d==1) 

 

 

 

cout << "\nEnter a Number to push: (TOP="<<top<<") "; 

 

 

cin>>k; 

 

 

push(k); 

 

 

if(d==2) 

 

 

 

p=pop(); 

    

cout<<"\nPop result  is: (TOP="<<top<<") "<<p<<endl; 

 

   if(d<1 || d>2) 
   break; 

getch(); 

 

Homework5: 
Write a program to implement 5-element stack to read and print letters with the 
choice to push and pop the stack.  

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Structure 

 

The struct is a very versatile data type. It is a collection of components of any 
data type in which the components are given names. The components of a 
record are called fields  or members. Each field has its own field identifier and 
data type. 
 C++ has its own vocabulary in relation to the general concept of a record. 
Structs are called records. 
 

Arrays are one of the most widely used data structures in programming 
languages. One downfall of using such a data type is that one must use 
homogeneous data types; an array can only hold multiple items of the same 
type. Structures overcome this problem by allowing the programmer to have an 
unlimited number of items of different data types! Objects contained within the 
structure are referred to as a member. 

 
To define a struct, use the keyword struct followed by the name of the structure. 
Then use curly braces followed by variable types and names:  
struct StructName  
{  
type1 var1;  
type2 var 2;  
…  
type3 var N;  
};  
Note the need for a semicolon at the end of the right curly brace! 
 
The above defines a structure named “StructName”. You can use StructName 
like it is a new data type. For example, the following will declare a variable to be 
of type “StructName”:  
StructName myVar; 
 To access the members (variables) within the structure, use the variable name 
followed by a dot and then the variable within the struct. This is called the 
member selector:  

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

myVar.var1; 
Here is an example structure:  
struct Recording  
{  
string title;  
string artist;  
float cost;  
int quantity;  
};  
Recording song; 
 
Recording is a pattern for a group of four variables. song is a struct variable with 
four members: title, artist, cost, and quantity are the member names. The 
accessing expression for members of a struct variable is the struct variable 
identifier followed by the member name with a period in between.  
song.title is a string variable.  
song.artist is a string variable.  
song.cost is a float variable.  
song.quantity is an int variable. 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Program #9: write a program using structure to make a database for 3 students 
getting their name, age and year. 
#include<iostream> 
#include<conio> 
#define size 3 
struct student 

 

char name[10]; 

 

int  age; 

 

int  year; 

} ; 
 
void main() 

 

int i; 
student a[size]; 

 

cout<<"\t\t\tEnter the information of students\n\n"<<endl; 

 

for(i=1;i<=size;i++) 

 

 

 

cout<<"\nEnter the information of student no."<<i; 

 

 

cout<<"\nname: "; 

 

 

cin>>a[i].name; 

 

 

cout<<"age : "; 

 

 

cin>>a[i].age; 

 

 

cout<<"year: "; 

 

 

cin>>a[i].year; 

 

 

cout<<"\n\nYour students are: \n"; 

 

for(i=1;i<=size;i++) 

 

 

 

cout<<"\n\nStudent no."<<i<<":\nname: "<<a[i].name<<"\nage : 

"<<a[i].age<<"\nyear: "<<a[i].year; 
 

 

getch(); 

 
 

Homework6: 
Write a program using structure to create a database of 4 workers with  their 
information that includes; name, year of birth and monthly salary.  

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Queue  

 

Queue is also an abstract data type or a linear data structure, in which the first 
element is inserted from one end called 

REAR

 (also called tail), and the deletion 

of existing element takes place from the other end called as 

FRONT

 (also called 

head).  
This makes queue as First In First Out (FIFO) data structure, which means that 
element inserted first will also be removed first. 
The process to add an  element into queue is called Enqueue and the  process of 
removal of an  element from queue is called Dequeue

 
 
 
 
 
 
 
 
 
 
 
 
 
 

 
The easiest way of implementing a queue is by using an Array. Initially the front 
(head)  and the rear  (tail)  of the queue points at the first index of the array 
(starting the index of array from 0). As we add elements to the queue, the rear 
keeps on moving ahead, always pointing to the position where the next element 
will be inserted, while the front remains at the first index. 
 

 
 
 
 
 
 
 
 
 
 
 

 

 

Queue operation 

 

 Enqueue       Dequeue 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

Program #10: write a program to implement the queue data structure with  5 
elements. 
#include<iostream> 
#include<conio.h> 
#define size 5 
int front = -1; 
int rear = -1; 
int a[size]; 
// Empty check function 
int isempty() 

if(front == rear) 

 

 

return 1; 

else 

 

 

return 0; 


// Full check function 
int isfull() 

if(rear == size-1) 

 

 

return 1; 

else 

 

 

return 0; 


//  Enqeueu (adding element to the  queue) function 
void enqueue(int x) 

if(isfull()) 

 

 

cout<<"Queue is FULL !!!No insertion allowed further."; 

 

 

else 

a[rear] = x; 
rear++; 


 

10 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

//  Dequeue (removing element from the queue) function 
int dequeue() 

 

int c; 
if(isempty()) 

 

 

 

cout<<"  Queue is Empty !!! "; 

else 

c=a[front]; 
front++; 
return(c); 


// main body 
void main() 

 int x,y; 
cout <<”Enter the 5 elements of the queue: “; 
for(int i=0; i< 5 ; i++) 

 

 

cin >> x; 
enqueue(x); 


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

y=dequeue(); 
cout << y << "\t"; 


getch(); 

 
 
 

Homework7: 
Write a program to implement the queue with  6 elements with  the choice to 
enqueue or dequeue from the  main  body. 

 
 
 
 

11 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

MATLAB: Operators and Flow Control 

 

 Relational and Logical Operators 

MATLAB has a logical data type, with the possible values 1, representing 
true, and 0, representing false. Logicals are produced by relational and 
logical operators/functions and by the functions true and false: 
>> a = true  % a = 1 
>> b = false % b = 0 
>> c = 1       % c = 1 
>> whos 
Name    Size    Bytes         Class 
    A       1x1        1        logical array 
    B       1x1        1        logical array 
    c        1x1        8        double array 
As this example shows, logicals occupy one byte, rather than the eight 
bytes needed by a double. 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
>> A = [1 2; 3 4]; B = 2*ones(2); 
>> A == B 
ans =  0 1 
          0 0 
>> A > 2 
ans =  0 0 
          1 1 

 

Note that a single = denotes assignment and 
never a test for equality in MATLAB. 
Comparisons between scalars produce logical 
1 if the relation is true and logical 0 if it is false. 
Comparisons are also defined between 
matrices of the same dimension and between a 
matrix and a scalar, the result being a matrix of 
logicals in both cases. 
For matrix–matrix comparisons corresponding 
pairs of elements are compared, while for 
matrix–scalar comparisons the scalar is 
compared with each matrix element. For 
example: 

 

12 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Note that an array can be real in the mathematical sense, but not real as 
reported  by isreal. For isreal(A) is true if A has no imaginary part. 
Mathematically, A is real if every component has zero imaginary part. How 
a mathematically real A is  formed can determine whether it has an 
imaginary part or not in MATLAB. The distinction can be seen as follows: 
>> a = 1; 
>> b = complex(1,0); 
>> c = 1 + 0i; 
>> [a b c] 
ans = 1 1 1 
>> whos a b c 
Name    Size    Bytes       Class 
   A        1x1        8      double array 
   B        1x1       16     double array (complex) 
   C        1x1        8      double array 
>> [isreal(a), isreal(b), isreal(c)] 
ans = 1 0 1 
 
 
 
 
 

 

13 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

 
 
 
 
 
 
 
 
 
 
 
Like the relational operators, the &, |, and ~ operators produce matrices of 
logical 0s and 1s when one of the arguments is a matrix. When applied to a 
vector, the all function returns 1 if all the elements of the vector are nonzero 
and 0 otherwise. The any function is defined in the same way, with “any” 
replacing “all”. Examples: 
>> x = [-1 1 1]; y = [1 2 -3]; 
>> x>0 & y>0     % ans = 0 1 0 
>> x>0 | y>0      % ans = 1 1 1 
>> xor(x>0,y>0) % ans = 1 0 1 
>> any(x>0)       % ans = 1 
>> all(x>0)          % ans = 0 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

 

 

14 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

 Flow Control 

MATLAB has four flow control structures: the if statement, the for loop, the 
while loop, and the switch statement.  
 

  1- If statement 

The simplest form of the if statement is 
if expression 
statements 
end 
Where the statements are executed if the elements of expression are all 
nonzero. For example, this code swaps x and y if x is greater than y: 
if x > y 
temp = y; 
y = x; 
x = temp; 
end 
When an if statement is followed on its line by further statements, a comma 
is needed to separate the if from the next statement: 
if x > 0, x = sqrt(x); end 
Statements to be executed only if expression is false can be placed after 
else, as in the example 
e = exp(1); 
if x > y 
disp(’X is bigger’) 
else 
disp(’Y is bigger’) 
end 
 

  2- For loop 

The for loop is one of the most useful MATLAB constructs. The syntax is: 
for variable = expression 
statements 
end 
For example, the sum of the first 25 terms of the harmonic series 1/i is computed 
by 
>> s = 0; 
>> for i = 1:25, s = s + 1/i; end, s 
s = 
3.8160 

15 

 


background image

                     

Lecture2: Stack, Structure, Queue, MATLAB Logical Operators and Flow Control.  

 

                                                                                                 

2

 

Computer Algorithms  

& Programming

 

Mustafa Al-Qassab 

2015-2016 Kirkuk 

  3- While loop 

The while loop has the form: 
while expression 
statements 
end 
The statements are executed as long as expression is true. The following 
example approximates the smallest nonzero floating point number: 
>> x = 1; while x>0, xmin = x; x = x/2; end, xmin 
xmin = 4.9407e-324 
 

  4- Switch statement  

The final control structure is the switch statement. It consists of “switch 
expression” followed by a list of “case expression statements”, optionally 
ending with  “otherwise statements” and followed by end. The switch 
expression is evaluated and  the statements following the first matching 
case expression are executed. If  none  of the cases produces a match then 
the statements following otherwise are executed. 
The next example evaluates the p-norm of a vector x (i.e., norm(x,p)) for 
just three 
values of p: 
switch p 
case 1 
y = sum(abs(x)); 
case 2 
y = sqrt(x’*x); 
case inf 
y = max(abs(x)); 
otherwise 
error(’p must be 1, 2 or inf.’) 
end 

 

 
Homework8: 
Write a MATLAB program using m.file to find the summation of even numbers 
from 0 to 10 using: 

1- for loop. 
2- while loop. 

16 

 




رفعت المحاضرة من قبل: Rashad HopeMaker
المشاهدات: لقد قام 7 أعضاء و 170 زائراً بقراءة هذه المحاضرة








تسجيل دخول

أو
عبر الحساب الاعتيادي
الرجاء كتابة البريد الالكتروني بشكل صحيح
الرجاء كتابة كلمة المرور
لست عضواً في موقع محاضراتي؟
اضغط هنا للتسجيل