Linear Lists – Linked List Representation

Slides:



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

Chapter 3: Linked List Tutor: Angie Hui
0 - 0.
1 Linked Lists Continued Lecture 5 Copying and sorting singly linked lists Lists with head and last nodes Doubly linked lists ADS2 Lecture 5.
Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
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.
Lists CS 3358.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
Linked Lists Chapter 4.
Linear Lists – Array Representation
Queues and Linked Lists
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
DATA STRUCTURES USING C++ Chapter 5
Chapter 24 Lists, Stacks, and Queues
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
Linked Lists Linked Lists Representation Traversing a Linked List
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Data Structures Using C++
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
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.
Double-Linked Lists and Circular Lists
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSCI2100B Linked List Jeffrey
CSE Lecture 12 – Linked Lists …
1 Designing Hash Tables Sections 5.3, 5.4, Designing a hash table 1.Hash function: establishing a key with an indexed location in a hash table.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Linked Lists.
Linked Lists.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
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
Data Structures: A Pseudocode Approach with C
Chapter 3: Abstract Data Types Lists, Stacks Lydia Sinapova, Simpson College Mark Allen Weiss: Data Structures and Algorithm Analysis in Java.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
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.
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.
Summary of lectures (1 to 11)
Chapter 3: Arrays, Linked Lists, and Recursion
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Linear List Linked Representation. Linked Representation list elements are stored, in memory, in an arbitrary order explicit information (called a link)
Data Structures Using Java1 Chapter 4 Linked Lists.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures Using C++1 Chapter 5 Linked Lists.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Data Structure & Algorithms
UNIT-II Topics to be covered Singly linked list Circular linked list
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
UNIT – I Linked Lists.
UNIT-3 LINKED LIST.
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Presentation transcript:

Linear Lists – Linked List Representation Computer College

Outlines Linked list nodes Linked list operations Insertion Deletion Linked list representation & implementation Other types of linked lists Sorted Doubly-linked Circular

Single Linked List Definition: A linked list is a collection of nodes that together form a linear ordering. Each node is a compound object that stores an element and a reference, called next, to another node. Structure of a node Data Link Node Node Node Node first A0 A1 A2 A3

Characteristics Information (data) Link to the next node Insert and delete nodes in any order The nodes are connected Each node has two components Information (data) Link to the next node The nodes are accessed through the links between them

For each node the node that is in front of it is called predecessor. Head Predecessor of X Node X Success-or of X tail For each node the node that is in front of it is called predecessor. The node that is after it is called successor.

Terminology Head (front, first node): The node without predecessor, the node that starts the lists. Tail (end, last node): The node that has no successor, the last node in the list. Current node: The node being processed. From the current node we can access the next node. Empty list: No nodes exist

Node Linking 1. Single linked lists : 2. Double linked lists : Each node contains two links - to the previous and to the next node 2. Double linked lists : Each node contains a link only to the next node 3. Circular lists: The tail is linked to the head.

Single linked List Properties Stores a collection of items non-contiguously. Each item in the list is stored with an indication of where the next item is. Must know where first item is. The list will be a chain of objects, called nodes, of type Node that contain the data and a reference to the next Node in the list. Allows addition or deletion of items in the middle of collection with only a constant amount of data movement. Contrast this with array.

Singly Linked List Let L = (e1,e2,…,en) Each element ei 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, en, has no node to link to and so its link field is NULL. This structure is also called a chain. e1 e2 e3 en …… first Link Field Data Field

Class ‘ChainNode’ public class Node { Object data; Node next; Node(Object obj, Node element) data = obj; next = element; }

Operations in ADT Notation Insert(L,obj) Inserts a node with information e before the current position Delete(L) Deletes the current node in L , the current position indicates the next node. RetrieveInfo(L)  obj Returns the information in the current node.

Insertion Insertion To insert a node X between the nodes A and B: .Create a link from X to B. .Create a link from A to X,

Insertion X A B

Adding an element at the beginning Create a new node; Element in the node has the same value as the new element; Node pointer points to the first element (non-header) Pointer from the header points to new node; Create(newnode); newnode.next header.next.next header.next newnode; O(1);

Code Fragment to insert at a head public void inserthead(Object obj) { Node newNode = new Node(obj); // make new Node newNode.next = head; // newNode --> old head head = newNode; // head --> newNode } The time complexity O(1)

Deletion Deletion To delete a node X between A and B: Create a link from A to B, Remove node X

Code Fragment to delete first node public Node deleteHead() // delete head item { // (assumes list not empty) Node temp = head; // save reference to link head = head.next; / delete it: head-->old next return temp; // return deleted link } The time complexity O(1)

Code Fragment to insert at a tail public void insertLast(Object obj) { Node newNode = new Node(obj); // make new link if( isEmpty() ) // if empty list, head = newNode; // first --> newNode else tail.next = newNode; // old tail --> newNode tail = newNode; // newNode <-- tail } The time complexity O(1)

Traversal public int countNodes() { int count = 0; Element e = head; while(e != null) count++; e = e.next; } return count; A method that computes the number of elements in any list:

Code Fragment to delete at a tail public Node deleteTail( ) // delete link with given key { Node current = head; // search for link Node previous = head; while(current.next != null) if(current.next == null) return null; // didn't find it else previous = current; // go to next link current = current.next; } } // found it if(current == head) // if first link, head= head.next; // change first Else // otherwise, previous.next = current.next; // bypass it return current; The time complexity O(1)

Delete any Node 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 20 10 30 11 first link data 80

The List ADT Implementation A0 A1 A2 AN-1 O(1) running time A0 A1 A2 by Array A0, A1, A2, ..., AN-1 Operation:findKth A0 A1 A2 AN-1 O(1) running time A0 A1 A2 AN-1 return Arr[2];

The List ADT Implementation A0 A1 A2 AN-1 O(N) running time A0 A1 A2 by Array A0, A1, A2, ..., AN-1 Operation: deletion A0 A1 A2 AN-1 O(N) running time A0 A1 A2 AN-1 A0 A2 A3 AN-1

The List ADT Implementation A0 A1 A2 AN-1 O(N) running time A0 A1 A2 by Array A0, A1, A2, ..., AN-1 Operation: insertion A0 A1 A2 AN-1 O(N) running time A0 A1 A2 AN-1 A0 A1 A’ A2 A3 AN-2 AN-1

The List ADT a node: element A3 Implementation next link A0, A1, A2, ..., AN-1 by Linked List Operation: FindKth next next next next null O(N) running time

The List ADT Implementation O(1) running time A0, A1, A2, ..., AN-1 by Linked List Operation: deletion O(1) running time

The List ADT Implementation O(1) running time A0, A1, A2, ..., AN-1 by Linked List Operation: insertion O(1) running time

The List ADT Implementation A0, A1, A2, ..., AN-1 by doubly linked list

The List ADT Summary Array List (Single) Linked List findKth O(1) O(n) running time coomparion when to use Array list or Linked list? Array list: numerous findKth operations + seldom delete/insert operations Linked list: numerous delete/insert operations + seldom findKth operations Array List (Single) Linked List findKth O(1) O(n) insert delete

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

Circular List Representation

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

Class ‘DoubleNode’ public class Node { Public String element; Public Node prev; Public Node next; Public Node(Object obj, Node p, Node n) Element=obj; Prev=p; Next=n; }

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?