CHAPTER 8 Lists. 2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lab 8 Ordered list. OVERVIEW In an ordered list the elements are maintained in ascending (or descending) order based on the data contained in the list.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
M180: Data Structures & Algorithms in Java
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.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
CHAPTER 7 Queues.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Chapter 3 Collections. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 3-2 Chapter Objectives Define the concept and terminology related.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Typical operations on data –Add data.
Queues.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
CHAPTER 3 COLLECTIONS SET Collection. 2 Abstract Data Types A data type consists of a set of values or elements, called its domain, and a set of operators.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Chapter 8 Lists. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 8-2 Chapter Objectives Examine list processing and various ordering techniques.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Stacks, Queues, and Deques
Data Structures Using C++ 2E
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Lists Based on content from: Java Foundations, 3rd Edition.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Data Structures Using C++
Chapter 6 Stacks. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine stack processing Define a stack abstract.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
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.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Lab 6 Stack ADT. OVERVIEW The stack is one example of a constrained linear data structure. In a stack, the elements are ordered from most recently added.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Collections Using Generics in Collections. 2 Chapter Objectives Define the concept and terminology related to collections Explore the basic structure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
The List ADT.
Data Abstraction: The Walls
Top Ten Words that Almost Rhyme with “Peas”
Queues.
DATA STRUCTURE QUEUE.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Programming II (CS300) Chapter 07: Linked Lists
Stacks, Queues, and Deques
The List Container and Iterators
Presentation transcript:

CHAPTER 8 Lists

2 A list is a linear collection Adding and removing elements in lists are not restricted by the collection structure We will examine three types of list collections: ordered lists unordered lists indexed lists

3 Ordered Lists The elements in an ordered list are ordered by some inherent characteristic of the elements names in alphabetical order scores in ascending order Adding an element must preserve the order

4 A conceptual view of an ordered list

5 Unordered Lists Unordered lists: elements are ordered by their placement in the list A new element can be put on the front or the rear of the list, or it can be inserted after a particular element already in the list

6 A conceptual view of an unordered list

7 Indexed Lists Indexed list: elements are referenced by their numeric position in the list There is no inherent relationship among the elements Every time the list changes, the indexes are updated

8 A conceptual view of an indexed list

9 List Operations Add and Remove elements Elements can be added to or removed from any position in the list Examine elements retrieve the contents of the first, last, or specified element Check the status of the list Get the size of the list; check if it is empty The key differences between the list types involve the way elements are added

10 List ADT Notation: L - list e - item of the same type as the information part of an element (a node) in L b - boolean value terminology: head: first node tail: last node current: current node

11 Create / Destroy a list InitList(L) Procedure to initialize the list L to empty. head = tail = current = null DestroyList(L) Procedure to make an existing list L empty. Remove all nodes and pointers (performed automatically in Java) MakeEmptyList(L) Procedure to make an existing list L empty. head = tail = current = null

12 Expand / Shrink the list Insert(L,e) Procedure to insert a node with information e before the current position, or if L is empty - as the only node in L. Preconditions: If list is not empty, current is not empty (i.e. current is not null) Postconditions: current = new node InsertAfter(L,e) Procedure to insert a node with information e after the current position, or if L is empty - as the only node in L. Preconditions: If list is not empty, current is not empty (i.e. current is not null) Postconditions: current = new node

13 Expand / Shrink the list Delete(L) Procedure to delete the current node in L and to have the current position indicate the next node. Preconditions: current is not empty (i.e. current is not null) Postconditions: current becomes the node after the deleted node (empty if the deleted node was the last node.

14 Read / Write operations StoreInfo(L,e) Procedure to update the information part of the current node Preconditions: current is not empty (i.e. current is not null) Postconditions: current has new information RetrieveInfo(L)  e Procedure to return the information in the current node. Preconditions: current is not empty (i.e. current is not null) Postconditions: none

15 Read / Write operations RetrieveNextInfo(L)  e Procedure to return the information in the node after the current node. Preconditions: current is not empty (i.e. current is not null). current.getNext() is not empty Postconditions: none

16 Changing the current node (moving along the list) Advance(L) Preconditions: current is not empty (i.e. current is not null). Postconditions: current = current.getNext() ToFirst(L) Preconditions: none Postconditions: current = head Tolast(L) Preconditions: none Postconditions: current = tail

17 Report the current position AtFirst(L)  b Returns TRUE if the current node is the first node. (the head) AtEnd(L)  b Returns TRUE if the current node is the last node (the tail)

18 Report status of the list Contains(L,e)  b Returns TRUE if the list contains an element with information e ListEmpty(L)  b Returns TRUE if the list is empty ListFull(L)  b Returns TRUE if the list is full (array implementation) CurrentEmpty(L)  b Returns TRUE if the current position is empty. Size(L)  int Returns the number of elements in the list

19 Common operations on a list

20 The operation particular to an ordered list

21 The operations particular to an unordered list

22 The operations particular to an indexed list

23 List Operations Define the common list operations in one interface Derive three others from it that define the interfaces of the three list types

24 The various list interfaces

25 Implementations Ordered list Unordered list Indexed list Array implementations Linked implementations Single linked list Double linked list

26 Array implementation of unordered list Representation T [ ] list; // array of type T int head; // index of the first element // used only if circular array int tail ; // index of the last element int count; // number of elements int Capacity = 100

27 Array implementation of unordered list Constructor list = new T [ Capacity ] head = 0 // used only if circular array tail = 0 count = 0 Capacity: default or specified in the declaration  Two constructors needed

28 Operations: addToFront /O(N)/ void addToFront (T element) Expand capacity if necessary Shift all elements one position to the right Store the new element at list[head] Increment count Increment tail Can you make addToFront O(1))?

29 Operations: addToRear /O(1)/ void addToRear (T element) Expand capacity if necessary Increment tail Store the new element at list[tail] Increment count

30 Operations: addAfter /O(N)/ void addAfter (T element1, T element2) Expand capacity if necessary Find index k of element2 Shift all elements one position to the right Store the new element at list[k+1] Increment count Increment tail