E.G.M. Petrakislists1 Lists  List: finite sequence of data elements  all elements have the same data type  The operations depend on the type of the.

Slides:



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

Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
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.
Stacks, Queues, and Linked Lists
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
1 Linked List Position (1). 2 Linked List Position (2)
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.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
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.
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
Stacks, Queues, and Deques
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
E.G.M. Petrakislists, stacks, queues1 Lists List: finite sequence of data elements –Ordered: each element has a position in list –All elements has the.
Stacks, Queues, and Deques
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
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.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
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.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
List Interface and Linked List Mrs. Furman March 25, 2010.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Lists Chapter 5 (continued)
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stacks and Queues.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Data Structures ADT List
Linked List (Part I) Data structure.
Basics of Algorithm Analysis
Lists List: finite sequence of data elements
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
ADT list.
Linked Lists Chapter 5 (continued)
Stacks, Queues, and Deques
Linked Lists Chapter 5 (continued)
Presentation transcript:

E.G.M. Petrakislists1 Lists  List: finite sequence of data elements  all elements have the same data type  The operations depend on the type of the list and not on the data type  List: the most general type  Ordered: element is ascending order  Stacks, Queues: not all operations are allowed

E.G.M. Petrakislists2 list or head info next list new element Insertion after current position current rear or tail

E.G.M. Petrakislists3 list or head list new element Insertion at current position current rear or tail

E.G.M. Petrakislists4 list or head list or head deleted element Deletion after current position current rear or tail

E.G.M. Petrakislists5 list or head list or head deleted element Deletion at current position current rear or tail

E.G.M. Petrakislists6 Terminology  empty: contains no elements  length: number of elements in list  head, or list: pointer to the beginning of the list  tail: pointer to the last element  current: pointer to current element  ordered: elements in ascending or descending order

E.G.M. Petrakislists7 Operations  setFirst: set “current” to “head”  setPos(i): sets current to the i-th element  currValue: returns value of “current” element  next/prev: “current” points to next/previous element  clear: delete all elements  insert: inserts an element at/after current position  append: inserts an element at “tail”  remove: delete the element at/after “current” position  isInList: true/false if current position is in list  isEmpty: true/false if list is empty

