Algo Analysis Lect - 1
Algo Analysis Lect - 1
ALGORITHM
Irshad Khan
CS & IT
Sarhad University of Science and Information
Technology
IRSHAD KHAN
Qualification
PhD (CS) In Progress
FAST-NUCES Peshawar
MS (CS)
FAST-NUCES Peshawar
MIT
Gomal University D.I. Khan
Experience
Assistant Professor
Sarhad University of Science and Information Technology
Lecturer
Sarhad University of Science and Information Technology
Lecturer
IM|Sciences Peshawar
Lecturer
CECOS University Peshawar
GRADING POLICY
Sessional…………………. ………………………20
Mid Term………….………………………………..30
Final Term………………………………………….50
LECTURE 1 OUTLINES
Algorithm
Basic Data Structure
Array
Stack
Queue
Linked List
ALGORITHM
• A set of rules for solving a problem in a finite
number of steps
Queue
Linked List
ARRAYS
An array is a list of a finite number of homogeneous data
elements such that
The elements of the array are referenced respectively by an
index set consisting of "n" consecutive numbers.
The elements of an array are stored respectively in
successive memory locations.
The simplest type of data structure is a Linear or one-
dimensional array.
The number 'n' of elements is known as the length or
size of the array.
In general the length or the number of data elements of
an array can be obtained from the index set by the
formula
Length=UB - LB + 1
Where UB is the largest index, called the upper bound,
and LB is the smallest index , called lower bound.
ARRAY (2)
OPERATIONS ON LINEAR ARRAY
(TRAVERSE)
Algorithm 1.(Traversing a linear Array)
This algorithm traverses a linear array LA
[End of loop.]
3. Exit
INSERTION IN LINEAR ARRAY
Algorithm 3.(Inserting into a Linear array) INSERT(LA,
N, K, ITEM)
Here LA is a linear array with N elements and K is a
7. Exit
DELETION IN LINEAR ARRAY
Algorithm 4. (Deleting from a linear Array)
DELETE(LA, N, K, ITEM)
Here LA is a linear array with N elements and K is
into a stack.
“Pop” is the term used to delete an element
from a stack.
These terms are used only with stacks, not
DDD 7
CCC 8
BBB 9
N–1
AAA
N
problem lies in
allowing the queue to
wrap around.
QUEUE OPERATIONS
Primary Operations
Insertion (Enqueue)
Deletion (Dequeue)
Other Operations
Traverse
Front
IsEmpty
IsFull
ALGORITHM: ENQUEUE / INSERTION
IN CIRCULAR QUEUE USING ARRAY
ALGORITHM: DEQUEUE / DELETION FROM
CIRCULAR QUEUE USING ARRAY
ALGORITHM: TRAVERSE CIRCULAR
QUEUE USING ARRAY
LINKED LIST DATA STRUCTURE
Link List is a Data Structure in which
elements are explicitly ordered, that is each
item contains within itself the address of
next item.
class Node {
public:
double data; // data
Node* next; // pointer to next
};
OPERATIONS OF LINKED LIST
Operations of List
IsEmpty: determine whether or not the list is
empty
InsertNode: insert a new node at a particular
position
FindNode: find a node with a given value
DeleteNode: delete a node with a given value
DisplayList: print all the nodes in the list
TRAVERSE ALGORITHMS
DELETE ALGORITHM
DELETE CONTINUE…
VARIATIONS OF LINKED LISTS
Circular linked lists
The last node points to the first node of the list
A B C
Head
How do we know when we have finished traversing
the list? (Tip: check if the pointer of the current
node is equal to the head.)
VARIATIONS OF LINKED LISTS
Doubly linked lists
Each node points to not only successor but the
predecessor
There are two NULL: at the first and last nodes in the
list
Advantage: given a node, it is easy to visit its
predecessor. Convenient to traverse lists backwards
A B C
Head Tail
In a Doubly linked list, each item is allocated space
as it is added to the list. A link is kept with each item
to the next item and previous item in the list.
Each node of the list has three elements:
NULL NULL
A B C D E
Head
HEAD
Tail