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



Similar documents
Unordered Linked Lists

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

Linked List as an ADT (cont d.)

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

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

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

DATA STRUCTURE - STACK

DATA STRUCTURES USING C

PROGRAMMING CONCEPTS AND EMBEDDED PROGRAMMING IN C, C++ and JAVA: Lesson-4: Data Structures: Stacks

10CS35: Data Structures Using C

Stacks. Linear data structures

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

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

PES Institute of Technology-BSC QUESTION BANK

Converting a Number from Decimal to Binary

Common Data Structures

Analysis of a Search Algorithm

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.

Chapter 3: Restricted Structures Page 1

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

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

Data Structure [Question Bank]

Module 2 Stacks and Queues: Abstract Data Types

Algorithms and Data Structures

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,QUEUES, AND LINKED LISTS

Data Structures and Data Manipulation

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

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

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

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

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!

Lecture 12 Doubly Linked Lists (with Recursion)

Universidad Carlos III de Madrid

Sequential Data Structures

BCS2B02: OOP Concepts and Data Structures Using C++

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

Node-Based Structures Linked Lists: Implementation

Linked Lists Linked Lists, Queues, and Stacks

Data Structures and Algorithms

Stack & Queue. Darshan Institute of Engineering & Technology. Explain Array in detail. Row major matrix No of Columns = m = u2 b2 + 1

7.1 Our Current Model

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

Ordered Lists and Binary Trees

Data Structures Fibonacci Heaps, Amortized Analysis

Data Structures and Algorithms(5)

Basic Data Structures and Algorithms

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

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

Glossary of Object Oriented Terms

Data Structures and Algorithms V Otávio Braga

Introduction to Data Structures and Algorithms

The ADT Binary Search Tree

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

Financial Management System

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

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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:

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

C++ INTERVIEW QUESTIONS

Introduction to Object-Oriented Programming

Data Structures and Algorithms

CSE373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks/Queues. Linda Shapiro Spring 2016

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

Java Software Structures

Data Structures. Level 6 C Module Descriptor

CmpSci 187: Programming with Data Structures Spring 2015

Data Structures and Algorithms Stacks and Queues

- 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

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

Cpt S 223. School of EECS, WSU

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

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

DATA STRUCTURE - QUEUE

Data Structures and Algorithms Lists

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Data Structures and Algorithms C++ Implementation

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

COMPUTER SCIENCE. Paper 1 (THEORY)

Section IV.1: Recursive Algorithms and Recursion Trees

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

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

Home Page. Data Structures. Title Page. Page 1 of 24. Go Back. Full Screen. Close. Quit

Sequences in the C++ STL

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

\Mankinds's progress is measured by the number of. Elementary data structures such as stacks, queues,

Data Structures UNIT III. Model Question Answer

Recursive Implementation of Recursive Data Structures

Data Structures and Algorithms

Efficient Data Structures for Decision Diagrams

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

TREE BASIC TERMINOLOGIES

Object Oriented Programming With C++(10CS36) Question Bank. UNIT 1: Introduction to C++

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

LINKED DATA STRUCTURES

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

Abstract Data Types. Chapter 2

Transcription:

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

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 2

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 3

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 4

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 5

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 6

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 7

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 8

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 9

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 10

STL Sequence Container: list TABLE 5 9 Various ways to declare a list object 11

TABLE 5 10 Operations specific to a list container 12

TABLE 5 10 Operations specific to a list container (cont d.) 13

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 14

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 15

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 16

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 17

Data Structures Using C++ 2E Chapter 7 Stacks

Objectives Learn about stacks Examine various stack operations Learn how to implement a stack as an array Learn how to implement a stack as a linked list Discover stack applications Learn how to use a stack to remove recursion 19

Stacks Data structure Elements added, removed from one end only Last In First Out (LIFO) FIGURE 7 1 Various examples of stacks 20

Stacks (cont d.) push operation Add element onto the stack top operation Retrieve top element of the stack pop operation Remove top element from the stack 21

Stacks (cont d.) FIGURE 7 2 Empty stack FIGURE 7 3 Stack operations 22

Stacks (cont d.) Stack element removal Occurs only if something is in the stack Stack element added only if room available isfullstack operation Checks for full stack isemptystack operation Checks for empty stack initializestack operation Initializes stack to an empty state 23

Stacks (cont d.) Review code on page 398 Illustrates class specifying basic stack operations FIGURE 7 4 UML class diagram of the class stackadt 24

Implementation of Stacks as Arrays First stack element Put in first array slot Second stack element Put in second array slot, and so on Top of stack Index of last element added to stack Stack element accessed only through the top Problem: array is a random access data structure Solution: use another variable (stacktop) Keeps track of the top position of the array 25

Implementation of Stacks as Arrays (cont d.) Review code on page 400 Illustrates basic operations on a stack as an array FIGURE 7 5 UML class diagram of the class stacktype 26

Implementation of Stacks as Arrays (cont d.) FIGURE 7 6 Example of a stack 27

Initialize Stack Value of stacktop if stack empty Set stacktop to zero to initialize the stack Definition of function initializestack FIGURE 7 7 Empty stack 28

Empty Stack Value of stacktop indicates if stack empty If stacktop = zero: stack empty Otherwise: stack not empty Definition of function isemptystack 29

Full Stack Stack full If stacktop is equal to maxstacksize Definition of function isfullstack 30

Push Two step process Store newitem in array component indicated by stacktop Increment stacktop FIGURE 7 8 Stack before and after the push operation 31

Push (cont d.) Definition of push operation 32

Return the Top Element Definition of top operation 33

Pop Remove (pop) element from stack Decrement stacktop by one FIGURE 7 9 Stack before and after the pop operation 34

Pop (cont d.) Definition of pop operation Underflow Removing an item from an empty stack Check within pop operation (see below) Check before calling function pop 35

Copy Stack Definition of function copystack 36

Constructor and Destructor 37

Copy Constructor Definition of the copy constructor 38

Overloading the Assignment Operator (=) Classes with pointer member variables Assignment operator must be explicitly overloaded Function definition to overload assignment operator for class stacktype 39

Stack Header File (cont d.) Stack operations analysis Similar to class arraylisttype operations TABLE 7 1 Time complexity of the operations of the class stacktype on a stack with n elements 40