Linked Lists

Similar documents
Fun facts about recursion

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Linked Lists in C and C++

1 P age DS & OOPS / UNIT II

+ Abstract Data Types

Data Structures. Outline. Introduction Linked Lists Stacks Queues Trees Deitel & Associates, Inc. All rights reserved.

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Dynamic Data Structures

3. Fundamental Data Structures

CMSC 341. Linked Lists, Stacks and Queues. CMSC 341 Lists, Stacks &Queues 1

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

11/2/ Dynamic Data Structures & Generics. Objectives. Array-Based Data Structures: Outline. Harald Gall, Prof. Dr.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Linked lists (6.5, 16)

Linked lists. Insert Delete Lookup Doubly-linked lists. Lecture 6: Linked Lists

Class 26: Linked Lists

Data Structures. COMS W1007 Introduction to Computer Science. Christopher Conway 1 July 2003

Linked List Implementation of Queues

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

8. Fundamental Data Structures

A linked list grows as data is added to it. In a linked list each item is packaged into a node.

Exceptions in Java

CS350 - Exam 1 (100 Points)

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

CMPT 225. Lecture 6 linked list

Introduction to Software Design

Programming. Lists, Stacks, Queues

Data Structure Series

Fundamentals of Data Structure

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

CMSC Introduction to Algorithms Spring 2012 Lecture 7

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Sequence Abstract Data Type

cs Java: lecture #6

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Lists. CITS2200 Data Structures and Algorithms. Topic 9

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

Introduction to Programming in C Department of Computer Science and Engineering

Sequential Containers Cont'd

Introduction to Scientific Computing

Tutorial 2: Linked Lists

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Linked Lists and Abstract Data Structures A brief comparison

Binary Trees

ITI Introduction to Computing II

[2:3] Linked Lists, Stacks, Queues

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Linked Structures. See Section 3.2 of the text.

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Data Structures. Topic #7

Ashish Gupta, Data JUET, Guna

SEQUENCES, ARRAYS, AND LINKED LISTS

Algorithms, Data Structures, and Problem Solving

PA3 Design Specification

CP2 Revision. theme: dynamic datatypes & data structures

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Sequential Containers Cont'd

Information Science 2

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

(Refer Slide Time: 02.06)

LAB 5, THE HIDDEN DELIGHTS OF LINKED LISTS

Linked lists. Comp Sci 1575 Data Structures. Definitions. Memory structure. Implementation. Operations. Comparison

Iterators and Sequences

DATA STRUCTURES AND ALGORITHMS

Optimizing Dynamic Memory Management

Linked Lists. private int num; // payload for the node private Node next; // pointer to the next node in the list }

ADTs, Arrays, Linked Lists

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

How to Win Coding Competitions: Secrets of Champions. Week 2: Computational complexity. Linear data structures Lecture 5: Stack. Queue.

Computers for Beginners. Tuesday, July 7, p.m. Instructor: Anne Swanson

Q &A on Entity Relationship Diagrams. What is the Point? 1 Q&A

Programming II (CS300)

Lesson 3: Understanding General Software Development

Programming II (CS300)

Datatypes. List of data structures. Datatypes ISC

Linked List. April 2, 2007 Programming and Data Structure 1

Data structure is an organization of information, usually in memory, for better algorithm efficiency.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

CSE 131 Computer Science 1 Module 9a: ADT Representations. Stack and queue ADTs array implementations Buffer ADT and circular lists

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Learning Goals. CS221: Algorithms and Data Structures Lecture #3 Mind Your Priority Queues. Today s Outline. Back to Queues. Priority Queue ADT

Section 05: Solutions

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

Computer Science Foundation Exam. Dec. 19, 2003 COMPUTER SCIENCE I. Section I A. No Calculators! KEY

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Chapter 18 Indexing Structures for Files. Indexes as Access Paths

LINKED IMPLEMENTATION OF LIST

Arrays & Linked Lists

Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems. COMP Week 8 & 9 1

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linear Data Structures

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Postfix (and prefix) notation

DDS Dynamic Search Trees

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

811312A Data Structures and Algorithms, , Exercise 1 Solutions

Artificial Intelligence Prof. Deepak Khemani Department of Computer Science and Engineering Indian Institute of Technology, Madras

Linked List Nodes (reminder)

Transcription:

Linked Lists 2-17-2005

Opening Discussion What did we talk about last class? Do you have any code to show? Do you have any questions about the assignment? Can you tell me what a linked list is and what is significant about them?

Growing the Stacks and Queues Let s go back to our code and look at what we have to do to make it so the arrays can grow on overflow.

The List as an ADT A more powerful abstract data type than the stack or queue is the list. Lists allow random access of elements so that you can add, search, and remove at will. As with the stack and the queue, the implementation can vary. Java has an interface for List in the java.util package that you can look at to get an idea of what a list should do.

Array Based Lists Just like with the stack and the queue, we can implement lists using arrays. That implementation though has some drawbacks to it. The main problem is that random insertions and removing require lots of copying, though we can jump to random elements quite quickly.

Linked Lists An alternate implementation of a list is using what are called Linked Lists. A linked list is a list where each element knows only about its neighbors. The simplest form of this is a singly linked list where each element knows about the next element in the list. If you keep track of the first one you can get to the entire list by following the links.

Nodes Each element of the list is typically called a node. The node stores the data that we need as well as the references/pointers to the other node(s) that it is linked to. The node class is not the linked list class. They are distinct types, though the linked list class will use the node class. We can make the node a private static inner class.

Heads and Tails One feature of a linked list is that you always have to keep track of at least one element in it. For a singly linked list it has to be the first one, the head. Sometimes it is also helpful to keep track of the last element of the list as well, the tail. Other references would be either short lived or for optimizations.

Inserting Linked lists excel at inserting and removing. Inserting at the beginning is very easy. Same is true for the end if we keep a tail. Inserting into the middle requires walking the list and keeping track of the previous element in the list. This is because you insert after elements and can t walk the list backwards if it is singly linked.

Removing Removing elements from a list is a very similar operation. In this case, we walk the list to find the element, keeping track of the previous one, then set the pointer to go around it.

Circular Linked Lists It is also possible to build a list where the tail points back to the head. In this case those two terms really aren t all that well defined. Instead we can have a pointer anywhere in the list. We still can t walk backwards, but we can walk all the way around to get to anything we want.

Doubly Linked Lists Another variation on lists that can be useful is the doubly linked list. In a doubly linked list, every element knows both the one before it and the one after it. With this added in, you can delete an element without walking the list, or add one without having to go looking for the previous one. These require a bit more work.

Sentinels One way to help simplify linked list code, especially for doubly linked lists, is to add a sentinel. This is a special node that signifies an end of the list. We can put it at the beginning and the end by making it a circular list. Doubly linked lists with sentinels are perhaps that easiest type of list because they lack special case code.

Code Now let s write some code to do a singly linked list.

Minute Essay What questions do you have about linked lists? Can you think of an application where you would want to use a linked list instead of an array? The design for assignment #3 is due next Tuesday.