Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Lists CS 3358.
COMP171 Fall 2005 Lists.
Stacks, Queues, and Linked Lists
Linked Lists.
Chapter 3: Lists, Stacks and Queues Concept of Abstract Data Type (ADTS) How to efficiently perform operations on list Stack ADT and its applications Queue.
§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.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Data Structures and Algorithms (60-254)
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
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.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
 Abstract Data Type Abstract Data Type  What is the difference? What is the difference?  Stacks Stacks  Stack operations Stack operations  Parsing.
Chapter 3 Stacks.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Stacks, Queues, and Deques
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.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
Lecture 4 Abstract Data Types (ADT). Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations.
Stacks, Queues, and Deques
Objectives of these slides:
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Lists, Stacks, and Queues Abstract Data Types (ADTs) The List ADT The Stack ADT The Queue ADT.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
CHAPTER 3 Lists, Stacks, and Queues §1 Abstract Data Type (ADT) 【 Definition 】 Data Type = { Objects }  { Operations } 〖 Example 〗 int = { 0,  1, 
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture6.
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.
CHP-4 QUEUE. 1.INTRODUCTION  A queue is a linear list in which elements can be added at one end and elements can be removed only at other end.  That.
Data Structures Using C++1 Chapter 5 Linked Lists.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
CHP-3 STACKS.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Linear Data Structures
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
CE 221 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - II Text: Read Weiss, §3.6 1Izmir University of Economics.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
STACKS & QUEUES for CLASS XII ( C++).
Queues.
G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY
Lists CS 3358.
DATA STRUCTURES AND OBJECT ORIENTED PROGRAMMING IN C++
UNIT-3 LINKED LIST.
Stacks and Queues.
Queues Queues Queues.
Cinda Heeren / Geoffrey Tien
Stacks.
Introduction to Data Structures
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Stacks, Queues, and Deques
UNIT-I Topics to be covere d 1.Introduction to data structures.
Further Data Structures
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks and Queues CSE 373 Data Structures.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Stacks, Queues, and Deques
LINEAR DATA STRUCTURES
Presentation transcript:

Lecture 4 Abstract Data Types (ADT)

Usual Data Types Integer, Double, Boolean You do not know the implementation of these. There are some operations which can be done with these (add, multiply, read, write) Programmer just uses these operations without knowing their implementation.

Abstract Data Types Implement these data types once in the program Describe these operations once Use these operations again and again without going into the implementation However, the complexity of these operation depend on the implementation and should not be considered constant An ADT may use a different ADT

List Sequence of elements Operations Add an element at the beginning/end/ith position Delete an element at the beginning/end/ith position Access(read/change) an element at the beginning/end/ith position.

Array Implementation List can be implemented as an array Need to know the maximum number of elements in the list at the start of the program Adding/Deleting an element can take O(n) operations if the list has n elements. Accessing/changing an element anywhere takes O(1) operations independent of n

Adding an element Adding at the beginning: Move all elements one position behind Add at position 1; Increment the current size by 1 For (j = A[0]+1; j > 0; j--) A[j] = A[j-1]; A[1] = new element; A[0]  A[0]+1; Normally first position (A[0])stores the current size of the list Actual number of elements currsize + 1 Complexity: O(n)

Adding at the End Add the element at the end Increment current size by 1; A[A[0]+1] = new element; A[0]  A[0]+1; Complexity: O(1)

Adding at kth position Move all elements one position behind, kth position onwards; Add the element at the kth position Increment current size by 1; For (j = A[0]+1; j > k; j--) A[j] = A[j-1]; A[k] = new element; A[0]  A[0]+1; Complexity: O(n-k)

Deleting an Element Deleting at the beginning: Move all elements one position ahead; Decrement the current size by 1 For (j = 1; j < A[0] ; j--) A[j] = A[j+1]; A[0]  A[0]-1; Complexity: O(n)

Deleting at the End Delete the element at the end Decrement current size by 1; A[0]  A[0]-1; Complexity: O(1)

Deleting at the kth position Move all elements one position ahead, k+1th position onwards; Decrement the current size by 1; For (j = k; j < A[0]+1; j++) A[j] = A[j+1]; A[0]  A[0]-1; Complexity: O(n-k)

Accessing an Element at the kth position A[k]; O(1) operation;

Linked List A node in the linked list consists of two elements Value of the corresponding element in the list Pointer to the next element The pointer is a null pointer for the last element. Consists of a sequence of nodes May be a special node at the beginning, known as the header node

NULL Header Size of the list

Adding an element at the beginning Create a new node; Element in the node has the same value as the new element; Node pointer points to the first element (non-header) Pointer from the header points to new node; Create(newnode); newnode.next  header.next.next header.next  newnode; O(1);

Adding an element at the end Create a new node; Element in the node has the same value as the new element; Node pointer points to the last element; Pointer from the last element points to new node; Create(newnode); newnode.next  last last.next  newnode;

Accessing an element; Move from the beginning till you find the element; For (findnode = start; findnode != NULL; findnode=findnode.next) Stop when you find the desired element; Complexity: O(n) May store a pointer for the special positions, e.g., last, but then update it as necessary;

Adding at the kth position Create new node; Access (k-1)th element; Pointer from new node points to kth element; Pointer from (k-1)th element points to kth element; Create(newnode); Acess (k-1)th node; newnode.next  (k-1)node.next; (k-1)node.next  newnode; Complexity: O(k)

Delete at the kth position Access the k-1th node; Deletenode = (k-1)thnode.next; (k-1)thnode.next = Deletenode.next; Delete (Deletenode); Complexity depends on access complexity; O(1) for deleting first element; O(1) or O(n) for deleting the last element; O(k) for any other element;

Advantage of Linked List Need not know the maximum number of elements ahead of time.

Doubly Linked List A node contains pointers to previous and next element; One can move in both directions;

NULL Header Size of the list

Circular List The last node points to the first one; Useful in modulo operations; Suppose you want to move k mod N positions in the list, where N is the size of the list, then you may want to fold back;

Examples Class enrollment vs student record

Representation of Polynomials iaixiiaixi Have an entry in a linked list for each term. An entry contains the degree of the corresponding term and the value of the coefficient. Two polynomials can be added by adding corresponding terms in the linked list.

Stacks Stack is a list where you can access add or delete elements at one end. All of these operations take constant time. Stacks are called ``last in first out’’ (LIFO), the last added element among the current ones can be accessed or deleted.

Deletion is ``pop’’ Addition is ``push’’ You can also find out whether the stack is empty but nothing else.

Function Calls When a subroutine calls another subroutine (including itself) it must first store its register contents. So, it pushes the register contents into a stack. The new function uses the registers. When the program returns to the old function, pop the stack. Nested Function and Stack Overflow

Stack Implementation Array Need to know the maximum number of elements Delete/Access/Add at the end O(1) operation. Store array length in the 0 th position. Delete/Access/Add at A[array length+1]

Linked List Implementation. Need not know the maximum size Need not have a special header. Add/Access/Delete in the beginning, O(1) Need several memory access, deletions.

Symbol Matching Braces, paranthenses, brackets, begin, ends must match each other [ { [ ( )] } ] [{]}() Easy check using stacks

Start from beginning of the file. Push the beginning symbols you wish to match, ignore the rest. Push left brace, parantheses, brackets, ignore the alphabets Whenever you encounter a right symbol, pop an element from the stack. If stack is empty, then error. Otherwise, if the popped element is the corresponding left symbol, then fine, else there is an error. What is the complexity of the operation?

Symbol Priority A*B + C A*(B + C) A+B+C*D A*D+B+C*E To do the correct operation, calculator needs to know the priority of operands We don’t need any priority nor parantheses if the operation is expressed in postfix or reverse polish notion.

Postfix A*B + C AB*C+ A*(B + C) = (B + C)*A = BC+A* A+B+C*D = AB+CD*+ A*D+B+C*E = ?

Implementation Whenever you see a number push it in the stack. Whenever you see an operator, pop 2 numbers, apply the operator, Push the result Complexity? Section 3.3 in Weiss

Queues Section 3.4 in Weiss List where an element is inserted at the end, and deleted from the beginning Insertion deletion at different ends. First in First out Deletion and Insertion must be O(1)

Linked List Implementation Insert at the end of the linked list Maintain a pointer to the last element Whenever there is insertion, update this ``last’’ pointer to point to the newly inserted element. Delete from the beginning of the list Both insertion and deletion O(1)

Array Implementation Insertion at the end has complexity O(1) But deletion at beginning has complexity O(n) Solution: Use a circular array implementation

Implementing Queues using circular arrays Maintain length of the queue as a separate variable, not as the first element (for convenience). Start inserting from the beginning of the array. Insert at the end of the current list When an element is deleted from the beginning, DO NOT move all elements forward Just mark the position as blank. Any problem?

We will soon reach the end of the array even though there are spaces in the beginning. We roll back to the beginning. When the last element is at the end of the array, we insert a new element at the beginning Example: Note Down from the Board

Implementation Routine Maintain two positions: Front, Rear Maintain Queue Size Initially, Front = 0, Rear = 0, Queue Size = 0

Deletion: If Queue Size = 0, conclude empty queue, and no deletion Else { Decrement Queue Size; Retrieve A[Front]; Change Front to (Front + 1) Modulo Array Size; }

If Queue Size = Array Size, conclude Full queue, and no insertion Else { Increment Queue Size; A[Rear] = new element; Change Rear to (Rear + 1) Modulo Array Size; } Insertion