1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
Data Structures: A Pseudocode Approach with C
Chapter 4 ADT Sorted List.
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 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Dynamic Structures & Arrays.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Applied Arrays: Lists and Strings. 2 Chapter 13 Topics  Meaning of a List  Insertion and Deletion of List Elements  Selection Sort of List Elements.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
1 Data Structures and Algorithms Pointers and Dynamic Data.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
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.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
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 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 13 Applied Arrays: Lists and Strings Dale/Weems/Headington.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Data Structure & Algorithms
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
114 3/30/98 CSE 143 Collection ADTs [Chapter 4] /30/98 Collection ADTs  Many standard ADTs are for collections  Data structures that manage groups.
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 14 Dynamic Data and Linked Lists
C++ Programming:. Program Design Including
CSCI-255 LinkedList.
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 13 Applied Arrays: Lists and Strings
C++ Plus Data Structures
Chapter 4 Linked Lists.
Traversing a Linked List
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
C++ Plus Data Structures
Chapter 4 Linked Lists.
Unsorted Lists CS3240, L. Grewe.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
Chapter 13 Applied Arrays: Lists and Strings
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Lists CMSC 202, Version 4/02.
Chapter 16-3 Linked Structures
Presentation transcript:

1 Chapter 16-1 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 Deletion of Elements in a Dynamic Linked List l Specification of a Dynamic Linked Sorted List l Insertion and Deletion of Elements in a Dynamic Linked Sorted List

3 What is a List? l A list is a varying-length, linear collection of homogeneous elements l Linear means that each list element (except the first) has a unique predecessor and each element (except the last) has a unique successor

4 To implement the List ADT The programmer must 1) choose a concrete data representation for the list, and 2) implement the list operations

5 Recall: 4 Basic Kinds of ADT Operations l Constructors -- create a new instance (object) of an ADT l Transformers -- change the state of one or more of the data values of an instance l Observers -- allow client to observe the state of one or more of the data values of an instance without changing them l Iterators -- allow client to access the data values in sequence

List Operations Transformers n Insert n Delete n Sort Observers n IsEmpty n IsFull n Length n IsPresent change state observe state 6

7 ADT List Operations Iterator n Reset n GetNextItem l Reset prepares for the iteration l GetNextItem returns the next item in sequence l No transformer can be called between calls to GetNextItem (Why?) Iteration Pair

8 Array-based class List Reset IsFull Length IsPresent Delete IsEmpty Insert GetNexItem Private data: length data [ 0] [1] [2] [MAX_LENGTH-1] currentPos SelSort

9 // Specification file array-based list (“list.h”) const int MAX_LENGTH = 50; typedef int ItemType; class List // Declares a class data type { public: // Public member functions List(); // constructor bool IsEmpty () const; bool IsFull () const; int Length () const; // Returns length of list void Insert (ItemType item); void Delete (ItemType item); bool IsPresent(ItemType item) const; void SelSort (); void Reset (); ItemType GetNextItem (); private: // Private data members int length; // Number of values currently stored ItemType data[MAX_LENGTH]; int CurrentPos; // Used in iteration }; 9

10 Implementation Structures l Use a built-in array stored in contiguous memory locations, implementing operations Insert and Delete by moving list items around in the array, as needed l Use a linked list in which items are not necessarily stored in contiguous memory locations l A linked list avoids excessive data movement from insertions and deletions

11 Implementation Possibilities for a List ADT List Linked list Built-in array Built-in dynamic data and pointers Built-in array of structs

12 Array Representation of a Linked List 2 head component link Node[0] Node[1] Node[2] Node[3] Node[4] Node[5] Node[6] Node[7]

13 Data Structure of Array Based Linked List struct NodeType { int component; int link; }; NodeType node[1000]; // Max nodes int head;

14 Insert a New Node into a Linked List 2 head component link Node[0] Node[1] Node[2] Node[3] Node[4] Node[5] Node[6] Node[7] Insert 25, Setting link to 7 Change link from 7 to 1

15 Delete a Node from a Linked List 2 head component link Node[0] Node[1] Node[2] Node[3] Node[4] Node[5] Node[6] Node[7] Change the link From 4 to 0 Deleted

16 A Linked List l A linked list is a list in which the order of the components is determined by an explicit link member in each node Each node is a struct containing a data member and a link member that gives the location of the next node in the list head ‘X’ ‘C’ ‘L’

17 Dynamic Linked List head “Ted” “Irv” “Lee” l A dynamic linked list is one in which the nodes are linked together by pointers and an external pointer (or head pointer) points to the first node in the list

18 Nodes can be located anywhere in memory l The link member holds the memory address of the next node in the list head 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL

19 // Type declarations struct NodeType { char component; NodeType* link; } typedef NodeType* NodePtr; // Variable DECLARATIONS NodePtr head; NodePtr ptr; 19 Declarations for a Dynamic Linked List. component. link ‘A’ 6000

20 Pointer Dereferencing and Member Selection. Component. link ‘A’ 6000 ptr. component. link ‘A’ 6000 *ptr ptr. component. link (*ptr).component ptr->component ‘A’ 6000

21 ptr is a pointer to a node. component. link ‘A’ 6000 ptr

22 *ptr is the entire node pointed to by ptr ptr. component. link ‘A’ 6000 *ptr

23 ptr->component is a node member ptr. component. link ptr->component (*ptr).component // Equivalent ‘A’ 6000

24 ptr->link is a node member ptr. component. link ptr->link (*ptr).link // Equivalent ‘A’ 6000

25 Traversing a Dynamic Linked List // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component // Or, do something else with node *ptr ptr = ptr->link; } ptr 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head

26 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

27 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

28 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

29 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

30 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

31 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

32 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

33 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

34 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

35 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

36 // Pre: head points to a dynamic linked list ptr = head; while (ptr != NULL) { cout component; // Or, do something else with node *ptr ptr = ptr->link; } ptr NULL 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head Traversing a Dynamic Linked List

37 The End of Chapter 16 Part 1