E.G.M. Petrakislists8 ADT List interface List { // List ADT in Java public void clear(); // Remove all Objects public void insert(Object item); // Insert at curr pos public void append(Object item); // Insert at tail public Object remove(); // Remove/return curr public void setFirst(); // Set to first pos public void next(); // Move to next pos public void prev(); // Move to prev pos public int length(); // Return curr length public void setPos(int pos); // Set curr position public void setValue(Object val); // Set current value public Object currValue(); // Return curr value public boolean isEmpty(); // True if empty list public boolean isInList(); // True if in list } // interface List

E.G.M. Petrakislists9 Iteration  Iterate through the whole list : MyList for (MyList.first( ); MyList.isInList( ); MyList.next( )) DoSomething(MyList.currValue( ));  If MyList: ( ) and current points to 32 thenMyList.insert(90) changes the list to be ( )

E.G.M. Petrakislists10 List Implementations  Array-based: very fast  the elements are stored in array  static: actual number of elements less than size allocated  Dynamic Memory: slower, more efficient  allocates memory for new elements  dynamic: no restriction on number of elements (except memory size)

E.G.M. Petrakislists11 Array Implementation (1)  Elements in continuous array positions  Head of list at pos 0  Insertion and Deletion cause shifting of elements

E.G.M. Petrakislists12 class AList implements List { // Array-based list class private static final int defaultSize = 10; private int msize; // Maximum size of list private int numInList; // Actual list size private int curr; // Position of curr private Object[] listArray; // Array holding list AList() { setup(defaultSize); } // Constructor AList(int sz) { setup(sz); } // Constructor private void setup(int sz) { // Do initializations msize = sz; numInList = curr = 0; ListArray = new Object[sz]; // Create listArray } public void clear() // Remove all Objects from list { numInList = curr = 0; } // Simply reinitialize values

E.G.M. Petrakislists13 public void insert(Object it) { // Insert at curr pos Assert.notFalse(numInList < msize, "List is full"); Assert.notFalse((curr >=0) && (curr <= numInList), "Bad value for curr"); for (int i=numInList; i>curr; i--) // Shift up listArray[i] = listArray[i-1]; listArray[curr] = it; numInList++; // Increment list size } public void append(Object it) { // Insert at tail Assert.notFalse(numInList < msize, "List is full"); listArray[numInList++] = it; // Increment list size } public Object remove() { // Remove and return Object Assert.notFalse(!isEmpty(), "No delete: list empty"); Assert.notFalse(isInList(), "No current element"); Object it = listArray[curr]; // Hold removed Object for (int i=curr; i<numInList-1; i++) // Shift down listArray[i] = listArray[i+1]; numInList--; // Decrement list size return it; }

E.G.M. Petrakislists14 public void setFirst() { curr = 0; } // Set to first public void prev() { curr--; } // Move curr to prev public void next() { curr++; } // Move curr to next public int length() { return numInList; } public void setPos(int pos) { curr = pos; } public boolean isEmpty() { return numInList == 0; } public void setValue(Object it) { // Set current value Assert.notFalse(isInList(), "No current element"); listArray[curr] = it; } public boolean isInList() // True if curr within list { return (curr >= 0) && (curr < numInList); } } // Array-based list implementation

E.G.M. Petrakislists15 Array Implementation (2) list nodes NULL

E.G.M. Petrakislists List List List List 4

E.G.M. Petrakislists List 4 List 2 List 3 List 1

E.G.M. Petrakislists18 class AList implements List { // Array-based list class private static final int defaultSize = 10; private int msize; // Maximum size of list private int numInList; // Actual list size private int curr; // Position of curr private int avail;// next available position private Object[] listArray; // Array holding list private int[] listarray_next;// array holding pointers to next Object private void setup(int sz) {// Do initializations msize = sz; numinlist = curr = 0; listarray = new Object[sz]; listarray_next = new int[sz]; avail = 0;// the first available element for (i=0; i < msize; i++) listarray_next[i] = i+1;// each elem points to its successor listarray_next[msize-1] = nothing;// the last elem has no next }

E.G.M. Petrakislists19 private int get_node( ) { // Get next available node if (avail == nothing) // from stack error(‘list overflow’) else { int pos = avail; avail = listarray_next[avail]; return pos; } private void free_node (int p) { // make node available listarray_next[p] = avail; // push node back to stack avail =p; } private void insert(int p, Object x) {// insert after node pointed to by “p” if (p = msize) error (‘void insertion’) else { int q = get_node( ); listarray[q] = x; listarray_next[q] = listarray_next[p]; listarray_next[p] = q; }

E.G.M. Petrakislists20 private void delete(int p, Object x) { if ( p > 0 || p >= msize) // deletes elem after elem error (‘void deletion’); // pointed to by “p” else { int q = listarray_next[p]; x = listarray[q]; listarray_next[p] = listarray_next[q]; free_node(q); } public AList() { setup(defaultSize); } // Constructor public AList(int sz) { setup(sz); } // Constructor public void clear( ) {…}// remove all ELEMs from list public void insert(Object it) {…}// insert ELEM at current position public void append(Object it) {…}// insert ELEM at tail of list public Object remove( ) {…}// remove and return current ELEM public void setFirst( ) {…}// set curr to first position; public void prev( ) {…}// move curr to previous position; public void next( ) {…}// move curr to next position; public int length( ) {…}// return current length of list } // Array-based list implementation

E.G.M. Petrakislists21 Dynamic Memory  Allocates memory for new elements as needed  Each node is a distinct object  The node class class Link { // A linked-list node private Object element; // Object for this node private Link next; // Pointer to next node Link(Object it, Link nextval) // Constructor { element = it; next = nextval; } Link(Link nextval) { next = nextval; } // Constructor Link next() { return next; } Link setNext(Link nextval) { return next = nextval; } Object element() { return element; } Object setElement(Object it) { return element = it; } }

E.G.M. Petrakislists22 class LList implements List { // Linked list class private Link head; // Pointer to list header private Link tail; // Pointer to last Object in list protected Link curr; // Position of current Object LList(int sz) { setup(); } // Constructor LList() { setup(); } // Constructor private void setup() { tail = head = curr = new Link(null); } public void setFirst() { curr = head; } public void next() { if (curr != null) curr = curr.next(); } public void prev() { // Move to previous position Link temp = head; if ((curr == null) || (curr == head)) // No prev { curr = null; return; } // so return while ((temp != null) && (temp.next() != curr)) temp = temp.next(); curr = temp; }

E.G.M. Petrakislists23 public Object currValue() { // Return current Object if (!isInList()) return null; return curr.next().element(); } public boolean isEmpty() // True if list is empty { return head.next() == null; } public void insert(Object it) {// Insert Object at current position Assert.notNull(curr, "No current element"); curr.setNext(new Link(it, curr.next())); if (tail == curr) // Appended new Object tail = curr.next(); } public Object remove() { // Remove/return curr Object if (!isInList()) return null; Object it = curr.next().element(); // Remember value if (tail == curr.next()) tail = curr; // Set tail curr.setNext(curr.next().next()); // Cut from list return it; // Return value }

E.G.M. Petrakislists24 public void append(Object it) // insert Elem at tail of list { tail.setNext(new Link(it, NULL)); } public int length( ) {// return current length of list int cnt = 0; for (Link temp = head.next(); temp != NULL; temp = temp.next()) cnt++; // count Elems return cnt; } public void setPos(int pos) {// set curr to position curr = head; for (int i = 0; (curr != NULL) && (i < pos) i++) curr = curr.next(); } public void setValue(Object val) {// set current Elem's value Assert.notFalse(isInList(), "No current element"); curr.next().setElement(val); } public bool isInList( )// TRUE if curr is within list { return (curr != NULL) && (curr.next() != NULL); } } // Linked list class implementation

E.G.M. Petrakislists25 Comparison  Array-Based Lists:  insertion and deletion are  (n)  prev and direct access are  (1)  fixed space allocated in advance  space reorganization if the array is full  faster in most cases  Linked Lists:  insertion and deletion are  (1)  prev and direct access are  (n)  space grows with number of elements  every element requires overhead  slower

E.G.M. Petrakislists26 Doubly Linked List  Allows for direct access to both next and previous elements of the current pointer  insert (delete) operations update both “next” and “prev” pointers  easy implementation curr.setNext(new DLink(it, curr.next(), curr)); if (curr.next().next() != null) curr.next().next().setPrev(curr.next()); if (tail == curr) // Appended new Object tail = curr.next();

E.G.M. Petrakislists27 Insertion in Doubly Linked List current

E.G.M. Petrakislists28 current Deletion in Doubly Linked List

E.G.M. Petrakislists29 Circular Linked Lists  The next pointer of the last element points to the first element