Linear Lists – Linked List Representation CSE, POSTECH.

Slides:



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

COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Linear Lists – Array Representation
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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.
List Representation - Chain Fall 2010 CSE, POSTECH.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
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.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Input iterator.
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.
Binary Search Trees CSE, POSTECH. Search Trees Search trees are ideal for implementing dictionaries – Similar or better performance than skip lists and.
Stacks CSE, POSTECH. 2 2 Stacks Linear list One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Binary Search Trees Chapter 6.
L.Chen1 Chapter 3 DATA REPRESENTATION(2) L.Chen A Chain Iterator Class A Chain Iterator Class ( 遍历器类 )  An iterator permits.
Iterators & Chain Variants. Iterators  An iterator permits you to examine the elements of a data structure one at a time.  C++ iterators Forward iterator.
1 Chapter 3 Data Representation Part 1. 2 Goals Introduce the different ways in which data may be represented Concepts –Abstract data types –Formula-based,
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.
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.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
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.
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Linear List Array representation Data structures A data structure is a particular way of storing and manipulating data in a computer so that it can be.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
L.Chen Chapter 3 DATA REPRESENTATION(3) L.Chen 3.8 APPLICATIONS Bin Sort Bin Sort (箱子排序)  It is a faster way to accomplish.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
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 lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
List Interface and Linked List Mrs. Furman March 25, 2010.
Data Structure Specification  Language independent  Abstract Data Type  C++  Abstract Class.
1 PART 4 Linked Lists Singly Linked Lists Circular Lists Applications Doubly Linked Lists Generalized Lists.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
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:
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
CSCE 210 Data Structures and Algorithms
CSE 1342 Programming Concepts
Reminder: Course Policy
The Class ArrayLinearList
UNIT-3 LINKED LIST.
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Programming Abstractions
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
LinkedList Class.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Linked List Yumei Huo Department of Computer Science
The Class chain.
The Class arrayList General purpose implementation of linear lists.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
Programming Abstractions
Iterators An iterator permits you to examine the elements of a data structure one at a time. C++ iterators Input iterator Output iterator Forward iterator.
The Class chain.
Linked Lists.
The Class chain.
Data Structures Chapter 4: Linked Lists.
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.
Presentation transcript:

Linear Lists – Linked List Representation CSE, POSTECH

2 2 Linked Representation of Linear List Each element is represented in a cell or node. Each node keeps explicit information about the location of other relevant nodes. This explicit information about the location of another node is called a link or pointer.

3 3 Singly Linked List Let L = (e 1,e 2,…,e n ) – Each element e i is represented in a separate node – Each node has exactly one link field that is used to locate the next element in the linear list – The last node, e n, has no node to link to and so its link field is NULL. This structure is also called a chain. e1e1 e2e2 e3e3 enen …… first Link Field Data Field 0

4 4 Class ‘ChainNode’ template class ChainNode { friend Chain ; private: T data; ChainNode *link; }; Since Chain is a friend of ChainNode, Chain has access to all members (including private members) of ChainNode.

5 5 Class ‘Chain’ template class Chain { public:Chain() { first = 0; } ~Chain(); bool isEmpty() const { return first == 0; } int Length() const; bool Find(int k, T& x) const; int Search(const T& x) const; Chain & Delete(int k, T& x); Chain & Insert(int k, const T& x); void Output(ostream& out) const; private:ChainNode *first; int listSize; };

6 6 Operation ‘~Chain’ template Chain ::~Chain() { ChainNode *next; while (first) { next = first->link; delete first; first = next; } The time complexity is  (n), where n is the length of the chain

7 7 Operation ‘Length’ template int Chain ::Length() const { ChainNode *current = first; int len = 0; while (current) { len++; current = current->link; } return len; } The time complexity is  (n), where n is the length of the chain

8 8 Operation ‘Find’ template bool Chain ::Find(int k, T& x) const {// Set x to the k’th element in the list if it exists // Throw illegal index exception if no such element exists checkIndex(k); // move to desired node ChainNode * current = first; for (int i = 0; i < k; i++) current = current->link; x = current->data; return true; } The time complexity is O(k) Exercise – write the code for checkIndex() operation & determine its time complexity

9 9 Operation ‘checkIndex’ template void Chain ::checkIndex(int Index) const { // Verify that Index is between 0 and listSize-1. if (Index = listSize) {ostringstream s; s << "index = " << Index << " size = "<< listSize; throw illegalIndex(s.str()); } } The time complexity is O(1)

10 Operation ‘Search’ template int Chain ::Search(const T& x) const {// Locate x and return its position if found else return -1 // search the chain for x ChainNode * current = first; int index = 0; // index of current while (current != NULL && current->data != x) { // move to next node current = current->link; index++; } // make sure we found matching element if (current == NULL) return -1; else return index; } The time complexity is O(n)

11 Operation ‘Delete’ To delete the fourth element from the chain, we – locate the third and fourth nodes – link the third node to the fifth – free the fourth node so that it becomes available for reuse first link data 0 80

12 Operation ‘Delete’ See Program 6.7 for deleting a node from a chain The time complexity is O(theIndex)

13 Operation ‘Insert’ ……first k=0 k th node k>0 To insert an element following the kth in chain, we – locate the kth node – new node’s link points to kth node’s link – kth node’s link now points to the new node

14 Operation ‘Insert’ See Program 6.8 for inserting a node into a chain The time complexity is O(theIndex)

15 Circular List Representation Programs that use chains can be simplified or run faster by doing one or both of the following: 1. Represent the linear list as a singly linked circular list (or simply circular list) rather than as a chain 2. Add an additional node, called the head node, at the front

16 Circular List Representation

17 Circular List Representation Why better (run faster) than linear list? – Requires fewer comparisons: compare the following two programs template int Chain ::Search(const T& x) const { ChainNode *current = first; int index = 1; // index of current while (current && current->data != x) { current = current->link; index++; } if (current) return index; return 0; } template int CircularList ::Search(const T& x) const { ChainNode *current = first->link; int index = 1; // index of current first->data = x; // put x in head node while (current->data != x) { current = current->link); index++; } // are we at head? return ((current == first) ? 0 : index); }

18 Doubly Linked List Representation An ordered sequence of nodes in which each node has two pointers: left and right.

19 Class ‘DoubleNode’ template class DoubleNode { friend Double ; private: T data; DoubleNode *left, *right; };

20 Class ‘Double’ template class Double { public:Double() { LeftEnd = RightEnd = 0; }; ~Double(); int Length() const; bool Find(int k, T& x) const; int Search(const T& x) const; Double & Delete(int k, T& x); Double & Insert(int k, const T& x); void Output(ostream& out) const; private:DoubleNode *LeftEnd, *RightEnd; };

21 Circular Doubly Linked List Add a head node at the left and/or right ends In a non-empty circular doubly linked list: – LeftEnd->left is a pointer to the right-most node (i.e., it equals RightEnd) – RightEnd->right is a pointer to the left-most node (i.e., it equals LeftEnd) Can you draw a circular doubly linked list with a head at the left end only by modifying Figure 6.7? READ Sections 6.1~6.4