Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Stacks, Queues, and Linked Lists
CSCE 3110 Data Structures & Algorithm Analysis
DATA STRUCTURES USING C++ Chapter 5
Chapter 4 Lists Pointers Singly Linked Lists
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.
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.
CHP-5 LinkedList.
CSCI2100B Linked List Jeffrey
Review Learn about linked lists
Chapter 4.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
CS Data Structures Chapter 4 Lists.
Chapter 4 1. Why “linked lists” 2 IndexWord A[0]BAT A[1]CAT A[2]EAT … A[n]WAT Insert “FAT” ? Or delete something.
Data Structures Using C++ 2E
Reference: Vinu V Das, Principles of Data Structures using C and C++
Introduction to Data Structure
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
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.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
Linked List Containers. Linked List Example Polynomials –Interested in representing and manipulating polynomials –Polynomial defined as: Y = coef_n *
Data Structures Chapter 4: Linked Lists 4-1. Singly Linked List DABC 561 data link(pointer, address) 以 -1 代表結尾 (null pointer) first = 2 header,
Chap 4 Linked Lists. Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Slide 1 Linked Data Structures. Slide 2 Learning Objectives  Nodes and Linked Lists  Creating, searching  Linked List Applications  Stacks, queues.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Linked List (Part I). Introduction  Weakness of storing an ordered list in array: Insertion and deletion of arbitrary elements are expensive. ○ Example:
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.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
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.
Linked List Containers. Linked Lists List consists of multiple listnodes List consists of multiple listnodes Each listnode consists of Each listnode consists.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
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.
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.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
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.
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Prof. I. J. Chung Data Structure #4 Professor I. J. Chung.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
Chapter 16: Linked Lists.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
Lectures linked lists Chapter 6 of textbook
Linked Lists Chapter 6 Section 6.4 – 6.6
Data Structure Dr. Mohamed Khafagy.
Data Structures 7th Week
Data Structures Interview / VIVA Questions and Answers
EEE2108: Programming for Engineers Chapter 4. Linked Lists
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
Introduction to C++ Linear Linked Lists
Data Structures Chapter 4: Linked Lists.
Presentation transcript:

Review of Sequential Representations Previously introduced data structures, including array, queue, and stack, they all have the property that successive nodes of data object were stored a fixed distance apart. The drawback of sequential mapping for ordered lists is that operations such as insertion and deletion become expensive. Also sequential representation tends to have less space efficiency when handling multiple various sizes of ordered lists.

Linked List A better solutions to resolve the aforementioned issues of sequential representations is linked lists. Elements in a linked list are not stored in sequential in memory. Instead, they are stored all over the memory. They form a list by recording the address of next element for each element in the list. Therefore, the list is linked together. A linked list has a head pointer that points to the first element of the list. By following the links, you can traverse the linked list and visit each element in the list one by one.

Linked List Insertion To insert an element into the three letter linked list: –Get a node that is currently unused; let its address be x. –Set the data field of this node to GAT. –Set the link field of x to point to the node after FAT, which contains HAT. –Set the link field of the node cotaining FAT to x.

Linked List Insertion And Deletion BAT CAT EAT FAT GAT HAT BAT CAT EAT FAT GAT HAT first

Designing a List in C++ Design Attempt 1: Use a global variable first which is a pointer of ThreeLetterNode. –Unable to access to private data members: data and link. Design Attempt 2: Make member functions public. –Defeat the purpose of data encapsulation. Design Attempt 3: Use of two classes. Create a class that represents the linked list. The class contains the items of another objects of another class.

Program 4.1 Composite Classes class ThreeLetterList; // forward delcarion class ThreeLetterNode { friend class ThreeLetterList; private: char data[3]; ThreeLetterNode * link; }; class ThreeLetterList { public: // List Manipulation operations. private: ThreeLetterNode *first; };

