The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Queues and Linked Lists
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
§3 The Stack ADT 1. ADT A stack is a Last-In-First-Out (LIFO) list, that is, an ordered list in which insertions and deletions are.
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Data Structures and Algorithms (60-254)
The Ranked Sequence ADT Definition A ranked sequence is a collection of items arranged in a linear order, where each item has a rank defining the relative.
Queue & List Data Structures & Algorithm Abstract Data Types (ADTs) ADT is a mathematically specified entity that defines a set of its instances,
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks, Queues & Deques CSC212.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Implementing and Using Stacks
Data Structures Using C++ 2E
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg 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.
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Information and Computer Sciences University of Hawaii, Manoa
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Introduction to Data Structures and Algorithms
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.
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.
Stacks & Queues CSC 172 SPRING 2002 LECTURE 4 Agenda  Stack  Definition  Implementation  Analysis  Queue  Definition  Implementation  Analysis.
Stacks Chapter 3 Objectives Upon completion you will be able to
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Linked Data Structures
Elementary Data Structures
Data Structure By Amee Trivedi.
Linked List Stacks, Linked List Queues, Dequeues
Chapter 15 Lists Objectives
Objectives In this lesson, you will learn to: Define stacks
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks and Queues 1.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
General List.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
The Priority Queue ADT A priority queue is a restricted form of a list, where items are arranged according to their priorities (or keys). The key assigned.
The Ranked Sequence ADT
LINEAR DATA STRUCTURES
Presentation transcript:

The List ADT Definition A list is a collection of objects, called nodes, connected in a chain by links. There may or may not be an ordering relationship between data in the nodes. Depending on this, we distinguish between ordered and unordered lists. For now, we consider only unordered lists. Operations (methods) on lists: insertFirst (node) Inserts a new node at the beginning of the list deleteFirst (node) Deletes a node from the beginning of the list size () Returns the number of nodes in the list empty () Returns true if the list is empty search (key) Searches for a node with the specified key traverse () Processes each node on the list (for example, prints it out)

A linked list interface and the Node class public interface LList { public void insertFirst (int item); public Node deleteFirst (); public int size(); public boolean empty (); public boolean search (int item); public void traverse (); } class Node { private int data; private Node next; public Node () { this(0, null); } public Node (int d) { data = d; } public Node (int d, Node n) { data = d; next = n; } public void setData (int newData) { data = newData; } public void setNext (Node newNext) { next = newNext; } public int getData () { return data; } public Node getNext () { return next; } public void displayNode () { System.out.print (data); } }

Implementation of an unordered linked list ADT class LinkListADT implements LList { private Node first; public LinkListADT () { first = null; } public boolean empty () { return (first == null); } public int size () { int count = 0; Node current = first; while (current != null) { count++; current = current.getNext(); } return count; } public void insertFirst (int newData) { Node newFirst = new Node (newData); newFirst.setNext(first); first = newFirst; } public Node deleteFirst () { Node temp = first; first = first.getNext(); return temp; } public boolean search (int key) { boolean result = false; Node current = first; while (current != null) { if (current.getData () == key) { result = true; return result; } else current = current.getNext(); } return result; } public void traverse () { System.out.print ("Current list: "); Node current = first; while (current != null) { current.displayNode (); System.out.print (" "); current = current.getNext(); } System.out.println (); } }

Efficiency of linked list operations empty, insertFirst and deleteFirst methods are O(1), while size, search and traverse methods are O(N). The size method can be rewritten in a more efficient, O(1), form, but the (linear) search and traverse methods do require a loop to process each of the nodes on the list. If we want to insert a node in a place different than the beginning of the list, we must first find that place by means of the search method, and then insert (or delete) the node. Although the actual insert / delete process involves simply an exchange of two pointers, the overall efficiency of these method will be O(N) because of the search involved. Ordered linked lists rely on an insertN method to add the new node in the appropriate place according to the prescribed ordering relationship. Therefore, the insertion in an ordered linked list will also be O(N) operation (if linear search is used).

