Tutorial 5 Linked List Variations, Stack, & Queue.

Slides:



Advertisements
Similar presentations
Stacks using Linked Lists. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what.
Advertisements

Data Structure HKOI training /4/2010 So Pak Yeung.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
CS252: Systems Programming Ninghui Li Program Interview Questions.
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.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
CS Data Structures II Review COSC 2006 April 14, 2017
Queue using an array. .head.tail Pointers head and tail always point to the first empty slot before or after elements in the list. Thus, initially they.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
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
Tutorial 5 Stack & Queue, Midtest. Last In First Out (LIFO) Stack implemented using Array with top pointer –
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Introduction to Data Structures Fall 2008 Dr. David A. Gaitros
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Linked Structures See Section 3.2 of the text.. First, notice that Java allows classes to be recursive, in the sense that a class can have an element.
Tutorial 4 Linked List, Stack, & Queue. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today.
September 05 Kraemer UGA/CSCI 2720 Lists – Part I CSCI 2720 Eileen Kraemer The University of Georgia.
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’
Tutorial 4 – List ADT. List ADT List: –A collection of homogeneous items ordered in linear fashion, e.g. List of names, phone numbers, integers, etc List.
1 Data Structures - Part II CS215 Lecture #8. Stacks  Last-In-First-Out (LIFO)  Stacks are often used in programming when data will need to be used.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Cousin of the Stack.  An abstract data type (container class) in which items are entered at one end and removed from the other end  First In First.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
1 Lecture 14: Queues Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
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.
Tutorial 3 - Linked List. Linked List: Revision The concept of ADT List ADT List using Array –Pro & cons  Discussed in T02Q3 and today in Q1! ADT List.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures David Kauchak cs302 Spring Data Structures What is a data structure? Way of storing data that facilitates particular operations.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Lecture 10 b Stacks b Queues. 2 Stacks b A stack ADT is linear b Items are added and removed from only one end of a stack b It is therefore LIFO: Last-In,
Stack, Queues, and Priority Queues: Linked List Based
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Week 4 - Monday CS221.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Data Structure By Amee Trivedi.
COSC160: Data Structures: Lists and Queues
Linked List Stacks, Linked List Queues, Dequeues
Chapter 15 Lists Objectives
March 29 – Testing and Priority QUeues
CMSC 341 Lecture 5 Stacks, Queues
structures and their relationships." - Linus Torvalds
Principles of Computing – UFCFA3-30-1
Stacks, Queues, and Deques
Stacks, Queues, and Deques
Lesson Objectives Aims
CSC 143 Queues [Chapter 7].
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data Structures and Algorithms for Information Processing
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Stacks and Queues CSE 373 Data Structures.
CSE 373 Data Structures Lecture 6
Stacks and Queues.
Queues Model Operations Pointer Implementations Array Implementation
CSE 373 Data Structures Lecture 6
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
structures and their relationships." - Linus Torvalds
Lecture 9: Stack and Queue
Presentation transcript:

Tutorial 5 Linked List Variations, Stack, & Queue

Linked List Variations (1) Two weeks ago, we have reviewed “Single Linked List” –Revisited in Stack data structure later There exists several Linked List Variations: –With or without Tail Pointer? –One link or double links per node? –Circular or not? –And combinations of these… Next slides: some diagrams of the variations asked in Q1, then one of you will explain the differences (max hops, memory space)! Node 1Node 2 Node... Node N Head

Linked List Variations (2) Single Linked List with Tail Pointer –Other than head, we also have tail pointer pointing to the last item –Pro: Can visit the tail very fast or add new item from tail –Cons: Slow to delete the tail…  –Revisited in Queue data structure later Node 1Node 2 Node... Node N HeadTail

Linked List Variations (3) Circular Single Linked List –Tail.Next = Head  at tail, the link cycles back to the head –Pro: Can visit all node from any node, good for Round Robin stuffs –Cons: Slow to delete the tail…  –Can choose to remember Tail only, save one pointer… Node 1Node 2 Node... Node N Head Node 1Node 2 Node... Node N Tail

Linked List Variations (4) Double Linked List –Two pointers per node: forward/backward –Pro: Can go backwards –Pro: If you have tail pointer, you can also delete tail efficiently! –Cons: Extra pointer is an overhead…  –Can be circular or not…, with or without tail… Node 1Node 2 Node... Node N Head Node 1Node 2 Node... Node N Head

Linked List Variations (5) Single Linked List with dummy head node –Pro: Simplify Single Linked List code –Cons: Same as standard Single Linked List We will discuss their pro/cons in Q1 Be careful, the lecturers can vary these combinations and ask you something about such variations in the mid/final exam… –Make sure you understand these concepts! DummyNode 1 Node... Node N Head

Behavior: Last In First Out (LIFO) Stack implemented using Array with top pointer – (Perhaps) the best implementation? –Using Single Link List with head pointer only – Stack Node 1Node 2 Node... Node N Head ADT Stack using SLL as underlying data structure Node 1 Node 2 Node... Node N Head is Top of Stack Node 0 Top: efficient Push: efficient Pop: efficient

Behavior: First In First Out (FIFO) Queue implemented using Circular Array – Better: use Single Linked List with Tail Pointer –Head pointer for dequeue/front, Tail pointer for enqueue/back We set these roles because pointer directions in SLL (arrows to the right) imply that only insertion is efficient at tail (thus only ok for enqueue) and both insertion/deletion are efficient at head (but since we already use tail for enqueue, head is for dequeue) –Or use Circular Single Linked List: save 1 pointer: Head = Tail.next ADT Queue using SLL with tail pointer as underlying data structure, circular SLL also ok Queue Node 1Node 2 Node... Node N Head Front of Queue Tail = Rear of Queue Node N+1 Back: efficient Enqueue: efficient Front: efficient Dequeue: efficient

Student Presentation T5: 1.>< 2.Ramanathan Anu 3.>< 4.Sal Visoth T10: 1.Puranik Pranay 2.Sistla 3.Suresh 4.>< T13: 1.Mai Xiangjun 2.Nikhil 3.Oh Kok Wei 4.Peck Shan Ren T9: 1.>< 2.Nguyen Sy Nguyen 3.Sandhya 4.>< T17: 1.Kalpit Jain 2.>< 3.Lee Jia Yi 4.Lee Yue Ting 9

Follow Up Questions Q1 –What other “creative” linked lists that you can think of? Q2: –Using stack to do this is a bit overkill… –Can you help Mr Scramble WITHOUT using stack? It is actually much easier Q3: –Again, using stack to do this is also overkill –Can you do this WITHOUT using stack? It is actually much easier Q4: –What if you reverse all the links? (reversed single linked list) With head pointer only? With both head and tail pointers? 10

Midterm Test Tips Analyzing the topics: –C/C++: Likely used as part of question in the essay But may appear as standalone question in MCQ –ADT: Likely embedded in Linked List/Stack/Queue question in essay –List ADT: Vector versus Linked List idea, we spend two weeks here, most likely this appear as the essay? If you want to put a bet, this is your best bet… –Stack and Queue: likely appear but perhaps? less emphasis as this just recently taught… Try: –Mixing and matching various data structure implementations: Funny variants of linked list Implementing stacks or queues using funny data structures, etc… –Try implementing queue using “circular vector”, it is tricky