Linked List Containers. Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n *

Slides:



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

Linked Lists Geletaw S..
Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
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.
Data Structures Using C++
CS Data Structures Chapter 4 Lists. Chain (1/3) Chain: Chain: A singly linked list in which the last node has a null link A singly linked list in.
CSE Lecture 12 – Linked Lists …
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
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.
1 Linked Lists Gordon College Prof. Brinton. 2 Linked List Basics Why use? 1.Efficient insertion or deletion into middle of list. (Arrays are not efficient.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
Chapter 4.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Main Index Contents 11 Main Index Contents Abstract Model of a List Obj. Abstract Model of a List Obj. Insertion into a List Insertion into a List Linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Data Structures Using C++ 2E
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.
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.
Data Structures Using Java1 Chapter 4 Linked Lists.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Chap 4 Linked Lists. Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
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 &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked List by Chapter 5 Linked List by
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
Data Structures Using C++1 Chapter 5 Linked Lists.
Linked List Containers. Concatenate Example Concatenate(LinkedList listB): Add all of the elements of a second list to the end of the first list Concatenate(LinkedList.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Tree Data Structures. Heaps for searching Search in a heap? Search in a heap? Would have to look at root Would have to look at root If search item smaller.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
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.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linked Lists. Introduction In linked list each item is embedded in a link Each item has two parts – Data – Pointer to the next item in the list Insert,
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
DATA STRUCTURE & ALGORITHMS CHAPTER 2: LINKED LIST.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
Linked List Containers. Useful Linked List Add-Ons Are there new variables/changes to the lists as they have been defined that could make our jobs as.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Data Structure Dr. Mohamed Khafagy.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked List (Part I) Data structure.
CS212D: Data Structures Week 5-6 Linked List.
Chapter 16 Linked Structures
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Data Structures Chapter 4: Linked Lists.
Chapter 9 Linked Lists.
Presentation transcript:

Linked List Containers

Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n * x exp_n + coef_n-1 * x exp_n-1 + … coef_0 * x 0 –Examples: 3x x x 14 – 3x x 6

Polynomials For each component of the polynomial, need to store coefficient and exponent class Term { public: void Term(int c, int e) { coef = c; exp = e;} private: int coef; int exp; };

Polynomials Polynomial itself implemented by a templated LinkedList of Terms class Polynomial { private: LinkedList poly; };

Polynomials Adding Polynomials: 3x 3 + 2x x 3 + 3x ================= 5x 3 + 3x 2 + 2x + 6

Polynomials Adding Polynomials: –Iterate through both lists –If exponent1 == exponent2, CoefficientSum = Coefficient1 + Coefficient2 If CoefficentSum != 0, add term to new polynomial representing answer –Else, Find higher exponent Add term to new polynomial representing answer

Polynomials

Polynomials The “Answer” polynomial is where LinkedLists have another win. There is now way beforehand to know how may terms there will be in the answer polynomial –Number of terms is anywhere from (the size of the largest list) to (the size of the largest list plus the size of the smallest list) –With an array representation, would have to overallocate to handle large answers –With LinkedLists, just grab a new Node when need it.

Circular Lists Another improvement on LinkedLists: –Use link pointer of last node to point to the first node. Changes to implementation: –Check to see if at last node: test “if (current -> link == first)” instead of “if (current->link == 0)” –Insertion and deletion need to preserve property that last nodes link is always set equal to first

Circular List Implementation Retrofitting our LinkedList to make circular: Don’t want to have just a head pointer: Why? –If inserting at tail, have to traverse the whole list from head to get to tail to update the tails link –If inserting at head, need to find last node to point its link to new head - this also traverses the whole list Circular linked lists are much more efficient if use tail (last) pointer as the pointer for the list.

Circular Linked Lists List1 Data1List1 Data2List1 Data3 tailhead (AKA first)

Circular Linked List void insertAtFront(Type data) { LinkedListNode* toAdd = new LinkedListNode(data); if (tail == 0) { // empty list, tail = toAdd; toAdd-> next = toAdd; // point to yourself } else { toAdd->next = tail->next; // point new head next to old head tail->next = toAdd; // point tail to new head } insertAtRear() only requires adding tail = toAdd in the else statement (to update rear to new node)

Doubly Linked Lists Biggest problem with linked lists as we’ve used so far: –Can only navigate in one direction –Requires traversals from front to a position, even if currently located right behind where want to be –Requires “trailing pointer” if want to easily get access to previous node for current node When update current to current->next, update prev to current

Doubly Linked Lists Work around these issues by storing both the left and right side neighbors of a linked list Makes add, delete, and other array manipulation operators more complicated as have to preserve doubly linked property

Doubly Linked Lists Definition: class DblListNode { friend class DblList; private: int data; DblListNode *right, *left; };

Doubly Linked Lists class DblList { public: // list manipulation private: DblListNode* head; // DblListNode* tail; // maybe use this if circular? };

Doubly Linked Lists LEFTDATARIGHTLEFT10RIGHTLEFT15RIGHTLEFT29RIGHT Example Node 3 Node Circular Doubly Linked List

Note that, given a pointer p, p = p->left->right = p->right->left Going back and forth is equally easy.

Doubly Linked List void DblList::Insert(DblListNode *new, DblListNode *current) { // insert new after x new->left = current; new->right = current->right; current->right->left = new; current->right = new; } 10L current R12L R R11L new

Doubly Linked List void DblList::Delete(DblListNode *toDelete) { // delete node pointed to by toDelete toDelete->left->right = toDelete->right; toDelete->right->left = toDelete->left; delete toDelete; } 10LR12LR11L toDelete

Generalized Lists Current implementation of lists: –Finite sequence of zero or more atomic elements A = (a 0, a 1, a 2 …. a n-1 ) –Elements of list are restricted to atoms: Individual pieces of information –An integer, A Rectangle Only structural property of a list is position –Given position, can easily access data Very much like arrays

Generalized Lists Relax the assumption of atomic elements: –Lists can now be composed of atoms or lists –Definition: A generalized list A is a finite sequence of zero or more elements a 0, a 1, …, a n-1 where a i is either an atom or a list. Elements that are not atomic are called sublists of A.

Generalized Lists Conventions for Generalized Lists: –List names are represented in capital letters –Atom names are lowercase letters Definitions for Generalized Lists: Length – Number of elements (atoms or sublists) in the list If length > 0 Head – First element of the list Tail – List composed of all elements except first of list

Generalized Lists Examples of Generalized Lists: –D = ( ) Length 0, Null List –A = (a, (b,c))Length 2 Head = aTail = ((b,c)) –B = (A, A, ())Length 3 Head = ATail = (A,()) –C = (a, C)Length 2 Head = aTail = (C)

Generalized Lists 2 Tricky Things Generalized Lists Allow: –Sharing of lists among lists B = (A, A, ( )) Just like having the number 1 held in the list twice – how do we represent efficiently and “safely”? –Recursive definitions of lists C = (a,C) actually represents the infinite list (a, (a, (a, (a, (a, (a, (a, (a, … )))))))) –May need to concern ourselves with these when implementing generalized list operations

Generalized Lists Need a new underlying representation: –A node in the list needs to be able to contain either an atomic piece of information or point to another list Tag = TRUE/FALSEData/DLinkLink (Next)

Generalized Lists Tag = represents type of node –Tag = false (0) means holds atomic data –Tag = true (1) means holds list data Data –If Tag = false, hold real data –If Tag = true, pointer to head of data list Link –Pointer to next item in list

Generalized Lists class GenListNode { friend class GenList; private: bool tag; GenListNode *link; union { char data;// or any other type of interest GenListNode* dlink; } } class GenList { private: GenListNode *front;// using name front because // we will use the term head // as something like a function }

Generalized Lists Union definition: –User-defined data type that, at any given time, contains only one object from its list of members (although that object can be an array or a class type). The member-list of a union represents the kinds of data the union can contain. A union requires enough storage to hold the largest member in its member-list. union NumericType { int iValue; long lValue; double dValue; };

Generalized Lists Example representations –D = ( ) Length 0, Null List D.front = 0; –A = (a, (b,c))Length 2 Head = aTail = ((b,c)) falseatrue0falseb c0 A.front

Generalized Lists B = (A, A, ( ) ) {where A is defined previously} falseatrue0falseb c0 A -> B -> true 00

Generalized Lists C = (a, C) falseatrue0

Generalized Lists Recursive class definition (lists within lists): Recursive operation definitions are probably the best way to go Recursive functions for generalized lists will have two parts: –A public driver which starts the recursive calls off at the right point –A private workhorse which does the actual recursion and work

Generalized List Copy Copying a list: –Assume list is not recursive, no shared sublists (as these are problematic) –Essential properties of algorithm: If data is an element, need to just copy the data => Requires a tag check to see if element or list Otherwise, if data is a sublist, need to copy the sublist, then copy the rest of the current list => Use recursion (calling copy again) on sublist and current node link

Generalized List Copy // Driver void GenList::Copy(const GenList &rhs) { first = Copy(rhs.first); } // Workhorse GenListNode* GenList::Copy(GenListNode* p) { GenListNode* q = 0; if (p != 0) { q = new GenListNode(); q-> tag = p->tag; if (q->tag == false) q-> data = p->data; else q->dlink = Copy(p->dlink); q->link = Copy(p->link); } return q; }

Generalized List Copy Verification of copy algorithm: –Works on empty list? Yes – returns q = 0 if empty –Works on all data list? – Yes – copies data elements since all tags will be false, then copies rest of list until next = 0 –Works on data and sublists? – Yes, copies data elements for false tags, copies sublists and returns pointer for sublists into dlink, and copies rest of original list until next = 0

Generalized List Equality Test for Equality –Requires: Same list structure (placement of atoms and sublists) Same list data Essential properties of algorithm: –Check equality of tags –If equal If data elements, check equality for data type If list elements, recursively check equality on sublist

Generalized List Equality int operator==(const GenList& l, const GenList& r) { return equal(l.first, r.first); } int equal(GenListNode* s, GenListNode* t) { int x; if ((!s) && (!t)) return 1; // both empty if (s && t && (s->tag == t->tag)) // data in lists, same { // type in this position // check data if not sublists if (s->tag == 0) { if (s->data == t->data) x = 1; else x = 0; } // check recursively on sublists otherwise else x = equals(s->dlink, t->dlink); // if equal so far, recurse on next nodes if (x != 0) return equals(s->link, t->link); } return 0; //otherwise return false }

Generalized List Depth Define depth of a list l to be: If l is empty, depth(l) = 0; Else, for a component s, Depth(s) = 0if s is an atom Depth(s) =1 + max {depth(x 1 ),…,depth(x n )} if s is the non-empty list (x 1, …, x n )

Generalized List Depth Example: A = (‘a’,’b’,’c’,’d’) depth(A) = 1; B = (‘a’,(‘c’,’d’),(‘b’,(‘e’,’c’))); depth(B) = 3;

Generalized List Depth int GenList::depth() { return depth(first); } int GenList::depth(GenListNode *s) { if (s == 0) return 0; GenListNode* p = s; int m = 0; while (p != 0) {if (p->tag == true) // sublist { int n = depth(p->dlink); // check depth of sublist if (m than current max, set as max } p = p->link;// continue until end of list } return m + 1;// include 1 for yourself }

Generalized Lists Previous algorithms avoided the possibility of shared lists and recursive lists Potential Problems: –When sharing lists, changes to the shared list need to be seen in the lists that make use of it B = (A, A, ( )) If A.delete() is called, removing A’s head, the two pointers for positions 0 and 1 in B need to be updated to point to the new head of the A. *Same problem if add a node at the front of A.

Generalized Lists Could maintain a list of all things that point to a list and ensure that all are updated when the list is changed. Better to work around problem by adding a head node to each list that doesn’t hold data. Tag is false. Data/dlink field contains a reference count, indicating how many things are pointing to the list. Link field points to real first item in the list

Generalized Lists Example representations –A header for a list with no internal elements D -> false10

Generalized Lists A = (a, (b, c)) B = (A, A, ( ) ) falseatrue0falseb c0 A -> B -> true 0false

Generalized Lists C = (a,C) falsea true0 false2 C ->

Generalized Lists Why are head nodes useful? –Any changes to the front of lists don’t have to be propagated to lists that are sharing the original list. The “head” node is always present and at the same address and points to whatever the real first data component is. –Reference counts can be used for determining when memory can be freed.