Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Stacks, Queues, and Linked Lists
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.
Stack & Queues COP 3502.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
0 of 37 Stacks and Queues Lecture of 37 Abstract Data Types To use a method, need to know its essentials: signature and return type o additionally,
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.
Data Structures (Second Part) Lecture 3 : Array, Linked List, Stack & Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering.
A queue is an ADT which allows data values to be accessed only one at a time and only the first inserted. The rule imposed on a queue is: First In First.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
CMPT 225 Stacks.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
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 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks, Queues & Deques CSC212.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Summary of lectures (1 to 11)
Tirgul 3 Topics of this Tirgul: Lists Vectors Stack Queue.
TCSS 342, Winter 2005 Lecture Notes
CSC 212 Stacks & Queues. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  Cannot force you to compile & test.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Stack Queue.
Stacks, Queues, and Deques
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Stacks, Queues, and Deques. 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.
Stacks, Queues, and Deques
Stacks and Queues Introduction to Computing Science and Programming I.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
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.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
CS 367 Introduction to Data Structures Lecture 5.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Click to edit Master text styles Stacks Data Structure.
Stacks.
Stacks and Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Queues.
Stacks, Queues, and Deques
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
Lecture 16 Stacks and Queues CSE /26/2018.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Presentation transcript:

Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue

Linked Lists Each list element (node) holds a data item and a reference (pointer) to the next node: Class ListNode { Object data; ListNode next = null; } A list consists of a chain of zero or more nodes: Class SimplestLinkedList { ListNode head = null; }

Insertion Insertion of a new element q between successive elements p and r: q.next = r; p.next = q; pr q

Deletion Deletion of an element q positioned between elements p and r: p.next = q.next; q.next = null; pr q

Doubly-Linked Lists Each node holds a data item and pointers to both the next and the previous node: Class ListNode { Object data; ListNode next; ListNode prev; } A list (with a sentinel) consists of a circular chain of one or more nodes:

Abstract List Operations Create an empty list Test if the list is empty Provide access to elements at different positions in the list: first last i-th element Insert a new element into the list Remove an element from the list Lookup an element by its contents Retrieve the contents of an element Replace the contents of an element Also useful: next(), previous()

Stack A collection of items that complies to a Last-In-First- Out (LIFO) policy: void push(Object o) - adds the object o to the collection. Object pop() - returns the most recently added object (and removes it from the collection). Object top() - returns the most recently added object (and leaves the stack unchanged).

Linked List with no Pointers 2 linked lists in one array, one for the occupied cells and one for the free cells. Instead of using pointers to the next node, each cell holds the data + the index of the next node in the list. When adding an object a cell is removed form the free list and it ’ s index is added to the occupied list. What is it good for ? Garbage collection. A solution for a language with no pointers. ( and there are such languages!)

Stack Implementation Using an Array class StackA { private int maxSize; private int[] items; private int top; public StackA(int size) { maxSize = size; items = new int[maxSize]; top = -1; } … // Stack operations }

Stack Implementation Using an Array public boolean isEmpty() { return (top == -1); } public void push(int item) { if (top >= maxSize-1) error( … ); items[++top] = item; } public int pop() { if (isEmpty()) error( … ); return items[top--]; } public int top() { if (isEmpty()) error( … ); return items[top]; }

java.util.Stack This java class implements a last-in-first-out stack of objects. The hierarchy: Object Vector(cloneable) Stack It has five methods. empty()-checks if the stack is empty. peek()-returns the top object without removing it. pop()-pops … push()-pushes … search()-search for item in the stack.

Stack Example: delimiter check Legal delimiters: {,[,(,),],} Each opening delimiter must have a matching closing one. Proper nesting: a{bc[d]e}f(g) OK a{bc[d}e]f(g) incorrect We can perform this task easily using a stack!

Stack example: delimiter check // For all characters c of a string do: switch (c) { case ‘ ( ’, ‘ [ ’, ‘ { ’ : stack.push(c); break; case ‘ ) ’, ‘ ] ’, ‘ } ’ : if (stack.isEmpty()) error( … ); if (stack.pop() does not match c) error( … ); default: break; } // When finished: if (!stack.isEmpty()) error( … );

Queue A collection of items that complies to a First-In-First- Out (FIFO) policy: void enqueue(Object o) - adds the object o to the collection. Object dequeue() - returns the least recently added object (and removes it from the collection). Object front() - returns the least recently added object (and leaves the queue unchanged). Also called peek().

Queue Implementation using a (circular) array class QueueA { private int maxSize; private int[] items; private int front; private int back; private int numItems; public QueueA(int size) { maxSize = size; items = new int[maxSize]; front = 0; rear = maxSize-1; numItems = 0; } … // Queue operations }

Queue Implementation using a (circular) array public boolean isEmpty() { return (numItems == 0); } public boolean isFull() { return (numItems == maxSize); } public void enqueue(int item) { if (isFull()) error( … ); back = (back+1) % maxSize; items[back] = item; numItems++; }

Queue Implementation using a (circular) array public int dequeue() { if (isEmpty()) error( … ); int temp = items[front]; front = (front+1) % maxSize; numItems--; return temp; } Public int peek() { if (isEmpty()) error( … ); return items[front]; } Question: can we do without keeping track of numItems?