Unordered Linked Lists



Similar documents
Linked List as an ADT (cont d.)

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

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

Application of Stacks: Postfix Expressions Calculator (cont d.)

10CS35: Data Structures Using C

DATA STRUCTURES USING C

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Data Structures Fibonacci Heaps, Amortized Analysis

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

Converting a Number from Decimal to Binary

STACKS,QUEUES, AND LINKED LISTS

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

Ordered Lists and Binary Trees

PES Institute of Technology-BSC QUESTION BANK

Sequential Data Structures

Data Structures and Algorithms

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

Lecture 12 Doubly Linked Lists (with Recursion)

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

The ADT Binary Search Tree

Node-Based Structures Linked Lists: Implementation

Common Data Structures

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

- 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

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

Analysis of a Search Algorithm

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

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

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Stacks. Linear data structures

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:

Universidad Carlos III de Madrid

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

Data Structures and Algorithms Lists

Sequences in the C++ STL

Data Structure [Question Bank]

Introduction to Object-Oriented Programming

Section IV.1: Recursive Algorithms and Recursion Trees

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

Data Structures and Algorithms(5)

Chapter 3: Restricted Structures Page 1

Algorithms and Data Structures Written Exam Proposed SOLUTION

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Linked Lists: Implementation Sequences in the C++ STL

LINKED DATA STRUCTURES

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

Atmiya Infotech Pvt. Ltd. Data Structure. By Ajay Raiyani. Yogidham, Kalawad Road, Rajkot. Ph : ,

Data Structures and Algorithms

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

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Data Structures and Algorithms

BCS2B02: OOP Concepts and Data Structures Using C++

Binary Heap Algorithms

Financial Management System

7.1 Our Current Model

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

Data Structures and Data Manipulation

Queues. Manolis Koubarakis. Data Structures and Programming Techniques

Algorithms and Data Structures

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

TREE BASIC TERMINOLOGIES

ADTs,, Arrays, Linked Lists

USER MANUAL. Issue 3 - December Manual Stock No Software Version 3.0 Zero 88 Lighting Ltd. 2003

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

Introduction to Data Structures and Algorithms

C++ INTERVIEW QUESTIONS

QUEUES. Primitive Queue operations. enqueue (q, x): inserts item x at the rear of the queue q

Efficient Data Structures for Decision Diagrams

Queue Implementations

DATA STRUCTURE - STACK

Sample Questions Csci 1112 A. Bellaachia

Lecture Notes on Binary Search Trees

COMPUTER SCIENCE. Paper 1 (THEORY)

Basic Data Structures and Algorithms

6 March Array Implementation of Binary Trees

Two Parts. Filesystem Interface. Filesystem design. Interface the user sees. Implementing the interface

Symbol Tables. Introduction

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Company Setup 401k Tab

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Pseudo code Tutorial and Exercises Teacher s Version

Curriculum Map. Discipline: Computer Science Course: C++

Binary Search Trees CMPSC 122

Glossary of Object Oriented Terms

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

Heaps & Priority Queues in the C++ STL 2-3 Trees

Data Structure with C

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

Analysis of Algorithms I: Binary Search Trees

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Chapter 13: Query Processing. Basic Steps in Query Processing

From Last Time: Remove (Delete) Operation

Recursive Implementation of Recursive Data Structures

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

Transcription:

Unordered Linked Lists Derive class unorderedlinkedlist from the abstract class linkedlisttype Implement the operations search, insertfirst, insertlast, deletenode See code on page 292 Defines an unordered linked list as an ADT class unorderedlinkedlist Data Structures Using C++ 2E 1

Unordered Linked Lists (cont d.) Search the list Steps Step one: Compare the search item with the current node in the list. If the info of the current node is the same as the search item, stop the search; otherwise, make the next node the current node Step two: Repeat Step one until either the item is found or no more data is left in the list to compare with the search item See function search on page 293 Data Structures Using C++ 2E 2

Unordered Linked Lists (cont d.) Insert the first node Steps Create a new node If unable to create the node, terminate the program Store the new item in the new node Insert the node before first Increment count by one See function insertfirst on page 294 Data Structures Using C++ 2E 3

Unordered Linked Lists (cont d.) Insert the last node Similar to definition of member function insertfirst Insert new node after last See function insertlast on page 294 Data Structures Using C++ 2E 4

Unordered Linked Lists (cont d.) Delete a node Consider the following cases: The list is empty The node is nonempty and the node to be deleted is the first node The node is nonempty and the node to be deleted is not the first node, it is somewhere in the list The node to be deleted is not in the list See pseudocode on page 295 See definition of function deletenode on page 297 Data Structures Using C++ 2E 5

Unordered Linked Lists (cont d.) TABLE 5-7 Time-complexity of the operations of the class unorderedlinkedlist Data Structures Using C++ 2E 6

Header File of the Unordered Linked List Create header file defining class unorderedlisttype See class unorderedlisttype code on page 299 Specifies members to implement basic properties of an unordered linked list Derived from class linkedlisttype Data Structures Using C++ 2E 7

Ordered Linked Lists Derive class orderedlinkedlist from class linkedlisttype Provide definitions of the abstract functions: insertfirst, insertlast, search, deletenode Ordered linked list elements are arranged using some ordering criteria Assume elements of an ordered linked list arranged in ascending order See class orderedlinkedlist on pages 300-301 Data Structures Using C++ 2E 8

