Arrays and Linked Lists

Slides:



Advertisements
Similar presentations
Lists CS 3358.
Advertisements

CHP-5 LinkedList.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Data Structures: A Pseudocode Approach with C
Linked list More terminology Singly-linked lists Doubly-linked lists DLLs compared to SLLs Circular Lists.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Topic 11 Linked Lists -Joel Spolsky
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Summary of lectures (1 to 11)
Arrays and Linked Lists "All the kids who did great in high school writing pong games in BASIC for their Apple II would get to college, take CompSci 101,
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
CS 307 Fundamentals of Computer ScienceLinked Lists 1 Topic 14 Linked Lists "All the kids who did great in high school writing pong games in BASIC for.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Binary Tree.
CS 46B: Introduction to Data Structures July 23 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Data Structure & Algorithms
LINKED LISTS.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Unit – I Lists.
Review Array Array Elements Accessing array elements
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CSCI-255 LinkedList.
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Data Structure Interview Question and Answers
Big-O notation Linked lists
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
Data Structure Dr. Mohamed Khafagy.
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
Data Structures Interview / VIVA Questions and Answers
LINKED LISTS CSCD Linked Lists.
Top Ten Words that Almost Rhyme with “Peas”
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Lists.
Topic 11 Linked Lists -Joel Spolsky
Chapter 15 Lists Objectives
Object Oriented Programming COP3330 / CGS5409
List Data Structure.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
CS2013 Lecture 4 John Hurley Cal State LA.
Problem Understanding
Chapter 17: Linked Lists.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
By Yogesh Neopaney Assistant Professor Department of Computer Science
Data Structures & Algorithms
BY PROF. IRSHAD AHMAD LONE.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Topic 11 Linked Lists -Joel Spolsky
LINEAR DATA STRUCTURES
Problem Understanding
Presentation transcript:

Arrays and Linked Lists "A list is only as strong as its weakest link." -Donald Knuth

Static vs Dynamic Static data structures Dynamic data structures Arrays. Fixed, predetermined capacity. If full, have to allocate more space and copy values into that space. Dynamic data structures Linked structures, like linked lists and trees. They grow / shrink one element at a time. Avoid some inefficiencies of static containers. CS 221 - Computer Science II

Efficiency of Array Operations Big-O of Array Operations To insert element at beginning of an array? Space available? No space available? While maintaining relative order? To insert element at the end of array? To insert or delete an element in the middle of the array? To access the kth element? CS 221 - Computer Science II

Advantages of Arrays Used to represent multiple data items of same type by using only single name. Can be used to implement other data structures like stacks, queues, trees, graphs, etc. 2D arrays are used to represent matrices. CS 221 - Computer Science II

Disadvantages of Arrays Array is static structure. Arrays are of fixed size. We must know in advance that how many elements are to be stored in array. Since array has fixed size, if allocate more memory than required, the memory space will be wasted. And if we allocate less memory than required, it will create problem. The elements of array are stored in consecutive memory locations. So insertions and deletions are very difficult and time consuming. CS 221 - Computer Science II

Single-Linked List Single-linked list is implementation-dependent data structure. Not defined by set of core operations. Have to understand how to manipulate nodes. Each node stores reference to element in linked list. Nodes also maintain references to next node in the list. Create new node when add new element to list. Remove node when element is removed from list. Allow garbage collector to reclaim that memory. CS 221 - Computer Science II

Anatomy of Single-Linked List A linked list consists of: A sequence of nodes, which can change during execution. Successive nodes are connected by node references. Last node reference points to null. Grows / shrinks during operation. Not limit number of elements. Keep references to first / last nodes in list. head A B C tail CS 221 - Computer Science II

Terminology Node’s successor is the next node in the sequence. The tail (last node) has no successor. Node’s predecessor is the previous node in the sequence. The head (first node) has no predecessor. List’s size is the number of elements in it. A list may be empty (i.e. contain no elements). CS 221 - Computer Science II

Single-Linked List Operations Inserting New Elements Inserting at beginning of list Inserting at end of list Inserting into middle of list Deleting Elements Deleting element from the beginning of list Deleting element from end of list Deleting element from middle of list Traversing List

