Header and Trailer Sentinels

Slides:



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

Chapter 3 Lists Dr Zeinab Eid.
Stacks, Queues, and Linked Lists
Linked Lists.
Queues and Linked Lists
Linear Lists – Linked List Representation
Data Structures ADT List
Linked Lists Linear collections.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Doubly Linked List This problem can be easily solved by using the double linked list. - Ed. 2 and 3.: Chapter 4 - Ed. 4: Chapter 3.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Data Structure Lecture-5
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Doubly Linked Lists. One powerful variation of a linked list is the doubly linked list. The doubly linked list structure is one in which each node has.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Linked Lists Dr. Tim Margush University of Akron © 2009.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
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?
Stacks, Queues & Deques CSC212.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
SAK 3117 Data Structures Chapter 6: LINKED LISTS.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
M180: Data Structures & Algorithms in Java Linked Lists Arab Open University 1.
M180: Data Structures & Algorithms in Java Linked Lists – Part 2 Arab Open University 1.
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
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.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked List, Stacks Queues
Lecture 6 of Computer Science II
Cpt S 122 – Data Structures Abstract Data Types
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Double-Ended Queues Chapter 5.
Doubly Linked Lists 6/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Linked List Stacks, Linked List Queues, Dequeues
Doubly Linked List Review - We are writing this code
Algorithm for deleting a node from a singly linked list
Priority Queue.
Data Structures and Database Applications Linked Lists
Programmazione I a.a. 2017/2018.
Queue.
Doubly Linked Lists or Two-way Linked Lists
CS212D: Data Structures Week 5-6 Linked List.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Chapter 4 Linked Lists.
Linked Lists.
CS210- Lecture 6 Jun 13, 2005 Announcements
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

Header and Trailer Sentinels Helps to add special nodes at both ends of the list: a header node at the beginning of the list, and a trailer node at the end of the list. These “dummy” nodes are known as sentinels (or guards), and they do not store elements It avoids some special cases when operating near the boundaries of a doubly linked list

Advantage we can treat all insertions in a unified manner, because a new node will always beplaced between a pair of existing nodes. In similar fashion, every element that is to be deleted is guaranteed to be stored in a node that has neighbors on each side.

Adding nodes(AddFirst())

Adding in between

size( ): Returns the number of elements in the list. isEmpty( ): Returns true if the list is empty, and false otherwise. first( ): Returns (but does not remove) the first element in the list. last( ): Returns (but does not remove) the last element in the list. addFirst(e): Adds a new element to the front of the list. addLast(e): Adds a new element to the end of the list. removeFirst( ): Removes and returns the first element of the list. removeLast( ): Removes and returns the last element of the list.

Class dnode { int data; node prev, next; public node(int x, node t1, node t2) data=x; prev=t1; next=t2; }

private void addBetween(int x, node predecessor, node successor) { node temp = new node(x, predecessor, successor); Predecessor.next=temp; Successor.prev=temp; }

int removeNode(node p) { node predecessor = p.prev; node successor = p.next; predecessor.next=successor; successor.prev=predecessor; return p.data; }

public void addFirst(int x) { addBetween(x, header, header public void addFirst(int x) { addBetween(x, header, header.next); } addLast(int x) { addBetween(x, trailer.prev, trailer);

public int removeFirst( ) { if (isEmpty( )) return null; return removeNode(header.next); } public int removeLast( ) return removeNode(trailer.prev);