1 Linked List Position (1). 2 Linked List Position (2)

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
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?
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures ADT List
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.
Data Structure Lecture-5
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
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.
1 Chapter 4 List 、 stack 、 queue Content: list stack queue.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Review Learn about linked lists
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:
LINKED QUEUES P LINKED QUEUE OBJECT Introduction Introduction again, the problem with the previous example of queues is that we are working.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
Deux applications. Freelists Les appels système new et delete sont lents: // Singly-linked list node with freelist template class Link { private: static.
1 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison.
Chapter 4 ADT Sorted List.
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.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
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.
Coursenotes A Practical Introduction to Data Structures and Algorithm Analysis Second Edition Clifford A. Shaffer Department of Computer Science Virginia.
CMSC 341 Lists 3. 2 Doubly-Linked Lists Option: add pointer to previous node Issues –doubles number of pointers –allows immediate (O(1)) access to previous.
1 An Introduction to Data Structures and Abstract Data Types.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
C++ Classes and Data Structures Jeffrey S. Childs
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Exam Review 5/28/15. Software Development (Review)  Make it work…then make it pretty can result in dangerous code  Better to take a unified, consistent.
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,
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
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++,
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
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.
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 &
Abstract Data Types and a review of C++ programming concepts CS 400/600 – Data Structures.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
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.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
UNIT-II Topics to be covered Singly linked list Circular linked list
Chapter 4 The easy stuff.
Data Structure Dr. Mohamed Khafagy.
Stacks Stack: restricted variant of list
CST230: Applied Data Structures
Basics of Algorithm Analysis
Lists List: finite sequence of data elements
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Presentation transcript:

1 Linked List Position (1)

2 Linked List Position (2)

