COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.

Slides:



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

Singly linked lists Doubly linked lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linked Lists Representation Traversing a Linked List
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Data Structures: A Pseudocode Approach with C
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.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
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.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
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 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Array 10 GB Hard Disk 2 GB 4 GB2 GB 3 GB DATA 4 GB Free Store data in the form of Array (Continuous memory locations) Solution-1: No Solution. Memory Insufficient.
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
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,
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 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
8/3/2007CMSC 341 BTrees1 CMSC 341 B- Trees D. Frey with apologies to Tom Anastasio.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists.
Arrays and Linked Lists
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 16 Linked Structures
Presentation transcript:

COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it back when it begins to rain. Robert Frost ( )

COSC 1P03 Data Structures and Abstraction 5.2 Dynamic Structures  collections  limitations of static structures (i.e. arrays)  fixed size  waste space  rearrangement of entries  sharing between structures?  dynamic data structures  change size over time  storage proportional to amount of information  linked structures  nodes (objects) connected together by references  dynamic allocation/deallocation  in array proximity implicit, in linked structure it is explicit  linear linked structures  aka linked lists

COSC 1P03 Data Structures and Abstraction 5.3 Sequentially-linked Structures  each node indicates its successor (via a reference)  first node located via reference  last node has no successor ( null )  each node represents one entity  empty collection has null reference

COSC 1P03 Data Structures and Abstraction 5.4 Representation  nodes are objects  dynamic creation  let entity objects be nodes?  add reference field  disadvantages  object must “know” it is on list  multiple lists  must modify class

COSC 1P03 Data Structures and Abstraction 5.5 Node Wrapper Class  separate class references objects  wrapper class, mixin  fields  reference ( p.theStudent )  link ( p.next )  constructor  visibility  class  fields  as sequentially-linked structure  general case  initial (empty) state  multiple lists  different sequence of Node s, same objects

COSC 1P03 Data Structures and Abstraction 5.6 Operations  insertion  where?  deletion  which node?  traversal  “visit” all nodes  like array traversal  search  special traversal  simple vs exhaustive

COSC 1P03 Data Structures and Abstraction 5.7 Insertion  insert where?  front  end  in some order (e.g. sorted)  at front  new entry points to previous front  list reference points to new entry  pattern  O(1)  repeated application  reverse order

COSC 1P03 Data Structures and Abstraction 5.8 Deletion  delete which node?  first  last  matching a key  only if not empty  delete first  move list pointer to second node  garbage collection  node  entity  pattern  O(1)

COSC 1P03 Data Structures and Abstraction 5.9 Traversal  sequential processing  to get to n th node must first get to (n-1) st node  travelling pointer  start at first node  move to node's successor  p = p.next  termination  no more nodes ( p == null )  pattern  O(n)  vs array traversal  sequential traversal pattern

COSC 1P03 Data Structures and Abstraction 5.10 Search  search key  two outcomes  success  failure  variant of traversal  two exits  end of list  found item  pattern  O(n)

COSC 1P03 Data Structures and Abstraction 5.11 Insertion at End of List  for insertion in order  find end of list  traversal  2 travelling pointers  initial state  q is predecessor of p  insert  pattern  traverse  updating p & q  insert  2 cases  q == null (empty list)  q != null (at end)  O(n)

COSC 1P03 Data Structures and Abstraction 5.12 Deletion of Last Node  must modify second last node  find second last node  traversal  2 travelling pointers  test  pattern  pre test  error  traverse  delete  2 cases  q == null (only node)  q != null (last node)  O(n)

Student Object The Student object pointer, Points to a object of type student Points to the next node in the list. Constructor, creates the node assigning the data and where the node is to point. No Class modifier, package-private, can be seen by all members within the same package. But not outside. should be omitted, Package Private or Protected

COSC 1P03 Data Structures and Abstraction 5.20 Insert at Front class Node { public StudenttheStudent; public Nodenext; public Node ( Student s, Node n ) { theStudent = s; next = n; };// constructor }// Node Assume Students A B C D are to be entered. A BCD List is initially empty List Constructor is called and a new node is created List Student A is assigned to the Data Area Next will point to null since the list is initially null.Once the constructor returns the object is assigned to ListThis process will repeat until there is no more data to enter

COSC 1P03 Data Structures and Abstraction 5.23 Deletion from front if ( list == null ) { throw new EmptyStructureException(); } else { entity = list.contents; list = list.next; }; A B C D entity List entity List entity List entity List If the list is not empty, set a pointer to the nodes contents Move the head of the list to the next node by following nextThe entity is return to the caller as data Since nothing is pointing to the node, the garbage collector reclaims the memory. This process can be repeated until the list is exhausted. A reference to an empty list should throw an exception, else crash!

List A BCD Initialize pointer p to the beginning of the list p Check to see if p != null i.e. the end of the list, it is not so continue.Process contents, E.g. Update something in the object pointed to by p.contents p Increment the pointer p, by following the next pointer Go back to the start of the loop and continue the process ppp p.next = null, So following p.next means p is now null. P!=null is now true, so we exit the algorithm Traversal of a List

List TraversalArray Traversal By comparison, these two algorithms are the same, the difference lies in the data structure type. Initialize the index to the first element of the array. Initialize a pointer to the first node of the list Stopping condition is the length of the array Stopping condition is the end of the list, or null pointer. Increment the array index. Increment the pointer

Searching a list List A BCD A search is a traversal with 1 extra condition. Assume we are searching for "E" p Set p to the beginning of the list. Start traversing the listCheck is p.contents = 'E', No Advance the pointer p and continue to loop p P.contents != 'E' p p p P== null, 'E' not in list Lets now assume our search criteria is 'B' p P.contents != 'B' p p P.contents == 'B' FOUND!!!!! The algorithm exits.At this point we would process the node pointed to by p

Insertion at the End Case 1 List The list is empty During initialization, q = null, p = null because list points to nullWhile loop is skipped since p == nullSince q has never been updated q == null, hence list is null i.e. empty This is the same as inserting at the front so call the constructor A

Insertion at the End Case 2 List A BCD We have a none empty list, and want to insert 'E' at the end. p q Pointers p and q are initialized While p != null we walk the pointers down the list. q always trailing p q p q p qpqp We found the end of the list, p == null so loop exits q != null, it is pointing to the last node, so do the else part Constructor is called to create a new node with 'E', it is linked to q.next E A

Deletion from the End List A BCD q p q p qp p q Initialize p and q as before. Walk down the list, except this time stop when p.next == nullP now points to the last node, the contents of the entity is removed.If q is null then there was only 1 node in the list.Setting the list = null effectively would remove the node. Otherwise, there is at least 2 nodes in the list, q pointing to the second lastSetting q.next == null will remove the last node. B

COSC 1P03 Data Structures and Abstraction 5.33 The End