Linear Data Structures
Linear Data Structures
Svetlin Nakov
Telerik Corporation
www.telerik.com
Table of Contents
1. 2.
3.
4.
Priority Queue
C# Implementation
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#) Can have several different implementations Different implementations can have different efficiency
Lists: fixed size and variable size Stacks: LIFO (Last In First Out) structure Queues: FIFO (First In First Out) structure
Trees
Contain pairs (key, value) Hash tables: use hash functions to search/insert
Lists
Static and Dynamic Implementations
ways
Statically (using array fixed size) size) Dynamically (linked implementation) Using resizable array (the List<T> class)
Static List
Implemented by an array
Provides direct access by index Has fixed capacity Insertion, deletion and resizing are slow operations
0 1 2 3 4 5 6 7
2 18 7 12 3
6 11 9
Linked List
Dynamic (pointer-based) implementation (pointer Different forms
using an array
All elements are of the same type T T can be any type, e.g. List<int>, e.g. List<int>, List<string>, List<DateTime> List<string>, Size is dynamically increased as needed
Basic functionality:
Count returns the number of elements Add(T) appends given element at the end
List<T> Functionality
list[index] access element by index Insert(index, T) inserts given element to the Insert(index, list at a specified position Remove(T) removes the first occurrence of given element RemoveAt(index) removes the element at the specified position Clear() removes all elements Contains(T) determines whether an element Contains(T) is part of the list
IndexOf() returns the index of the first occurrence of a value in the list (zero-based) zero-based) Reverse() reverses the order of the elements in Reverse() the list or a portion of it Sort() Sort() sorts the elements in the list or a portion of it ToArray() converts the elements of the list to ToArray() an array TrimExcess() sets the capacity to the actual number of elements
Primes in an Interval
Live Demo
Stacks
Static and Dynamic Implementation
In First Out) structure Elements inserted (push) at top Elements removed (pop) from top Useful in many situations
E.g. the execution stack of the program
Can be implemented in several
ways
Statically (using array) Dynamically (linked implementation) Using the Stack<T> class
Static Stack
Static (array-based) (array-
implementation
Has limited (fixed) capacity The current index (top) moves left / right with (top) each pop / push
0 1 2 3 4 5 6 7
2 18 7 12 top
Linked Stack
Dynamic (pointer-based) implementation (pointer-
Each item has 2 fields: value and next Special pointer keeps the top element
top 2 next 7 next 4 next 5 next null
array
Elements are from the same type T T can be any type, e.g. Stack<int> Size is dynamically increased as needed
Basic functionality:
Push(T) inserts elements to the stack Pop() removes and returns the top element from the stack
Stack<T> Example
Console.WriteLine("Top = {0}", stack.Peek()); while (stack.Count > 0) { string personName = stack.Pop(); stack.Pop(); Console.WriteLine(personName); Console.WriteLine(personName); } }
Stack<T>
Live Demo
We are given an arithmetical expression with brackets that can be nested Goal: extract all sub-expressions in brackets subExample: Example:
1 + (2 - (2+3) * 4 / (3+1)) * 5 (3+1))
Result:
(2+3) | (3+1) | (2 - (2+3) * 4 / (3+1))
Algorithm:
For each '(' push its index in a stack '( For each ')' pop the corresponding start index ')
Matching Brackets
Live Demo
Queues
Static and Dynamic Implementation
ways
Statically (using array) Dynamically (using pointers) Using the Queue<T> class
Static Queue
Static (array-based) (array-
implementation
Has limited (fixed) capacity Implement as a circular array Has head and tail indices, pointing to the head and the tail of the cyclic queue
0 1 2 3 4 5 6 7
7 12 2 head
5 tail
Linked Queue
Dynamic (pointer-based) implementation (pointer-
Each item has 2 fields: value and next Dynamically create and delete objects
head 2 next 7 next 4 next tail 5 next null
Enqueue(T) adds an element to the end of the queue Dequeue() removes and returns the element at the beginning of the queue
Queue<T> Example
S = N, N+1, 2*N, N+2, 2*(N+1), 2*N+1, 4*N, N+1, 2*N, N+2, 2*(N+1), 2*N+1, 4*N,
*2
*2
*2
Priority Queue
Priority Queue
What is a
Priority Queue
Data type to efficiently support finding the item with the highest priority Basic operations
Enqueue(T element) Enqueue(T Dequeue
There is no build-in build-
implement
IComparable<T>
It is not necessary to
use OrderedBag
Priority Queue
Live Demo
49
Summary
ADT are defined by list of operations independent of their implementation The basic linear data structures in the computer programming are:
Priority Queue
Implemented by the OrderedBag<T> class
Questions?
http://academy.telerik.com
Exercises
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. sequence. Keep the sequence in List<int>. List<int>. Write a program that reads N integers from the console and reverses them using a stack. Use the Stack<int> class. Write a program that reads a sequence of integers List<int>) (List<int>) ending with an empty line and sorts them in an increasing order. order.
2.
3.
Exercises (2)
4.
Write a method that finds the longest subsequence of equal numbers in given List<int> and returns the result as new List<int>. Write a program to List<int>. test whether the method works correctly. Write a program that removes from given sequence all negative numbers. 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}
5.
6.
Exercises (3)
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
Exercises (4)
9.
Using the Queue<T> class write a program to print its first 50 members for given N. N. Example: Example: N=2 2, 3, 5, 4, 4, 7, 5, 6, 11, 7, 5, 9, 6, ...
Exercises (5)
10.
Write a program that finds the shortest sequence of operations from the list above that starts from N and finishes in M. Hint: use a queue.
Example: N = 5, M = 16 Sequence: 5 7 8 16
Exercises (6)
11.
Write a class Student, that has three fields: name Student, (String), age(Integer) and age(Integer) paidSemesterOnline(Boolean). When in a queue paidSemesterOnline(Boolean). 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
Exercises (6)
12.
Implement the data structure linked list. Define a list. class ListItem<T> that has two fields: value (of type T) and nextItem (of type ListItem<T>). ListItem<T>). Define additionally a class LinkedList<T> with a single field firstElement (of type ListItem<T>). ListItem<T>). Implement the ADT stack as auto-resizable array. autoarray. Resize the capacity on demand (when no space is available to add / insert a new element). Implement the ADT queue as dynamic linked list. LinkedQueue<T>) Use generics (LinkedQueue<T>) to allow storing different data types in the queue. queue.
13.
14.
* We are given a labyrinth of size N x N. Some of its cells are empty (0) and some are full (x). We can (0 (x 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: "u
0 0 0 0 0 0 0 x * x 0 0 0 0 x 0 0 0 x x 0 0 x x 0 0 x 0 x 0 x x 0 0 0 x 3 2 1 2 3 4 4 x * x 4 5 5 6 x 6 5 6 x x 8 7 x x u x u x x 10 8 9 x 10 u x