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.

Slides:



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

Singly linked lists Doubly linked lists
Linked Lists CS-212 Dick Steflik. Linked Lists A sequential collection of information Can be unordered; i.e. in no specific order Can be ordered; may.
Stacks, Queues, and Linked Lists
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
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 USING C++ Chapter 5
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
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
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 2007 Pearson Education, Inc. All rights reserved C Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
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.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Week 4 - Friday.  What did we talk about last time?  Continued doubly linked list implementation  Linked lists with iterators.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
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.
STACKS & QUEUES for CLASS XII ( C++).
Elementary Data Structures
Cpt S 122 – Data Structures Abstract Data Types
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Chapter 12 – Data Structures
Chapter 4 The easy stuff.
Week 4 - Friday CS221.
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Chapter 15 Lists Objectives
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Linked List (Part I) Data structure.
Lists List: finite sequence of data elements
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
Programming Abstractions
Stacks, Queues, and Deques
Presentation transcript:

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 is the empty list. The first element is element 1 and the last element is element N in a list of size N. Operations can include: insert, remove, findKth, find, next, previous. Must consider special cases, no previous, no next, not found …

Generic List Functions void clear(); // reinitialize the list bool insert(const Elem &); //insert an item into the list before the current item. Return false if the list is full. bool append(const Elem &); //insert an item after the last. bool remove(Elem &); //remove the item at the current. Return false if current is empty. void setStart(); //set current to the beginning. void setEnd(); // set current to the last. void prev(); // set current to the previous item. No change if current is at the first. void next(); // set current to the next item. No change if current is at the end.

More List Functions int leftLength(); // the size of the list before the current int rightLength(); // the size of the list after and including the current. bool setPos(int); // set current to the position indicated by the argument. Return false if there are not enough in the list. bool getValue (Elem &); // return the value of the current item in the argument. Return false if there is no current item. void print(); // print out the items in the list.

Array Implementation Can use an array to implement a list. Fixed size. FindKth very fast. Insert and remove will cause lots of movement. Find is linear unless the list is ordered.

Array Based List Implementation template class List{ private: int maxSize; int listSize; int curr; Elem * listArray; public: methods discussed };

Some Array Based List Functions Constructor List(int size=DEFAULTSIZE) { maxSize=size; listSize=curr=0; listArray=new Elem[size]; } void setStart() {curr=0}; bool append(const Elem & item) { if (listSize==maxSize) return false; listArray[listSize++]=item; }

Linked List Allows dynamic memory. Each node has a link to the next item in the list. (extra memory). Last items next points to NULL. Insert and remove take constant time. FindKth is now linear.

Programming Trick To insert or remove at the beginning is a special case (must change the head pointer). Instead, create a header node that contains no data. This node will never be deleted. The head pointer will always point to the header node. Created in the constructor. Called node 0.

Iterator and Container Classes A container class is designed to hold collections of objects (list, stack, queue, tree, …). This class provides services such as insertion, deletion, searching, sorting, find,… It is common to associate iterator objects with container classes. Being friends is appropriate.

Iterator Classes An iterator is an object that returns an item of a container. This is much like using pointers. We do not want the user to get indiscriminate use of a pointer (then they can change the data in a private area). Use iterator classes to handle these pointers for the user. This way, the user can keep pointers to multiple locations in a container.

Iterators (cont.) You can think of an iterator as a bookmark within the container. Easily allows multiple bookmarks in one container. The container class can use iterator objects. Package both classes in same.h and.cxx files. Need to give an incomplete class declaration since both classes need both classes.

List Implementation Now a list should have 3 classes associated with it. –ListNode – Holds the data of 1 element in the list (many times implemented as a struct) –List – Holds a pointer to the first item. –ListItr – keeps track of a position in the List. Package them all together, friends will be o.k.

Doubly Linked List One of the operations was to give the previous. This is fine and can be done with one extra pointer as long as we will just use the previous function once in a row. To allow unlimited backing up, need a previous pointer in each node. Uses more memory, but will not be significantly slower. Now no need for a separate previous pointer for insertion and deletion.

Circular Linked List Instead of having the last nodes next point to NULL, have it point to the first. This is useful in circular applications, when the first follows the last.

Polynomial Application Many high level polynomials have many terms with a 0 coefficient. Would need to store them if using an array. Use a linked list to not take memory for items with a 0 coefficient.

Big Numbers To allow for arbitrary large numbers, use a linked list where each holds one digit. –This is good conceptually, but for efficiency, we can use an int to hold several digits per node. Similarly, we can use a linked list for a string of arbitrary length.

MultiLists Sometimes we have one set of data, but want to process it in different orders. –DMV info, may want to go in name order or drivers license order, or in tag order. Rather than having the data duplicated several times for several lists, use multiple pointers. –Having duplicated data makes the update process difficult and error prone. Each pointer is the next for a particular list. –Nextname, nextlic, nexttag –Now insertions are really inserting into 3 lists.

Freelists The system new and delete are very slow. When your program does a free, do not give it back to the system. Instead, keep it around in another list to be used to give back the next time the program does a new. If your freelist is empty, then the class will actually need to do a new. Can accomplish this by overloading the system new and free operations. (Can still access the system new and free).

Freelist Implementation Class ListNode{ public: ELEM element; ListNode * next; static Listnode * freelist; ListNode(const ELEM & elemval, link * nextval=NULL) {element=elemval; next=nextval;} ListNode(ListNode * nextval=NULL) {next=nextval;} ~ListNode() {} void * operator new(size_t); void operator delete(void *);}; ListNode * ListNode::freelist=NULL; void * ListNode::operator new(size_t){ if (freelist==NULL) return ::new ListNode; ListNode * temp=freelist; freelist=freelist->next; return temp; } void ListNode::operator delete(void* ptr) { ((ListNode *) ptr)->next=freelist; freelist = (ListNode*) ptr;}

Array Implementation Can have array based lists or Array based linked lists. Array based lists are simple but have a lot of data movement for insertions and deletions. Array based linked list will have essentially 2 linked lists in one array, one for the free items and one for the actual data.

Array Linked Lists Use and extra field for a next pointer. This pointer will be an integer that points to a position in the array (via a subscript). NULL will need to be –1 rather than 0. Need a head pointer to tell which item is first.

Array Linked List Example Jim Jack2 Jill0 Beth5 Bob1 Head is 3 Free is 4

Array Linked List Initialization The initial linked list will have Head = NULL = -1 The initial free list will be 0 and Next[i]=i+1 and Next[last]=NULL To accomplish a new, we remove the front of the free list {newptr=free; free=next[free];} To accomplish a remove we add the node to the front of the free list {next[deleted]=free; free=deleted;} Need to use subscripts instead of pointers (->)

Stacks LIFO: Last In, First Out. Restricted form of a list. –Insert only at the front. –Remove from the front. Notation –Insert: PUSH –Remove: POP –The accessible element is called TOP.

Queues FIFO: First In, First Out. Restricted form of a list. –Insert at one end. –Remove from the other end. Notation –Insert: Enqueue. –Delete: Dequeue. –First Element: FRONT. –Last Element: REAR.

Queue Implementations Linked Queue –Very easy and intuititive. –Always add at the rear (trivial with a rear pointer). –Always delete at the front (trivial with pointer to front). Array based Queue –Have an int to tell where the rear is (start at 0, increment when add). –Have int to tell where front is (start at 0, increment when delete). –Front chases the Rear. –No data movement. –May get to end and think queue is full when there are empty spots. –Make circular. When Front and Rear get to end of array reset them to the beginning of the array. –If rear passes front during insertion then full. –If front passes rear during a deletion, then empty.