Lists, Stacks, Queues Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
Chapter 3 – Lists A list is just what the name implies, a finite, ordered sequence of items. Order indicates each item has a position. A list of size 0.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Chapter 12: Data Structures
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
TCSS 342, Winter 2005 Lecture Notes
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Chapter 24 Dispensers and dictionaries. This chapter discusses n Dictionaries n Dispensers u stacks u queues u priority queues.
Lists, Stacks, Queues Svetlin Nakov Telerik Corporation
Objectives of these slides:
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Arrays, Lists, Stacks, Queues Static and Dynamic Implementation Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Chapter 8: Data Abstractions Senem Kumova Metin. 8-2 Chapter 8: Data Abstractions 8.1 Basic Data Structures – Arrays – Lists, Stacks, Queues – Trees 8.2.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
Arrays, Lists, Stacks, Queues Processing Sequences of Elements SoftUni Team Technical Trainers Software University
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
Dictionaries, Hash Tables, Collisions Resolution, Sets Svetlin Nakov Telerik Corporation
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 7 Queues Introduction Queue applications Implementations.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Lists, Stacks, Queues Svetlin Nakov Telerik Software Academy Manager Technical Trainer
CPS120: Introduction to Computer Science Nell Dale John Lewis Abstract Data Types.
Linear Data Structures: Stacks and Queues
Data Structure By Amee Trivedi.
Set Collection A Bag is a general collection class that implements the Collection interface. A Set is a collection that resembles a Bag with the provision.
Chapter 12 – Data Structures
Data Structure Interview Question and Answers
Stacks and Queues.
Cinda Heeren / Geoffrey Tien
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
CS313D: Advanced Programming Language
Queues.
structures and their relationships." - Linus Torvalds
Lesson 6. Types Equality and Identity. Collections.
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
structures and their relationships." - Linus Torvalds
Queues.
Abstract Data Types (ADTs)
Presentation transcript:

Lists, Stacks, Queues Svetlin Nakov Telerik Corporation

1. Abstract Data Types (ADT) 2. Lists – The List Class  Static and Linked 3. Stacks – The Stack Class  Static and Linked 4. Queues – The Queue Class  Circular and Linked  Priority Queue  C# Implementation

Basic Data Structures

 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

 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

Static and Dynamic Implementations

 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)  Dynamically (linked implementation)  Using resizable array (the List class)

 Implemented by an array  Direct access by index (fast)  Insertion and deletion and resizing are slow operations L

 Dynamic (pointer-based) implementation  Direct access to first/last element  No access by index  go through all previous elements (slow)  Insertion and deletion are fast  Resizing – add new element at the end or beginning 2next7next head4next5next null

Auto-Resizable Indexed Lists

 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, List, List  T can be any type, e.g. List, List, List  Size is dynamically increased as needed  Basic functionality:  Count – returns the number of elements  Add(T) – appends given element at the end

