You are on page 1of 26

DATA STRUCTURE

AND ALGORITHM
AND
LINKED LIST
Data Structure
• Data Structure is the most fundamental and
building block concept in computer science.
• It is a must to learn data structure to produce
efficient software systems.
Data Structure
• A data structure is a particular way of organizing
and storing data in a computer so that it can be used
effectively.
• We usually use Primitive and Compound data in
organizing and storing data in a computer through
programming.
Why Data Structure
• Primitive and Compound Data types are very
sufficient for solving simple problems.
• But when our program starts to deal with complex
problems, data structure is a must.
Data Structure - Example
Data Structure - Example
Landmark Coordinate List
C
House A (100,100) From -> To
House B (104,100) (House A, House B)
Table coordinate House C (105,102) (House B, House C)
B Church (103,103) (House C, School)
School (105,104) (School, House C)
Market (100,104) (School, Market)
Hash Table
(House A, Church)
Landmark Coordinate
(Church, School)
House A (House B, Church, Market)
House B (House C) (Church, Market)

House C (School) (Market, Church)


A
Church (School, Market) (House A, Market)

School (House C, Market) (Market, House A)

Market (Church, House A)


Algorithm
• Algorithm is a step-by-step procedure, which
defines a set of instructions to be executed in a
certain order to get the desired output.
• Algorithms are generally created independent of
underlying languages, i.e. an algorithm can be
implemented in more than one programming
language.
Algorithm
• From the data structure point of view, following are
some important categories of algorithms:
– Search − Algorithm to search an item in a data structure.
– Sort − Algorithm to sort items in a certain order.
– Insert − Algorithm to insert item in a data structure.
– Update − Algorithm to update an existing item in a data
structure.
– Delete − Algorithm to delete an existing item from a data
structure.
Algorithm
• Not all procedures can be called an algorithm. An algorithm should have the
following characteristics:
– Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.
– Input − An algorithm should have 0 or more well-defined inputs.
– Output − An algorithm should have 1 or more well-defined outputs, and should
match the desired output.
– Finiteness − Algorithms must terminate after a finite number of steps.
– Feasibility − Should be feasible with the available resources.
– Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
Algorithm
• Creating an algorithm for adding two • int a,b,c;
numbers:
• Step 1 − START • a = 5; b=6;
• Step 2 − declare three integers a, b & c
• c = a+b;
• Step 3 − define values of a & b
• Step 4 − add values of a & b • cout<<c;
• Step 5 − store output of step 4 to c
• Step 6 − print c
• Step 7 − STOP
Algorithm
• How to get the shortest path List
C from House A to Church.
From -> To
• 1. Find places you can go from (House A, House B)
House A.
(House B, House C)
B • 2. From each of those places, find (House C, School)
all the paths. (the will lead to (School, House C)
school)
(School, Market)
• 3. As you go, keep track of the (House A, Church)
distance traveled.
(Church, School)
• 4. Repeat this process until you (Church, Market)
get to school.
(Market, Church)
A
• 5. Compare the distance you’ve (House A, Market)
traveled. (Market, House A)
• 6. Find the shortest path.
Algorithm
• How to get the shortest path Hash Table
C from House A to Church. Landmark Leads to
• 1. Look for House A and check all
the places where it will leads to. House A (House B,
Church, Market)
• 2. For each of those places, check
B
the other places where it leads House B (House C)
until it leads to School.
• 3. As you go, keep track of the House C (School)
distance.
• 4. Repeat until all places from A Church (School,
was checked. Market)
• 5. Compare the distance you’ve School (House C,
A
traveled. Market)
• 6. Find the shortest path. Market (Church, House
A)
Data Structure and Algorithm
• List
• We talk about data-structures
– Store a given number
as: of elements.
1. Mathematical/Logical – Read elements by
position.
models or abstract data – Modify element at a
type. position.

2. Implementation.
– Implementation:
Array, Linkedlist
Data Structure and Algorithm

A B C D

0 1 2 3

A B C D

#0 #2
A C
#1
#3
B
D
Array VS Linked List
1.Its easier to search in array since it was numbered (indexed).
2.Its harder to search for linked list because it has no number. You need to
traverse all of the node until you find what your are searching for.

