List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
M180: Data Structures & Algorithms in Java
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
Data Structures: A Pseudocode Approach with C
CS Data Structures II Review COSC 2006 April 14, 2017
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
Chapter 3: Arrays, Linked Lists, and Recursion
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques
Data Structures Using C++ 2E
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Reference: Vinu V Das, Principles of Data Structures using C and C++
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Final Exam Review CS Total Points – 60 Points Writing Programs – 50 Points Tracing Algorithms, determining results, and drawing pictures – 50.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Linear Data Structures
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
STACKS & QUEUES for CLASS XII ( C++).
Cpt S 122 – Data Structures Abstract Data Types
Pointers and Linked Lists
Chapter 12 – Data Structures
Chapter 18: Linked Lists.
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Recursive Linked List Operations
CS6045: Advanced Algorithms
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Stacks, Queues, and Deques
Presentation transcript:

List Structures

What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element has a unique predecessor (except first) and a unique successor (except last) Examples: grocery, to-do, address

Lists Lists may be keys required to be unique, other lists may not. Note: Stacks and Queues are lists How would you implement a list?

Define key. A field in a record whose value is used to determine the logical (or physical) order of the records in a list –Examples: social security number, name, zip code

Design Alternatives Consider alternative designs for the actual implementation of the list For example, –array –singly linked list –doubly linked list –circular linked list How would the choice be made? Might it change in a later version?

Array-Based List item 1item 2item 3item 4item 5

Singly Linked List item 1item 2item 3item 4item 5 null

Linked Stacks A simple linked list easily implements a stack. Only one end of the stack is accessed (Top) Only one pointer is required Push / Pop are performed at Top 9  17  22  26  34  Top

Head and Tail Pointers item 1item 2item 3item 4item 5 null

Linked Queues A linked can implement a queue Both ends of the stack must be accessed (Front/Back) Enqueued elements are added at the Back Dequeued elements are removed from the Front 9  17  22  26  34 Front Back

Circular Linked Lists –Last node contains a pointer to the first node in the list –A single pointer has access to both ends of the list 9  17  22  26  34 Last

Doubly-Linked Lists –Each node has two pointers one to its successor one to its predecessor 9  17  22  26  34 First  Last Prev Data Next

9  17  22  26  34  Top 9  17  22  26  34 First  Last

9  17  22  26  34  Top 9  17  22  26  34 First  Last

9  17  22  26  34  Top 9  17  22  26  34 First  Last

..\ds\stl\stl docs\list.html

Lists and Iterators Each container class is equipped with an associated iterator class. Iterators maintain pointer to “current” element

Lists Datatype Interface should allow –creation of empty list –tests for empty –re-initialization to empty –access to front and back –insertion and removal –operators for moving iterator to list beginning and end

List Iterator Datatype Interface provides –Operators for moving iterator to beginning of list begin( ) end( ) –Operators for moving through the list –Operator for returning the current item on the list (*) –Generic algorithms (Find, Remove, etc.)

Sample List Declarations list list_one; list list_three; // list of Widgets list list; //list of pointers to // Widgets list list_four (list_one); list list_five; list_five = list_three;

Adding Elements to a List list_one.push_front(12); list_three.push_back(Widget(6)); // insert widget 8 at end of list list_three.insert(list_three.end(), Widget(8)); // find location of first 5 in list list ::iterator location = find (list_one.begin(), list_one.end(), 5); // and insert an 11 immediately before it location = list_one.insert(location,11);

Erasing Elements from a List list_nine.erase(location); //erase all values between the first 5 and next 7 list ::iterator start = find(list_nine.begin(), list_nine.end(), 5); list ::iterator stop = find(location, list_nine.end(), 7); list_nine.erase(start,stop);

Number of Elements cout << “Number of elements:” << list_nine.size(); if (list_nine.empty() ) cout << “list is empty”; int num = 0; count (list_five.begin(),list_five.end(), 17, num); if (num > 0) cout << “contains a 17”;

Generic Algorithms // place elements into sequence list_ten.sort(); // sorting using the widget compare function list_twelve.sort(widgetCompare); // elements are now reversed list_ten.reverse(); list_one.swap(list_four); // generic algorithm

Insert Iterators Assignment to an iterator is normally an overwriting operation, replacing the contents of the target location. (list_one holds 1, 2, 3 and list_two holds 7, 8, 9, 10) copy (list_one.begin(), list_one.end(), list_two.begin()); will erase first 3 values of list_two so it now holds 1, 2, 3, 10

Insert Iterators (continued) For lists (and sets) often instead want to perform insertion. Can be done by creating a list insert iterator. copy (list_one.begin(), list_one.end(), back_inserter(list_two) ); list_one now holds 7,8, 9, 10, 1, 2, 3

Forms of Insert Iterators back_inserter(list) –inserts at back of list (uses push_back to add values) front_inserter ( list ) –inserts at front of list (uses push_front to add values inserter (container, iterator) –copies elements into the location specified by the iterator

Program Assignment Write a program which generates a concordance for a text file ("r:\users\ds\story.txt") Read a word from the input file, if not in the list insert. Print the list in ascending sequence –Add count of times it appears –Paragraph numbers in which it appears –Create struct to hold data overload ==, <