Linear Data Structures
Lists, Stacks, Queues
Svetlin Nakov
Telerik Corporation
[Link]
Table of Contents
1. 2.
Abstract Data Types (ADT) Lists The List<T> Class List<T>
Static and Linked
3.
Stacks The Stack<T> Class
Static and Linked
4.
Queues The Queue<T> Class
Circular and Linked
Priority Queue
C# Implementation
Abstract Data Types
Basic Data Structures
Abstract Data Types
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#) Can have several different implementations Different implementations can have different efficiency
Basic Data Structures
Linear structures
Lists: fixed size and variable size Stacks: LIFO (Last In First Out) structure Queues: FIFO (First In First Out) structure
Trees
Binary, ordered, balanced, etc.
Dictionaries (maps)
Contain pairs (key, value) Hash tables: use hash functions to search/insert
Lists
Static and Dynamic Implementations
The List ADT
Data structure
(container) that contains a sequence of elements
Can have variable size Elements are arranged linearly, in sequence
Can be implemented in several
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
Singly-linked and doubly-linked Singlydoubly Sorted and unsorted
Singly-linked list Singly-
Each item has 2 fields: value and next
2 head next 7 next 4 next 5 next null
Linked List (2)
Doubly-linked List Doubly-
Each item has 3 fields: value, next and prev value,
head 2 next prev null 7 next prev 4 next prev tail 5 next prev null
The List<T> Class List<T>
AutoAuto-Resizable Indexed Lists
The List<T> Class
Implements the abstract
data structure list
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> Simple Example
static void Main() { List<string> list = new List<string>() { "C#", "Java" }; [Link]("SQL"); [Link]("Python"); foreach (string item in list) { [Link](item); } // Result: // C# // Java // SQL // Python }
Inline initialization: the compiler adds specified elements to the list.
List<T> Simple Example
Live Demo
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
List<T> Functionality (2)
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 Example
static List<int> FindPrimes(int start, int end) { List<int> primesList = new List<int>(); List<int> for (int num = start; num <= end; num++) { bool prime = true; for (int div = 2; div <= [Link](num); div++) [Link](num); { if (num % div == 0) { prime = false; break; break; } } if (prime) (prime) { [Link](num); } } return primesList; }
Primes in an Interval
Live Demo
Union and Intersection Example
int[] Union(int[] firstArr, int[] secondArr) { List<int> union = new List<int>(); [Link](firstArray); foreach (int item in secondArray) if (! [Link](item)) [Link](item); return [Link](); } int[] Intersection(int[] firstArr, int[] secondArr) { List<int> intersect = new List<int>(); foreach (int item in firstArray) if ([Link](secondArray, item) != -1) [Link](item); return [Link](); }
Union and Intersection
Live Demo
Stacks
Static and Dynamic Implementation
The Stack ADT
LIFO (Last
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
The Stack<T> Class
The Standard Stack Implementation in .NET
The Stack<T> Class
Implements the stack
data structure using an
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
The Stack<T> Class (2)
Basic functionality: functionality:
Peek() returns the top element of the stack without removing it Count returns the number of elements Clear() removes all elements Contains(T) determines whether given element is in the stack ToArray() converts the stack to an array TrimExcess() sets the capacity to the actual number of elements
Stack<T> Example
Using Push(), Pop() and Peek() methods Push(),
static void Main() { Stack<string> stack = new Stack<string>(); [Link]("1. [Link]("2. [Link]("3. [Link]("4. Ivan"); Nikolay"); Maria"); George");
[Link]("Top = {0}", [Link]()); while ([Link] > 0) { string personName = [Link](); [Link](); [Link](personName); [Link](personName); } }
Stack<T>
Live Demo
Matching Brackets Example
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 Solution
string expression = "1 + (2 - (2+3) * 4 / (3+1)) * 5"; Stack<int> stack = new Stack<int>(); for (int index = 0; index < [Link]; index++) (int 0; [Link]; { char ch = expression[index]; if (ch == '(') { [Link](index); } else if (ch == ')') { int startIndex = [Link](); int length = index - startIndex + 1; string contents = [Link](startIndex, length); [Link](startIndex, [Link](contents); [Link](contents); } }
Matching Brackets
Live Demo
Queues
Static and Dynamic Implementation
The Queue ADT
FIFO (First In First Out) structure Elements inserted at the tail (Enqueue) (Enqueue) Elements removed from the head (Dequeue) Dequeue) Useful in many situations
Print queues, message queues, etc.
Can be implemented in several
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
The Queue<T> Class
Standard Queue Implementation in .NET
The Queue<T> Class
Implements the queue
data structure using a
circular resizable 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:
Enqueue(T) adds an element to the end of the queue Dequeue() removes and returns the element at the beginning of the queue
The Queue<T> Class (2)
Basic functionality: functionality:
Peek() returns the element at the beginning of the queue without removing it Count returns the number of elements Clear() removes all elements Contains(T) determines whether given element is in the queue ToArray() converts the queue to an array TrimExcess() sets the capacity to the actual number of elements in the queue
Queue<T> Example
Using Enqueue() and Dequeue() methods
static void Main() { Queue<string> queue = new Queue<string>(); [Link]("Message [Link]("Message One"); [Link]("Message [Link]("Message Two"); [Link]("Message [Link]("Message Three"); [Link]("Message [Link]("Message Four"); while ([Link] > 0) { string message = [Link](); [Link](message); } }
The Queue<T> Class
Live Demo
Sequence N, N+1, 2*N
We are given the sequence:
+1 +1 +1
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
Find the first index of given number P Example: N = 3, P = 16
S = 3, 4, 6, 5, 8, 7, 12, 6, 10, 9, 16, 8, 14, 12, 10, 16, 14, Index of P = 11
Sequence Solution with a Queue
int n = 3, p = 16; Queue<int> queue = new Queue<int>(); [Link](n); int index = 0; while ([Link] > 0) { int current = [Link](); [Link](); index++; index++; if (current == p) { [Link]("Index = {0}", index); return; } [Link](current+1); [Link](2*current); }
Sequence N, N+1, 2*N
Live Demo
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-
Priority Queue in .NET
Can be easily implemented using PowerCollections
Priority Queue Implementation
class PriorityQueue<T> where T:IComparable<T> PriorityQueue<T> { private OrderedBag<T> bag; bag; public int Count Necessary to provide { get { return [Link]; } [Link]; comparable elements private set{ } } public PriorityQueue() { bag = new OrderedBag<T>(); OrderedBag<T>(); } public void Enqueue(T element) { [Link](element); [Link](element); } public T Dequeue() { var element = [Link](); [Link](); [Link](); return element; } }
47
Priority Queue Additional Notes
The generic type is needed to
implement
IComparable<T>
It is not necessary to
use OrderedBag
Other Data Structures also can be used
Adding and Removing Element in the Priority
Queue is with complexity logN
Keeps the elements Sorted
Always returns the best element that fulfills some condition
E.g. the smallest or the biggest element
48
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:
List (static, linked)
Implemented by the List<T> class in .NET List<T>
Stack (static, linked)
Implemented by the Stack<T> class in .NET Stack<T>
Queue (static, linked)
Implemented by the Queue<T> class in .NET Queue<T> .NET
Priority Queue
Implemented by the OrderedBag<T> class
Linear Data Structures
Questions?
[Link]
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.
We are given the following sequence:
S1 = N; S2 = S1 + 1; S3 = 2*S1 + 1; S4 = S1 + 2; S5 = S2 + 1; S6 = 2*S2 + 1; S7 = S2 + 2; ...
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.
We are given numbers N and M and the following operations:
a) N = N+1 b) N = N+2 c) N = N*2
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.
Exercises (7) (7)
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 (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