Lists List: finite sequence of data elements

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

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
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 Linked List Position (1). 2 Linked List Position (2)
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:
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
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.
Linked List Improvements & Memory. BigO's What is BigO for our basic linked list operations? InsertStart Insert at middle InsertEnd Retrieve First Value.
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.
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.
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Chapter 16: Linked Lists.
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
Chapter 4 The easy stuff.
Linked Lists Chapter 5 (continued)
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Data Structures and Algorithms
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Doubly linked lists.
Programmazione I a.a. 2017/2018.
CMSC 341 Lecture 5 Stacks, Queues
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
Queues.
Chapter 4 Linked Lists.
Arrays and Linked Lists
Chapter 18: Linked Lists.
Data Structures ADT List
Linked List (Part I) Data structure.
Basics of Algorithm Analysis
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
ADT list.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Doubly Linked List Implementation
Data Structures and Algorithms
Chapter 4 Linked Lists.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Lecture 16 Section 6.2 Thu, Mar 1, 2007
CSI 1340 Introduction to Computer Science II
Stacks, Queues, and Deques
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

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. Petrakis lists

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

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

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

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

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. Petrakis lists

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 find(key): set current to the first occurrence of “key” isInList: true/false if current position is in list isEmpty: true/false if list is empty E.G.M. Petrakis lists

