CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.

Slides:



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

Linked Lists Geletaw S..
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.
Lists CS 3358.
COMP171 Fall 2005 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.
Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
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.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 3: Arrays, Linked Lists, and Recursion
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 Object-Oriented Design Inheritance and Class Examples.
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.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO CHAPT. 9.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
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.
Chapter 5 Linked Lists II
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
CS2006- Data Structures I Chapter 5 Linked Lists III.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
10/07/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
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.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Link-Based Implementations
Unit – I Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
CC 215 Data Structures Queue ADT
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Lists CS 3358.
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
LINKED LISTS CSCD Linked Lists.
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Lists CSE 373 Data Structures.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Linked Lists Chapter 5 (continued)
Lists CSE 373 Data Structures.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1

Readings 2  Reading  Section 3.1 ADT (recall, lecture 1): Abstract Data Type (ADT): Mathematical description of an object with set of operations on the object.  Section 3.2 The List ADT

List ADT 3  What is a List?  Ordered sequence of elements A 1, A 2, …, A N  Elements may be of arbitrary type, but all are of the same type  Common List operations are:  Insert, Find, Delete, IsEmpty, IsLast, FindPrevious, First, Kth, Last, Print, etc.

Simple Examples of List Use 4  Polynomials  x x 85  Unbounded Integers   Text  “ This is an example of text ”

List Implementations 5  Two types of implementation:  Array-Based  Pointer-Based

List: Array Implementation 6  Basic Idea:  Pre-allocate a big array of size MAX_SIZE  Keep track of current size using a variable count  Shift elements when you have to insert or delete ANAN …A4A4 A3A3 A2A2 A1A1 MAX_SIZE-1count-1…3210

List: Array Implementation 7 FEDCBA MAX_SIZE Insert Z in kth position EDCZBA MAX_SIZE F 6

Array List Insert Running Time 8  Running time for N elements?  On average, must move half the elements to make room – assuming insertions at positions are equally likely  Worst case is insert at position 0. Must move all N items one position before the insert  This is O(N) running time. Probably too slow

Review Big Oh Notation 9  T(N) = O(f(N)) if there are positive constants c and n 0 such that: T(N) n 0  T(N) = O(N) linear

List: Pointer Implementation 10  Basic Idea:  Allocate little blocks of memory (nodes) as elements are added to the list  Keep track of list by linking the nodes together  Change links when you want to insert or delete Value NULL L node ValueNext node Next

© 2005 Pearson Addison- Wesley. All rights reserved 4-11 Preliminaries Figure 4.1 a) A linked list of integers; b) insertion; c) deletion

© 2005 Pearson Addison- Wesley. All rights reserved 4-12 Pointer-Based Linked Lists  A node in a linked list is usually a struct struct Node{ int item; Node *next; };  A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node

© 2005 Pearson Addison- Wesley. All rights reserved 4-13 Pointer-Based Linked Lists  The head pointer points to the first node in a linked list  If head is NULL, the linked list is empty

© 2005 Pearson Addison- Wesley. All rights reserved 4-14 Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

© 2005 Pearson Addison- Wesley. All rights reserved 4-15 Displaying the Contents of a Linked List  Reference a node member with the -> operator p->item;  A traverse operation visits each node in the linked list  A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->next) cout item << endl;

© 2005 Pearson Addison- Wesley. All rights reserved 4-16 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next

© 2005 Pearson Addison- Wesley. All rights reserved 4-17 Deleting a Specified Node from a Linked List  Deleting an interior node prev->next=cur->next;  Deleting the first node head=head->next;  Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

© 2005 Pearson Addison- Wesley. All rights reserved 4-18 Deleting a Specified Node from a Linked List Figure 4.10 Deleting a node from a linked list Figure 4.11 Deleting the first node

© 2005 Pearson Addison-Wesley. All rights reserved 4-19 Inserting a Node into a Specified Position of a Linked List  To insert a node between two nodes newPtr->next = cur; prev->next = newPtr; Figure 4.12 Inserting a new node into a linked list

© 2005 Pearson Addison-Wesley. All rights reserved 4-20 Inserting a Node into a Specified Position of a Linked List  To insert a node at the beginning of a linked list newPtr->next = head; head = newPtr; Figure 4.13 Inserting at the beginning of a linked list

