250 likes | 257 Views
Learn about the List Abstract Data Type (ADT) and its basic implementations using arrays and linked lists. Explore the operations and advantages of each implementation method.
E N D
CE 221Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 Izmir University of Economics
Introduction • We will discuss three of the most simple and basic data structures. • Virtually every significant program will use at least one of these structures explicitly, and a stack is always implicitly used in a program, whether or not you declare one. Izmir University of Economics
Abstract Data Types (ADTs) • An abstract data type (ADT) is a set of objects. ADTs are mathematical abstractions; implementation is not considered. • Objects such as lists, sets, and graphs along with their operations can be viewed as abstract data types just as integers, reals, and booleans are data types. • There is no rule telling which operations must be supported for each ADT; this is a design issue. Izmir University of Economics
The List ADT - I • A general list of the form: A0, A1, A2, ..., AN-1 • The size of this list is N. Special list of size 0 is an empty list. • For any list except empty list, Ai follows or succeeds Ai-1 (i<N); Ai-1 precedes Ai,(i>0). The predecessor of A0 and the successor of AN-1 are not defined. • First element of the list is A0 and the last element is AN-1. The position of element Ai is i. Izmir University of Economics
The List ADT - II • Associated with these definitions are some popular operations on the list ADT: • printList, makeEmpty, find (returns the position of the first occurence of an item), insert and remove ( generally insert and remove from some position), findKth (returns the element in some position). • Example: 34, 12, 52, 16, 12. find(52)2; insert(x, 2) 34, 12, x, 52, 16, 12. Izmir University of Economics
Simple Array Implementation of Lists • Use arrays for the underlying implementation. Arrays are created with a fixed capacity; grow it when needed by doubling its capacity. • With this implementation mechanism; printList takes O(N) time; findKth takes O(1) time. • Insertions and deletions are expensive. In the worst case, inserting into position 0 (at the front of the list) requires pushing the entire array down one spot to make room, and deleting the first requires shifting all up by one: O(N). On the average half the list is moved: still O(N). Best case occurs when they are performed at the higher end: O(1). Izmir University of Economics
Simple Linked Lists - I • In order to avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, since otherwise entire parts of the list will need to be moved. • The linked list consists of a series of nodes. Each node contains the element and a link (next) to the successor node. • To execute printList() or find(x), start at the first node and then traverse the list by following the next links: O(N) • findKth(i) takes O(i) (but you can sort the calls by i) Izmir University of Economics
Simple Linked Lists - II • The remove operation can be executed in one next pointer change. • The insert command requires obtaining a new node from the system by using a new calland then executing two pointer maneuvers. Izmir University of Economics
Simple Linked Lists - III • If we know where a change is to be made, inserting or removing an item from a linked list involves only a constant number of changes to node links. • The special case of adding to the front or removing the first item is thus a constant-time operation if there exists a link to the front of the list. The same holds when adding at the end as long as another link is provided to the last node. • Removing the last item is trickier! Why? We need to find the next-to-last item and change its next link to NULL and then update the pointer to the last node. A third link to next-to-last node does not work (it too needs to be updated.) Solution: each node maintain a link to its previous node: Doubly linked list Izmir University of Economics
Vector: Array Implementation - I • Vector implementation: Advantage: indexable in constant time; Disadvantage: insertion and removal of items at locations other than the end are expensive. • List implementation: Advantage: Insertions and removals are cheap; Disadvantage: is not easily indexable. Izmir University of Economics
Vector: Array Implementation - II Izmir University of Economics
Vector: Array Implementation - III Izmir University of Economics
Vector: Array Implementation - IV Izmir University of Economics
Vector: Array Implementation – V(optional) Izmir University of Economics
Linked List Implementation - I • The List will,this time, be implemented as a doubly linked list. We will also maintain pointers to both ends of the list. • The two extra nodes created at the front and at the back of the list are header node and tail node respectively (sentinels nodes). Izmir University of Economics
Linked List Implementation - IIa Izmir University of Economics
Linked List Implementation - IIb later Izmir University of Economics
Linked List Implementation - III Izmir University of Economics
Linked List Implementation - IV Izmir University of Economics
Linked List Implementation - V Izmir University of Economics
Linked List Implementation - VI Izmir University of Economics
Linked List Implementation – VII (optional) Izmir University of Economics
Linked List Implementation – VIII (optional) Izmir University of Economics
Linked List Implementation – IX (optional) this Izmir University of Economics
Homework Assignments • 3.2, 3.4, 3.5, 3.15, 3.29, 3.34.a, 3.34.b, 3.36, 3.37 • You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me. Izmir University of Economics