data structure: list as abstract data type

Post on 22-Jan-2018

501 Views

Category:

Engineering

8 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Abstract Data Types: Lists

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

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.

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

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

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

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.

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.

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

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

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.

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.

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

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

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 info@learnbywatch.com to know more about this course or get discount.

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

15

top related