8-1.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

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.
Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Double-Linked Lists and Circular Lists
Lecture Stacks. A stack is a Last-In-First-Out (LIFO) or a First-In-Last-Out (FILO) abstract data type E.g. a deck of cards in which cards may be added.
Algorithm Analysis.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
Queues.
1 Lecture 25 Abstract Data Types –III Overview  Basic Node Design  Example: The Dynamic Phone List  Manipulating the Phone List  Inserting Nodes into.
Singly Linked Lists - Ed. 2, 3: Chapter 4 - Ed. 4.: Chapter 3.
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.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
 Pearson Education, Inc. All rights reserved Data Structures.
C++ Classes and Data Structures Jeffrey S. Childs
Dr. Salah Hammami KSU-CCIS-CS Ahmad Al-Rjoub CSC 113 King Saud University College of Computer and Information Sciences Department of Computer Science Chapter.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
1 Chapter 7 The Linked List as a Data Structure. 2 The List ADT A list is a list of elements. The list of elements consist of the data acted upon by list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Chapter 17 – Data Structures Outline Introduction Self-Referential Classes Dynamic Memory Allocation Linked Lists Stacks Queues Trees.
Introduction to Data Structures and Algorithms
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
Chapter 5 Linked Lists II
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Templatized Stack.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
Data Structures: Linked Lists
17 Data Structures.
Chapter 22 Custom Generic Data Structures
Storage Strategies: Dynamic Linking
Doubly Linked List Review - We are writing this code
CISC181 Introduction to Computer Science Dr
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Stack and Queue APURBO DATTA.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
8-1.
Basic Data Types Queues
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Queues.
A Data Structure Bestiary
A Data Structure Bestiary
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
ADT list.
A List Implementation That Links Data
Stacks: Implemented using Linked Lists
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
ITI Introduction to Computing II Lab-12
Programming II (CS300) Chapter 07: Linked Lists
Queues: Implemented using Linked Lists
Lecture 16 Stacks and Queues CSE /26/2018.
Linked List Insert After
A List Implementation That Links Data
Presentation transcript:

8-1

Or p.setNext(q);

:Object :Object

public class List { private node head; private node tail; private String Name; public List() { head = tail = null; Name = “No Name”; } public List(String name) { Name = name; public boolean isEmpty() { return head == null;

Method insertAtFront The Node constructor call sets data to refer to the new object passed as an argument and sets reference next to null. Set reference next of the new Node object to the current head node of the list. Call isEmpty to determine whether the list is empty If the list is empty, assign head and tail to the new Node object. If the list is not empty, assign head to the new Node object and. public Node (Object obj){ data=obj; next=null } .

Method insertAtBack The Node constructor call sets data to refer to the new object passed as an argument and sets reference next to null Call isEmpty to determine whether the list is empty If the list is empty, assign head and tail to the new Node object. If the list is not empty, find the last node by traversing “walking through the list” (or use the tail Node object ). Set reference next of the last Node object to the new Node Object. Set tail to the new Node Object. public Node (Object obj){ data=obj; next=null }

Linked List InsertAtBack (cont) public void insertAtBack(Object obj) { Node newnode = new Node(obj); if (isEmpty()) head = tail = newnode; else { Node current = head; // Start at head of list while (current.getNext() != null) // Find the end of the list current = current.getNext(); current.setNext(newnode)// Insert the newObj tail=newnode; } } // insertAtRear} Another solution using the tail if(isempty()) else{ tail.setNext(newnode); Order is important in these two statements

Method removeFromFront Call isEmpty to determine whether the list is empty. If the list is empty, return null. If the list is not empty, save a reference to the head node (call it first for example). If there is only one node (i.e. head==tail), set both head and tail to null. If there is more than one node, make head point to its next node. Return the data of the saved first node (the previous list head).

else

Method removeFromBack Call isEmpty to determine whether the list is empty. If the list is empty, return null. If there is only one node, set both head and tail to null. If there is more than one node, create a Node reference current and assign it to the list’s head. Using current, “Walk through” the list, each time moving current to the next node and saving the previous node, until you reach the last node in the list (i.e. current.next = null). The last node is now current, and the node before last is previous. Set the next node of previous to null and set tail to previous. Return the data of the current node (the previously last node).

Previous

Stacks and Queues A Stack is a Last in First out (LIFO) data structure. We can use a linked list implementation of stacks. We use the methods insertAtFront and removeFromFront to insert and remove items from the stack.

Stacks and Queues A Queue is a First in First out (FIFO) data structure. We can use a linked list implementation of a Queue. We use the methods insertAtBack and removeFromFront to insert and remove items from the Queue.