Anda di halaman 1dari 10

CS250 - Data Structures and

Algorithm
BESE-2A/B

Lecture 03
Aasma Zahid
Outline
• Problem with arrays
• Solution
• Introduction to Linked List
• Arrays vs. Linked List
• Linked List Pros & Cons

9/14/2012 DSA - Fall 2012 - SEECS, NUST 2


Our Prime Concern
• Performance & Efficiency
– In terms of time;
• faster the execution, better the algorithm
– In terms of space;
• lesser the space, better the data structure

9/14/2012 DSA - Fall 2012 - SEECS, NUST 3


Arrays
Most common data structure to implement
collections or to store data
Pros Cons
• Easier to use and access • Fixed size
• Faster access to the • One block allocation
elements – Fails if enough contiguous
– Takes constant time memory isn’t available
– Base + ( item_number * • Complex position based
number_of_bytes) insertion
– Expensive to maintain
ordered data OR insertion at
beginning
9/14/2012 DSA - Fall 2012 - SEECS, NUST 4
Arrays
• Solution
– Dynamic Memory Allocation
– Faster insertion and deletion of elements without
changing the existing items

• Linked List

9/14/2012 DSA - Fall 2012 - SEECS, NUST 5


Linked List
• Linked structure is a collection of
nodes storing data and links to
other nodes

• Dynamic Memory Allocation


– Consequently, nodes can locate
anywhere in the memory and each
node can point to any other node

9/14/2012 DSA - Fall 2012 - SEECS, NUST 6


Linked List
• Linked structure is a collection of nodes storing
data and link to other nodes

struct Node {
int data;
Node * next;
};

9/14/2012 DSA - Fall 2012 - SEECS, NUST 7


Array vs. Linked List

9/14/2012 DSA - Fall 2012 - SEECS, NUST 8


Linked List
Pros Cons
• Dynamic allocation • Complex to use and access
– the size is not required to be – relatively complex as
known in advance compared to arrays
• Flexibility • No constant time access to
– insert at (or delete from) any the elements
position in constant time
• No single allocation of
memory needed

9/14/2012 DSA - Fall 2012 - SEECS, NUST 9


Question?

9/14/2012 DSA - Fall 2012 - SEECS, NUST 10

Anda mungkin juga menyukai