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



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

Unordered Linked Lists

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

Converting a Number from Decimal to Binary

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

Node-Based Structures Linked Lists: Implementation

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

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

DATA STRUCTURES USING C

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

LINKED DATA STRUCTURES

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

ADTs,, Arrays, Linked Lists

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

Lecture 12 Doubly Linked Lists (with Recursion)

Ordered Lists and Binary Trees

Analysis of a Search Algorithm

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

10CS35: Data Structures Using C

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

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

Sequences in the C++ STL

PES Institute of Technology-BSC QUESTION BANK

16 Collection Classes

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

Linked Lists Linked Lists, Queues, and Stacks

Glossary of Object Oriented Terms

Data Structures and Algorithms

BCS2B02: OOP Concepts and Data Structures Using C++

STACKS,QUEUES, AND LINKED LISTS

Data Structures and Algorithms

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

- 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 Heap Algorithms

Sequential Data Structures

C++ INTERVIEW QUESTIONS

Linked Lists: Implementation Sequences in the C++ STL

Stacks. Linear data structures

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.

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

Object Oriented Software Design II

El Dorado Union High School District Educational Services

D06 PROGRAMMING with JAVA

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:

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

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

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

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

Data Structures Fibonacci Heaps, Amortized Analysis

WORKSPACE WEB DEVELOPMENT & OUTSOURCING TRAINING CENTER

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

Data Structure with C

Common Data Structures

Chapter 13: Query Processing. Basic Steps in Query Processing

Abstract Data Types. Chapter 2

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

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

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

Chapter 3: Restricted Structures Page 1

Symbol Tables. Introduction

The C Programming Language course syllabus associate level

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

C++ Programming Language

C++FA 5.1 PRACTICE MID-TERM EXAM

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

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

Data Structures and Data Manipulation

Binary storage of graphs and related data

Record Storage and Primary File Organization

Pointers and Linked Lists

Financial Management System

Cpt S 223. School of EECS, WSU

An Incomplete C++ Primer. University of Wyoming MA 5310

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

: provid.ir

Illustration 1: Diagram of program function and data flow

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

DATA STRUCTURE - STACK

MA-WA1920: Enterprise iphone and ipad Programming

Linked List Problems

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

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Data Structures Using C++

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

CSE 211: Data Structures Lecture Notes VII

Chapter 8: Bags and Sets

An Internet Course in Software Development with C++ for Engineering Students

Facebook Twitter YouTube Google Plus Website

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

TREE BASIC TERMINOLOGIES

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

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

6 March Array Implementation of Binary Trees

CEC225 COURSE COMPACT

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Project 4 DB A Simple database program

Transcription:

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

Test #1 Next Thursday During Class Cover through (near?) end of Chapter 5

Objectives Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Data Structures Using C++ 2E 3

Objectives (cont d.) Learn how to construct a doubly linked list Discover how to use the STL container list Learn about linked lists with header and trailer nodes Become aware of circular linked lists Data Structures Using C++ 2E 4

Linked Lists Collection of components (nodes) Every node (except last) Contains address of the next node Node components Data: stores relevant information Link: stores address FIGURE 5-1 Structure of a node Data Structures Using C++ 2E 5

Linked Lists (cont d.) Head (first) Address of the first node in the list Arrow points to node address Stored in node Down arrow in last node indicates NULL link field FIGURE 5-2 Linked list FIGURE 5-3 Linked list and values of the links Data Structures Using C++ 2E 6

Linked Lists (cont d.) Two node components Declared as a class or struct Data type depends on specific application Link component: pointer Data type of pointer variable: node type itself Data Structures Using C++ 2E 7

Linked Lists: Some Properties Head stores address of first node Info stores information Link stores address of next node Assume info type int FIGURE 5-4 Linked list with four nodes TABLE 5-1 Values of head and some of the nodes of the linked list in Figure 5-4 Data Structures Using C++ 2E 8

Linked Lists: Some Properties (cont d.) Pointer current: same type as pointer head current = head; Copies value of head into current current = current->link; Copies value of current->link (2800) into current FIGURE 5-5 List after the statement current = current->link; executes Data Structures Using C++ 2E 9

Linked Lists: Some Properties (cont d.) TABLE 5-2 Values of current, head, and some of the nodes of the linked list in Figure 5-5 Data Structures Using C++ 2E 10