ADT List template <class ELEM> class list { // list class ADT in C++ public: list(const int = LIST_SIZE); // constructor ~list( ); // destructor void clear ( ); // remove all elements void insert(const ELEM &); // inset at current void append(const ELEM &); // insert at tail ELEM remove ( ); // remove current void setFirst ( ); // set current to HEAD void prev ( ) ; // set current to previous void next ( ); // set current to next int length ( ) const; // return size of list void setPos(const int); // set current to new pos void setValue(const ELEM &) // set current’s value bool isEmpty( ) const; // true/false on empty bool isInList( ) const; // true/false if current is in list bool find(const ELEM &); // find value of current }; E.G.M. Petrakis lists

Iteration Iterate through the whole list : MyList for (MyList.first( ); MyList.isInList( ); MyList.next( )) DoSomething(MyList.currValue( )); If MyList: (12 32 15) and current points to 32 then MyList.insert(90) changes the list to be (12 32 90 15) E.G.M. Petrakis lists

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. Petrakis lists

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

template <class ELEM> class list { //array based list class private: int msize // maximum size of list int numinlist //actual number of ELEMs int curr; // position of “current” ELEM* listarray; //array holding ELEMs public: list (const int=LIST_SIZE); // constructor ~list( ); // destructor void clear( ); // remove all ELEMs from list void insert(const ELEM &); // insert ELEM at current position void append(const ELEM &); // insert ELEM at tail of list ELEM remove( ); // remove and return current ELEM void setFirst( ); // set current to first position void prev( ); // move current to previous position void next( ); // move current to next position int length( ) const; // return current length of list void setPos(const int); // set current to specified position void setValue(const ELEM &); // set current ELEM's value ELEM currValue( ) const; // return current ELEM's value bool isEmpty( ) const; // return TRUE if list is empty bool isInList( ) const; // TRUE if current is within list bool find(const ELEM &); // find value (from current position) }; E.G.M. Petrakis lists

template <class ELEM> List<ELEM>::List(int sz) // constructor: initialize { msize = sz; numinlist = 0; curr = 0; listarray = new ELEM[sz]; } List<ELEM>::~List( ) // destructor: return array space { delete [ ] listarray; } void List<ELEM>::clear( ) // remove all Elems from list { numinlist = 0; curr = 0; } // reinitialize values void List<ELEM>::insert(const Elem item) { // insert Elem at current position // the array must not be full and “curr” must be a legal position assert((numinlist < msize) && (curr >= 0) && (curr <= numinlist)); for(int i = numinlist; i > curr; i--) // shift element up to make room listarray[i] = listarray[i - 1]; listarray[curr] = item; numinlist++; // increment current list size } E.G.M. Petrakis lists

template <class ELEM> void List<ELEM>::append(const Elem item) { // insert Elem at tail of list assert(numinlist < msize); // list must not be full listarray[numinlist++] = item; // increment list size } ELEM List<ELEM>::remove( ) { // remove and return current Elem assert( !isEmpty( ) && isInList( ) ); // must be an Elem to remove Elem temp = listarray[curr]; // store removed Elem for (int i=curr; i < numlist-1; i++) // shift elements down listarray[i] = listarray[i+1]; numinlist--; // decrement current list size return temp; void List<ELEM>::setFirst( ) // set curr to first position { curr = 0; } void List<ELEM>::prev( ) // move curr to previous position { curr--; } E.G.M. Petrakis lists

template <class ELEM> void List<ELEM>::next( ) // move curr to next position { curr++; } int List::length( ) const // return current length of list { return numinlist; } void List<ELEM>::setPos(int pos) // set curr to specified position {curr = pos; } void List<ELEM>::setValue(const Elem val) { // set current Elem's value assert(isInList( )); // curr must be at valid position listarray[curr] = val; } Elem List<ELEM>::currValue( ) const { // return current Elem's value assert(isInList( )); // must be at a valid position return listarray[curr]; E.G.M. Petrakis lists

} template <class ELEM> bool List<ELEM>::isEmpty( ) const // return TRUE if list is empty { return numinlist == 0; } bool List<ELEM>::isInList( ) const // TRUE if curr is within list { return (curr >= 0) && (curr < numinlist); } bool List<ELEM>::find(int val) { // find value (starting at curr) while (isInList( )) // stop if reach end if (key(currValue( )) == val) return TRUE; // found it else next( ); return FALSE; // not found } E.G.M. Petrakis lists

Array Implementation (2) 1 11 5 26 list 10 12 8 9 13 nodes NULL E.G.M. Petrakis lists

31 3 14 37 6 5 12 List 1 17 26 List 2 19 32 List 3 1 18 13 11 4 15 List 4 E.G.M. Petrakis lists

List 4 List 2 List 3 List 1 E.G.M. Petrakis lists 1 26 2 11 10 3 5 16 2 11 10 3 5 16 4 25 17 6 13 7 8 19 9 14 22 12 31 15 37 24 18 32 20 21 23 List 4 List 2 List 3 List 1 E.G.M. Petrakis lists

template <class ELEM> class list { // array based list class private: int msize // maximum size of list int numinlist // actual number of ELEMs int curr; // position of “current” int avail; // next available position ELEM* listarray; // array holding ELEMs int* listarray_next; // array holding pointers to next ELEMs int get_node( ); // get position of available node void free_node( ); // return node in array void insert(int, const ELEM&); // insert after the node pointed by p void delete(int, ELEM*); // delete after the node pointed by p public: list (const int=LIST_SIZE); // constructor ~list( ); // destructor void clear( ); // remove all ELEMs from list void insert(const ELEM&); // insert ELEM at current position void append(const ELEM&); // insert ELEM at tail of list ELEM remove( ); // remove and return current ELEM void setFirst( ); // set curr to first position; void prev( ); // move curr to previous position; void next( ); // move curr to next position; int length( ) const; // return current length of list }; E.G.M. Petrakis lists

template <class ELEM> List<ELEM>::list(int sz) { // constructor: initialize msize = sz; numinlist = 0; curr = 0; listarray = new Elem[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 } List<ELEM>::~list( ) // destructor: return array space { delete [ ] listarray; delete [ ] listarray_next; E.G.M. Petrakis lists

template <class ELEM> int list<ELEM>::get_node( ) { //get next available node if (avail == nothing) // from stack error(‘list overflow’) else { int pos = avail; avail = listarray_next[avail]; return pos; } template <class ELEM> // make node available void list<ELEM>::free_node (int p) { listarray_next[p] = avail; // push node back to stack avail =p; E.G.M. Petrakis lists

template <class ELEM> // insert after node pointed to by “p” void list<ELEM> ::insert(int p, const ELEM& x) { if (p < 0 || p >= msize) error (‘void insertion’) else { int q = get_node( ); listarray[q] = x; listarray_next[q] = listarray_next[p]; listarray_next[p] = q; } template <class ELEM> void list<ELEM> ::delete(int p; ELEM* x) { if ( p > 0 || p >= msize) // deletes elem after elem error (‘void deletion’); // pointed to by “p” int q = listarray_next[p]; *x = listarray[q]; listarray_next[p] = listarray_next[q]; free_node(q); E.G.M. Petrakis lists

Dynamic Memory The node class Allocates memory for new elements as needed Each node is a distinct object The node class template <class ELEM> class link { // a linked-list node public: ELEM element // node value link *next; // pointer to next node link(const ELEM & val, link *nextval = NULL); {element = val; next = nextval;} link(link *nextval = NULL) {next = nextval;} ~link( ) { } } E.G.M. Petrakis lists

template <class Elem> class List { // Linked list class private: Link<Elem>* head; // pointer to list header Link<Elem>* tail; // pointer to last Elem in list Link<Elem>* curr; // position of "current" Elem public: List( ); // constructor ~List( ); // destructor void clear( ); // remove all Elems from list void insert(const Elem); // insert Elem after current position void append(const Elem); // insert Elem at tail of list Elem remove( ); // remove and return after current Elem void setFirst( ); // set curr to first position void prev( ); // move curr to previous position void next( ); // move curr to next position int length( ) const; // return current length of list void setPos(int); // set curr to specified position void setValue(const Elem); // set current Elem's value Elem currValue( ) const; // return current Elem's value bool isEmpty( ) const; // return TRUE if list is empty bool isInList( ) const; // TRUE if curr is within list bool find(Elem); // find value (from current position) }; E.G.M. Petrakis lists

template <class Elem> List<Elem>::List( ) // constructor { head = new Link<Elem>; tail = head; curr = head; } // initialize List<Elem>::~List( ) { // destructor while(head != NULL) { // return link nodes to free store curr = head; head = headnext; delete curr; } void List<Elem>::clear( ) { // remove all Elems from list while (headnext != NULL) { // return link nodes to free store curr = headnext; // keeps header node !! headnext = currnext; // otherwise, similar to ~List( ) tail = head; // reinitialize E.G.M. Petrakis lists

template <class Elem> void List<Elem>::insert(const Elem item) // insert Elem after current { assert(curr != NULL); // must be pointing to list Elem currnext = new Link<Elem>(item, currnext); if (tail == curr) // appended new Elem tail = currnext; } void List<Elem>::append(const Elem item) // insert Elem at tail of list { tail = tailnext = new Link<Elem>(item, NULL); } Elem List<Elem>::remove( ) { // remove elem after current assert(isInList( )); // must be valid position in list Elem temp = currnextelement; // remember value Link<Elem>* ltemp = currnext; // remember link node currnext = ltempnext; // remove from list if (tail == ltemp) tail = curr; // removed last Elem: set tail delete ltemp; // send link to free store return temp; // return value removed E.G.M. Petrakis lists

template <class Elem> void List<Elem>::setFirst( ) // set curr to first position { curr = head; } void List <Elem>::next( ) // move curr to next position { if (curr != NULL) curr = currnext; } void List <Elem>::prev( ) { // move curr to previous pos Link<Elem>* temp = head; if ((curr == NULL) || (curr == head)) // no previous Elem { curr = NULL; return; } // so just return while ((temp!=NULL) && (tempnext != curr)) temp=tempnext; curr = temp; } int List <Elem>::length( ) const { // return current length of list int cnt = 0; for (Link<Elem>* temp = headnext; temp != NULL; temp = tempnext) cnt++; // count Elems and return cnt; E.G.M. Petrakis lists

template <class Elem> bool List <Elem>::find(Elem val) { // find value (starting at curr) while (isInList( )) if (key(currnextelement) == val) return TRUE; else curr = currnext; return FALSE; // not found } void List<Elem>::setPos(int pos) { // set curr to position curr = head; for (int i = 0; (curr != NULL) && (i < pos) i++) curr = currnext; void List<Elem>::setValue(const Elem val) { // set current Elem's value assert(isInList()); currnextelement = val; E.G.M. Petrakis lists

template <class Elem> Elem List<Elem>::currValue const // return value of current Elem { assert(isInList( )); return currnextelement; } bool List<Elem>::isEmpty( ) const // return TRUE if list is empty { return headnext == NULL; } bool List<Elem>::isInList( ) const // TRUE if curr is within list { return (curr != NULL) && (currnext != NULL); } E.G.M. Petrakis lists

Comparison Array-Based Lists: Linked 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. Petrakis lists

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 next = new (item, currnext, curr); if (currnext != NULL) currnextprev = currnext E.G.M. Petrakis lists

Insertion in Doubly Linked List current E.G.M. Petrakis lists

Deletion in Doubly Linked List current current E.G.M. Petrakis lists

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