• No results found

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
40
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures Using C++ 2E 

Chapter 5

Linked Lists

(2)

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

(3)

 Data Structures Using C++ 2E 3

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

(4)

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

(5)

 Data Structures Using C++ 2E 5

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

(6)

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 

(7)

 Data Structures Using C++ 2E 7

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

(8)

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 

(9)

 Data Structures Using C++ 2E 9

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

(10)

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

(11)

 Data Structures Using C++ 2E 11

STL Sequence Container: list

TABLE 5­9 Various ways to declare a list object

(12)

TABLE 5­10 Operations specific to a list container

(13)

 Data Structures Using C++ 2E 13

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

(14)

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 

(15)

 Data Structures Using C++ 2E 15

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

(16)

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

(17)

 Data Structures Using C++ 2E 17

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

(18)

Data Structures Using C++ 2E 

Chapter 7

(19)

 Data Structures Using C++ 2E 19

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

(20)

Stacks

• Data structure

– Elements added, removed from one end only – Last In First Out (LIFO)

(21)

 Data Structures Using C++ 2E 21

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

(22)

Stacks (cont’d.)

FIGURE 7­2 Empty stack

(23)

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

 Data Structures Using C++ 2E 23

(24)

Stacks (cont’d.)

• Review code on page 398

– Illustrates class specifying basic stack operations

(25)

 Data Structures Using C++ 2E 25

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

(26)

Implementation of Stacks as Arrays  (cont’d.)

• Review code on page 400

– Illustrates basic operations on a stack as an array 

(27)

 Data Structures Using C++ 2E 27

Implementation of Stacks as Arrays  (cont’d.)

FIGURE 7­6 Example of a stack

(28)

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

(29)

 Data Structures Using C++ 2E 29

Empty Stack

• Value of stackTop indicates if stack empty

– If stackTop = zero: stack empty – Otherwise: stack not empty

• Definition of function isEmptyStack

(30)

Full Stack

• Stack full

– If stackTop is equal to maxStackSize

• Definition of function isFullStack

(31)

 Data Structures Using C++ 2E 31

Push

• Two­step process

– Store newItem in array component indicated by  stackTop

– Increment stackTop

FIGURE 7­8 Stack before and after the push operation

(32)

Push (cont’d.)

• Definition of push operation

(33)

 Data Structures Using C++ 2E 33

Return the Top Element

• Definition of top operation

(34)

Pop

• Remove (pop) element from stack

– Decrement stackTop by one

(35)

 Data Structures Using C++ 2E 35

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

(36)

Copy Stack

• Definition of function copyStack

(37)

 Data Structures Using C++ 2E 37

Constructor and Destructor

(38)

Copy Constructor

• Definition of the copy constructor

(39)

 Data Structures Using C++ 2E 39

Overloading the Assignment Operator  (=)

• Classes with pointer member variables

– Assignment operator must be explicitly overloaded

• Function definition to overload assignment operator 

for class stackType

(40)

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

References

Related documents

By payment of separate rates and user charges, users collectively meet the current and amortised costs of water supply, wastewater and solid waste disposal provided by Westland

In 2006, my colleague Cheryl Rau and I completed a study (Ritchie & Rau, 2006) which involved early childhood educators, teacher educators, specialist educators from an

The concept of tribal sovereign status and the integration of knowledge, concepts, and theories around its integration into the social work curriculum are recent adaptations to social

As the analysis will show, the answer turns out to depend on the specific measure of absorptive capacity we look at, and which particular Arab country we are evaluating, but

· National Pension Fund Association, Overview of Japanese Individual Defined Contribution Plan (http://www.npfa.or.jp/401K/status/). · Ministry of Economy, Trade and Industry,

(yo maa dadaati sa ideva tavat ahamanna manna madanta madmi) The Sun is called Prachanda Chandika, another name for Chinna Masta. The Goddess cuts off her head

The current research describes the adaptation of an informant-rating instrument (the Extreme Demand Avoid- ance Questionnaire; O’Nions et al. 2014b ; EDA-Q) for use as a

Southwest Chicken Salad 7.00 – Spring mix lettuce with crispy chicken strips, diced tomato, roasted corn, black beans and a chipotle avocado ranch dressing. Bistro Roast Beef