3.Extending the array is very hard. To extend array… you must create new
array with extended length. Copy the data from previous array to the new
array. (very costly).
4.Extending linked list is very easy. To extend linked list… you just need to
create new node.
Array VS Linked List
3.Insertion in array is very costly. You need to move the rest of the data from
where you insert a new data.
4.Insertion in linked list is very easy. You just need to create new node then
properly rearrange the pointers from each node.

5.Deletion in array is very costly. After you delete a data, you need to move
the rest of the data to fill the blank data.
6.Deletion in linked list is very easy. You just need to remove the node then
properly rearrange the pointers from each node.
Introduction to Linked List
Memory
a x
`
8
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

int x;
x = 8; Memory Manager
int a[4];
a[3] = 2; //constant time
//204+3*4
Introduction to Linked List
I want to extend my array Memory
a x
` 4 2
6 5 8
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

int x;
x = 8; Memory Manager
int a[4];
a[3] = 2;
Sorry but that’s not possible.

If you like, ill create a new larger array for you and you can
just copy the content of the smaller array to the bigger one.
Introduction to Linked List
Oh no… Memory
a x a2
` 4 2
6 5 8 6 5 4 2 7
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

int x;
x = 8; Memory Manager
int a[4];
a[3] = 2;
Is that fine with you?

Well you can still store more numbers on that array.


IT WOULD BE A WASTE OF MEMORY IF YOU WILL LEAVE IT
BLANK .
Introduction to Linked List
Memory

` 6 4 2
5
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

6,5,4,2
Memory Manager
I want these variables to be
continuous…
Sorry but my job is only to
Hmmm… I need to do something that manage memory…
make these continuous…

I need to define that 208 is the first on


my list… 220 is the next… followed by
236 and 252….
Introduction to Linked List
Memory
data next data next data next data next
` 6 220 4 252 2 0
5 236
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

6,5,4,2
Memory Manager
struct node{
int data;
node* next; Yes you can do that…. But define it
} properly
What if I add another information Mah boy! That is what we call a
from each block that points the Linked List.
address of the next block?....
Introduction to Linked List
Memory
data next data next data next data next
` 6 220 4 252 2 0
5 236
200 204 208 212 216 220 224 228 232 236 240 244 248 252 256 260 264 268
Programmer

6,5,4,2,3 Linked List’s logical view:


6 220 5 228
236 4 252 2 264 3 0
struct node{
208 220 236 252 264
int data;
node* next; 10 236 NULL NULL
} HEAD = 208
228
Address of the head node give us access to the entire lists.
Access to elements: T is proportional to “n”
Insertion, deletion… 0(n)
Linked List implementation
struct Node{ 1 NULL

int data; 200


Node* next;
}
200 200
Node* head = NULL; head temp
Node* temp = new Node;
temp->data = 1;
temp->next = NULL;
head = temp;
Linked List implementation
struct Node{
int data;
Node* next;
}
Node* head = NULL;
Node* temp = new Node; 1 220
NULL 2 NULL

temp->data = 1;
200 220
temp->next = NULL;
head = temp;

temp = new Node; 200 220


200
temp->data = 2;
temp->next = NULL; head temp
head->next = temp;
Linked List implementation
struct Node{
int data;
Node* next;
}
Node* head = NULL;
Node* temp = new Node; 1 220 2 NULL 3 NULL

temp->data = 1;
200 220 240
temp->next = NULL;
head = temp;

temp = new Node; 200 240


220
temp->data = 2;
temp->next = NULL; head temp
head->next = temp;

temp = new Node;


temp->data = 3;
temp->next = NULL;
Linked List implementation
struct Node{
int data;
Node* next;
}
Node* head = NULL;
Node* temp = new Node; 1 220 2 240
NULL 3 NULL

temp->data = 1;
200 220 240
temp->next = NULL;
head = temp;

temp = new Node; 200 240 200


temp->data = 2;
temp->next = NULL; head temp tempPtr
head->next = temp;

temp = new Node;


temp->data = 3; while(tempPTR->next != NULL){
temp->next = NULL; tempPTR = tempPTR->next;
Node* tempPtr = head; }
tempPtr->next = temp;

You might also like