static void Main() { List list = new List (new string[]{ "C#", "Java" }); List list = new List (new string[]{ "C#", "Java" }); list.Add("SQL"); list.Add("SQL"); list.Add("Python"); list.Add("Python"); foreach (string item in list) foreach (string item in list) { Console.WriteLine(item); Console.WriteLine(item); } // Result: // Result: // C# // C# // Java // Java // SQL // SQL // Python // Python} Inline initialization: the compiler adds specified elements to the list.

Live Demo

 list[index] – access element by index  Insert(index, T) – inserts given element to the 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 is part of the list

 IndexOf(T) – 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  Sort() – sorts the elements in the list or a portion of it  ToArray() – converts the elements of the list to an array  TrimExcess() – sets the capacity to the actual number of elements

Dynamic pointer-based list

 Implements the abstract data structure list using pointers  Each element links to next and previous element  New elements are inserted before the first or after the last  Basic functionality:  Count – returns the number of elements  AddFirst(T) – inserts element at beginning  AddLast(T) – appends element at the end

static void Main() { LinkedList list = new LinkedList () { "C#", "Java" }; LinkedList list = new LinkedList () { "C#", "Java" }; list.AddFirst("SQL"); list.AddFirst("SQL"); list.AddLast("Python"); list.AddLast("Python"); foreach (string item in list) foreach (string item in list) { Console.WriteLine(item); Console.WriteLine(item); } // Result: // Result: // SQL // SQL // C# // C# // Java // Java // Python // Python} Inline initialization: the compiler adds specified elements to the list.

Live Demo

 First – get the list's first node  Last – get the list's last node  Insert(index, T) – inserts given element to the list at a specified position  Remove(T) – removes the first occurrence of given element  Clear() – removes all elements  Contains(T) – determines whether an element is part of the list

 Find() – returns the first occurrence of a value in the list (as a LinkedListNode )  Reverse() – reverses the order of the elements in the list or a portion of it  ToArray() – converts the elements of the list to an array

Static and Dynamic Implementation

 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 class

The Standard Stack Implementation in.NET

 Implements the stack data structure using an array  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  Basic functionality:  Push(T) – inserts elements to the stack  Pop() – removes and returns the top element from the stack

 Basic 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

 Using Push(), Pop() and Peek() methods static void Main() { Stack stack = new Stack (); Stack stack = new Stack (); stack.Push("1. Ivan"); stack.Push("1. Ivan"); stack.Push("2. Nikolay"); stack.Push("2. Nikolay"); stack.Push("3. Maria"); stack.Push("3. Maria"); stack.Push("4. George"); stack.Push("4. George"); Console.WriteLine("Top = {0}", stack.Peek()); Console.WriteLine("Top = {0}", stack.Peek()); while (stack.Count > 0) while (stack.Count > 0) { string personName = stack.Pop(); string personName = stack.Pop(); Console.WriteLine(personName); Console.WriteLine(personName); }}

Live Demo

Static and Dynamic Implementation

 FIFO (First In First Out) structure  Elements inserted at the tail (Enqueue)  Elements removed from the head (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 class

Standard Queue Implementation in.NET

 Implements the queue data structure using a circular resizable array  Elements are from the same type T  T can be any type, e.g. Queue  T can be any type, e.g. Queue  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

 Basic 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

 Using Enqueue() and Dequeue() methods static void Main() { Queue queue = new Queue (); Queue queue = new Queue (); queue.Enqueue("Message One"); queue.Enqueue("Message One"); queue.Enqueue("Message Two"); queue.Enqueue("Message Two"); queue.Enqueue("Message Three"); queue.Enqueue("Message Three"); queue.Enqueue("Message Four"); queue.Enqueue("Message Four"); while (queue.Count > 0) while (queue.Count > 0) { string message = queue.Dequeue(); string message = queue.Dequeue(); Console.WriteLine(message); Console.WriteLine(message); }}

Live Demo

 What is a Priority Queue  Data type to efficiently support finding the item with the highest priority  Basic operations  Enqueue(T element)  Dequeue  Always returns current highest priority item  There is no build-in Priority Queue in.NET  PowerCollections.OrderedBag can be used

 Using OrderedBag as a priority queue  Use Add(T) instead of Enqueue(T)  Use RemoveFirst() instead of Dequeue() using Wintellect.PowerCollections; … OrderedBag bagPriorityQueue = new OrderedBag (); bagPriorityQueue.Add (4); bagPriorityQueue.Add (1); bagPriorityQueue.Add (4); bagPriorityQueue.Add (2); while(bagPriorityQueue.Count > 0) { Console.Write (bagPriorityQueue.RemoveFirst() + " "); } // Output: //

Live Demo 39

 ADT are defined by list of operations independent of their implementation  Basic linear data structures in programming:  List (static, linked)  Implemented by the List and LinkedList classes in.NET  Stack (static, linked)  Implemented by the Stack class in.NET  Queue (static, linked)  Implemented by the Queue class in.NET  Priority Queue  Implemented by the OrderedBag class

Questions?

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.

4. Write a program that reads an array from the console. Then starts from the element at index 0 removes it if the next element is larger. Then for each other element, removes it if it's previous or next (remaining) neighbor is larger. Example: { 5,4,7,5,8,9,11} -> {5, 4,7,5,8,9,11} -> {5, 7,5,8,9,11} -> {5,7, 5,8,9,11} -> {5,7, 8,9,11} -> {5,7, 9,11} -> {5,7, 11 }  result: {5,7,11} 5. * 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

6. * 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 0x xx0 000x0x345xux2x6xux 1*x8x10 2x xx10 456xux