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.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

Chapter 17 Linked Lists.
Linked Lists.
Linear Lists – Linked List Representation
Chapter 17 Linked List Saurav Karmakar Spring 2007.
List Representation - Chain Fall 2010 CSE, POSTECH.
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Understand arrays and their usefulness. Understand records and the difference between.
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.
Binomial Heaps. Min Binomial Heap Collection of min trees
©Brooks/Cole, 2003 Chapter 11 Data Structures. ©Brooks/Cole, 2003 Data Structure Data structure uses collection of related variables that can be accessed.
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 Lists – Linked List Representation CSE, POSTECH.
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.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Simulated Pointers Limitations Of Java Pointers May be used for internal data structures only. Data structure backup requires serialization and deserialization.
Chapter 11 Data Structures. Understand arrays and their usefulness. Understand records and the difference between an array and a record. Understand the.
The Class Chain. next (datatype ChainNode) element (datatype Object) Use ChainNode abcde null firstNode size = number of elements.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Copyright © 2012 Pearson Education, Inc. Chapter 20: Binary Trees.
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.
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:
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.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Chapter 16: Linked Lists.
Reminder: Course Policy
C++ Programming:. Program Design Including
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Stacks.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
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.
Chapter 20: Binary Trees.
Linked List Yumei Huo Department of Computer Science
The Class chain.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
A Bag Implementation that Links Data
Chapter 21: Binary Trees.
Simulated Pointers.
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.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Simulated Pointers.
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
Chapter 11 Data Structures.
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.
Lists.
The Class chain.
Binary Tree Properties & Representation
Data Structures & Algorithms
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
LINKED LIST Dr. T. Kokilavani Assistant Professor
Data Structures Chapter 4: Linked Lists.
Presentation transcript:

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

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

Linked Representation c a e d b first pointer (or link) in e is NULL use a variable first to get to the first element a

Normal Way To Draw A Linked List b c d e NULL first link or pointer field of node data field of node

Chain first NULL a b c d e 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 (or 0) pointer.

Node Representation template <class T> class ChainNode { private: T data; ChainNode<T> *link;   // constructors come here }; link data Better to define as a struct than a class as otherwise need to make every using class a friend so that node fields may be accessed.

Constructors Of ChainNode ? ChainNode() {} ? data ChainNode(const T& data) {this->data = data;} ChainNode(const T& data, chainNode<T>* link) {this->data = data; this->link = link;} link data

Get(0) a b c d e first desiredNode = first; // gets you to first node NULL first desiredNode = first; // gets you to first node return desiredNode->data; Move to desired node. Note that desired node may not exist. 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) a b c d e NULL first desiredNode = first->link; // gets you to second node return desiredNode->data; Node that desiredNode may not exist!

Get(2) a b c d e NULL first desiredNode = first->link->link; // gets you to third node return desiredNode->data; Note that desiredNode may not exist

Get(5) a b c d e NULL First desiredNode = first->link->link->link->link->link; // desiredNode = NULL return desiredNode->data; // NULL.element Get an exception now as NULL->data is illegal!

Delete An Element Delete(0) deleteNode = first; b c d e NULL first Delete(0) Note that node 0 may not exist! deleteNode = first; first = first->link; delete deleteNode;

first get to node just before node to be removed Delete(2) a b d e NULL first c b beforeNode c first get to node just before node to be removed beforeNode = first->link;

save pointer to node that will be deleted first null a b c d e beforeNode save pointer to node that will be deleted deleteNode = beforeNode->link;

now change pointer in beforeNode Delete(2) first null a b c d e beforeNode now change pointer in beforeNode beforeNode->link = beforeNode->link->link; delete deleteNode;

Insert(0,’f’) first newNode a b c d e NULL first f newNode Step 1: get a node, set its data and link fields First, do a checkIndex. newNode = new ChainNode<char>(theElement, first);

Insert(0,’f’) first newNode Step 2: update first first = newNode; a b c d e NULL first f newNode Step 2: update first first = newNode;

first = new chainNode<char>(‘f’, first); One-Step Insert(0,’f’) a b c d e NULL first f newNode first = new chainNode<char>(‘f’, first);

first find node whose index is 2 Insert(3,’f’) a b c d e NULL first f newNode beforeNode c first find node whose index is 2 next create a node and set its data and link fields ChainNode<char>* newNode = new ChainNode<char>( ‘f’, beforeNode->link); finally link beforeNode to newNode beforeNode->link = newNode;

Two-Step Insert(3,’f’) beforeNode = first->link->link; a b c d e NULL first f newNode beforeNode beforeNode = first->link->link; beforeNode->link = new ChainNode<char> (‘f’, beforeNode->link);