Data Structures Intro/Linked List Review CIS 237 – Data Structures.

Slides:



Advertisements
Similar presentations
Lists Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks, Queues, and Linked Lists
Linked Lists.
1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Linear Lists – Array Representation
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
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.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSE Lecture 12 – Linked Lists …
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Ceng-112 Data Structures I Chapter 5 Queues.
Data Structures: A Pseudocode Approach with C
Ceng-112 Data Structures I 1 Chapter 3 Linear Lists.
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.
Chapter 4 ADT Sorted List.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
Ceng-112 Data Structures ISerap ATAY, Ph. D. 1 Chapter 3 – Part 2 Linear Lists.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists (part 2)
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists (part 2)
1 Data Structures and Algorithms Stacks and Queues.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Chapter 7 A Queues. © 2004 Pearson Addison-Wesley. All rights reserved7 A-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear,
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
Stacks Chapter 3 Objectives Upon completion you will be able to
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
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.
Data Structures( 数据结构 ) Chapter3:Linked List. 2 西南财经大学天府学院 Vocabulary Linear List 线性表 Linked List 链表 Retrieval 检索 Traversal 遍历 Node 结点 Circularly Linked.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Chapter 16: Linked Lists.
CSE 1342 Programming Concepts
C++ Programming:. Program Design Including
Queues Chapter 4.
Stacks and Queues Chapter 4.
Queues Chapter 4.
Chapter 4 Linked Lists
Data Structures: A Pseudocode Approach with C
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Doubly Linked List Implementation
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Pointers & Dynamic Data Structures
Doubly Linked List Implementation
Chapter 16-3 Linked Structures
Presentation transcript:

Data Structures Intro/Linked List Review CIS 237 – Data Structures

Pseudocode Algorithm sample (ref pageNumber ) This algorithm reads a file and prints a report. PrepageNumber must be initialized PostReport printed. pageNumber contains number of pages in the report. Return number of lines printed. 1 Open file 2 lines = 0 3 loop (not end of file) 1read file 2if (full page) 1 form feed 2 pageNumber = pageNumber write page heading 3 end if 4 write report line 5lines = lines end loop 5 close file 6 return lines

Kinds of Data Atomic Data –integer –floating point –character Data Structure –arrays –classes Abstract Data Type

Characteristics –Declaration of data –Declaration of operations –Encapsulation of data and operations Abstraction Encapsulation

Designing Abstract Data Types classes/templates data member functions the Big-3 overloading operators

Lists Linear Lists GeneralRestricted UnorderedOrderedFIFO (queue) LIFO (stack)

Linked Lists Component Type –Must work with operators used on them Nodes The list

Linked Lists - Nodes datalink applegrapepeachpear

Linked List - ADT applegrapepeachpear head count 4 head count 0

Keyed List Data Structure applegrapepeachpear head count 4 Simple List Operations –Empty –Create –Full*

Other List Operations Size Ordered Insert (Ascending) Remove a Value Find a Value in the List Print List (overload of the operator << ) The Destructor and Destroy = and the Copy Constructor and Copy

orderedInsert Algorithm Algorithm orderedInsert (val dataIn <dataType) Inserts data into a new node in the linked list so the list is in ascending order PredataIn contains the data to be inserted Postdata is inserted in sequence Returntrue if successful, false otherwise 1. if list empty or dataIn < data at the head //inserting at head of list Create a new head with dataIn If create fails return false 2. else 1. trailp = Null 2. p = head 3. while (p is not Null and dataIn is greater than p->data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr 6. if create fails return false 3. end if 4. count = count return true

Remove Algorithm Algorithm remove( dataout ) Deletes data from a linked list and returns success or failure PredataOut is the data Postnode with data removed from list Return true if found and removed, false if not there 1.p = head 2.trailp = Null 3.while p is not Null and p->data not dataout 1.trailp = p 2.p = p->next 4.end while 5.if p is Null return false //not in list 6.if p is head 1.head = head->next 7.else 1.trailp->next = p->next 8.end if 9.delete p 10.count = count return true

