Professional Documents
Culture Documents
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)
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?
` 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…
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
temp->data = 1;
200 220
temp->next = NULL;
head = temp;
temp->data = 1;
200 220 240
temp->next = NULL;
head = temp;
temp->data = 1;
200 220 240
temp->next = NULL;
head = temp;