© 2005 Pearson Addison-Wesley. All rights reserved 4-21 Inserting a Node into a Specified Position of a Linked List  Inserting at the end of a linked list is not a special case if cur is NULL newPtr->next = cur; prev->next = newPtr; Figure 4.14 Inserting at the end of a linked list

© 2005 Pearson Addison- Wesley. All rights reserved 4-22 Inserting a Node into a Specified Position of a Linked List  Determining the point of insertion or deletion for a sorted linked list of objects for(prev = NULL, cur= head; (cur != null)&& (newValue > cur->item); prev = cur, cur = cur->next);

© 2005 Pearson Addison- Wesley. All rights reserved 4-23 A Pointer-Based Implementation of the ADT List  Public methods  isEmpty  getLength  insert  remove  retrieve  Private method  find  Private Data Members  head  size  Local variables to member functions  cur  prev

© 2005 Pearson Addison- Wesley. All rights reserved 4-24 Constructors and Destructors  Default constructor initializes size and head  Copy constructor allows a deep copy  Copies the array of list items and the number of items  A destructor is required for dynamically allocated memory

© 2005 Pearson Addison- Wesley. All rights reserved 4-25 Comparing Array-Based and Pointer-Based Implementations  Size  Increasing the size of a resizable array can waste storage and time  Storage requirements  Array-based implementations require less memory than a pointer-based ones

© 2005 Pearson Addison- Wesley. All rights reserved 4-26 Comparing Array-Based and Pointer-Based Implementations  Access time  Array-based: constant access time  Pointer-based: the time to access the i th node depends on i  Insertion and deletions  Array-based: require shifting of data  Pointer-based: require a list traversal

© 2005 Pearson Addison- Wesley. All rights reserved 4-27 Saving and Restoring a Linked List by Using a File  Use an external file to preserve the list between runs  Do not write pointers to a file, only data  Recreate the list from the file by placing each item at the end of the list  Use a tail pointer to facilitate adding nodes to the end of the list  Treat the first insertion as a special case by setting the tail to head

Pointer Implementation Issues 28  Whenever you break a list, your code should fix the list up as soon as possible  Draw pictures of the list to visualize what needs to be done  Pay special attention to boundary conditions:  Empty list  Single item – same item is both first and last  Two items – first, last, but no middle items  Three or more items – first, last, and middle items

Pointer List Insert Running Time 29  Running time for N elements?  Insert takes constant time (O(1))  Does not depend on input size  Compare to array based list which is O(N)

© 2005 Pearson Addison-Wesley. All rights reserved 4-30 Circular Linked Lists  Last node references the first node  Every node has a successor  No node in a circular linked list contains NULL Figure 4.25 A circular linked list

© 2005 Pearson Addison-Wesley. All rights reserved 4-31 Circular Linked Lists Figure 4.26 A circular linked list with an external pointer to the last node

© 2005 Pearson Addison- Wesley. All rights reserved 4-32 Doubly Linked Lists  Each node points to both its predecessor and its successor  Circular doubly linked list  precede pointer of the dummy head node points to the last node  next reference of the last node points to the dummy head node  No special cases for insertions and deletions

© 2005 Pearson Addison- Wesley. All rights reserved 4-33 Doubly Linked Lists Figure 4.28 A doubly linked list

© 2005 Pearson Addison- Wesley. All rights reserved 4-34 Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

© 2005 Pearson Addison- Wesley. All rights reserved 4-35 Doubly Linked Lists  To delete the node to which cur points (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede;  To insert a new node pointed to by newPtr before the node pointed to by cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

© 2005 Pearson Addison- Wesley. All rights reserved 4-36 Summary  Each pointer in a linked list is a pointer to the next node in the list  Algorithms for insertions and deletions in a linked list involve traversing the list and performing pointer changes  Inserting a node at the beginning of a list and deleting the first node of a list are special cases

© 2005 Pearson Addison- Wesley. All rights reserved 4-37 Summary  Recursion can be used to perform operations on a linked list  In a circular linked list, the last node points to the first node  Dummy head nodes eliminate the special cases for insertion into and deletion from the beginning of a linked list

Assignment 1  Add to the LList class implementation a member function to reverse the order of the elements on the list. Your algorithm should run in Θ (n) time for a list of n elements. 38