Docsity

Lists, Stacks, and Queues-Data Structures-Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

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

7/7/2012 Lists, Stacks, and Queues 3-

Chapter 3

Lists, Stacks, and Queues

EE 232 Data Structures Session-05 , Spring-

7/7/2012 Lists, Stacks, and Queues 3-

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

7/7/2012 Lists, Stacks, and Queues 3-

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

7/7/2012 Lists, Stacks, and Queues 3-

Abstract Data Types (ADT)

Data type  a set of objects + a set of operations

Example  integer

  • set of whole numbers {…, -2, -1, 0, 1, 2, 3,…}
  • operations: +, -, x, /

7/7/2012 Lists, Stacks, and Queues 3-

Abstract Data Types (ADT)…

Why do we call it abstract?

 In an ADT’s definition, it is not mentioned how to implement the set of operations

 ADTs are not directly usable

 They have to be implemented

 A given ADT may have more than one implementation

7/7/2012 Lists, Stacks, and Queues 3-

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

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Operations

PrintList : prints the list  MakeEmpty : create an empty list  Find : returns the position of an object in a list

  • list: 34,12, 52, 16, 12
  • Find (52)  3  Insert : insert an object to the list
  • Insert (X,3)  34, 12, 52, X, 16, 12  Delete : delete an element from the list
  • Delete (52)  34, 12, X, 16, 12  FindKth : retrieve the element at a certain position

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

 The List ADT can be implemented using an array or a linked list

Simple array implementation  Elements are stored in contiguously

 an estimate of the maximum size of the list is required and this usually requires a high overestimate, which wastes considerable space

4 -3 5 -2 -1 2 6 -2 1 - A 1 A N

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Linked Lists  consists of a series of structures, which are not necessarily adjacent in memory  Each structure or node contains the element and a pointer (called Next ) to a structure containing its successor

A 1 A N

4 -3 (^) …….. -1 NULL

A node in the list

Pointer is a variable that contains the address where some other data are stored

Conceptual view

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Running time of the operations

PrintList (L)  Find (L, key)  FindKth (L,i) takes O ( i )

A 1 800 A 2 712 A 3 992 A 4 692 A 5 0 1000 800 712 992 692 Actual view

O ( N )

O ( N )

O ( N )

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Deletion from a Linked List  Requires one pointer change

 Running time O (1)

NULL A 1 A 4

4 -3 5 -

A 3 delete

A 2

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Array versus Linked Lists

 Linked lists are more complex to code and manage than arrays, but they have some distinct advantages  Dynamic

  • a linked list unlike an array easily grows and shrinks in size when necessary  Easy and fast insertions and deletions

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

#ifndef _List_H #define _List_H struct Node; typedef struct Node* PtrToNode; typedef PtrToNode List; typedef PtrToNode Position; List MakeEmpty( List L ); int IsEmpty( List L ); int IsLast( Position P, List L ); Position Find( ElementType X, List L ); void Delete( ElementType X, List L ); Position FindPrevious( ElementType X, List L ); void Insert( ElementType X, List L, Position P ); void DeleteList( List L ); Position Header( List L ); Position First( List L ); Position Advance( Position P ); ElementType Retrieve( Position P );

#endif /* _List_H */

struct Node { ElementType Element; Position Next; };

Programming Details…

7/7/2012 Lists, Stacks, and Queues 3-

The List ADT…

Programming Details…

/* Return true if L is empty */

int IsEmpty( List L ) { return L->Next == NULL; }

Running time?