3 Linked List Class (1) / Linked list implementation template class LList: public List { private: Link * head; // Point to list header Link * tail; // Pointer to last Elem Link * fence;// Last element on left int leftcnt; // Size of left int rightcnt; // Size of right void init() { // Intialization routine fence = tail = head = new Link ; leftcnt = rightcnt = 0; }

4 Linked List Class (2) void removeall() { // Return link nodes to free store while(head != NULL) { fence = head; head = head->next; delete fence; } public: LList(int size=DefaultListSize) { init(); } ~LList() { removeall(); } // Destructor void clear() { removeall(); init(); }

5 Linked List Class (3) void setStart() { fence = head; rightcnt += leftcnt; leftcnt = 0; } void setEnd() { fence = tail; leftcnt += rightcnt; rightcnt = 0; } void next() { // Don't move fence if right empty if (fence != tail) { fence = fence->next; rightcnt--; leftcnt++; } } int leftLength() const { return leftcnt; } int rightLength() const { return rightcnt; } bool getValue(Elem& it) const { if(rightLength() == 0) return false; it = fence->next->element; return true; }

6 Insertion

7 Insert/Append // Insert at front of right partition template bool LList ::insert(const Elem& item) { fence->next = new Link (item, fence->next); if (tail == fence) tail = fence->next; rightcnt++; return true;} // Append Elem to end of the list template bool LList ::append(const Elem& item) { tail = tail->next = new Link (item, NULL); rightcnt++; return true;}

8 Removal

9 Remove // Remove and return first Elem in right // partition template bool LList ::remove(Elem& it) { if (fence->next == NULL) return false; it = fence->next->element; // Remember value // Remember link node Link * ltemp = fence->next; fence->next = ltemp->next; // Remove if (tail == ltemp) // Reset tail tail = fence; delete ltemp; // Reclaim space rightcnt--; return true; }

10 Prev // Move fence one step left; // no change if left is empty template void LList ::prev() { Link * temp = head; if (fence == head) return; // No prev Elem while (temp->next!=fence) temp=temp->next; fence = temp; leftcnt--; rightcnt++; }

11 Setpos // Set the size of left partition to pos template bool LList ::setPos(int pos) { if ((pos rightcnt+leftcnt)) return false; fence = head; for(int i=0; i<pos; i++) fence = fence->next; return true; }

12 Freelists System new and delete are slow. // Singly-linked list node with freelist template class Link { private: static Link * freelist; // Head public: Elem element; // Value for this node Link* next; // Point to next node Link(const Elem& elemval, Link* nextval =NULL) { element = elemval; next = nextval; } Link(Link* nextval =NULL) {next=nextval;} void* operator new(size_t); // Overload void operator delete(void*); // Overload };

13 Freelists (2) template Link * Link ::freelist = NULL; template // Overload for new void* Link ::operator new(size_t) { if (freelist == NULL) return ::new Link; Link * temp = freelist; // Reuse freelist = freelist->next; return temp; // Return the link } template // Overload delete void Link ::operator delete(void* ptr){ ((Link *)ptr)->next = freelist; freelist = (Link *)ptr; }

14 Comparison of Implementations Array-Based Lists: Insertion and deletion are  (n). Prev and direct access are  (1). Array must be allocated in advance. No overhead if all array positions are full. Linked Lists: Insertion and deletion are  (1). Prev and direct access are  (n). Space grows with number of elements. Every element requires overhead.

15 Space Comparison “Break-even” point: DE = n(P + E); n = DE P + E E: Space for data value. P: Space for pointer. D: Number of elements in array.

16 Doubly Linked Lists Simplify insertion and deletion: Add a prev pointer. // Doubly-linked list link node template class Link { public: Elem element; // Value for this node Link *next; // Pointer to next node Link *prev; // Pointer to previous node Link(const Elem& e, Link* prevp =NULL, Link* nextp =NULL) { element=e; prev=prevp; next=nextp; } Link(Link* prevp =NULL, Link* nextp =NULL) { prev = prevp; next = nextp; } };

17 Doubly Linked Lists

18 Doubly Linked Insert

19 Doubly Linked Insert // Insert at front of right partition template bool LList ::insert(const Elem& item) { fence->next = new Link (item, fence, fence->next); if (fence->next->next != NULL) fence->next->next->prev = fence->next; if (tail == fence) // Appending new Elem tail = fence->next; // so set tail rightcnt++; // Added to right return true; }

20 Doubly Linked Remove

21 Doubly Linked Remove // Remove, return first Elem in right part template bool LList ::remove(Elem& it) { if (fence->next == NULL) return false; it = fence->next->element; Link * ltemp = fence->next; if (ltemp->next != NULL) ltemp->next->prev = fence; else tail = fence; // Reset tail fence->next = ltemp->next; // Remove delete ltemp; // Reclaim space rightcnt--; // Removed from right return true; }

22 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison Equality: sequential search Relative order: sorting Record comparison

23 Comparator Class How do we generalize comparison? Use ==, =: Disastrous Overload ==, =: Disastrous Define a function with a standard name Implied obligation Breaks down with multiple key fields/indices for same object Pass in a function Explicit obligation Function parameter Template parameter

24 Comparator Example class intintCompare { public: static bool lt(int x, int y) { return x < y; } static bool eq(int x, int y) { return x == y; } static bool gt(int x, int y) { return x > y; } };

25 Comparator Example (2) public: int ID; char* name; }; class IDCompare { public: static bool lt(Payroll& x, Payroll& y) { return x.ID < y.ID; } }; class NameCompare { public: static bool lt(Payroll& x, Payroll& y) { return strcmp(x.name, y.name) < 0; } };

26 Dictionary ADT // The Dictionary abstract class. template <class Key, class Elem, class KEComp, class EEComp> class Dictionary { public: virtual void clear() = 0; virtual bool insert(const Elem&) = 0; virtual bool remove(const Key&, Elem&) = 0; virtual bool removeAny(Elem&) = 0; virtual bool find(const Key&, Elem&) const = 0; virtual int size() = 0; };

27 Unsorted List Dictionary template <class Key, class Elem, class KEComp, class EEComp> class UALdict : public Dictionary { private: AList * list; public: bool remove(const Key& K, Elem& e) { for(list->setStart(); list->getValue(e); list->next()) if (KEComp::eq(K, e)) { list->remove(e); return true; } return false; } };

28 Linked Stack (1) template class LStack: public Stack { private: Link * top; // Pointer to first elem int size; // Count number of elems public: LStack(int sz =DefaultListSize) { top = NULL; size = 0; } bool push(const Elem& item) { top = new Link (item, top); size++; return true; }

29 Linked Stack (2) bool pop(Elem& it) { if (size == 0) return false; it = top->element; Link * ltemp = top->next; delete top; top = ltemp; size--; return true; } bool topValue(Elem& it) const { if (size == 0) return false; it = top->element; return true; }