4
An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation An Abstract Data Type (ADT) is a data type together with the operations, whose properties are specified independently of any particular implementation ADT are set of definitions of operations (like the interfaces in C#) ADT are set of definitions of operations (like the interfaces in C#) Can have several different implementations Can have several different implementations Different implementations can have different efficiency Different implementations can have different efficiency

7
Data structure (container) that contains a sequence of elements Data structure (container) that contains a sequence of elements Can have variable size Can have variable size Elements are arranged linearly, in sequence Elements are arranged linearly, in sequence Can be implemented in several ways Can be implemented in several ways Statically (using array fixed size) Statically (using array fixed size) Dynamically (linked implementation) Dynamically (linked implementation) Using resizable array (the List class) Using resizable array (the List class)

12
Implements the abstract data structure list using an array Implements the abstract data structure list using an array All elements are of the same type T All elements are of the same type T T can be any type, e.g. List, List, List T can be any type, e.g. List, List, List Size is dynamically increased as needed Size is dynamically increased as needed Basic functionality: Basic functionality: Count – returns the number of elements Count – returns the number of elements Add(T) – appends given element at the end Add(T) – appends given element at the end

15
list[index] – access element by index list[index] – access element by index Insert(index, T) – inserts given element to the list at a specified position Insert(index, T) – inserts given element to the list at a specified position Remove(T) – removes the first occurrence of given element Remove(T) – removes the first occurrence of given element RemoveAt(index) – removes the element at the specified position RemoveAt(index) – removes the element at the specified position Clear() – removes all elements Clear() – removes all elements Contains(T) – determines whether an element is part of the list Contains(T) – determines whether an element is part of the list

16
IndexOf() – returns the index of the first occurrence of a value in the list (zero-based) IndexOf() – returns the index of the first occurrence of a value in the list (zero-based) Reverse() – reverses the order of the elements in the list or a portion of it Reverse() – reverses the order of the elements in the list or a portion of it Sort() – sorts the elements in the list or a portion of it Sort() – sorts the elements in the list or a portion of it ToArray() – converts the elements of the list to an array ToArray() – converts the elements of the list to an array TrimExcess() – sets the capacity to the actual number of elements TrimExcess() – sets the capacity to the actual number of elements

22
LIFO (Last In First Out) structure LIFO (Last In First Out) structure Elements inserted (push) at top Elements inserted (push) at top Elements removed (pop) from top Elements removed (pop) from top Useful in many situations Useful in many situations E.g. the execution stack of the program E.g. the execution stack of the program Can be implemented in several ways Can be implemented in several ways Statically (using array) Statically (using array) Dynamically (linked implementation) Dynamically (linked implementation) Using the Stack class Using the Stack class

23
Static (array-based) implementation Static (array-based) implementation Has limited (fixed) capacity Has limited (fixed) capacity The current index ( top ) moves left / right with each pop / push The current index ( top ) moves left / right with each pop / push S218712 0 1 2 3 4 5 6 7 top

24
Dynamic (pointer-based) implementation Dynamic (pointer-based) implementation Each item has 2 fields: value and next Each item has 2 fields: value and next Special pointer keeps the top element Special pointer keeps the top element2next7next top4next5next null

26
Implements the stack data structure using an array Implements the stack data structure using an array Elements are from the same type T Elements are from the same type T T can be any type, e.g. Stack T can be any type, e.g. Stack Size is dynamically increased as needed Size is dynamically increased as needed Basic functionality: Basic functionality: Push(T) – inserts elements to the stack Push(T) – inserts elements to the stack Pop() – removes and returns the top element from the stack Pop() – removes and returns the top element from the stack

27
Basic functionality: Basic functionality: Peek() – returns the top element of the stack without removing it Peek() – returns the top element of the stack without removing it Count – returns the number of elements Count – returns the number of elements Clear() – removes all elements Clear() – removes all elements Contains(T) – determines whether given element is in the stack Contains(T) – determines whether given element is in the stack ToArray() – converts the stack to an array ToArray() – converts the stack to an array TrimExcess() – sets the capacity to the actual number of elements TrimExcess() – sets the capacity to the actual number of elements

34
FIFO (First In First Out) structure FIFO (First In First Out) structure Elements inserted at the tail (Enqueue) Elements inserted at the tail (Enqueue) Elements removed from the head (Dequeue) Elements removed from the head (Dequeue) Useful in many situations Useful in many situations Print queues, message queues, etc. Print queues, message queues, etc. Can be implemented in several ways Can be implemented in several ways Statically (using array) Statically (using array) Dynamically (using pointers) Dynamically (using pointers) Using the Queue class Using the Queue class

35
Static (array-based) implementation Static (array-based) implementation Has limited (fixed) capacity Has limited (fixed) capacity Implement as a circular array Implement as a circular array Has head and tail indices, pointing to the head and the tail of the cyclic queue Has head and tail indices, pointing to the head and the tail of the cyclic queue S71225 0 1 2 3 4 5 6 7 headtail

38
Implements the queue data structure using a circular resizable array Implements the queue data structure using a circular resizable array Elements are from the same type T Elements are from the same type T T can be any type, e.g. Stack T can be any type, e.g. Stack Size is dynamically increased as needed Size is dynamically increased as needed Basic functionality: Basic functionality: Enqueue(T) – adds an element to the end of the queue Enqueue(T) – adds an element to the end of the queue Dequeue() – removes and returns the element at the beginning of the queue Dequeue() – removes and returns the element at the beginning of the queue

39
Basic functionality: Basic functionality: Peek() – returns the element at the beginning of the queue without removing it Peek() – returns the element at the beginning of the queue without removing it Count – returns the number of elements Count – returns the number of elements Clear() – removes all elements Clear() – removes all elements Contains(T) – determines whether given element is in the queue Contains(T) – determines whether given element is in the queue ToArray() – converts the queue to an array ToArray() – converts the queue to an array TrimExcess() – sets the capacity to the actual number of elements in the queue TrimExcess() – sets the capacity to the actual number of elements in the queue

46
What is a Priority Queue What is a Priority Queue Data type to efficiently support finding the item with the highest priority Data type to efficiently support finding the item with the highest priority Basic operations Basic operations Enqueue(T element) Enqueue(T element) Dequeue Dequeue There is no build-in Priority Queue in.NET There is no build-in Priority Queue in.NET Can be easily implemented using PowerCollections Can be easily implemented using PowerCollections

48
The generic type is needed to implement IComparable The generic type is needed to implement IComparable It is not necessary to use OrderedBag It is not necessary to use OrderedBag Other Data Structures also can be used Other Data Structures also can be used Adding and Removing Element in the Priority Queue is with complexity logN Adding and Removing Element in the Priority Queue is with complexity logN Keeps the elements Sorted Keeps the elements Sorted Always returns the best element that fulfills some condition Always returns the best element that fulfills some condition E.g. the smallest or the biggest element E.g. the smallest or the biggest element 48

52
1. Write a program that reads from the console a sequence of positive integer numbers. The sequence ends when empty line is entered. Calculate and print the sum and average of the elements of the sequence. Keep the sequence in List. 2. Write a program that reads N integers from the console and reverses them using a stack. Use the Stack class. 3. Write a program that reads a sequence of integers ( List ) ending with an empty line and sorts them in an increasing order.

53
4. Write a method that finds the longest subsequence of equal numbers in given List and returns the result as new List. Write a program to test whether the method works correctly. 5. Write a program that removes from given sequence all negative numbers. 6. Write a program that removes from given sequence all numbers that occur odd number of times. Example: {4, 2, 2, 5, 2, 3, 2, 3, 1, 5, 2} {5, 3, 3, 5}

54
7. Write a program that finds in given array of integers (all belonging to the range [0..1000]) how many times each of them occurs. Example: array = {3, 4, 4, 2, 3, 3, 4, 3, 2} 2 2 times 3 4 times 4 3 times 8. * The majorant of an array of size N is a value that occurs in it at least N/2 + 1 times. Write a program to find the majorant of given array (if exists). Example: {2, 2, 3, 3, 2, 3, 4, 3, 3} 3

57
11. Write a class Student, that has three fields: name (String), age (Integer) and paidSemesterOnline (Boolean). When in a queue the students who paid online are with higher priority than those who are about to pay the semester. Write a program which with a given queue of student determine whose turn it is. Hint: use priority queue 57

58
12. Implement the data structure linked list. Define a class ListItem that has two fields: value (of type T ) and nextItem (of type ListItem ). Define additionally a class LinkedList with a single field firstElement (of type ListItem ). 13. Implement the ADT stack as auto-resizable array. Resize the capacity on demand (when no space is available to add / insert a new element). 14. Implement the ADT queue as dynamic linked list. Use generics (LinkedQueue ) to allow storing different data types in the queue.

59
15. * We are given a labyrinth of size N x N. Some of its cells are empty ( 0 ) and some are full ( x ). We can move from an empty cell to another empty cell if they share common wall. Given a starting position ( * ) calculate and fill in the array the minimal distance from this position to any other cell in the array. Use " u " for all unreachable cells. Example: 000x0x0x0x0x 0*x0x0 0x0000 000xx0 000x0x345xux2x6xux 1*x8x10 2x6789 345xx10 456xux