Docsity

Data Structures: Lists, Stacks, and Queues, Slides of Data Structures and Algorithms

Partial preview of the text

Download Data Structures: Lists, Stacks, and Queues and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 3

Lists, Stacks, and Queues

EE 232 Data Structures Session-18 , Spring- 2020

Chapter 3: Lists, Stacks, and Queues

Our goals: we will learn ❑ The concept of Abstract Data Types (ADTS) ❑ How to efficiently perform operations on lists ❑ The Stack ADT and its use in implementing recursion ❑ The Queue ADT and its use in operating systems and algorithm design

Chapter 3: Lists, Stacks, and Queues

3.1 Abstract Data Types (ADT)

3.2 The List ADT

3.3 The Stack ADT

3.4 The Queue ADT

Abstract Data Types (ADT)…

Abstract Data Type ❖ a set of objects and ❖ the set of operations that can be performed on the objects ❑ Abstract data types are mathematical abstractions; nowhere in an ADT’s definition is there any mention of how the set of operations is implemented.

Abstract Data Types (ADT)…

❑ Objects such as lists, sets, and graphs, along with their operations, can be viewed as ADTs. ❑ For the set ADT, we might have such operations as add, remove, size, and contains. ❑ Alternately, we might only want the two operations union and find, which would define a different ADT on the set. ❑ There is no rule telling us which operations must be supported for each ADT; this is a design decision.

Chapter 3: Lists, Stacks, and Queues

3.1 Abstract Data Types (ADT)

3.2 The List ADT

3.3 The Stack ADT

3.4 The Queue ADT

The List ADT

Objects ❖ A general list is of the form A 0

, A

1

,…, A

N- 1

  • where the size of the list is N
  • and a list of size 0 is called an empty list
  • For any list except the empty list, we say that A i follows or succeeds A i- 1 for i < N and that A i- 1 precedes A i for i > 0.
  • We will not define the predecessor of A 0 or the successor of AN- 1.
  • The position of element Ai in a list is i.

The List ADT…

Set of Operations (Some popular operations): ❖ PrintList : prints the list ❖ MakeEmpty : create an empty list ❖ Find : returns the position of the first occurrence of an item. ❖ list: 34,12, 52, 16, 12. Find (52) → 2 ❖ Insert : insert an object into a given position

  • Insert (X, 2) → 34, 12, X, 52,16, 12 ❖ Delete : delete an element from some position Delete (52) → 34, 12, X, 16, 12 ❖ FindKth : returns the element in some position.

The List ADT…

❑ The interpretation of what is appropriate for a function is entirely up to the programmer, as is the handling of special cases (for example, what does find(1) return above?). ❑ We could also add operations such as next and previous, which would take a position as argument and return the position of the successor and predecessor, respectively.

The List ADT…

Running time of the operations for an array implementation of Lists ❖ The running time for PrintList and Find?

  • O ( N ) ❖ The running time for FindKth?
  • O (1) (as good as can be expected) ❖ The running time for Insert?
  • O ( N )

The List ADT…

  • In the worst case, inserting into position 0 (in other words, at the front of the list) requires pushing the entire array down one spot to make room. ❖ The running time for Delete?
  • O ( N ) ❑ On average, half of the list needs to be moved for either operation, so linear time is still required. ❑ On the other hand, if all the operations occur at the high end of the list, then no elements need to be shifted, and then adding and deleting take O(1) time. Deleting the first element requires shifting all elements in the list up one spot.

The List ADT…

❑ There are many situations where the list is built up by insertions at the high end, and then only array accesses (i.e., findKth operations) occur. ❖ In such a case, the array is a suitable implementation. ❑ If insertions and deletions occur throughout the list and, in particular, at the front of the list, then the array is not a good option. ❖ The alternative is the linked list.

The List ADT…

Simple Linked Lists ❖ consists of a series of nodes or structures, which are not necessarily adjacent in memory ❖ Each structure or node contains the element and a link or pointer (called Next ) to a structure containing its successor. The last cell’s next link points to nullptr. A 0 A N- 1

4 - 3 …….. - 1 NULL

A node in the list Conceptual view of a linked list