Find Algorithm Algorithm findList(target ) Searches for the target and return success or failure Pretarget is the item with the value being sought Returntrue if found, false otherwise 1.p = head 2.while p is not Null and p->data is not target 1.p = p->next 3.end while 4.if p is not Null and p->data equals target 1.return true 5.else 1.return false 6.end if

Destroy the list starting at L Algorithm destroy(ref node L) Deletes all the nodes in the list starting at L Pre L contains the node at which to start destroying Postnodes from L to end gone 1.while L is not null 1.doomed = L 2.L = L ->next 3.delete doomed 2.end while

Assignment Algorithm operator=(const ref rightList) Makes a deep copy of the list from rightList to the current instance PrerightList is an initialized list Returnreturns the current instance 1. If (the current instance and the rightList are not the same) 1. destroy the current instance 2. head is assigned the copy of the rightList 3. count = rightList->count 2. end if 3. return *this

Copy A List Algorithm copy (pointer Node L) Makes a copy from the list that begins with L, does a deep copy PreL is a node at which to start copying Post current instance contains a copy of the list 1.first = null 2.last = null 3.if L is not null 1.first = last = a new node with L->data as its data 2.for p = L->next; p not null; p = p->next, last=last->next 1.last->next = new node with p->data as data 3.end for 4.return first 4.end if

Operator << Algorithm operator<<(ostream &out, list l) Prints each data item to out using an iterator. Data type must have << operator overloaded Preout must contain an output stream and a list Post all values in list will be printed out 1.for itr.start(); itr.move(); itr.next() 1.out << itr.value() << 2.end for

Dummy Header grapepeachpear head count 3 head count 0

Dummy Header Changes Constructor must create dummy node Traversals begin at head->next (head for trailer) Empty must check head->next No special cases –orderedInsert –remove

Sample - orderInsert Algorithm orderedInsert (val dataIn <dataType) Inserts data into a new node in the linked list so the list is in ascending order PredataIn contains the data to be inserted Postdata is inserted in sequence Returntrue if successful, false otherwise 1. trailp = head 2. p = head->next 3. while (p is not Null and dataIn is greater than p->data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr 6. if create fails return false 7. count = count return true

Doubly Linked List grape peachpear head count 3 rear count head 0 rear

Changes for Doubly Linked List Node –Must now have a previous pointer List –Has rear –Constructor must now initialize rear Copy –Must create node with prev pointer –Must return the last node copied –Calls to copy must then include rear

Ascending Insert - doubly linked/dummy header Algorithm orderedInsert (val dataIn <dataType) Inserts data into a new node in the linked list so the list is in ascending order PredataIn contains the data to be inserted Postdata is inserted in sequence Returntrue if successful, false otherwise 1. trailp = head 2. p = head->next 3. while (p is not Null and dataIn is greater than p->data) 1. trailp = p 2. p = p->next 4. end while 5. trailp->next = a new node with data in as data and p as its next ptr and trailp as prev ptr 6.if create fails return false 7.if trailp is the rear//either new rear or must set prev pointer of after node 1.rear is now trailp->next 8.else 1.p->prev = trailp->next 9.end if 10. count = count return true

Remove - doubly linked/dummy header Algorithm remove( dataout ) Deletes data from a linked list and returns success or failure PredataOut is the data Postnode with data removed from list Return true if found and removed, false if not there 1.p = head->next 2.trailp = head 3.while p is not Null and p->data not dataout 1.trailp = p 2.p = p->next 4.end while 5.if p is Null return false //not in list 6.trailp->next = p->next 7.if p is rear//if deleting last node reset rear else set next nodes prev ptr 1.rear = trailp 8.else 1.trailp->next->prev = trailp 9.end if 10.delete p 11.return true

Reverse Iterator Parts –Curr –Itr the list being iterated Last More –Stops at itr.head not NULL Back Value

Complex Component Types Use any type Should have relational operators overloaded << overloaded if using overloaded << in application Constructor in node/list Find

Circular List with a Dummy Header grape peachpear head count 3 head count 0

Changes For Circular List No need for rear –Reverse iterator start at itr.head->prev Loop control –List operations head not Null –Iterator operations itr.head Constructor set up next and prev Insert/Remove eliminate rear check Copy mark first element