ADT list.

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. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Stacks, Queues, and Linked Lists
Data Structures ADT List
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
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.
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.
Queue Overview Queue ADT Basic operations of queue
CS Data Structures II Review COSC 2006 April 14, 2017
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
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.
Stacks, Queues & Deques CSC212.
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.
Chapter 7 Stacks II CS Data Structures I COSC 2006
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part R1. Elementary Data Structures.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
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.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
1 Stacks & Queues CSC Stacks & Queues Stack: Last In First Out (LIFO). –Used in procedure calls, to compute arithmetic expressions etc. Queue: First.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
Stacks II David Lillis School of Computer Science and Informatics
CS505 Data Structures and Algorithms
Tirgul 12 Data Structures (2) 1.
Queues Rem Collier Room A1.02
Stacks.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
Queue data structure.
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Priority Queue.
CMSC 341 Lecture 5 Stacks, Queues
Stacks.
Queues.
General Trees & Binary Trees
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Data Structures ADT List
Data Structures ADT List
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
Queues: Implemented using Arrays
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Stacks: Implemented using Linked Lists
Stacks and Queues 1.
Queues CSC212.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Data Structures ADT List
ADT Queue (Array Implementation)
More Data Structures (Part 1)
Stacks CS-240 Dick Steflik.
Lecture 16 Stacks and Queues CSE /26/2018.
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Queues: Implemented using Linked Lists
Chapter 9 The Priority Queue ADT
Lecture 16 Stacks and Queues CSE /26/2018.
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

ADT list

ADT List Elements Structure Operations Domain User of an ADT must only know this Specification Implementer must know all these. Representation Implementation

ADT List: Specification Operations: We assume all operations operate on a list L. Method FindFirst ( ) requires: list L is not empty. input: none results: first element set as the current element. output: none. Method FindNext ( ) requires: list L is not empty. Cur is not last. input: none results: element following the current element is made the current element. output: none. Method Retrieve (Type e) requires: list L is not empty. input: none results: current element is copied into e. output: element e.

ADT List: Specification Operations: Method Update (Type e). requires: list L is not empty. input: e. results: the element e is copied into the current node. output: none. 5. Method Insert (Type e). requires: list L is not full. input: e. results: a new node containing element e is created and inserted after the current element in the list. The new element e is made the current element. If the list is empty e is also made the head element. output: none.

ADT List: Specification Operations: Method Remove ( ) requires: list L is not empty. input: none results: the current element is removed from the list. If the resulting list is empty current is set to NULL. If successor of the deleted element exists it is made the new current element otherwise first element is made the new current element. output: none. 7. Method Full (boolean flag) input: none. returns: if the number of elements in L has reached the maximum number allowed then flag is set to true otherwise false. output: flag.

ADT List: Specification Operations: Method Empty (boolean flag). input: none. results: if the number of elements in L is zero, then flag is set to true otherwise false. Output: flag. Method Last (boolean flag). input: none. requires: L is not empty. Results: if the last element is the current element then flag is set to true otherwise false. Output: flag

ADT List: Implementations Linked List: Has 3 classes The Node class The LinkList class The main class Array: Has 2 classes The Array class

ADT List: Implementations (Linked List) The Node class: public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; } public Node (T val) { data = val; next = null; } }

ADT List: Implementations (Linked List) The LinkList class: public class LinkList<T> extends Object { private Node<T> head; private Node<T> current; public LinkList () { head = current = null; } public boolean empty () { return head == null; } public boolean last () { return current.next == null;}

ADT List: Implementations (Linked List) public boolean full () { return false;} public void findfirst () { current = head;} public void findnext () { current = current.next;} public T retrieve () { return current.data;} public void update (T val) { current.data = val;}

ADT List: Implementations (Linked List) public void insert (T val) { Node<T> tmp; if (empty()){ current = head = new Node<T> (val); } else { tmp = current.next; current.next = new Node<T> (val); current = current.next; current.next = tmp;}

ADT List: Implementations (Linked List) public void remove () { if (current == head) { head = head.next; } else { Node<T> tmp = head; while (tmp.next != current) tmp = tmp.next; tmp.next = current.next; } if (current.next == null) { current = head; } current = current.next; } }

ADT List: Implementations (Array) The Array class public class ArrayList<T> { private int maxsize; private int size; private int current; private T[] nodes; /** Creates a new instance of ArrayList */ public ArrayList(int n) { maxsize = n; size = 0; current = -1; nodes = (T[]) new Object[n]; }

ADT List Implementation (Array) public boolean full () { return size == maxsize; } public boolean empty () return size == 0; } public boolean last () { return current == (size-1); } public void findfirst () { current = 0; } public void findnext () { current++; }

ADT List Implementation (Array) public T retrieve () { return nodes[current]; } public void update (T val) { nodes[current] = val; } public void insert (T val) { for (int i = size-1; i > current; --i) { nodes[i+1] = nodes[i]; } current++; nodes[current] = val; size++;

ADT List Implementation (Array) public void remove () { for (int i = current + 1; i < size; i++) { nodes[i-1] = nodes[i]; } size--; if (size == 0) current = -1; else if (current == size) current = 0;

ADT List: Queue Queue operations are: Enqueue(e) -> Insert element e at the rear of the queue. Dequeue -> Remove and return from the queue the object at the front length() -> Return the number of elements in the queue Isempty() -> Return a Boolean value that indicates whether queue is empty. Isfull() -> Return a Boolean value that indicates whether queue is full.

ADT List: Queue Queue can be implemented in two ways Arrays Linked Lists

ADT Queue (Array Implementaion) public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }

ADT Queue (Array Implementation) public boolean full () { return size == maxsize ? true : false; } public int length () { return size; public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1); size++;

ADT Queue (Array Implementation) public T serve () { T e = nodes[head]; head = (head + 1); size--; return e; }

ADT Queue (Linked List Implementation) public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }

ADT Queue (Linked List Implementation) public boolean full() { return false; } public int length (){ return size;

ADT Queue (Linked List Implementation) public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; size++;

ADT Queue (Linked List Implementation) public Type serve() { Type x; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }

ADT List: Stack push(int e): inserts an element. int pop(): removes and returns the last inserted element. int topE(): returns the last inserted element without removing it (data). boolean isEmpty(): indicates whether no elements are stored. boolean isFull(): indicates whether the stack full or not.

ADT Stack (Linked List Implementation) public class Node<T> extends Object { public T data; public Node<T> next; public Node () { data = null; next = null; }//constructor }

ADT Stack (Linked List Implementation) public class LinkListStack<T> extends Object { private Node<T> top; public LinkListStack () { top = null; } //constructor //operation public boolean isEmpty () { return top == null; } public boolean isFull () { return false; }

ADT Stack (Linked List Implementation) public T topE () { if(top.data==null) throw EmptyStackException return top.data; } public void push (T val) { Node n = new Node(val); n.next = top; top = n;

ADT Stack (Linked List Implementation) public T pop (){ if(top.data==null) throw EmptyStackException Node temp = top; top = top.next; return temp.data; }