Nested Classes The Three Letter List problem can also use nested classes to represent its structure. class ThreeLetterList { public: // List Manipulation operations. private: class ThreeLetterNode { // nested class public: char data[3]; ThreeLetterNode *link; }; ThreeLetterNode *first; };

Pointer Manipulation in C++ Addition of integers to pointer variable is permitted in C++ but sometimes it has no logical meaning. Two pointer variables of the same type can be compared. –x == y, x != y, x == 0 a x b y a x b y b x b y x = y*x = * y

Program 4.11 Attaching A Node To The End Of A List Template Void List ::Attach(Type k) { ListNode *newnode = new ListNode (k); if (first == 0) first = last =newnode; else { last->link = newnode; last = newnode; } };

Program 4.13 Concatenating Two Chains Template void List :: Concatenate(List b) // this = (a1, …, am) and b = (b1, …, bn) m, n ≥, // produces the new chain z = (a1, …, am, b1, bn) in this. { if (!first) { first = b.first; return;} if (b.first) { for (ListNode *p = first; p->link; p = p->link); // no body p->link = b.first; }

When Not To Reuse A Class If efficiency becomes a problem when reuse one class to implement another class. If the operations required by the application are complex and specialized, and therefore not offered by the class.

Circular Lists By having the link of the last node points to the first node, we have a circular list. –Need to make sure when current is pointing to the last node by checking for current->link == first. –Insertion and deletion must make sure that the circular structure is not broken, especially the link between last node and first node.

Diagram of A Circular List first last

Linked Stacks and Queues front rear top 0 0 Linked Stack Linked Queue

Revisit Polynomials a.first b.first

Program 4.20 Polynomial Class Definition struct Term // all members of Terms are public by default { int coef;// coefficient int exp;// exponent void Init(int c, int e) {coef = c; exp = e;}; }; class Polynomial { friend Polynomial operator+(const Polynomial&, const Polynomial&); private: List poly; };

Operating On Polynomials With linked lists, it is much easier to perform operations on polynomials such as adding and deleting. –E.g., adding two polynomials a and b a.first b.first p q (i) p->exp == q->exp c.first

Operating On Polynomials a.first b.first p q (ii) p->exp exp c.first

Operating On Polynomials a.first b.first p q (iii) p->exp > q->exp c.first

Memory Leak When polynomials are created for computation and then later on out of the program scope, all the memory occupied by these polynomials is supposed to return to system. But that is not the case. Since ListNode objects are not physically contained in List objects, the memory they occupy is lost to the program and is not returned to the system. This is called memory leak. Memory leak will eventually occupy all system memory and causes system to crash. To handle the memory leak problem, a destructor is needed to properly recycle the memory and return it back to the system.

List Destructor Template List ::~List() // Free all nodes in the chain { ListNode * next; for (; first; first = next) { next = first->link; delete first; }

Free Pool When items are created and deleted constantly, it is more efficient to have a circular list to contain all available items. When an item is needed, the free pool is checked to see if there is any item available. If yes, then an item is retrieved and assigned for use. If the list is empty, then either we stop allocating new items or use new to create more items for use.

Using Circular Lists For Polynomials By using circular lists for polynomials and free pool mechanism, the deleting of a polynomial can be done in a fixed amount of time independent of the number of terms in the polynomial.

Deleting A Polynomial with a Circular List Structure a.first av 1 second 2 3 av

Doubly Linked Lists The problem of a singly linked list is that supposed we want to find the node precedes a node ptr, we have to start from the beginning of the list and search until find the node whose link field contains ptr. To efficiently delete a node, we need to know its preceding node. Therefore, doubly linked list is useful. A node in a doubly linked list has at least three fields: left link field (llink), a data field (item), and a right link field (rlink).

Doubly Linked List A head node is also used in a doubly linked list to allow us to implement our operations more easily. llink item rlink Head Node llink item rlink Empty List

Deletion From A Doubly Linked Circular List llink item rlink Head Node

Insertion Into An Empty Doubly Linked Circular List node newnode node

PolyNode Class in C++ enum Triple{ var, ptr, no }; class PolyNode { PolyNode *link; int exp; Triple trio; union { char vble; PolyNode *dlink; int coef; };

PolyNode in C++ (Cont.) trio == var : the node is a head node. –vble indicates the name of the variable. Or it is an integer point to the variable in a variable table. –exp is set to 0. trio == ptr : coefficient itself is a list and is pointed by the field dlink. exp is the exponent of the variable on which the list is based on. trio == no, coefficient is an integer and is stored in coef. exp is the exponent of the variable on which the list is based on.

Representing 3x 2 y vary0ptr10 varx0no320 P triovbleexplink triovbleexplink

Representation of P(x, y, z) vz0p2p10 vy0vy0p3p20p4p10 vx0 vx0 n110 n380 n280 vx0n200 vx0n14n630 P(x, y, z)

Recursive Algorithms For Lists A recursive algorithm consists of two components: –The recursive function (the workhorse); declared as a private function –A second function that invokes the recursive function at the top level (the driver); declared as a public function.

Program 4.6 Copying A List // Driver void GenList::Copy(const GenList& l) { first = Copy(l.first); } // Workhorse GenListNode* GenList::Copy(GenListNode *p) // Copy the nonrecursive list with no shared sublists pointed at by p { GenListNode *q = 0; if (p) { q = new GenListNode; q->tag = p->tag; if (!p->tag) q->data = p->data; else q->dlink = Copy(p->dlink); q->link = Copy(p->link); } return q; }

Linked Representation for A t fafb0 t0 t fc fe0 fd0 b s r u w v x t

Generalized List Representation Example fat0 fbtc0 tt00 fat0 A B C D = 0 Empty list A=(a, (b, c)) B=(A, A, ()) C=(a, C)

Recursiveness GenList::Copy Level of recursion Value of pContinuing level p p 1b2r3u 2s3u4v 3t4w50 405x4v 3t603u 2s5x2r 1b4w30 2r 1b

Important List Functions List Equality (Program 4.37) List Depth (Program 4.38) –An empty list has depth 0.

Reference Counts, Shared and Recursive Lists Lists may be shared by other lists for the purpose of space saving. Lists that are shared by other lists create problems when performing add or delete functions. For example, let ’ s look at the previous A, B, C, D example. When deleting the front node of list A would requires List B to update its pointers. The use of the data field of a head node to record the reference count can resolve the aforementioned problem. The list can not be deleted unless the reference count is 0.

Example of Reference Counts, Shared and Recursive Lists Y Z W A=(a, (b, c)) C=(a, C) f3fat0 f1fbfc0 f1ttt0 f2fat0f10 B=(A, A, ()) X f10

Erasing A List Recursively // Driver GenList::~GenList() // Each head node has a reference count. We assume first ≠ 0. { Delete(first); first = 0; } // Workhorse void GenList::Delete(GenListNode* x) { x->ref--;// decrement reference coutn of head node. if (!x->ref) { GenListNode *y = x; // y traverses top-level of x. while (y->link) { y= y->link; if (y->tag == 1) Delete (y->dlink);} y->link = av; // Attach top-level nodes to av list av = x; }

Issue In Erasing Recursive Lists When erasing a recursive list (either direct recursive or indirect recursive), the reference count does not become 0. Then the nodes are not returned to available list. This will cause memory leak issue.