Traversing a Linked List Basic linked list operations Search list to determine if particular item is in the list Insert item in list Delete item from list These operations require list traversal Given pointer to list first node, we must step through list nodes Data Structures Using C++ 2E 11

Traversing a Linked List (cont d.) Suppose head points to a linked list of numbers Code outputting data stored in each node Data Structures Using C++ 2E 12

Item Insertion and Deletion Generic definition of a node on page 270 TABLE 5-3 Inserting a node in a linked list Data Structures Using C++ 2E 13

Item Insertion and Deletion (cont d.) Sequence of statements to insert node Very important Use only one pointer (p) to adjust links of the nodes Using two pointers Can simplify insertion code somewhat Data Structures Using C++ 2E 14

Item Insertion and Deletion (cont d.) Memory still occupied by node after deletion Memory is inaccessible Deallocate memory using a pointer to this node FIGURE 5-10 List after the statement p->link = p->link->link; executes Data Structures Using C++ 2E 15

Building a Linked List If linked list data unsorted Linked list unsorted Ways to build linked list Forward New node always inserted at end of the linked list See example on page 274 See function buildlistforward on page 277 Backward New node always inserted at the beginning of the list See example on page 277 See function buildlistbackward on page 278 Data Structures Using C++ 2E 16

Linked List as an ADT 11 basic operations Two types of linked lists: sorted, unsorted class linkedlisttype Implements basic linked list operations as an ADT Derive two classes using inheritance unorderedlinkedlist and orderedlinkedlist Unordered linked list functions buildlistforward and buildlistbackward Two more functions accommodate both operations insertfirst and insertlast Data Structures Using C++ 2E 17

Structure of Linked List Nodes Node has two instance variables Simplify operations (insert, delete) Define class to implement linked list node as a struct Definition of the struct nodetype Data Structures Using C++ 2E 18

Member Variables of the class linkedlisttype class linkedlisttype Three instance variables Data Structures Using C++ 2E 19

Linked List Iterators To process each node Must traverse list starting at first node Iterator Object producing each element of a container One element at a time Operations on iterators: ++ and * See code on pages 280-281 class linkedlisttype Functions of class linkedlistiterator Data Structures Using C++ 2E 20

Linked List Iterators (cont d.) Abstract class linkedlisttype Defines basic properties of a linked list as an ADT See code on page 282 Empty list: first is NULL Definition of function isemptylist Data Structures Using C++ 2E 21

Linked List as an ADT (cont d.) Default constructor Initializes list to an empty state Destroy the list Deallocates memory occupied by each node Initialize the list Reinitializes list to an empty state Must delete the nodes (if any) from the list Default constructor, copy constructor Initialized list when list object declared Data Structures Using C++ 2E 22

Linked List as an ADT (cont d.) Print the list Must traverse the list starting at first node Length of a list Number of nodes stored in the variable count Function length Returns value of variable count Retrieve the data of the first node Function front Returns the info contained in the first node If list is empty, assert statement terminates program Data Structures Using C++ 2E 23

Linked List as an ADT (cont d.) Retrieve the data of the last node Function back Returns info contained in the last node If list is empty, assert statement terminates program Begin and end Function begin returns an iterator to the first node in the linked list Function end returns an iterator to the last node in the linked list Data Structures Using C++ 2E 24

Linked List as an ADT (cont d.) Copy the list Makes an identical copy of a linked list Create node called newnode Copy node info (original list) into newnode Insert newnode at the end of list being created See function copylist on page 289 Data Structures Using C++ 2E 25

Linked List as an ADT (cont d.) Destructor When class object goes out of scope Deallocates memory occupied by list nodes Memory allocated dynamically Resetting pointers first and last Does not deallocate memory Must traverse list starting at first node Delete each node in the list Calling destroylist destroys list Data Structures Using C++ 2E 26

Linked List as an ADT (cont d.) Copy constructor Makes identical copy of the linked list Function copylistc checks whether original list empty Checks value of first Must initialize first to NULL Before calling the function copylist Overloading the assignment operator Similar to copy constructor definition Data Structures Using C++ 2E 27

TABLE 5-6 Time-complexity of the operations of the class linkedlisttype Data Structures Using C++ 2E 28