Arrays and Linked Lists

Similar documents
Unordered Linked Lists

STACKS,QUEUES, AND LINKED LISTS

What is a Stack? Stacks and Queues. Stack Abstract Data Type. Java Interface for Stack ADT. Array-based Implementation

Sequential Data Structures

Universidad Carlos III de Madrid

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Linked List as an ADT (cont d.)

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Singly linked lists (continued...)

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Analysis of a Search Algorithm

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Lecture 12 Doubly Linked Lists (with Recursion)

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Data Structures and Algorithms Lists

Abstract Data Types. Chapter 2

ADTs,, Arrays, Linked Lists

Queues Outline and Required Reading: Queues ( 4.2 except 4.2.4) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Ordered Lists and Binary Trees

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

7.1 Our Current Model

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

DATA STRUCTURE - STACK

Sequences in the C++ STL

Algorithms and Data Structures

recursion, O(n), linked lists 6/14

Data Structures and Algorithms

DATA STRUCTURES USING C

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Common Data Structures

Data Structures Fibonacci Heaps, Amortized Analysis

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Course: Programming II - Abstract Data Types. The ADT Queue. (Bobby, Joe, Sue, Ellen) Add(Ellen) Delete( ) The ADT Queues Slide Number 1

Node-Based Structures Linked Lists: Implementation

Data Types. Abstract Data Types. ADTs as Design Tool. Abstract Data Types. Integer ADT. Principle of Abstraction

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Linked Lists: Implementation Sequences in the C++ STL

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

LINKED DATA STRUCTURES

Symbol Tables. Introduction

D06 PROGRAMMING with JAVA

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Lecture Notes on Binary Search Trees

Last not not Last Last Next! Next! Line Line Forms Forms Here Here Last In, First Out Last In, First Out not Last Next! Call stack: Worst line ever!

Class 32: The Java Collections Framework

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

Financial Management System

Lecture Notes on Binary Search Trees

10CS35: Data Structures Using C

Data Structures and Data Manipulation

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Chapter 3: Restricted Structures Page 1

Glossary of Object Oriented Terms

Circular Linked List. Algorithms and Data Structures

Introduction to Object-Oriented Programming

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Converting a Number from Decimal to Binary

Data Structures in the Java API

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

CmpSci 187: Programming with Data Structures Spring 2015

22c:31 Algorithms. Ch3: Data Structures. Hantao Zhang Computer Science Department

Cours de C++ Utilisations des conteneurs

Introduction to Data Structures

Programming with Data Structures

Data Structures and Algorithms

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

C++ INTERVIEW QUESTIONS

El Dorado Union High School District Educational Services

The ADT Binary Search Tree

Stacks and queues. Algorithms and Data Structures, Fall Rasmus Pagh. Based on slides by Kevin Wayne, Princeton

CSE 211: Data Structures Lecture Notes VII

1. Relational database accesses data in a sequential form. (Figures 7.1, 7.2)

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Algorithms and Data Structures Written Exam Proposed SOLUTION

Structure for String Keys

Persistent Binary Search Trees

Queues and Stacks. Atul Prakash Downey: Chapter 15 and 16

Data Structures and Algorithms Stacks and Queues

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Simple sorting algorithms and their complexity. Bubble sort. Complexity of bubble sort. Complexity of bubble sort. Bubble sort of lists

Recursive Implementation of Recursive Data Structures

Project 4 DB A Simple database program

Stacks. Linear data structures

Cpt S 223. School of EECS, WSU

PES Institute of Technology-BSC QUESTION BANK

Transcription:

Arrays and Linked Lists Data Structures and Algorithms Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++ Goodrich, Tamassia and Mount (Wiley, 2004)

Outline Arrays Singly Linked Lists Doubly Linked Lists Linked Lists 2

Arrays Built-in in most programming languages Two kinds (programmer responsibility): Unordered: attendance tracking Ordered: high scorers Operations: Insertion Deletion Search Linked Lists 3

Arrays Operation Unordered Ordered Insertion O(1) O(n) Deletion O(n) O(n) Search O(n) O(logn) Pluses & minuses + Fast element access; simple -- Impossible to resize; slow deletion Many applications require resizing! Required size not always immediately available. Linked Lists 4

Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Linked Lists 5

Abstract Data Types - Example An ADT modeling a simple stock trading system Data stored: buy/sell orders Operations on the data: order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order Linked Lists 6

Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It gives a unified view of diverse ways of storing data, such as a cell of an array a node of a linked list Just one method: Object * getelement(): returns the element stored at the position Linked Lists 7

