CS250 - Data Structures and
Algorithm
BESE-2A/B
Lecture 03
•
•
•
•
•
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
Pros
• Easier to use and access
• Faster access to the
elements
– Takes constant time
– Base + ( item_number *
number_of_bytes)
Cons
• Fixed size
• One block allocation
– Fails if enough contiguous
memory isn’t available
• Complex position based
insertion
9/14/2012
– Expensive to maintain
ordered data OR insertion at
beginning
DSA - Fall 2012 - SEECS, NUST 4
Arrays
Most common data structure to implement
collections or to store data
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
• Dynamic allocation
– the size is not required to be
known in advance
• Flexibility
– insert at (or delete from) any
position in constant time
• No single allocation of
memory needed
Cons
• Complex to use and access
– relatively complex as
compared to arrays
• No constant time access to
the elements
9/14/2012 DSA - Fall 2012 - SEECS, NUST 9
Question?
9/14/2012 DSA - Fall 2012 - SEECS, NUST 10

Link list 1

  • 1.
    CS250 - DataStructures and Algorithm BESE-2A/B Lecture 03
  • 2.
    • • • • • Outline Problem with arrays Solution Introductionto Linked List Arrays vs. Linked List Linked List Pros & Cons 9/14/2012 DSA - Fall 2012 - SEECS, NUST 2
  • 3.
    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
  • 4.
    Pros • Easier touse and access • Faster access to the elements – Takes constant time – Base + ( item_number * number_of_bytes) Cons • Fixed size • One block allocation – Fails if enough contiguous memory isn’t available • Complex position based insertion 9/14/2012 – Expensive to maintain ordered data OR insertion at beginning DSA - Fall 2012 - SEECS, NUST 4 Arrays Most common data structure to implement collections or to store data
  • 5.
    Arrays • Solution – DynamicMemory Allocation – Faster insertion and deletion of elements without changing the existing items • Linked List 9/14/2012 DSA - Fall 2012 - SEECS, NUST 5
  • 6.
    Linked List • Linkedstructure 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
  • 7.
    Linked List • Linkedstructure 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
  • 8.
    Array vs. LinkedList 9/14/2012 DSA - Fall 2012 - SEECS, NUST 8
  • 9.
    Linked List Pros • Dynamicallocation – the size is not required to be known in advance • Flexibility – insert at (or delete from) any position in constant time • No single allocation of memory needed Cons • Complex to use and access – relatively complex as compared to arrays • No constant time access to the elements 9/14/2012 DSA - Fall 2012 - SEECS, NUST 9
  • 10.
    Question? 9/14/2012 DSA -Fall 2012 - SEECS, NUST 10