Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright © 2008-2011.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

1 Linked lists Sections 3.2, 3.3, 3.5 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors.
Lists A list is a finite, ordered sequence of data items. Important concept: List elements have a position. Notation: What operations should we implement?
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
1 Linked List Position (1). 2 Linked List Position (2)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
E.G.M. Petrakislists1 Lists  List: finite sequence of data elements  all elements have the same data type  The operations depend on the type of the.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (4) Data Structures 11/18/2008 Yang Song.
Queues.
CS 106 Introduction to Computer Science I 12 / 11 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 4 Linked Structures – Stacks Modified. Chapter Scope Object references as links Linked vs. array-based structures Managing linked lists Linked.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Information and Computer Sciences University of Hawaii, Manoa
1 CSE 1342 Programming Concepts Lists. 2 Basic Terminology A list is a finite sequence of zero or more elements. –For example, (1,3,5,7) is a list of.
Cosc237/data structures1 Data Types Every data type has two characteristics: 1.Domain - set of all possible values 2.set of allowable operations Built-in.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Stacks And Queues Chapter 18.
Review of Lists, Stacks, and Queues CS 400/600 – Data Structures.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
UNCA CSCI September, 2001 These notes were prepared by the text’s author Clifford A. Shaffer Department of Computer Science Virginia Tech Copyright.
April 27, 2017 COSC Data Structures I Review & Final Exam
Linear Data Structures
Computer Engineering Rabie A. Ramadan Lecture 6.
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.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©
Linked Data Structures
CSE 1342 Programming Concepts
Cpt S 122 – Data Structures Abstract Data Types
School of Computing Clemson University Fall, 2012
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 4 The easy stuff.
QueueStack CS1020.
September 29 – Stacks and queues
Queues Rem Collier Room A1.02
Chapter 15 Lists Objectives
Stacks.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CST230: Applied Data Structures
Lists List: finite sequence of data elements
CSCI 333 Data Structures Chapter 4 13 and 16 September 2002.
ADT list.
Stacks and Queues 1.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks and Queues.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Dynamic allocation (continued)
Presentation transcript:

Coursenotes CS3114: Data Structures and Algorithms Clifford A. Shaffer Yang Cao Department of Computer Science Virginia Tech Copyright ©

2 Doubly Linked Lists class DLink { private E element; private DLink next; private DLink prev; DLink(E it, DLink n, DLink p) { element = it; next = n; prev = p; } DLink(DLink n, DLink p) { next = n; prev = p; } DLink next() { return next; } DLink setNext(DLink nextval) { return next = nextval; } DLink prev() { return prev; } DLink setPrev(DLink prevval) { return prev = prevval; } E element() { return element; } E setElement(E it) { return element = it; } }

3 Doubly Linked Lists

4 Doubly Linked Insert

5 public void insert(E it) { curr.setNext( new DLink (it, curr.next(), curr)); if (curr.next().next() != null) curr.next().next().setPrev(curr.next()); if (tail == curr) tail = curr.next(); cnt++; }

6 Doubly Linked Remove

7 public E remove() { if (curr.next() == null) return null; E it = curr.next().element(); if (curr.next().next() != null) curr.next().next().setPrev(curr); else tail = curr; curr.setNext(curr.next().next()); cnt--; return it; }

8 Stacks LIFO: Last In, First Out. Restricted form of list: Insert and remove only at front of list. Notation: Insert: PUSH Remove: POP The accessible element is called TOP.

9 Stack ADT public interface Stack { /** Reinitialize the stack. */ public void clear(); /** Push an element onto the top of the it Element being pushed onto the stack.*/ public void push(E it); /** Remove and return top The element at the top of the stack.*/ public E pop(); A copy of the top element. */ public E topValue(); Number of elements in the stack. */ public int length(); };

10 Array-Based Stack // Array-based stack implementation private int maxSize; // Max size of stack private int top; // Index for top private E [] listArray; Issues: Which end is the top? Where does “top” point to? What are the costs of the operations?

11 Linked Stack class LStack implements Stack { private Link top; private int size; What are the costs of the operations? How do space requirements compare to the array-based stack implementation?

12 Queues FIFO: First in, First Out Restricted form of list: Insert at one end, remove from the other. Notation: Insert: Enqueue Delete: Dequeue First element: Front Last element: Rear

13 Queue Implementation (1)

14 Queue Implementation (2)

15 Dictionary Often want to insert records, delete records, search for records. Required concepts: Search key: Describe what we are looking for Key comparison –Equality: sequential search –Relative order: sorting

Records and Keys Problem: How do we extract the key from a record? 16

Records and Keys Problem: How do we extract the key from a record? Records can have multiple keys. 17

Records and Keys Problem: How do we extract the key from a record? Records can have multiple keys. Fundamentally, the key is not a property of the record, but of the context. Solution: We will explicitly store the key with the record. 18

19 Dictionary ADT public interface Dictionary { public void clear(); public void insert(K k, E e); public E remove(K k); // Null if none public E removeAny(); // Null if none public E find(K k); // Null if none public int size(); };

Payroll Class // Simple payroll entry: ID, name, address class Payroll { private Integer ID; private String name; private String address; Payroll(int inID, String inname, String inaddr) { ID = inID; name = inname; address = inaddr; } public Integer getID() { return ID; } public String getname() { return name; } public String getaddr() { return address; } } 20

Using Dictionary // IDdict organizes Payroll records by ID Dictionary IDdict = new UALdictionary (); // namedict organizes Payroll records by name Dictionary namedict = new UALdictionary (); Payroll foo1 = new Payroll(5, "Joe", "Anytown"); Payroll foo2 = new Payroll(10, "John", "Mytown"); IDdict.insert(foo1.getID(), foo1); IDdict.insert(foo2.getID(), foo2); namedict.insert(foo1.getname(), foo1); namedict.insert(foo2.getname(), foo2); Payroll findfoo1 = IDdict.find(5); Payroll findfoo2 = namedict.find("John"); 21

22 Unsorted List Dictionary class UALdictionary implements Dictionary { private static final int defaultSize = 10; private AList > list; // Constructors UALdictionary() { this(defaultSize); } UALdictionary(int sz) { list = new AList >(sz); } public void clear() { list.clear(); } /** Insert an element: append to list */ public void insert(K k, E e) { KVpair temp = new KVpair (k, e); list.append(temp); }

Sorted vs. Unsorted List Dictionaries If list were sorted –Could use binary search to speed search –Would need to insert in sorted order, slowing insert Which is better? –If lots of searches, sorted list is good –If inserts are as likely as searches, then sorting is no benefit. 23