Linked List (Part II). Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Array Representation
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.
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.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
Linked Lists CENG 213 Data Structures.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Review Learn about linked lists
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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.
Chapter 4.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
(Walls & Mirrors - Beginning of Chapter 4)
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.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Reference: Vinu V Das, Principles of Data Structures using C and C++
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.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Data Structures Chapter 4: Linked Lists 4-1. Singly Linked List DABC 561 data link(pointer, address) 以 -1 代表結尾 (null pointer) first = 2 header,
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.
Chapter 4 (cont.) Additional Lists Operations Circular Lists The link field of the last node points to the first node in the list... BATCATFATWAT.
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++,
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
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 &
Chapter 5 Linked Lists II
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 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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 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.
Lecture 16 Linked Lists. In this lecture Fundamentals Applications Memory Allocation Creating a List Inserting Nodes.
CS 240Chapter 10 – TreesPage Chapter 10 Trees The tree abstract data type provides a hierarchical to the representation of certain types of relationships.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 20: Binary Trees.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
Data Structure and Algorithms
Lists CS 3358.
Stack.
Chapter 20: Binary Trees.
Chapter 21: Binary Trees.
Linked Lists.
Linked List (Part I) Data structure.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Data Structures and Programming Techniques
Chapter 16 Linked Structures
CSI 1340 Introduction to Computer Science II
General List.
Data Structures Chapter 4: Linked Lists.
Data Structures & Programming
Presentation transcript:

Linked List (Part II)

Introduction  Definition of equivalence relation: A relation ≡ over a set S, is said to be an equivalence relation over S iff it is reflexive, symmetric, and transitive over S. ○ Example: the “equal to” (=) relationship is an equivalence relation, since 1. x = x. 2. x = y implies y = x. 3. x = y and y = z implies x = z.

Equivalence Class Problem  To partition the set S into equivalence classes such that two members x and y of S are in the same equivalence class iff x ≡ y.  Example: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ 0. Then, we get the following equivalence classes: {0, 2, 4, 7, 11}; {1, 3, 5}; {6, 8, 9, 10}

Idea  Phase 1 The equivalence pairs (i, j) are read in and stored.  Phase 2 Begin at 0 and find all pairs of (0, j). ○ If 0 and j are in the same class, include k if any (j, k) exists. Because, i ≡ j and j ≡ k implies i ≡ k (transitivity). Continue in this way until the entire equivalence class containing 0 has been found and output. Start an object that is not output for finding new equivalence class.