Insertion: At Beginning Steps: Create a new node with new element. Connect new node to list. Update head and count variables. Special Case: if empty Same steps but have to update tail.

Insertion: At Beginning General Case: One or more elements in list. Create a new node with new element. new node count 3 D head A B C tail

Insertion: At Beginning General Case: One or more elements in list. Connect new node to list. count 3 new node head A B C tail D

Insertion: At Beginning General Case: One or more elements in list. Update head and count variables. count 4 new node head A B C tail D

Insertion: At Beginning Special Case: Empty list. Create a new node with new element. Connect new node to list (no change). new node count D head null tail

Insertion: At Beginning Special Case: Empty list. Update head and count variables. Update tail. new node count 1 head tail D

Insertion: At End Steps: Special Case: if empty Create a new node with new element. Connect list to new node. Update tail and count variables. Special Case: if empty Most of same steps but can’t connect list to new node and have to update head.

Insertion: At End General Case: One or more elements in list. Create a new node with new element. new node count 3 D head A B C tail

Insertion: At End General Case: One or more elements in list. Connect list to new node. count 3 new node tail head A B C D

Insertion: At End General Case: One or more elements in list. Update tail and count variables. count 4 tail new node head A B C D

Insertion: At End Special Case: Empty list. Create a new node with new element. Can’t connect list to new node, because tail is null. new node count D head null tail

Insertion: At End Special Case: Empty list. Update head and count variables. Update tail. new node count 1 head tail D

Deletion: At Beginning Steps: Remove deleted node from list, have to use temporary variable. Update head variable. Update count variable. Special Case: if only one node left Most of same steps but don’t need temporary variable and have to update tail.

Deletion: At Beginning General Case: Two or more elements in list. Remove deleted node from list, have to use temporary variable. count 4 head next B C D tail A

Deletion: At Beginning General Case: Two or more elements in list. Update head variable. 4 head count next B C D tail A

Deletion: At Beginning General Case: Two or more elements in list. Update count variable. head count 3 next B C D tail

Deletion: At Beginning Special Case: One node left. Remove deleted node from list (no change). Update head variable. Update count variable. Update tail. count A head null tail

Deletion: At End To delete last element, have to update link from node previous to last node. In order to update this link, have to traverse nodes to that node.

List Traversal Steps: Initialize temporary variable to head. Advance temporary variable until find element or position.

List Traversal Initialize temporary variable to head. target A B C D 4 current tail head A B C D target count 4

List Traversal Advance temporary variable until find element or position. current tail head A B C D target count 4

List Traversal Advance temporary variable until find element or position. current tail head A B C D target count 4

Deletion: At End Steps: Special Case: if only one node left Use temporary variable to traverse nodes until reach node before last one. Remove deleted node from list. Update tail variable. Update count variable. Special Case: if only one node left Most of same steps but don’t have to traverse nodes and have to update head.

Deletion: At End General Case: Two or more elements in list. Use temporary variable to traverse nodes until reach node before last one. count 4 current tail head B C D A

Deletion: At End General Case: Two or more elements in list. Use temporary variable to traverse nodes until reach node before last one. count 4 current tail head B C D A

Deletion: At End General Case: Two or more elements in list. Use temporary variable to traverse nodes until reach node before last one. count 4 current tail head B C D A

Deletion: At End General Case: Two or more elements in list. Remove deleted node from list. count 4 current tail head B C D A

Deletion: At End General Case: Two or more elements in list. Update tail variable. count 4 current tail head B C D A

Deletion: At End General Case: Two or more elements in list. Update count variable. count 3 current tail head B C A

Deletion: At End Special Case: One node left. Remove deleted node from list (no change). Update tail variable. Update count variable. Update head. count D head null tail

Advantages of Linked Lists Insertions / deletions don’t require shifting. No wasted space. Size is not fixed; can be extended or reduced. Elements do not have to be stored in consecutive memory. CS 221 - Computer Science II

Disadvantages of Linked Lists Requires more space – need references to successor and stored elements. No direct access to elements by position. To find a particular element, have to go through all those elements that come before that element. We can only traverse from the beginning. Sorting the elements stored in linked list are more difficult and inefficient. CS 221 - Computer Science II

CS 221 - Computer Science II