Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.

Slides:



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

Linked Lists Geletaw S..
Lists CS 3358.
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Lists: An internal look
CHP-5 LinkedList.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
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.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
Chapter 16 Stack and Queues part2
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 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.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
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.
More Linking Up with Linked Lists Chapter 11 5/19/2015 Adopted from instructor resource slides Nyhoff, ADTs, Data Structures and Problem Solving with C++,
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
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 &
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Brief Review.
Queues. Like Stacks, Queues are a special type of List for storing collections of entities. Stacks are Lists where insertions (pushes) and deletions (pops)
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
1 Data Organization Example 1: Heap storage management –Keep track of free chunks of memory Example 2: A simple text editor –Maintain a sequence of lines.
Review Dr. Yingwu Zhu. Outline ADT concept C++ Class List with class implementation.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
Exam1 Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Unit – I Lists.
C++ Programming:. Program Design Including
Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2009
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
CE 221 Data Structures and Algorithms
Data Structure Dr. Mohamed Khafagy.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Array Lists Chapter 6 Section 6.1 to 6.3
Arrays and Linked Lists
Chapter 16 Linked Structures
Dynamic allocation (continued)
Data Structures and Algorithms Memory allocation and Dynamic Array
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010

Consider Every Day Lists Groceries to be purchased Job to-do list List of assignments for a course Dean's list Can you name some others??

List List is a collection of things It is homogeneous --- the elements are all of the same type It has finite length The elements are arranged sequentially

LIST ADT Collection of data elements A sequence with a finite number of data items, all of the same type Basic operations Construction---create a empty list Empty---check if the list is empty Insert---insert an item Delete---remove a item Traverse---go through the list or part of it

Building a List class It consists of two steps: Design the class Implement the class

Implementation of the List In this chapter, two approaches to implement List: Array Based Static array Dynamic array Pointer (Link list) Based

Static Array based An array is a common choice for storing list elements Element are sequential Most languages support array Algorithm development is easy An array thus seems to be a natural storage structure for implementing a list

Static Array based Normally sequential orderings of list elements match with array elements Of course, the array must be large enough to hold all of the list elements It means we need to chose carefully

Static Array based Three variables we use to tract array: Array--the array that stores the list Capacity--array’s capacity Size--the number of list elements that are stored in the array Constructor: Since we use static array, we can let the complier handle the memory allocation, and our constructor only need to set size to 0 Empty: only need to check whether size is 0

Static Array based Traverse: Lists are easy traversed using a loop: for index i ranging from 0 to size-1: process array[i]

Static Array based Insert: it’s more complicated For example: if we wish to insert the new value 56 after the element 48: Original--- 23, 25, 34, 48, 61, 79, 82, 89, 91, 99 Produce--- 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

Static Array based Insert: we need to do Check the capacity Need to move array elements to make room for the new element

Static Array based Algorithm for insert: If size is equal to capacity: display error due to limit space If (pos size): display error due to error position Else: for i in ranging from size to pos+1: array[i]=array[i-1] //C++ starts index from 0 array[pos]=item size++

Static Array based The efficiency of this insert algorithm obviously depends on the number of array elements that must be shifted to make room for the new item In worst case, the new item must be inserted at the beginning of the list O(n) The best case occurs when the new item is inserted at the end of it O(1) Two important special kinds of lists for which are stacks and queues

Static Array based Delete: also requires shifting array elements For example, to delete the second item in: 23, 25, 34, 48, 56, 61, 79, 82, 89, 91, 99

Static Array based Algorithm for delete: If size is zero: issue a error message If (pos =size): issue an error message Otherwise: for index i ranging from pos to size-2: array[i]=array[i+1] size--

Static Array based The computing time of such a function is easily seen to be the same as insert function --- O(n) in the worst case and O(1) for the best

List Implantation by Linked list In any structure used to store the elements of a list, it must be possible to perform at least the following operation: 1. Locate the First element 2. Given the location of any list element, find its successor 3. Locate the end of the list

Linked List Linked list nodes contain Data part – stores an element of the list Next part – stores link/pointer to next element (when no next element, null value)

Design the list class Should contain at least the following function members Constructor empty() insert() delete() display()

Construction To construct an empty list, we simply make first a null link to indicate that it does not refer to any node: first = null_value ;

Empty We can then perform the Empty operation--- determining whether a list is empty, simply by checking whether first is null: first == null_value ?

Traverse We begin by initializing an auxiliary variable ptr to point to the first node: Initialize a variable ptr to point to first node Process data where ptr points

Traverse Traverse (ctd) set ptr = ptr->next, process ptr->data Continue until ptr == null

Insertion To insert a new data value into a linked list, we must first obtain a new node and store the value in its data part The second step is to connect this new node to existing list Two cases in this situation: (1) insertion after some element in the list and (2) insertion at the beginning of the list

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

Insertion Note: insertion also works at end of list pointer member of new node set to null Insertion at the beginning of the list predptr must be set to first pointer member of newptr set to that value (Where first points to) first set to value of newptr In all cases, no shifting of list elements is required !

Deletion For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list

Deletion Delete node containing 22 from list. Suppose ptr points to the node to be deleted predptr points to its predecessor (the 17) Do a bypass operation : Set the next pointer in the predecessor to point to the successor of the node to be deleted Deallocate the node being deleted. predptr ptr To free space

Deletion The second case is easier Just set the first points to the second node in the list and then returning the deleted node to the storage pool

Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary

Linked Lists - Disadvantages No longer have direct access to each element of the list Many sorting algorithms need direct access Binary search needs direct access Access of n th item now less efficient must go through first element, and then second, and then third, etc.

Outline linked lists implementation An Array-Based Implementation of Linked Lists

Pointer Based Node Since each node has two different parts, a data part and a next part, it is nature to have a node class with two data members. class Node { ElementType data; Node *next; }

Pointer Based Node To declare a pointer to a node: Node *ptr To allocate a new node pointer to by ptr: ptr = new Node; ptr = new Node(data_nalue); ptr = new Node(data_value, link_value); To access the data nad next part of the node pointed to by ptr: ptr -> data ptr -> next

Array-Based Implementation of Linked Lists Given a list with names Implementation would look like this

How to do it??? First of all, define a 2D array int array[10][2];

How to do it??? Constructor( ) we make “first” variable equals to 7 to indicate we start with position 7 first ==7; How to choose a start point??

How to do it??? Put 88 into data position and set next position to NULL array[first][0]=88; array[first][1]=-1; size==1;

How to do Insertion??? Find a empty position (in this example it’s node 1) Insert(array, 1) { ptr==1; // find a empty position pre_ptr==7; // you need a function to find pre_ptr array[new_node][0]==54; array[new_node][1]==array[pre_ptr][1]; array[pre_ptr][1]==ptr; size++; }

Insertion To insert 20 after 17 Need address of item before point of insertion predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node 20 newptr predptr

How to do Deletion???

Implementation Details For Insertion, there are also two cases to consider: insertion after some element in the list and insertion at the beginning of the list For deletion, there are also two cases to consider: Deleting an element that has a predecessor Delete the first element in the list