List ADT The List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: size(), isempty() Accessor methods: first(), last() prev(p), next(p) Update methods: replace(p, e) insertbefore(p, e), insertafter(p, e), insertfirst(e), insertlast(e) remove(p) Linked Lists 8

Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node element next node A B C D Linked Lists 9

Node struct struct Node { // Instance variables Object* element; Node* next; next } // Initialize a node Node() { this(null, null); } Node(Object* e, Node* n) { element = e; next = n; } element node Linked Lists 10

Singly linked list struct SLinkedList { Node* head; // head node of the list long size; // number of nodes in the list }; /* Default constructor that creates an empty list */ SLinkedList() { head = null; size = 0; } //... update and search methods would go here... boolean isempty() {return head ==null; } void insertafter(node * node, Object* element) {...}... Linked Lists 11

Inserting at the Head head Rome Seattle Toronto 1. Allocate a new node 2. Insert new element 3. Make new node point to old head Vienna head Rome Seattle Toronto head 4. Update head to point to new node Vienna Rome Seattle Toronto Linked Lists 12

Algorithm addfirst Algorithm addfirst(v) Input node v to be added to the beginning of the list Output v.setnext (head) {make v point to the old head node} head v {make variable head point to new node} size size + 1 {increment the node count} Linked Lists 13

Removing at the Head head Vienna Rome Seattle Toronto head 1. Update head to point to next node in the list 2. Disallocate the former first node Vienna Rome Seattle Toronto the garbage collector to reclaim (Java), or head the programmer does the job (C/C++) Rome Seattle Toronto Linked Lists 14

Algorithm removefirst Algorithm removefirst() if head = null then Indicate an error: the list is empty t head head head.getnext() {make head point to next node or null} Disallocate node t size size - 1 {decrement the node count} {null out t's next pointer or free t's memory} Linked Lists 15

Singly linked list with tail sentinel head tail struct SLinkedListWithTail { Node* head; // head node Node* tail; // tail node of the list Rome Seattle Toronto } /* Default constructor that creates an empty list */ SLinkedListWithTail() { head = null; tail = null; } //... update and search methods would go here... Linked Lists 16

Inserting at the Tail head tail 1. Allocate a new node 2. Insert new element 3. Have new node point to null Rome Seattle Toronto head tail Rome Seattle Toronto Zurich 4. Have old last node point to new node 5. Update tail to point to new node head Rome tail Seattle Toronto Zurich Linked Lists 17

Algorithm addlast Algorithm addlast(v) Input node v to be added to the end of the list Output v.setnext (NULL) {make new node v point to null object} tail.setnext(v) {make old tail node point to new node} tail size; size size + 1 {make variable tail node point to new node} {increment the node count} Linked Lists 18

Removing at the Tail Removing at the tail of a singly linked list cannot be efficient! There is no constant-time way to update the tail to point to the previous node head tail Rome Seattle Toronto Zurich Linked Lists 19

Doubly Linked List A doubly linked list is often more convenient! Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev elem next node header nodes/positions trailer elements Linked Lists 20

Node struct prev next elem node /* Node of a doubly linked list of strings */ struct DNode { string* element; DNode *next, *prev; // Pointers to next and previous node }; /* Initialize a node. */ DNode(string* e, DNode* p, DNode *n) { element = e; prev = p; next = n; } string* getelement() { return element; } Linked Lists 21

Class for doubly linked list struct DList { DNode* header, *trailer; // sentinels long size; // number of nodes in the list }; /* Default constructor that creates an empty list */ DList() { header = new DNode(NULL, NULL, NULL); trailer = new DNode(NULL, header, NULL); header->next = trailer; size = 0; } //... update and search methods would go here... Linked Lists 22

Insertion We visualize operation insertafter(p, X), which returns position q p A B C p A B q C p X q A B X C Linked Lists 23

Insertion Algorithm Algorithm insertafter(p,e): Create a new node v v.setelement(e) v.setprev(p) { link v to its predecessor } v.setnext(p.getnext()) { link v to its successor } (p.getnext()).setprev(v) { link p s old successor to v } p.setnext(v) {link p to its new successor, v } return v {the position for the element e } Linked Lists 24

Deletion We visualize remove(p), where p == last() p A B C D A B C p D A B C Linked Lists 25

Deletion Algorithm Algorithm remove(p): t = p.element {temporary variable to hold the return value} (p.getprev()).setnext(p.getnext()) {linking out p} (p.getnext()).setprev(p.getprev()) Disallocate node p {invalidating the position p} return t Linked Lists 26

Worst-case running time In a doubly linked list + insertion at head or tail is in O(1) + deletion at either end is on O(1) -- element access is still in O(n) Linked Lists 27