Program 4.26 What kind of data structure can be used to hold these pairs? void Equivalence () { initialize; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

Consideration  Consider implementation using array (for easy random access) m: the number of input pairs n: the number of objects  Declare a Boolean array, pairs[n][n]. pairs[i][j] =true iff (i, j) is an input pair.

Implementation Using Array  Example: (0, 2) = true and (2, k) = true for all k implies (0, k) = true.  Disadvantages: Could be wasteful of space if n is small. At least O(n 2 ) of time is required to perform initialization (0, 2) = true and (2, 1) = trueImplies (0, 1) = true (2, 3) = trueImplies (0, 3) = true 0

Implementation Using Linked List  Use one linked list to represent each row of the array pairs first first is a 1D array with each element first[i] is a pointer to the first node for row i.

Program 4.27 void Equivalence () { read n;//read in the number of objects initialize first[0:n-1] to 0 and out[0:n-1] to false; while more pairs { input the next pair (i, j); process this pair; } initialize for output; for (each object not yet output) output the equivalence class that contains this object; }

Example – Phase 1  Consider the following equivalence relations: 0 ≡ 4, 3 ≡ 1, 6 ≡ 10, 8 ≡ 9, 7 ≡ 4, 6 ≡ 8, 3 ≡ 5, 2 ≡ 11, and 11 ≡ first

Example – Phase 2  A Boolean array out[n] is used for determining whether object i is yet to be printed. The array is initialized to false. For each i such that out[i] = false, the elements in the list first[i] are output. For satisfying transitivity, a linked stack is created. out

first out , 11, , 7 1 null 2 1, 2 Linked stack: The first equivalence class

Program 4.28 – Part I class ENode { friend void Equivalence(); public: ENode(int d=0, ENode *next=0) //constructor {data = d; link = next;} private: int data; ENode *link; };

Program 4.28 – Part II (Phase 1) void Equivalence() { ifstream inFile("equiv.in", ios::in); if (!inFile) throw "Cannot open input file."; int n; inFile >> n; ENode **first = new ENode*[n]; bool *out = new bool [n]; for (int i=0; i<n, i++) first[i] = 0; for (int i=0; i<n, i++) out[i] = false; //Phase 1 int x, y; inFile >> x >> y; while (inFile.good()) { first[x] = new ENode(y, first[x]); first[y] = new ENode(x, first[y]); inFile >> x >> y; }

Program 4.28 – Part III (Phase 2) for (int i=0; i<n; i++) { if (!out[i]) { cout << endl; << "A new class: " << i; out[i] = true; ENode *x = first[i]; ENode *top = 0; //initialize stack while (1) { while (x) { int j = x->data; if (!out[j]) { cout << ", " << j; out[j] = true; ENode *y = x->link; x->link = top; top = x; x = y; } else x = x->link; } if (!top) break; x = first[top->data]; top = top->link; //pop } Check if the stack is empty Check every node of out[i] if out[i] is true.

Array of Pointers  Declare a pointer: ENode *ptr = new ENode(1, 0);  Declare a pointer of array with fixed length: ENode *ptr[3]; for (int i=0; i<3; i++) ptr[i] = new ENode(i, 0);  Declare a pointer of array with arbitrary length: ENode **ptr; ptr = new ENode * [3]; for (int i=0; i<3; i++) ptr[i] = new ENode(i, 0); 012

Program 4.28 – Part IV for (int i=0; i<n; i++) { while (first[i]) { ENode *delnode = first[i]; first[i] = delnode->link; delete delnode; } delete [] first; delete [] out; }

Analysis of Equivalence()  Define m: the number of input pairs. n: the number of objects.  Space complexity: At most 2m nodes are inserted into first. The array out of length n is used. Space complexity: O(m+n).

Analysis of Equivalence()  Time complexity: Phase 1: ○ The initialization of first and out takes O(n) time. ○ The processing of each input pair is O(1) and there are m pairs. ○ Totally, the complexity for this phase is O(m+n). Phase 2: ○ The for-loop executes n times. Each unprinted node is put onto the linked stack at most once and there are 2m nodes to examine. ○ The time for this phase is O(m+n).

Introduction  In Chapter 2, we use array to implement a sparse matrix. The sequential representation permits easy access of matrix terms by row. However, accessing all the terms in a specific column is difficult.  To provide easy access both by row and by column, we devise a linked representation for a sparse matrix.

Introduction  Node structure for sparse matrices. Header nodes Element nodes ○ The field head is used to distinguish whether the node is a header node (true) or an element node (false). rowcolvaluehead next downright head

Header Nodes  Number of header nodes = 1 + max {number of rows, number of columns}.  The header node for row i is also the header node for column i. The down field: used to link into a column list. The right field: used to link into a row list. The next field: used to link the next header nodes.  The list of header nodes has its header node, H, where the fields row and col are used to store the matrix dimension, and value is used to stored the number of nonzero ters.

Node Structure next downright rowcolvalue downright Header nodeElement node downright rowcolvalue Special use for the first node of the list of header nodes. Link to the next nonzero term in the same column. Link to the next nonzero term in the same row.

Example  Consider the following 5x4 sparse matrix:  How many header nodes?  How many element nodes?

546 HH0H1H2H3H4 H0 H1 H2 H3 H

Representation of MatrixNode class MatrixNode { friend class Matrix; public: MatrixNode(bool h=false, int r=-1, int c=-1, int v=0, MatrixNode *rp=0, MatrixNode *dp=0, MatrixNode *nt=0); private: MatrixNode *down, *right, *next; int row, col, value; bool head; };

Representation of Matrix class Matrix { public: Matrix(int r=0, int c=0); private: MatrixNode *headnode; MatrixNode **head; };

Constructor of Matrix Matrix::Matrix(int r, int c) { headnode = 0; head = 0; if (r <= 0 || c <= 0) return; int p = max(r, c); head = new MatrixNode*[p]; for (int i=0; i<p; i++) head[i] = new MatrixNode(true); for (int i=0; i<p-1; i++) head[i]->next = head[i+1]; headnode = new MatrixNode(true, r, c, 0, 0, 0, head[0]); }

C++ Program of Insertion void Matrix::Insertion(int row, int col, int value) { MatrixNode *prev1 = head[row]; MatrixNode *temp1 = head[row]->right; while (temp1 != NULL) { if (col col) break; prev1= temp1; temp1 = temp1->right; } MatrixNode * prev2 = head[col]; MatrixNode * temp2 = head[col]->down; while (temp2 != NULL) { if (row row) break; prev2= temp2; temp2 = temp2->down; } MatrixNode *newNode = new MatrixNode(false, row, col, value, temp1, temp2); prev1->right = newNode; prev2->down = newNode; }

546 HH0H1H2H3H4 H0 H1 H2 H3 H prev1 temp1 temp2 prev2 129 Insert a node at (1, 2)

Analysis of Insertion()  Suppose there are n nonzero entries.  Space complexity: O(1)  Time complexity: O(n) Compared to the implementation using array, inserting an arbitrary entry into the matrix has no need for data shift anymore. Easy to access a specific row or column.

Introduction  The difficulties of using a singly linked list The search of the list is limited to single direction. ○ The only way to the preceding node is to start at the beginning. ○ The same problem arises when one wishes to delete an arbitrary node from the list.  Doubly linked list A node in a doubly linked list has at least two fields ○ left (left link): link to the preceding node ○ right (right link): link to the following node

Representation class DbListNode { friend class DbList; public: DbListNode(int d=0, DbListNode *llink=0, DbListNode *rlink=0) { data = d; left = llink; right = rlink; }; private: int data; DbListNode *left, *right; }; class DbList { private: DbListNode *first, *last; }; leftrightleftrightleftright0 left0 first last dummy

Construction and Destruction DbList::DbList() { first = new DbListNode(); last = new DbListNode(); first->right = last; first->left = NULL; last->left = first; last->right = NULL; } DbList::~DbList() { while (first != NULL) { DbListNode *temp = first->right; delete first; first = temp; }

Inserting a Node into an Ordered List  Suppose the list is sorted in non-decreasing order. void DbList::Insertion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data >= d) break; temp = temp->right; } DbListNode *newNode = new DbListNode(d, temp->left, temp); temp->left->right = newNode; temp->left = newNode; } leftright 1 leftright 2 leftright 3 temp temp->left newNode

Deleting a Node from an Ordered List bool DbList::Deletion(int d) { DbListNode *temp = first->right; while (temp != last) { if (temp->data == d) { temp->left->right = temp->right; temp->right->left = temp->left; delete temp; return true; } temp = temp->right; } return false; } leftright 1 leftright 2 leftright 3 temp->righttemp->lefttemp