Linked Lists CSC 172 SPRING 2002 LECTURE 3 Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Data Structures ADT List
Linked Lists Linear collections.
C and Data Structures Baojian Hua
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 7 : September 8.
LISTS & TREES Lecture 8 CS2110 – Fall List Overview 2  Purpose  Maintain an ordered set of elements (with possible duplication)  Common operations.
Lists Lists as an abstract data type (ADT)
Linked Lists CSC 172 SPRING 2002 LECTURE 3 Agenda  Average case lookup – unsorted list & array  Amortized insert for array  Sorted insert  Binary.
E.G.M. Petrakislists1 Lists  List: finite sequence of data elements  all elements have the same data type  The operations depend on the type of the.
Trees II CSC 172 SPRING 2002 LECTURE 15. Binary Trees Every binary tree has two “slots” for children It may have none, either, or both An empty (0-node)
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Binary Search Example CSC 172 SPRING 2004 LECTURE 5.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Linked Lists CSC 172 SPRING 2004 LECTURE 6. ANNOUNCEMENTS Project 2 due Wed, Feb 18 th, 5PM, CSB Read Weiss Chapter 17 Department T shirts available $10.
CSE 373 Data Structures and Algorithms Lecture 13: Priority Queues (Heaps)
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
Searching: Binary Trees and Hash Tables CHAPTER 12 6/4/15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Binary Trees Chapter 10. Introduction Previous chapter considered linked lists –nodes connected by two or more links We seek to organize data in a linked.
COSC 1030 Lecture 9 Binary Trees. Topics Basic Concept and Terminology Applications of Binary Tree Complete Tree Representation Traversing Binary Trees.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Data structures Abstract data types Java classes for Data structures and ADTs.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Scott Grissom, copyright 2004 Ch 6 Data Structures Slide 1 Linear Data Structures Chapter 6 focuses on: Lists Stacks Queues You can skip the Array implementation.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structure & Algorithm
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Introduction to Data Structures and Algorithms
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Week 5 - Wednesday.  What did we talk about last time?  Recursion  Definitions: base case, recursive case  Recursive methods in Java.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
HEAPS. Review: what are the requirements of the abstract data type: priority queue? Quick removal of item with highest priority (highest or lowest key.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
CSE 1342 Programming Concepts
CSCI-255 LinkedList.
Efficiency of in Binary Trees
Priority Queue.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Top Ten Words that Almost Rhyme with “Peas”
Stacks.
Queues.
Data Structures ADT List
Data Structures ADT List
ADT list.
CSE 373 Data Structures and Algorithms
Queues CSC212.
Data Structures ADT List
ADT Queue (Array Implementation)
Abstract Data Types Stacks CSCI 240
Presentation transcript:

Linked Lists CSC 172 SPRING 2002 LECTURE 3

Data Structures?  We make distinctions in the level of abstraction  Abstract Data Type (ADT) - functionality/behavior  Dictionary, Stack, Queue  Data Model – organization  List, Tree, Graph  Data Structure – implementation  Array, Linked list, BST, Adjacency Matrix

The Dictionary ADT  Computer programs commonly maintain a set of values where we wish to  Insert elements into the set  Delete Elements from the set  Look up elements (see if they are currently in the set)  There are lots of ways to implement this  Some are more efficient than others

The Dictionary Interface public interface Dictionary { public abstract void insert(Object o); public abstract void delete(Object o); public abstract boolean lookup(Object o); }

List Data Model  A list is a finite sequence of zero or more elements  Grocery list  Laundry list  (a 1,a 2,…,a n )  Formally, we can think of a list as either  Empty, or  An element followed by a (possibly empty) list

Lists  Empty (no elements)  An element (head), followed by a list (tail)  Head sometimes called CAR  Tail sometimes called CDR  Length of a list is  0 for an empty list  1 + length(tail) for a non-empty list

How can we implement this?  We can implement a Dictionary ADT... using a list data model... with an array data structure... or a linked-list data structure.

Arrays  An array is a collection of data items of the same type  Every element of the collection can be accessed separately. Object [] data = new Object[10];

Array Implementation public class myInfo implements Dictionary{ private int defaultCapacity = 100, length = 0; private Object[] datum; public myInfo() { datum = new Object[defaultCapacity]; } public myInfo(int initCapacity) { datum = new Object[initCapacity]; }

Implementation public class myInfo implements Dictionary{ private int defaultCapacity = 100, length = 0; private Object[] datum; public myInfo() { datum = new Object[defaultCapacity]; } public myInfo(int initCapacity) { datum = new Object[initCapacity]; }

Insert Is this ok? public void insert (Object o) { datum[length++] = o; } What is the run time? What if I have more than 100 elements?

Expand private void expand(){ Object[] tempData = new Object[datum.length * 2]; for (int j = 0 ; j<length;j++) tempData[j] = datum[j]; datum = tempData; } So, what is the runtime of this

Insert Better? public void insert (Object o) { if (length >= (datum.length –1)) expand(); datum[length++] = o; } This is what the Java class Vector gets you What is the (worst case) run time, now?

Self-referential data types class Node { private Object data; // the “data” private Node next; // the “link” } datanext

Linked List  A linked list “has-a” reference to a node  The “head” of the list  The ending node has “null” for the next datanext dataNext head dataNext null

Linked-List Implementation public class myInfo implements Dictionary{ private int length = 0; private Node head = null;

Adding a first node

public void insert(Object obj){ Node newLink = new Node(); newLink.data = obj; newLink.next = head; head = newLink; } What is the run time of this?

Lookup: Array public boolean lookup(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) return true; return false; } Run time?

Lookup: Linked List public boolean lookup(Object o){ Object temp = head; while (temp != null){ if ((temp.data).equals(o)) return true; temp = temp.next; } return false; } Run time?

Lookup: Linked List, recursive public boolean lookup(Object o){ return lookupNode(o,head); } public boolean lookupNode(Object o, Node n){ if (n == null) return false; else if((n.data).equals(o) return true; else return lookupNode(o,n.next); } Run time?

Deleting a node from a List

Delete: Linked List, recursive public boolean delete(Object o){ return deleteNode(o,head); }

Delete: Linked List, recursive public void deleteNode(Object o, Node n){ if (n == null) return ; if (n.next == null) return; else if(((n.next).data).equals(o)) { n.next = (n.next).next return ; } else return deleteNode(o,n.next); } Run time?

Delete: Array  We have to look it up O(n)  Deletion is setting it to null  But, we have to “shift” all the remaining elements.

Delete: Array public void delete(Object o){ for (int j = 0 ; j < length;j++) if (datum[j].equals(o)) { for (int k = j; k<(length-1);k++) datum[k] = datum[k+1]; return; }; } Run time?

Compare (worst case) Run Time (what about space?) ARRAYLINKED-LIST InsertN (in case of expansion) 1 LookupNN DeleteN (lookup + shift)N (for lookup)

Workshop sing-up. The coefficient for the workshop differential is positive and significant. On average, each additional workshop attended in CSC 172 increases the final grade point by In substantive terms, this means that a student who attends five additional workshops in CSC 172 should expect his/her grade to increase by almost one full letter grade.