Presentation is loading. Please wait.

Presentation is loading. Please wait.

UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.

Similar presentations


Presentation on theme: "UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C."— Presentation transcript:

1 UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C

2 ADT = properties + operations An ADT describes a set of objects sharing the same properties and behaviors – The properties of an ADT are its data (representing the internal state of each object double d; -- bits representing exponent & mantissa are its data or state – The behaviors of an ADT are its operations or functions (operations on each instance) sqrt(d) / 2; //operators & functions are its behaviors Thus, an ADT couples its data and operations – OOP emphasizes data abstraction EC6301-II-ECE-C

3 Abstract Data Types Abstract data type A data type whose properties (data and operations) are specified independently of any particular implementation The goal in design is to reduce complexity through abstraction EC6301-II-ECE-C

4 The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i If N=0, then empty list Linearly ordered(non empty list) – A i-1 precedes A i – A i follows A i-1 EC6301-II-ECE-C

5 Operations printList: print the list makeEmpty: create an empty list find: locate the position of an object in a list – list: 34,12, 52, 16, 12 – find(52)  2 insert: insert an object to a list – insert(x,3)  34, 12, 52, x, 16, 12 remove: delete an element from the list – remove(52)  34, 12, x, 16, 12 findKth: retrieve the element at a certain position EC6301-II-ECE-C

6 Array-Based Implementations Recall that – an array is a named collection of homogeneous items – An item’s place within the collection is called an index If there is no ordering on the items in the container, we call the container unsorted If there is an ordering, we call the container sorted EC6301-II-ECE-C

7 Array Implementation... Requires an estimate of the maximum size of the list  waste space printList and find: linear time findKth: constant insert and delete: slow and expensive WORST CASE – e.g. insert at position 0 (making a new element) requires first pushing the entire array down one spot to make room – e.g. delete at position 0 requires shifting all the elements in the list up one If all the operations occur at the high end of the list, then no elements need to be shifted. EC6301-II-ECE-C

8 Array-Based Implementations Figure 9.1 A list EC6301-II-ECE-C

9 Array-Based Implementations Figure 9.3 A sorted list of integers EC6301-II-ECE-C

10 Linked Implementation Linked implementation An implementation based on the concept of a node A node is made up of two pieces of information – the item that the user wants in the list, and – a pointer to the next node in the list EC6301-II-ECE-C

11 Pointer Implementation (Linked List) Ensure that the list is not stored contiguously – use a linked list – a series of structures that are not necessarily adjacent in memory  Each node contains the element and a pointer to a structure containing its successor  the last cell’s next link points to NULL  Compared to the array implementation, the pointer implementation uses only as much space as is needed for the elements currently on the list ûbut requires space for the pointers in each cell EC6301-II-ECE-C

12 Linked Lists A linked list is a series of connected nodes Each node contains at least – A piece of data (any type) – Pointer to the next node in the list Head: pointer to the first node The last node points to NULL A  Head BCA datapointer node EC6301-II-ECE-C

13 A Simple Linked List Class We use two classes: Node and List Declare Node class for the nodes – data : double -type data in this example – next : a pointer to the next node in the list class Node { public: double data;// data Node*next;// pointer to next }; EC6301-II-ECE-C

14 Linked Implementation Figure 9.4 Anatomy of a linked list EC6301-II-ECE-C

15 Linked Implementation Figure 9.7 Store a node with info of 67 after current EC6301-II-ECE-C

16 Linked Implementation Figure 9.8 Remove node next(current) EC6301-II-ECE-C


Download ppt "UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C."

Similar presentations


Ads by Google