1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Lists CS 3358.
COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
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.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Stack & Queues COP 3502.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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 Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
CS Data Structures II Review COSC 2006 April 14, 2017
Linked List
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
CS 106 Introduction to Computer Science I 12 / 06 / 2006 Instructor: Michael Eckmann.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Stacks and Queues.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Summary of lectures (1 to 11)
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.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Stacks, Queues, and Deques
Definition Stack is an ordered collection of data items in which access is possible only at one end (called the top of the stack). Stacks are known.
Objectives of these slides:
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
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.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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,
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
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.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Stacks & Queues. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy to use.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
Data Structure By Amee Trivedi.
Chapter 12 – Data Structures
Lists CS 3358.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Data Structures and Database Applications Queues in C#
CMSC 341 Lecture 5 Stacks, Queues
Principles of Computing – UFCFA3-30-1
DATA STRUCTURE SUBMUTTED BY:- MADHU MADHAN Lecturer in computer engg. G.P. MEHAM (ROHTAK)
Linked List.
Stacks, Queues, and Deques
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

1 Linked-list, stack and queue

2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue

3 Outline Abstract Data Type (ADT)‏  Introduction to Abstract Data Type (ADT)‏  Examples of Abstract Data Type Linked list Stack Queue

4 Introduction to Abstract Data Type (ADT)‏ Abstract Data Type (ADT)‏  A data type consists of a collection of values together with a set of basic operations on these values  In C++, an ADT is usually implemented using the keyword class. A collection of values is associated to private data members, and a set of basic operations is associated to public member functions  “Abstract” means that users ONLY know the interface (i.e. what operations it provides) without the detailed knowledge about the implementation (i.e. how these operations are implemented).

5 Outline Abstract Data Type (ADT)‏ Linked list  Major components  A Simple Linked List Class in C++  Variations of Linked List  Question 1: Tracing operations  Question 2: Polynomial using linked list  Practice: derivative of polynomial Stack Queue

6 Major components A linked list is a series of connected nodes Each node contains at least  A piece of data (any type)‏  Pointer to the next node in the list Head: pointer to the first node The last node points to NULL A  BC Head A node

7 A Simple Linked List Class in C++ We use two classes: Node and List Declare Node class for the nodes  data : double -type data in this example  next : a pointer to the next node in the list class Node { public: doubledata;// data Node*next;// pointer to next };

8 A Simple Linked List Class in C++ Declare List, which contains  head : a pointer to the first node in the list. Since the list is empty initially, head is set to NULL class List { public: List(void) { head = NULL; }// constructor ~List(void);// destructor bool IsEmpty() { return head == NULL; } Node* InsertNode(int index, double x); int FindNode(double x); int DeleteNode(double x); void DisplayList(void); private: Node* head; };

9 A Simple Linked List Class in C++ Operations of List  IsEmpty determine whether or not the list is empty  InsertNode insert a new node at a particular position  FindNode find a node with a given value  DeleteNode delete a node with a given value  DisplayList print all the nodes in the list  The detail implemented is listed in the lecture slides

10 Variations of Linked List Variations on the data arrangement  Unsorted linked list Data inside a linked list is stored in an arbitrary order  Sorted linked list Data is sorted by the “key” on each node For example: A student record may use student ID as “key” A list of student records may be sorted in ascending order of the student ID Variations on the structure  Singular linked list (described in the previous slide)‏  Circular linked list The last node points to the first node of the list  Doubly linked list Each node points to not only successor but the predecessor

11 Variations of Linked Lists Circular linked lists  The last node points to the first node of the list How do we know when we have finished traversing the list? Solution: We can use a while loop, we can set an initial pointer “current” pointing to “head”. We can traverse the linked list until we found that we go back to the “head” again A Head BC

12 Variations of Linked Lists Doubly linked lists  Each node points to not only successor but the predecessor  There are two NULL: at the first and last nodes in the list  Advantage: given a node, it is easy to visit its predecessor. Convenient to traverse lists backwards Head AB  C 

13 Question 1: Tracing operations Suppose we have a sorted linked list storing a sequence of integers  The input sequence is as follow: Step 1: Insert 3 Step 2: Insert 9 Step 3: Delete 3  Draw the linked list before and after each operation The drawing should contain “Head”, links connect nodes, and the NULL pointer. You should also draw “prev”, “curr” if applicable

14 Linked list example: Step 1 Before step 1: Insert 3 After step 1: Insert 3 Head   3  Curr, Prev

15 Linked list example: Step 2 Before step 2: Insert 9 After step 2: Insert 9 Head  3 Prev  Curr Head 3  9

16 Linked list example: Step 3 Before step 3: delete 3 After step 3: delete 3 Head 3 Prev  Curr Head  9  9

17 Outline Abstract Data Type (ADT)‏ Linked list Stack  Stack ADT  Operations: Push and Pop Queue

18 Stack ADT A stack is a list in which insertion and deletion take place at the same end  This end is called top  The other end is called bottom Stacks are known as LIFO (Last In, First Out) lists.  The last element inserted will be the first to be retrieved

19 Operations: Push and Pop Primary operations: Push and Pop Push  Add an element to the top of the stack Pop  Remove the element at the top of the stack top empty stack A top push an element top push another A B top pop A

20 Implementation of Stacks Any list implementation could be used to implement a stack  Arrays (static: the size of stack is given initially)‏  Linked lists (dynamic: never become full)‏ We will explore implementations based on array and linked list Let’s see how to use an array to implement a stack first

21 Stack class class Stack { public: Stack(int size = 10);// constructor ~Stack() { delete [] values; }// destructor bool IsEmpty() { return top == -1; } bool IsFull() { return top == maxTop; } double Top(); // examine, without popping void Push(const double x); double Pop(); void DisplayStack(); private: int maxTop;// max stack size = size - 1 int top;// current top of stack double* values;// element array };

22 Stack class Attributes of Stack  maxTop : the max size of stack  top : the index of the top element of stack  values : point to an array which stores elements of stack Operations of Stack  IsEmpty : return true if stack is empty, return false otherwise  IsFull : return true if stack is full, return false otherwise  Top : return the element at the top of stack  Push : add an element to the top of stack  Pop : delete the element at the top of stack  DisplayStack : print all the data in the stack

23 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue  Queue ADT  Enqueue and Dequeue

24 Queue ADT Like a stack, a queue is also a list. However, with a queue, insertion is done at one end, while deletion is performed at the other end. Accessing the elements of queues follows a First In, First Out (FIFO) order.  Like customers standing in a check-out line in a store, the first customer in is the first customer served. rearfront

25 Enqueue and Dequeue Primary queue operations:  Enqueue and Dequeue Like check-out lines in a store, a queue has a front and a rear.  Enqueue insert an element at the rear of the queue  Dequeue remove an element from the front of the queue Insert (Enqueue)‏ Remove (Dequeue)‏ rearfront

26 Implementation of Queue Just as stacks can be implemented as arrays or linked lists, so with queues. Dynamic queues have the same advantages over static queues as dynamic stacks have over static stacks

27 An example question List out one similarity and one difference between Stack and Queue  Similarity Both ADTs can be implemented using static array Both support fast insertion and extraction (although the rule to govern the insertion and extraction is different)‏ And more…  Difference Stack is a LIFO data structure, while Queue is a FIFO data structure And more…