Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)

Slides:



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

Linked Lists.
Linear Lists – Array Representation
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Review Learn about linked lists
List Representation - Chain Fall 2010 CSE, POSTECH.
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.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
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.
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.
Data Structures Using C++ 2E
Linear Lists – Linked List Representation CSE, POSTECH.
Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
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.
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.
Dictionaries Collection of pairs.  (key, element)  Pairs have different keys  E.g. a pair contains two components, student id and name (1234, Nan)
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Stacks Chapter 8. Objectives In this chapter, you will: Learn about stacks Examine various stack operations Learn how to implement a stack as an array.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
ITERATORS. Iterator An iterator in C++ is a concept that refines the iterator design pattern into a specific set of behaviors that work well with the.
Data Structures Using C++1 Chapter 5 Linked Lists.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
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.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
SNU IDB Lab. Ch6. Linear Lists – Linked Representation © copyright 2006 SNU IDB Lab.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Simulated Pointers Limitations Of C++ Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
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.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
The Class arrayList General purpose implementation of linear lists. Unknown number of lists.
Reminder: Course Policy
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
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.
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
The Class arrayList General purpose implementation of linear lists.
Chapter 18: Linked 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.
Linked Representation
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.
Chapter 17: Linked Lists.
The Class chain.
The Class chain.
Data Structures & Algorithms
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 List Linked Representation

Linked Representation 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

Memory Layout abcdecaedb A linked representation uses an arbitrary layout. Layout of L = (a,b,c,d,e) using an array representation. The figures show computer memory. An array uses a contiguous chunk of memory.

Linked Representation pointer (or link) in e is NULL caedb use a variable firstNode to get to the first element a firstNode

Normal Way To Draw A Linked List link or pointer field of node data field of node abcde NULL firstNode

Chain A chain is a linked list in which each node represents one element. There is a link or pointer from one element to the next. The last node has a NULL pointer. abcde NULL firstNode

Node Representation template struct chainNode { // data members T element; chainNode *next; // constructors come here }; next element

Constructors Of chainNode chainNode() {} ? ? ? element next element chainNode(const T& element) {this->element = element;} chainNode(const T& element, chainNode * next) {this->element = element; this->next = next;}

get(0) checkIndex(0); desiredNode = firstNode; // gets you to first node return desiredNode  element; abcde NULL firstNode Do a checkIndex first. Then move to desired node. Once you are at the desired node, you can return the element in that node as in return desiredNode.element; When firstNode = null, there is no element whose index is 0.

get(1) checkIndex(1); desiredNode = firstNode  next; // gets you to second node return desiredNode  element; abcde NULL firstNode

get(2) checkIndex(2); desiredNode = firstNode  next  next; // gets you to third node return desiredNode  element; abcde NULL firstNode

get(5) checkIndex(5); // throws exception desiredNode = firstNode  next  next  next  next  next; // desiredNode = NULL return desiredNode  element; // NULL.element abcde NULL firstNode

Get(theIndex) Check theIndex currentNode = firstNode For (i=0; i<theIndex;i++) –currentNode = currentNode->next; Return currentNode->element

Erase An Element erase(0) abcde NULL firstNode deleteNode = firstNode; firstNode = firstNode  next; delete deleteNode;

abde NULL firstNode c erase(2) first get to node just before node to be removed c c beforeNode = firstNode  next ; b beforeNode

erase(2) save pointer to node that will be deleted deleteNode = beforeNode  next; beforeNode abcde null firstNode

erase(2) now change pointer in beforeNode beforeNode  next = beforeNode  next  next; delete deleteNode; beforeNode abcde null firstNode

Erase(theIndex) Check theIndex If theIndex == 0 –Delete firstNode –firstNode = firstNode->next Else –Find beforeNode –deleteNode = beforeNode->next; –beforeNode->next = beforNode->next->next –Delete deleteNode listSize--

insert(0,’f’) abcde NULL firstNode f newNode Step 1: get a node, set its data and link fields newNode = new chainNode (theElement, firstNode);

insert(0,’f’) abcde NULL firstNode f newNode Step 2: update firstNode firstNode = newNode;

One-Step insert(0,’f’) abcde NULL firstNode f newNode firstNode = new chainNode (‘f’, firstNode);

insert(3,’f’) first find node whose index is 2 abcde NULL firstNode f newNode beforeNode c next create a node and set its data and link fields chainNode * newNode = new chainNode ( ‘f’, beforeNode  next); finally link beforeNode to newNode beforeNode  next = newNode;

Two-Step insert(3,’f’) beforeNode = firstNode  next  next; beforeNode  next = new chainNode (‘f’, beforeNode  next); abcde NULL firstNode f newNode beforeNode c

Insert(theIndex, theElement) Check theIndex (0-listSize) If theIndex == 0 – create newNode –newNode = firstNode Else –Create newNode –Find the beforeNode –newNode->next = beforeNode->next –beforeNode->next = newNode listSize++

Other chain structures

Chain With Header Node abcde NULL headerNode abcde NULL firstNode insert/erase code is simplified

Empty Chain With Header Node headerNode NULL

The Class chainWithHeader template class chainWithHeader : public linearList { public: // other ADT methods defined here protected: void checkIndex(int theIndex) const; chainNode * HeaderNode; int listSize; };

Circular List abcde firstNode

Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abcde firstNode

Doubly Linked List abcde NULL firstNode NULL lastNode

Node Representation template struct doublyChainNode { // data members T element; chainNode *previous; chainNode *next; // constructors come here }; next element previous

Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abcde NULL firstNode NULL lastNode

Doubly Linked Circular List abcde firstNode

Insert(0,’f’) insert(listSize,’g’) erase(0) erase(listSize-1) abce headerNode d

Doubly Linked Circular List With Header Node abce headerNode d

Empty Doubly Linked Circular List With Header Node headerNode

The STL Class list/vector  Linked implementation of a linear list.  Doubly linked circular list with header node.  Has many more methods than chain.  Similar names and signatures.

Lab 2 Check the note and download your lab material before lab, bring your labtop with you in class!