data structure: list as abstract data type

15
Abstract Data Types: Lists

Upload: learn-by-watch

Post on 22-Jan-2018

497 views

Category:

Engineering


8 download

TRANSCRIPT

Page 1: Data Structure: List as abstract data type

Abstract Data Types: Lists

Page 2: Data Structure: List as abstract data type

List

• A List is a sequence of zero or more elements.

a1, a2, a3, a4, ………..,an

Where n 0,

a1 is the first element

an is the last element

n is length of the list

if n = 0, the list is empty (no element)

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com 2

Page 3: Data Structure: List as abstract data type

More about lists

• Elements are linearly ordered.

ai precedes ai+1

for i = 1, 2, 3,… (n-1)

ai follows ai-1

for i = 2, 3,… n

• Element ai is at position i

a1, a2, a3, a4, ………..,an

Not a list

a1, a3, a5, a7, ………..,an

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

3

1 2 3 4 n Position ofelement.

Page 4: Data Structure: List as abstract data type

More about lists

a1, a2, a3, a4, a5

a1, a2, a3, a4, a5, a6, a7

a1, a2, a3, a4

a1, a2, a3

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

4

Page 5: Data Structure: List as abstract data type

LIST: Abstract Data Type

• L: List of elements

• e: an element

• p: position

LIST ADT

Properties- Sequence of linearly

ordered elements.

Operations• Insert(e, p, L)• Locate(e,L)• Retrieve(p,L)• Delete(p,L)• Next(p,L)• Previous(p,L)• MakeNull(L)• First(L)• PrintList(L)• END(L)

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com 5

Page 6: Data Structure: List as abstract data type

Print all elements of the LIST ADT

• PRINTLIST(L)• Print the elements of L in order of occurrence.

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

6

Page 7: Data Structure: List as abstract data type

End of list

a1, a2, a3, a4 ,…………, an

• END(L)• Returns the position following an on the list.

a1, a2, a3, a4, a5, a6, a7

• END(L) will return 8 for this list

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

7

1 2 3 4 7 Position of element.

Page 8: Data Structure: List as abstract data type

First position on LIST ADT

a1, a2, a3, a4 ,…………, an

• FIRST(L)• Returns the first position on the list.

• If L is empty the position returned is END(L)

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

8

1 2 3 4 n Position of element.

Page 9: Data Structure: List as abstract data type

Next and Previous position on LIST ADT

• NEXT(p, L)• Returns the position following the position p on the list.

• PREVIOUS(p, L)• Returns the position preceding the position p on the list.

• If p is the last position on the list then NEXT(p,L) will return END(L).

• PREVIOUS is undefined if p is 1.

• Both functions are undefined if L has no position p.

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

9

Page 10: Data Structure: List as abstract data type

Insert an element in LIST ADT

a1, a2, a3, a4, a5, a6, a7

• INSERT(e, p, L)

a1, a2, a3, a4 ,……ap ,…… an

a1, a2, a3, a4 ,……,ap-1, e, ap ,…… an

• INSERT(3, 5, a1, a2, a3, a4, a5, a6, a7)

a1, a2, a3, a4, a5, a6, a7

a1, a2, a3, a4, 3, a5, a6, a7

• if p = END(L) then L becomes a1, a2, a3, a4, a5, a6, a7, e

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

10

Page 11: Data Structure: List as abstract data type

Locate an element in LIST ADT

33, 90, 76, 21, 65, 36, 87

• LOCATE(e, L)• Returns the position of e on list L

• LOCATE(76,L)• Return 3

• If e appears more than once, then the position of first occurrence is returned.

• If e does not appear than END(L) is returned.

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

11

1 2 3 4 7 Position of element.

Page 12: Data Structure: List as abstract data type

Retrieve an element from LIST ADT

33, 90, 76, 21, 65, 36, 87

• Retrieve(p, L)• Returns the element at position p from the List.

• Retrieve(3, L) will return 76

• The result is undefined if p = END(L) or if L has no position p

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

12

1 2 3 4 7 Position of element.

Page 13: Data Structure: List as abstract data type

Delete an element from the list

a1, a2, a3, a4, a5, a6, a7

• DELETE(p, L)

a1, a2, a3, a4 ,……ap ,…… an

a1, a2, a3, a4 ,……,ap-1, ap+1 ,…… an

• DELETE(5, L)

a1, a2, a3, a4, a5, a6, a7

a1, a2, a3, a4, a6, a7

• Result is undefined if L has no position p

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

13

Page 14: Data Structure: List as abstract data type

Empty entire LIST ADT

• MAKENULL(L)• L becomes empty and returns position END(L)

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

14

Page 15: Data Structure: List as abstract data type

Buy this course

• You can buy this course from www.learnbywatch.com/course/data-structure-using-c-online-course

https://goo.gl/I1ZHK3

• You can also send an e-mail at [email protected] to know more about this course or get discount.

Data Structure using C | Dr. Yogendra Pal | www.LearnByWatch.com

15