
Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Circular Queue
This is a particular kind of queue where new items are added to the rear of
the queue as items are read off or removed from the front of the queue. So
there is constant stream of data flowing into and out of the queue.
A circular queue is an abstract data type that contains a collection of data
which allows addition of data at the end of the queue and removal of data
at the beginning of the queue. Circular queues have a fixed size.
Circular queue follows FIFO principle. Queue items are added at the rear
end and the items are deleted at front end of the circular queue.
• Use an array of size N in a circular fashion.
• Three variables keep track of the front, rear, and size.
size is number of entries in the queue.
front index of the front element, used for deletion (dequeue).
rear index immediately past the rear element, where we add new
elements (enqueue).
Queue Initialization
1

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
0
1
2
3
23
23
20
23
20
16
20
16
16
12
20
16
12
12
7
16
7
12
16
5
12
7
5
7
5
5
F=0
R=0
F=0
R=1
F=0
R=2
F=1
R=2
F=1
R=3
F=2
R=3
F=2
R=0
F=2
R=1
F=3
R=1
F=0
R=1
R=1
F=1
F=-1
R=-1
en 23
en 20
en 16
de
en 12
de
en 7
en 5
de
de
de
de
Sequence of
operation
2

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Example 1: Circular queue C++ program of size 5.
#include<iostream.h>
#include<conio.h>
# define size 5
int cqueue[size], front=-1, rear=-1, item, i, ch=1;
void insert()
{
if((front==0 && rear==size-1) || (front==rear+1))
cout<<"\ncqueue is full..!\n";
else
{
cout<<"Enter a number: ";
cin>>item;
if(rear==-1)
front=rear=0;
else if(rear==size-1)
rear=0;
else
rear++;
cqueue[rear]=item;
cout<<"\nfront="<<front<<"\trear="<<rear<<endl;
}
}
void delet()
{
if(front==-1)
cout<<"\ncqueue is empty..!\n";
else
{
item = cqueue[front];
if(front==rear)
front=rear=-1;
3

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
else if(front==size-1)
front=0;
else
front++;
cout<<"you deleted "<<item<<endl;
cout<<"\nfront="<<front<<"\trear="<<rear<<endl;
}
}
void display()
{
if(front==-1)
cout<<"\ncqueue is empty..!\n";
else if(rear>=front)
{
for(i=front;i<=rear;i++)
cout<<cqueue[i]<<"\t";
}
else if(front>rear)
{
for(i=front;i<=size-1;i++)
cout<<cqueue[i]<<"\t";
for(i=0;i<=rear;i++)
cout<<cqueue[i]<<"\t";
}
cout<<"\nfront="<<front<<"\trear="<<rear<<endl;
}
4

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
void main()
{
cout<<"front="<<front<<"\trear="<<rear<<"\t\tsize="<<size<<endl;
while(ch!=4)
{
cout<<"********************\n* 1.\tinsert *\n* 2.\tdelete *\n*
3.\tdisplay *\n* 4.\texit *\n********************\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
break;
default:
cout<<"\nInvalid choice! Please try again...\n";
}
}
getch();
}
Homework 1:
Write a C++ program to create circular queue using structure.
5

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Linked List
A linked list is a series of connected nodes, where each node is a data
structure.
A linked list can grow or shrink in size as the program runs.
There are generally two types of linked list; single link list & double linked
list.
Single linked list
Each node in a linked list contains one or more members that represent
data. In addition to the data, each node contains a pointer, which can point
to another node.
6

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Example 2: Single linked list C++ program using structure.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int i,data,location;
struct node
{
int info;
struct node *next;
};
node *start=NULL,*current,*new_node,*temp1,*temp2,*temp3;
int empty()
{
if(start==NULL)
return(1);
else
return(0);
}
int list_length()
{
if(empty())
cout<<"\nList is empty!\n";
else
{
int length=0;
temp1=start;
while(temp1!=NULL)
{
temp1=temp1->next;
length++;
7

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
}
return(length);
}
}
void insert_begin()
{
new_node=new(node);
cout<<"\nEnter a value:\t";
cin>>new_node->info;
new_node->next=NULL;
cout<<endl;
if(empty())
{
start=new_node;
current=new_node;
}
else
{
new_node->next=start;
start=new_node;
}
}
void insert_end()
{
if(empty())
cout<<"\nNo items, please start with choosing 1.insert_begin\n";
else
{
new_node=new(node);
cout<<"\nEnter a vlaue:\t";
cin>>new_node->info;
new_node->next=NULL;
8

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
current->next=new_node;
current=new_node;
}
}
void insert_middle(int location)
{
if(location>=list_length())
cout<<"the node is not in the middle! check your list first\n";
else
{
new_node=new(node);
cout<<"\nEnter a value:\t";
cin>>new_node->info;
temp1=start;
for(i=1;i<location;i++)
temp1=temp1->next;
temp2=temp1->next;
temp1->next=new_node;
new_node->next=temp2;
}
}
int delet_begin()
{
data=start->info;
temp1=start;
start=start->next;
delete(temp1);
return(data);
}
int delet_middle(int location)
{
9

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
temp1=start;
for(i=1;i<location-1;i++)
temp1=temp1->next;
temp2=temp1->next;
temp3=temp2->next;
temp1->next=temp3;
data=temp2->info;
delete(temp2);
return(data);
}
int delet_end()
{
data=current->info;
temp1=start;
while(temp1->next!=current)
temp1=temp1->next;
delete(current);
current=temp1;
current->next=NULL;
return(data);
}
void display()
{
new_node=start;
cout<<"\nYour linked list values are:\n";
while(new_node!=NULL)
{
cout<<new_node->info<<"\t";
new_node=new_node->next;
}
cout<<endl<<endl;
}
10

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
void search()
{
int i=1;
cout<<"Enter data you like to search for: ";
cin>>data;
temp1=start;
while(temp1->info!=data && temp1->next!=NULL)
{
temp1=temp1->next;
i++;
}
if(temp1->info==data)
cout<<"\nData found in node "<<i<<endl<<endl;
else
cout<<"\nData not found!"<<endl<<endl;
}
void main()
{
int ch=1;
while(ch!=4)
{
cout<<"*************************\n* 1.\tinsert_begin \t*\n*
2.\tinsert_middle\t*\n* 3.\tinsert_end \t*\n* 4.\tdelete_begin \t*\n*
5.\tdelete_middle\t*\n* 6.\tdelete_end \t*\n* 7.\tsearch \t*\n*
8.\tlist_length\t*\n* 9.\tdisplay \t*\n* 10.exit
\t*\n*************************\nEnter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
insert_begin();
break;
11

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
case 2:
{
if(empty())
cout<<"\nLinked list is empty, choose 1.insert_begin to enter the 1st
value."<<endl<<endl;
else
{
cout<<"Enter the location of the previous node: ";
cin>>location;
insert_middle(location);
}
}
break;
case 3:
insert_end();
break;
case 4:
{
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
cout<<"\nYou deleted "<<delet_begin()<<endl<<endl;
}
break;
case 5:
{
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
{
cout<<"Enter the location of the node: ";
12

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
cin>>location;
if(location>=list_length())
cout<<"the node is not in the middle! check your list first\n";
else
cout<<"\nYou deleted "<<delet_middle(location)<<endl<<endl;
}
}
break;
case 6:
{
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
cout<<"\nYou deleted "<<delet_end()<<endl<<endl;
}
break;
case 7:
{
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
search();
}
break;
case 8:
{
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
cout<<"\nList length = "<<list_length()<<endl<<endl;
}
13

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
break;
case 9:
if(empty())
cout<<"\nLinked list is empty"<<endl<<endl;
else
display();
break;
case 10:
exit(0);
break;
default:
cout<<"\nInvalid choice! Please try again...\n";
break;
}
}
getch();
}
Homework 2:
Modify the search function in single linked list in order to keep searching for
the required data in the whole list and output the sequence number of the
nodes which have the data and how many times the data has been found
in the search.
14

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
MATLAB
Multimedia System
A M ultimedia System is a system capable of processing multimedia data
and applications. It is characterized by the processing, storage, generation,
manipulation and delivery of Multimedia information (video, audio and
image).
Characteristics of a Multimedia System
A Multimedia system has four basic characteristics:
1. Multimedia systems must be computer controlled.
2. Multimedia systems are integrated.
3. The information they handle must be represented digitally.
4. The interface to the final presentation of media is usually interactive.
Components of a Multimedia System
Now let us consider the Components (Hardware and Software) required for
a multimedia system:
1. Capture devices: Video Camera, Video Recorder, Audio
Microphone, Keyboards, mice, graphics tablets, 3D input devices.
2. Storage Devices: Hard disks, CD-ROMs, DVD, etc
3. Communication Networks: Ethernet, Intranets, Internets.
4. Computer Systems: Multimedia Desktop machines, Workstations,
MPEG/VIDEO/DSP Hardware
5. Display Devices: CD-quality speakers, HDTV, Hi-Res monitors,
Color printers etc.
Multimedia Applications
Examples of Multimedia Applications include:
• World Wide Web.
• Video conferencing.
• Home shopping.
• Games.
15

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Signal Generation and Visualization
MATLAB can
show how to generate widely used periodic and aperiodic
waveforms, sequences (impulse, step, ramp), multichannel signals, pulse
trains, sinc functions available in the Signal Processing Toolbox™.
Periodic Waveforms
In addition to the sin and cos functions in MATLAB®, the toolbox offers
other functions that produce periodic signals such as sawtooth and square.
Example 3: generate 1.5 seconds of a 50 Hz sawtooth (respectively
square) wave with a sample rate of 10 kHz.
fs = 10000;
t = 0:1/fs:1.5;
x1 = sawtooth(2*pi*50*t);
x2 = square(2*pi*50*t);
subplot(211)
plot(t,x1)
axis([0 0.2 -1.2 1.2]);
xlabel(
'Time (sec)'
);
ylabel(
'Amplitude'
);
title(
'Sawtooth Periodic Wave'
);
subplot(212)
plot(t,x2)
axis([0 0.2 -1.2 1.2]);
xlabel(
'Time (sec)'
);
ylabel(
'Amplitude'
);
title(
'Square Periodic Wave'
);
Homework 3:
Generate 1 second of a 100 Hz sin (respectively cos) wave with sample
rate of 20 kHz.
16

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Image Representation
Image Format: an image is a rectangular array of values (pixels). Each
pixel represents the measurement of some property of a scene measured
over a finite area. The property could be many things, but we usually
measure either the average brightness (one value) or the brightnesses of
the image filtered through red, green and blue filters (RGB; three values).
The values are normally represented by an eight bit integer, giving a range
of 256 levels of brightness.
We talk about the resolution of an image: this is defined by the number of
pixels and number of brightness values. Image format types:
17

Lecture3 – C++: Circular Queue & Single Link List. Matlab: Multimedia System
2
Computer Algorithms
& Programming
Mustafa Al-Qassab
2015-2016 Kirkuk
Example 4: read an image using MATLAB and manipulate the image by
adding grayscale, addition and complement effects.
I = imread(
'C:\Users\Public\Pictures\Sample Pictures\colorful2.jpg'
);
%
Read source image and save in matrix I
subplot(2,2,1);
% Upper left figure
imshow(I);
% Display output image
I2 = rgb2gray(I);
% converting image to gary (only black and white)
subplot(2,2,2);
% Upper right figure
imshow(I2);
% Display output image
I3 = imadd(I,I);
% adding two images
subplot(2,2,3);
% Lower left figure
imshow(I3);
% Display output image
I4 = imcomplement(I);
subplot(2,2,4);
% Lower right figure
imshow(I4);
% Display of the output image
18