2. References :-
-Data Structures and program design in C++
-classical data structures
-Starting out with C++ ( 5th edition)
3. Data and Information
Data is all things and facts that may be numbers ,
letters ,images or sounds or a mixed of all of them
that are processed by computer programs .
Information is the data after processing.
Processing
Data Information
4. Processing operations
The types of processing are
1- insertion
2- deletion
3- merge
4- sorting
5- searching
6- analysis
.
Data structure is the way how you c an
Connect between data into memory to be
one item.
5. Physical data structure is the space to store the data in
memory.
Logical data structure is the opinion of user about how
to program and deal with the data.
Data structures types:
1- Static data structure:
Which has a fixed size like arrays , tables, records, etc.
2- Dynamic Data Structure
which has a variant size, it has two types:
6. ** linear data structures
The data distribute in one level
- Lists
- Stacks
- Queues
** Non-linear data structures:
The data distribute in many levels and used pointers in
each item to point to the next item into memory in order
to facilitate the insertion and deletion operations, like tree
and graph.
To select an appropriate data structure for specific data,
there are many factors such as size of data, space
storage, the dynamic of data and the programming
techniques.
7. Arrays
Array is a finite ordered set of elements of
the same type, all integers, or all
characters but not may contains both. The
size of array can not change during
program’s
execution.
Concept : An array allows you to store and work with
multiple values of the same data type.
8. Properties of Arrays
1- It has a limit number of elements, and in
one type.
2- It has fixed size.
3- Easy to access any element into array
using the address of first element.
9. There are many types of arrays:
1- One dimensional array.
2- Two dimensional array.
3- Three dimensional array.
Even though an entire array has only one name,
the elements may be accessed and used as
individual variables. This is possible because each
element is assigned a number used as index to
pinpoint a specific element within an array.
10. Why use an array?
There are many reasons, one of the most important is
that once the data is in the array it can be used more
than once without having to be input again.
11. One dimensional array
Note : the subscript (index) numbering in C++
starts at zero.
Concept :- Arrays may be initialized where they are
defined.
int a[7] = {2, 5, 9, 3, 7, 8, 6};
a(0) a(1) a(2) a(3) a(4) a(5) a(6)
2 5 9 3 7 8 6
12. We can compute the address of any element (I) by
using the relation:
Location (a(i)) = Base address + (I-1) * size
Where :
Base address: is the address of the first element
size : is the size of the location in
memory
13. Two dimensional array
It is a table consists of a number of rows and columns, so it has two
indices one for row number and one for column number.
We can compute the address of any elements in two ways:
1- Row-wise method
In this way the elements store in memory row by row
sequentially. The equation in array a[N,M] is :
location a[i,j]=base address+[M*(i-1)]+j-1]*size
where: base address: address of first location
M : is number of columns
14. 2- Column-wise method
In this way the elements store in memory column by
column sequentially
The equation to compute the location a[I,j] is :
location a[i,j]=base address+[N*(j-1)]+i-1]*size
where: base address: address of first location
N : is number of rows
15. Stacks
A Stack is a data structure that holds a sequence of elements.
Unlike arrays and list new items may be inserted, or items may be
deleted at one end called the top of stack. The items is stored and
retrieved in a Last-In-First-Out manner.
Stack Operations:
A stack has two primary operations: push and pop. The push
operation causes a value to be stored or pushed onto the stack. The
pop operation retrieves a value from the stack.
16. In push operation, if the stack is not full, item is added to
the top of the stack. If the stack is full, an error of
Overflow is returned, and the stack is unchanged.
Algorithm PUSH
if top >= size of stack
error “stack overflow”
else increment top
add item.
17. In pop operation, if the stack is not empty, item is deleted
from the top of the stack. If the stack is empty, an error of
Underflow is returned, and the stack is unchanged.
Algorithm POP
if stack is empty
error “stack underflow”
else delete element
Decrement top
18. The program is:-
#include <iostream.h>
void push (int s[],int item);
bool empty();
int i,top=5,item,s[5];
void main()
{
cout<<"enter elements....."<<endl;
for(i=0;i<top;i++)
cin>>s[i];
push(s,item);
for(i=0;i<=top;i++)
cout<<s[i];
}
20. Applications of stack
1- In Arithmetic Expressions
To keep the number and type of parenthesis
There are three forms of Arithmetic expressions
** Infix Notation
** Postfix Notation
** prefix Notation
Evaluate a postfix Notation:
Each time an operand is read, it is pushed onto stack. When an
operator is reached, it is executed on the two top elements of the
stack(poped and performed the indicated operation on them)
The result will pushed on the stack, so that it will be available for
use as an operand of the next operator.
21. 2- In Recursion:
The concept of defining a function, so that it calls itself as a
subroutine. The main benefit is that it allows to take advantage of
the repeated structure present in many problems, i.e. it is useful
way for defining objects that have a repeated similar structural
form, such as :
Void message()
{
cout<<“This is a recursive functionn”;
message();
}
In this function there is no way to stop, so to be useful, it must
contains a condition to stop as follows:
22. void message(int times)
{
if(times >0)
{ cout<<“This is a recursive functionn”;
message(times-1);}
}
Another example is the factorial of non-negative integer
number (n):
1 If n=0
n!=
n*(n-1)! If n>0
The function to find the factorial of non-negative integer number is as
shown below:
23. The function is :
Int fact (int n)
{
If(n==0)
Return 1;
Else
Return n*fact(n-1)
}
Another example is the sum of elements in array like a[i] :
a[0] if n=1
sum(a) =
sum[a,n-1]+a[n-1] if n>1
24. Queue:
It is an ordered collection of items, which may be deleted at on
end called the front of the queue, or may be inserted at the other
end called the rear of the queue.
A queue provides access to it’s elements in First In First Out
elements in queue are processed like (FIFO) order. The
customers standing in a grocery checkout line. The first customer
in the line is the first one served.
The figure below illustrate simple queue contains three elements
A, B, C:
front A B C rear
25. Operations on queue:
--Insertion
to insert an element into a linear queue ,it
must be check if it is full or not as shown below:
Void add_q(int q[],int item)
{if (r>=size-1)
cout<<“OVERFLOW”;
elseq[r++]=item;
if (f==0)
f=1;
}
26. --Deletion
to delete one element from a linear
queue ,it must be check if it is empty or
not as shown below:
Void del_q(int q[],int item)
{if (f=-1)
cout<<“UNDERFLOW”;
else
{q[f]=-1;
if (f==r)
f=-1;r=-1;}
else f++;
}}
27. Pointers
A pointer is a variable contains an
address of location in memory.
Every pointer points to one object, but not
more, or don’t points to any object i.e.
has a value NULL.
Two pointers may point to one object.
When one object in memory lost i.e. no
pointer points to it it’s called Garbage.
In C++ to create a pointer points to object:
int *p=new int
28. Linked Linear List
A linked list is a series of connected nodes,
where each node is a data structure consists
of two parts, Data and a pointer to the next
node. The nodes of linked list are usually
dynamically allocated, used and then deleted,
allowing the linked list to grow or shrink in
size as the program runs. If new information
needs to be added to linked list, the program
simply allocates another node and insert it
into the series. If a particular piece of
information needs to be removed from L.L.L,
the program deletes the node containing that
information.