Understanding ADT List Operations and Implementations

Slide Note
Embed
Share

In this detailed content, you will learn about the specifications and operations involved in working with an ADT list. The structure, domain operations, and user instructions are clearly outlined for efficient implementation. The content also delves into the representation and implementation aspects, providing a comprehensive guide for both users and implementers. Additionally, different methods such as FindFirst, FindNext, Retrieve, Update, Insert, Remove, Full, Empty, and Last are explained with their specific requirements and outcomes. The text also covers the implementations of ADT List using Linked List and Array classes, offering insights into the Node, LinkList, and Array structures. Enrich your understanding of ADT List with this informative resource.


Uploaded on Sep 22, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. ADT list

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

  3. ADT List: Specification Operations: We assume all operations operate on a list L. 1. Method FindFirst ( ) requires: list L is not empty. input: none results: first element set as the current element. output: none. 2. 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. 3. Method Retrieve (Type e) requires: list L is not empty. input: none results: current element is copied into e. output: element e. 3

  4. ADT List: Specification Operations: 4. 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. 4

  5. ADT List: Specification Operations: 6. 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. 5

  6. ADT List: Specification Operations: 8. 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 9. 6

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

  8. 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; } }

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

  10. 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;} 10

  11. 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;} } 11

  12. 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; } else { current = current.next; } } } 12

  13. 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]; }

  14. 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++; } 14

  15. 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++; } 15

  16. 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; } } 16

  17. 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.

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

  19. 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]; } 19

  20. 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++; } 20

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

  22. 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; } 22

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

  24. 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++; } 24

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

  26. 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.

  27. 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 }

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

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

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

More Related Content