Ordered Linked Lists (cont d.) Search the list Steps describing algorithm Step one: Compare the search item with the current node in the list. If the info of the current node is greater than or equal to the search item, stop the search; otherwise, make the next node the current node Step two: Repeat Step one until either an item in the list that is greater than or equal to the search item is found, or no more data is left in the list to compare with the search item Data Structures Using C++ 2E 9

Ordered Linked Lists (cont d.) Insert a node Find place where new item goes Insert item in the list See code on page 304 Definition of the function insert Data Structures Using C++ 2E 10

Ordered Linked Lists (cont d.) Insert a node (cont d.) Consider the following cases Data Structures Using C++ 2E 11

Ordered Linked Lists (cont d.) Insert first and insert last Function insertfirst Inserts new item at beginning of the list Must be inserted at the proper place Function insertlast Inserts new item at the proper place Data Structures Using C++ 2E 12

Ordered Linked Lists (cont d.) Delete a node Several cases to consider See function deletenode code on page 306 Data Structures Using C++ 2E 13

Ordered Linked Lists (cont d.) TABLE 5-8 Time-complexity of the operations of the class orderedlinkedlist Data Structures Using C++ 2E 14

Header File of the Ordered Linked List See code on page 308 Specifies members to implement the basic properties of an ordered linked list Derived from class linkedlisttype See test program on page 309 Tests various operations on an ordered linked list Data Structures Using C++ 2E 15

Doubly Linked Lists Traversed in either direction Typical operations Initialize the list Destroy the list Determine if list empty Search list for a given item Insert an item Delete an item, and so on See code on page 311 Class specifying members to implement properties of an ordered doubly linked list Data Structures Using C++ 2E 16

Doubly Linked Lists (cont d.) Linked list in which every node has a next pointer and a back pointer Every node contains address of next node Except last node Every node contains address of previous node Except the first node FIGURE 5-27 Doubly linked list Data Structures Using C++ 2E 17

Doubly Linked Lists (cont d.) Default constructor Initializes the doubly linked list to an empty state isemptylist Returns true if the list empty Otherwise returns false List empty if pointer first is NULL Data Structures Using C++ 2E 18

Doubly Linked Lists (cont d.) Destroy the list Deletes all nodes in the list Leaves list in an empty state Traverse list starting at the first node; delete each node count set to zero Initialize the list Reinitializes doubly linked list to an empty state Can be done using the operation destroy Length of the list Length of a linked list stored in variable count Returns value of this variable Data Structures Using C++ 2E 19

Doubly Linked Lists (cont d.) Print the list Outputs info contained in each node Traverse list starting from the first node Reverse print the list Outputs info contained in each node in reverse order Traverse list starting from the last node Search the list Function search returns true if searchitem found Otherwise, it returns false Same as ordered linked list search algorithm Data Structures Using C++ 2E 20

Doubly Linked Lists (cont d.) First and last elements Function front returns first list element Function back returns last list element If list empty Functions terminate the program Data Structures Using C++ 2E 21

Doubly Linked Lists (cont d.) Insert a node Four cases Case 1: Insertion in an empty list Case 2: Insertion at the beginning of a nonempty list Case 3: Insertion at the end of a nonempty list Case 4: Insertion somewhere in a nonempty list Cases 1 and 2 requirement: Change value of the pointer first Cases 3 and 4: After inserting an item, count incremented by one Data Structures Using C++ 2E 22

Doubly Linked Lists (cont d.) Insert a node (cont d.) Figures 5-28 and 5-29 illustrate case 4 See code on page 317 Definition of the function insert Data Structures Using C++ 2E 23

Doubly Linked Lists (cont d.) Delete a node Four cases Case 1: The list is empty Case 2: The item to be deleted is in the first node of the list, which would require us to change the value of the pointer first Case 3: The item to be deleted is somewhere in the list Case 4: The item to be deleted is not in the list See code on page 319 Definition of function deletenode Data Structures Using C++ 2E 24

STL Sequence Container: list TABLE 5-9 Various ways to declare a list object Data Structures Using C++ 2E 25

TABLE 5-10 Operations specific to a list container Data Structures Using C++ 2E 26

TABLE 5-10 Operations specific to a list container (cont d.) Data Structures Using C++ 2E 27

Linked Lists with Header and Trailer Nodes Simplify insertion and deletion Never insert item before the first or after the last item Never delete the first node Set header node at beginning of the list Containing a value smaller than the smallest value in the data set Set trailer node at end of the list Containing value larger than the largest value in the data set Data Structures Using C++ 2E 28

Linked Lists with Header and Trailer Nodes (cont d.) Header and trailer nodes Serve to simplify insertion and deletion algorithms Not part of the actual list Actual list located between these two nodes Data Structures Using C++ 2E 29

Circular Linked Lists Last node points to the first node Basic operations Initialize list (to an empty state), determine if list is empty, destroy list, print list, find the list length, search for a given item, insert item, delete item, copy the list FIGURE 5-34 Circular linked lists Data Structures Using C++ 2E 30

Summary Linked list topics Traversal, searching, inserting, deleting Building a linked list Forward, backward Linked list as an ADT Ordered linked lists Doubly linked lists STL sequence container list Linked lists with header and trailer nodes Circular linked lists Data Structures Using C++ 2E 31