The Queue ADT -- a linked list implementation public interface LLQueue { public void enqueue (int item); public int dequeue(); public int size(); public boolean empty(); public int front(); } class LLQueueADT implements LLQueue { private int size; private Node front; private Node rear; public LLQueueADT () { size = 0; front = null; rear = null; } public boolean empty () { return (size == 0); } public int size () { return size; } public void enqueue (int number) { Node newNode = new Node (); newNode.setData(number); newNode.setNext(null); if (this.empty()) front = newNode; else rear.setNext(newNode); rear = newNode; size++; } public int dequeue () { int i; i = front.getData(); front = front.getNext(); size--; if (this.empty()) rear = null; return i; } public int front () { return front.getData(); }

The Stack ADT -- a linked list implementation public interface LLStack { public void push (int item); public int pop(); public int size(); public boolean empty(); public int ontop(); } class LLStackADT implements LLStack { private Node top; private int size; public LLStackADT () { top = null; size = 0; } public boolean empty () { return (top == null); } public int size () { return size; } public void push (int number) { Node newNode = new Node (); newNode.setData(number); newNode.setNext(top); top = newNode; size++; } public int pop () { int i; i = top.getData(); top = top.getNext(); size--; return i; } public int ontop () { int i = pop(); push(i); return i; }

Double-ended Queues The double-ended queue (or dequeu) supports insertions and deletions at the front and at the rear of the queue. Therefore, in addition to enqueue, dequeue (which we call now insertLast and deleteFirst, respectively), and first methods, we must also provide insertFirst, deleteLast and last methods. The Dequeue ADT can be used for implementing both stacks and queues. Dequeue methods Stack methods Queue methods size() size() size() empty() empty() empty() last() ontop() -- first() -- front() insertFirst() insertLast() push(item) enqueue(item) deleteLast() pop() -- deleteFirst() -- dequeue()

An implementation of a doubly linked list Nodes in a doubly linked list have pointers to both, the next node and the previous node. For simplicity, we can add two dummy nodes to the list: the header node, which comes before the first node, and the trailer node, which comes after the last node. The doubly linked list ADT has the following interface: public interface DLList { public void insertFirst (int item); public void insertLast (int item); public DLNode deleteFirst (); public DLNode deleteLast (); public int size(); public int last (); public int first (); public boolean empty (); public void traverse (); }

Implementation of a doubly linked list (cont.) class DLNode { private int data; private DLNode next, prev; public DLNode () { this(0, null, null); } public DLNode (int d) { data = d; next = null; prev = null; } public DLNode (int newData, DLNode newNext, DLNode newPrev) { data = newData; next = newNext; prev = newPrev; } public void setData (int newData) { data = newData; } public void setNext (DLNode newNext) { next = newNext; } public void setPrev (DLNode newPrev) { prev = newPrev; } public int getData () { return data; } public DLNode getNext () { return next; } public DLNode getPrev () { return prev; } public void displayDLNode () { System.out.print (data); }

Implementation of a doubly linked list (cont.) class DLListADT implements DLList { private DLNode header; private DLNode trailer; private int size; public DLListADT () { header = new DLNode(); trailer = new DLNode(); header.setNext(trailer); header.setPrev(null); header.setData(0); trailer.setPrev(header); trailer.setNext(null); trailer.setData(0); size = 0; } public boolean empty () { return (size == 0); } public int size () { return size; } public void insertFirst (int newData) { DLNode oldFirst = header.getNext(); DLNode newFirst = new DLNode (newData, oldFirst, header); oldFirst.setPrev(newFirst); header.setNext(newFirst); size++; } public void insertLast (int newData) { DLNode oldLast = trailer.getPrev(); DLNode newLast = new DLNode (newData, trailer, oldLast); oldLast.setNext(newLast); trailer.setPrev(newLast); size++; } public DLNode deleteFirst () { DLNode oldFirst = header.getNext(); DLNode newFirst = oldFirst.getNext(); newFirst.setPrev(header); header.setNext(newFirst); size--; return oldFirst; }

Implementation of a doubly linked list (cont.) public DLNode deleteLast () { DLNode oldLast = trailer.getPrev(); DLNode newLast = oldLast.getPrev(); trailer.setPrev(newLast); newLast.setNext(trailer); size--; return oldLast; } public boolean search (int key) { boolean result = false; DLNode current = header.getNext(); while (current != trailer) { if (current.getData () == key) { result = true; return result; } else current = current.getNext(); } return result; } public int last () { return (trailer.getPrev().getData()); } public int first () { return (header.getNext().getData()); } public void traverse () { System.out.print ("Current list: "); DLNode current = header.getNext(); while (current != trailer) { current.displayDLNode (); System.out.print (" "); current = current.getNext(); } System.out.println (); }

Application of stacks and queues: parsing and evaluation of arithmetic expressions Parsing of an expression is a process of checking its syntax and representing it in a form which can be uniquely interpreted. Example: Infix forms: A * B / C + D  (A * B) / C + D  ((A * B) / C) + D Postfix form: A B * C / D + Prefix form: + / * A B C D Postfix and prefix forms are unique: expression can be evaluated by scanning its postfix form from left to right, or its prefix form from right to left.

Algorithm for converting infix expressions into postfix form Data structures needed: –A queue containing the infix expression (call it infix). –A stack which may contain +, -, *, /, (, # (call it operator stack). –A queue containing the final postfix expression (call it postfix). Methods needed: –InfixPriority, given a non-operand token, returns an integer associated with its priority according to the table below. –StackPriority, given a token returns integer associated with its priority according to the table below. Token * / + - ( ) # Priority (undefined 0 Value for the stack)

Algorithm for converting infix to postfix Initialize operator stack by pushing # Dequeue next token from infix Token is an operand? Token right parenthesis? Enqueue the Pop all entries remaining operand on on the operator stack and postfix queue enqueue them on postfix Pop entries from operator Pop operator stack stack and enqueue them and enqueue on on postfix queue until a postfix operators whose matching left parenthesis stack priority is >= is popped infix priority of the token, except if "(" Disregard the left and Push token on right parenthesis operator stack + Token is # + _ _ Repeat until token is "#"

Example 1: Translate the following infix form to a postfix form: A * B + (C - D / E) Infix queue Operator stack Postfix queue A # A * * # A B * # A B + + # A B * ( ( + # A B * C ( + # A B * C - - ( + # A B * C D - ( + # A B * C D / / - ( + # A B * C D E / - ( + # A B * C D E ) + # A B * C D E / - # A B * C D E / - + #

The algorithm adapted for logical expressions Same data structures needed: –The infix queue. –The operator stack, which now may contain , , , , , (, #. –The postfix queue. Methods needed: –InfixPriority, given a non-operand token, returns an integer associated with its priority according to the table below. –StackPriority, given a token returns integer associated with its priority according to the table below. Token      ( ) # Priority (undefined 0 Value for the stack)

Example 2: Translate the following infix form to a postfix form: (P  Q)  (  P  Q) Infix queue Operator stack Postfix queue ( # P ( # P   ( # P Q  ( # P Q ) # P Q    # P Q  ( (  # P Q    (  # P Q  P  (  # P Q  P   (  # P Q  P  Q  (  # P Q  P  Q )  # P Q  P  Q  # # P Q  P  Q  

Evaluation of postfix expressions The idea: given the postfix expression, get a token and: 1.If the token is an operand, push its value on the (value) stack. 2.If the token is an operator, pop two values from the stack and apply that operator to them; then push the result back on the stack. Example 1: A B * C D E / - + Let A = 5, B = 3, C = 6, D = 8, E = 2. value stack push (5) 5 push (3) 5 3 push (pop * pop) 15 push (6) 15 6 push (8) push (2) push (pop / pop) push (pop - pop) 15 2 push (pop + pop) 17

Evaluation of postfix expressions (contd.) Example 2: P Q  P  Q   Let P = true, Q = true value stack push (true) true push (true) true true push (pop  pop) true push (true) true true push (  pop) true false push (true) true false true push (pop  pop) true true push (pop  pop) true To determine if the expression is a tautology, it must evaluate to true for all combinations of truth values of its components, i.e for P = false and Q = true; P = true and Q = false; P = false and Q= false.