EE 2204 - Data Structures and Algorithms N Radhakrishnan Assistant Professor Anna University, Chennai.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists CS 3358.
COMP171 Fall 2005 Lists.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
CHP-5 LinkedList.
Data Structures Using C++ 2E
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture - 1 on Data Structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Data Type and Data Structure Data type Set of possible values for variables.
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Lecture 6: Linked Lists Linked lists Insert Delete Lookup Doubly-linked lists.
Arrays. The array data structure An array is an indexed sequence of components –Typically, the array occupies sequential storage locations –The length.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
Chapter 3: Arrays, Linked Lists, and Recursion
Chapter 3 Data Structures and Abstract Data Type Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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 Data Structure.
Data Structures and Algorithm Analysis Lecturer: Jing Liu Homepage:
Lecture No.01 Data Structures Dr. Sohail Aslam
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Data Structures Week 5 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Chapter 2 ARRAYS.
Prepared By Ms.R.K.Dharme Head Computer Department.
Lists. Container Classes Many applications in Computer Science require the storage of information for collections of entities e.g. a student registration.
 DATA STRUCTURE DATA STRUCTURE  DATA STRUCTURE OPERATIONS DATA STRUCTURE OPERATIONS  BIG-O NOTATION BIG-O NOTATION  TYPES OF DATA STRUCTURE TYPES.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
DATA STRUCTURE & ALGORITHMS (BCS 1223) NURUL HASLINDA NGAH SEMESTER /2014.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
4-1 Topic 6 Linked Data Structures. 4-2 Objectives Describe linked structures Compare linked structures to array- based structures Explore the techniques.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
The List ADT A sequence of zero or more elements A 1, A 2, A 3, … A N-1 N: length of the list A 1 : first element A N-1 : last element A i : position i.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Linked List by Chapter 5 Linked List by
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
CS 201 Data Structures and Algorithms Chapter 3: Lists, Stacks, and Queues - I Text: Read Weiss, §3.1 – 3.5 1Izmir University of Economics.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
Data Structure and Algorithms
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
UNIT-II Topics to be covered Singly linked list Circular linked list
LINKED LISTS.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
Introduction toData structures and Algorithms
Lecture 6 of Computer Science II
Unit – I Lists.
Cpt S 122 – Data Structures Abstract Data Types
Data Structure Interview Question and Answers
CE 221 Data Structures and Algorithms
Data Structure and Algorithms
Lists CS 3358.
Data Structures Interview / VIVA Questions and Answers
CSCE 210 Data Structures and Algorithms
Introduction to Data Structure
LINKED LISTS CSCD Linked Lists.
Arrays and Linked Lists
Linked Lists.
Introduction to Data Structures
11-3 LINKED LISTS A linked list is a collection of data in which each element contains the location of the next element—that is, each element contains.
Introduction to Data Structure
Data Structures and Algorithms
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
Presentation transcript:

EE Data Structures and Algorithms N Radhakrishnan Assistant Professor Anna University, Chennai

3 August August August 2015 Anna University, Chennai Topics  Introduction  Definitions  Classification of Data Structures  Arrays and Linked Lists  Abstract Data Types [ADT] The List ADT  Array-based Implementation  Linked List Implementation  Cursor-based Implementation  Doubly Linked Lists

3 August August August 2015 Anna University, Chennai Data Structure [Wikipedia]  Data Structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.  Different kinds of data structures are suited to different kinds of applications.  Storing and retrieving can be carried out on data stored in both main memory and in secondary memory.

3 August August August 2015 Anna University, Chennai Merriam-Webster's Definition  Way in which data are stored for efficient search and retrieval.  The simplest data structure is the one- dimensional (linear) array.  Data items stored non-consecutively in memory may be linked by pointers.  Many algorithms have been developed for storing data efficiently

3 August August August 2015 Anna University, Chennai Algorithms [Wikipedia]  An algorithm is a step-by-step procedure for calculations.  An algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function.  The transition from one state to the next is not necessarily deterministic; some algorithms incorporate random input.

3 August August August 2015 Anna University, Chennai Merriam-Webster's Definition  Procedure that produces the answer to a question or the solution to a problem in a finite number of steps.  An algorithm that produces a yes or no answer is called a decision procedure; one that leads to a solution is a computation procedure.  Example: A mathematical formula and the instructions in a computer program

3 August August August 2015 Anna University, Chennai Data Structure Classification  Primitive / Non-primitive Basic Data Structures available / Derived from Primitive Data Structures  Homogeneous / Heterogeneous Elements are of the same type / Different types  Static / Dynamic memory is allocated at the time of compilation / run-time  Linear / Non-linear Maintain a Linear relationship between element

3 August August August 2015 Anna University, Chennai ADT - General Concept  Problem solving with a computer means processing data  To process data, we need to define the data type and the operation to be performed on the data  The definition of the data type and the definition of the operation to be applied to the data is part of the idea behind an Abstract Data Type (ADT)

3 August August August 2015 Anna University, Chennai ADT - General Concept  The user of an ADT needs only to know that a set of operations are available for the data type, but does not need to know how they are applied  Several simple ADTs, such as integer, real, character, pointer and so on, have been implemented and are available for use in most languages

3 August August August 2015 Anna University, Chennai Data Types  A data type is characterized by: A set of values A data representation, which is common to all these values, and A set of operations, which can be applied uniformly to all these values

3 August August August 2015 Anna University, Chennai Primitive Data Types  Languages like ‘C’ provides the following primitive data types: boolean char, byte, int float, double  Each primitive type has: A set of values A data representation A set of operations  These are “set in stone”.

3 August August August 2015 Anna University, Chennai ADT Definition [Wikipedia]  In computer science, an abstract data type (ADT) is a mathematical model for a certain class of data structures that have similar behavior.  An abstract data type is defined indirectly, only by the operations that may be performed on it and by mathematical constraints on the effects (and possibly cost) of those operations.

3 August August August 2015 Anna University, Chennai ADT Definition [Wikipedia]  An ADT may be implemented by specific data types or data structures, in many ways and in many programming languages; or described in a formal specification language.  example, an abstract stack could be defined by three operations: push, that inserts some data item onto the structure, pop, that extracts an item from it, and peek, that allows data on top of the structure to be examined without removal.

3 August August August 2015 Anna University, Chennai Definition from techforum4you  Abstract data types or ADTs are a mathematical specification of a set of data and the set of operations that can be performed on the data.  They are abstract in the sense that the focus is on the definitions and the various operations with their arguments.  The actual implementation is not defined, and does not affect the use of the ADT.

3 August August August 2015 Anna University, Chennai ADT in Simple Words  Definition: Is a set of operation Mathematical abstraction No implementation detail  Example: Lists, sets, graphs, stacks are examples of ADT along with their operations

3 August August August 2015 Anna University, Chennai Why ADT?  Modularity divide program into small functions easy to debug and maintain easy to modify group work  Reuse do some operations only once  Easy to change the implementation transparent to the program

3 August August August 2015 Anna University, Chennai Implementing an ADT  To implement an ADT, you need to choose: A data representation  must be able to represent all necessary values of the ADT  should be private An algorithm for each of the necessary operation:  must be consistent with the chosen representation  all auxiliary (helper) operations that are not in the contract should be private  Remember: Once other people are using it It’s easy to add functionality

3 August August August 2015 Anna University, Chennai The List ADT  The List is an Ordered sequence of data items called elements A 1, A 2, A 3, …,A N is a list of size N size of an empty list is 0 A i+1 succeeds A i A i-1 preceeds A i Position of A i is i First element is A 1 called “head” Last element is A N called “tail”

3 August August August 2015 Anna University, Chennai Operations on Lists  MakeEmpty  PrintList  Find  FindKth  Insert  Delete  Next  Previous

3 August August August 2015 Anna University, Chennai List – An Example  The elements of a list are 34, 12, 52, 16, 12 Find (52) -> 3 Insert (20, 4) -> 34, 12, 52, 20, 16, 12 Delete (52) -> 34, 12, 20, 16, 12 FindKth (3) -> 20

3 August August August 2015 Anna University, Chennai List - Implementation  Lists can be implemented using: Arrays Linked List Cursor [Linked List using Arrays]

3 August August August 2015 Anna University, Chennai Arrays  Array is a static data structure that represents a collection of fixed number of homogeneous data items or  A fixed-size indexed sequence of elements, all of the same type.  The individual elements are typically stored in consecutive memory locations.  The length of the array is determined when the array is created, and cannot be changed.

3 August August August 2015 Anna University, Chennai Arrays  Any component of the array can be inspected or updated by using its index. This is an efficient operation O(1) = constant time  The array indices may be integers (C, Java) or other discrete data types (Pascal, Ada).  The lower bound may be zero (C, Java), one (Fortran), or chosen by the programmer (Pascal, Ada)

3 August August August 2015 Anna University, Chennai Different Types of Arrays  One-dimensional array: only one index is used  Multi-dimensional array: array involving more than one index  Static array: the compiler determines how memory will be allocated for the array  Dynamic array: memory allocation takes place during execution

3 August August August 2015 Anna University, Chennai One Dimensional Static Array  Syntax: ElementType arrayName [CAPACITY]; ElementType arrayName [CAPACITY] = { initializer_list };  Example in C++: int b [5]; int b [5] = {19, 68, 12, 45, 72};

3 August August August 2015 Anna University, Chennai Array Output Function void display(int array[],int num_values) { for (int I = 0; i<num_values; i++) cout<< array[i] << “ ”; }

3 August August August 2015 Anna University, Chennai List Implemented Using Array

3 August August August 2015 Anna University, Chennai Operations On Lists  We’ll consider only few operations and not all operations on Lists  Let us consider Insert  There are two possibilities: Ordered List Unordered List

3 August August August 2015 Anna University, Chennai Insertion into an Ordered List

3 August August August 2015 Anna University, Chennai Insertion in Detail

3 August August August 2015 Anna University, Chennai Insertion

3 August August August 2015 Anna University, Chennai Deletion

3 August August August 2015 Anna University, Chennai Find / Search  Searching is the process of looking for a specific element in an array  For example, discovering whether a certain score is included in a list of scores.  Searching, like sorting, is a common task in computer programming.  There are many algorithms and data structures devoted to searching.  The most common one is the linear search.

3 August August August 2015 Anna University, Chennai Linear Search  The linear search approach compares the given value with each element in the array.  The method continues to do so until the given value matches an element in the list or the list is exhausted without a match being found.  If a match is made, the linear search returns the index of the element in the array that matches the key.  If no match is found, the search returns -1.

3 August August August 2015 Anna University, Chennai Linear Search

3 August August August 2015 Anna University, Chennai Linear Search Function int LinearSearch (int a[], int n, int key) { int i; for(i=0; i<n; i++) { if (a[i] == key) return i; } return -1; }

3 August August August 2015 Anna University, Chennai Using the Function  LinearSearch (a,n,item,loc)  Here "a" is an array of the size n.  This algorithm finds the location of the element "item" in the array "a".  If search item is found, it sets loc to the index of the element; otherwise, it sets loc to -1  index=linearsearch(array, num, key)

3 August August August 2015 Anna University, Chennai PrintList Operation int myArray [5] = { 19,68,12,45,72 }; /* To print all the elements of the array for (int i=0;i<5;i++) { printf("%d", myArray[i]); }

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Implementing Deletion

3 August August August 2015 Anna University, Chennai Deletion - Another Method

3 August August August 2015 Anna University, Chennai PrintList O(N) Find Insert O(N) (on avarage half Delete needs to be moved) FindKth Next O(1) Previous Operations Running Times

3 August August August 2015 Anna University, Chennai Disadvantages of Using Arrays  Need to define a size for array High overestimate (waste of space)  insertion and deletion is very slow need to move elements of the list  redundant memory space it is difficult to estimate the size of array

3 August August August 2015 Anna University, Chennai Linked List  Series of nodes not adjacent in memory contain the element and a pointer to a node containing its succesor  Avoids the linear cost of insertion and deletion!

3 August August August 2015 Anna University, Chennai Singly Linked List

3 August August August 2015 Anna University, Chennai Doubly Linked List

3 August August August 2015 Anna University, Chennai Singly Linked List

3 August August August 2015 Anna University, Chennai Singly-linked List - Addition  Insertion into a singly-linked list has two special cases.  It's insertion a new node before the head (to the very beginning of the list) and after the tail (to the very end of the list).  In any other case, new node is inserted in the middle of the list and so, has a predecessor and successor in the list.

3 August August August 2015 Anna University, Chennai Empty list case  When list is empty, which is indicated by (head == NULL) condition, the insertion is quite simple.  Algorithm sets both head and tail to point to the new node.

3 August August August 2015 Anna University, Chennai Add first  In this case, new node is inserted right before the current head node.

3 August August August 2015 Anna University, Chennai Add First - Step 1  It can be done in two steps: Update the next link of the new node, to point to the current head node.

3 August August August 2015 Anna University, Chennai Add First - Step 2 Update head link to point to the new node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Add last  In this case, new node is inserted right after the current tail node.  It can be done in two steps: Update the next link of the current tail node, to point to the new node. Update tail link to point to the new node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Insert - General Case  In general case, new node is always inserted between two nodes, which are already in the list. Head and tail links are not updated in this case.  We need to know two nodes "Previous" and "Next", between which we want to insert the new node.  This also can be done in two steps: Update link of the "previous" node, to point to the new node. Update link of the new node, to point to the "next" node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Singly-linked List - Deletion  There are four cases, which can occur while removing the node.  We have the same four situations, but the order of algorithm actions is opposite.  Notice, that removal algorithm includes the disposal of the deleted node - unnecessary in languages with automatic garbage collection (Java).

3 August August August 2015 Anna University, Chennai List has only one node  When list has only one node, that the head points to the same node as the tail, the removal is quite simple.  Algorithm disposes the node, pointed by head (or tail) and sets both head and tail to NULL.

3 August August August 2015 Anna University, Chennai Remove First  In this case, first node (current head node) is removed from the list.  It can be done in two steps: Update head link to point to the node, next to the head. Dispose removed node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Remove Last  In this case, last node (current tail node) is removed from the list. This operation is a bit more tricky, than removing the first node, because algorithm should find a node, which is previous to the tail first.  It can be done in three steps: Update tail link to point to the node, before the tail. In order to find it, list should be traversed first, beginning from the head. Set next link of the new tail to NULL. Dispose removed node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Remove - General Case  In general case, node to be removed is always located between two list nodes. Head and tail links are not updated in this case.  We need to know two nodes "Previous" and "Next", of the node which we want to delete.  Such a removal can be done in two steps: Update next link of the previous node, to point to the next node, relative to the removed node. Dispose removed node.

3 August August August 2015 Anna University, Chennai

3 August August August 2015 Anna University, Chennai Advantages of Using Linked Lists  Need to know where the first node is the rest of the nodes can be accessed  No need to move the elements in the list for insertion and deletion operations  No memory waste

3 August August August 2015 Anna University, Chennai Cursor Implementation Problems with linked list implementation:  Same language do not support pointers! Then how can you use linked lists ?  new and free operations are slow Actually not constant time  SOLUTION: Implement linked list on an array - called CURSOR

3 August August August 2015 Anna University, Chennai Cursor Implementation - Diagram

3 August August August 2015 Anna University, Chennai Cursor Implementation If L = 5, then L represents list (A, B, E) If M = 3, then M represents list (C, D, F)

3 August August August 2015 Anna University, Chennai Arrays - Pros and Cons  Pros Directly supported by C Provides random access  Cons Size determined at compile time Inserting and deleting elements is time consuming

3 August August August 2015 Anna University, Chennai Linked Lists - Pros and Cons  Pros Size determined during runtime Inserting and deleting elements is quick  Cons No random access User must provide programming support

3 August August August 2015 Anna University, Chennai Application of Lists  Lists can be used  To store the records sequentially  For creation of stacks and queues  For polynomial handling  To maintain the sequence of operations for do / undo in software  To keep track of the history of web sites visited

3 August August August 2015 Anna University, Chennai Why Doubly Linked List ?  given only the pointer location, we cannot access its predecessor in the list.  Another task that is difficult to perform on a linear linked list is traversing the list in reverse.  Doubly linked list A linked list in which each node is linked to both its successor and its predecessor  In such a case, where we need to access the node that precedes a given node, a doubly linked list is useful.

3 August August August 2015 Anna University, Chennai Doubly Linked List  In a doubly linked list, the nodes are linked in both directions. Each node of a doubly linked list contains three parts: Info: the data stored in the node Next: the pointer to the following node Back: the pointer to the preceding node

3 August August August 2015 Anna University, Chennai Operations on Doubly Linked Lists  The algorithms for the insertion and deletion operations on a doubly linked list are somewhat more complicated than the corresponding operations on a singly linked list.  The reason is clear: There are more pointers to keep track of in a doubly linked list.

3 August August August 2015 Anna University, Chennai Inserting Item  As an example, consider the Inserting an item.  To link the new node, after a given node, in a singly linked list, we need to change two pointers: newNode->next and location->next.  The same operation on a doubly linked list requires four pointer changes.

3 August August August 2015 Anna University, Chennai Singly Linked List Insertion

3 August August August 2015 Anna University, Chennai Doubly Linked List Insertion

3 August August August 2015 Anna University, Chennai The Order is Important

3 August August August 2015 Anna University, Chennai Doubly Linked List - Deletion  One useful feature of a doubly linked list is its elimination of the need for a pointer to a node's predecessor to delete the node.  Through the back member, we can alter the next member of the preceding node to make it jump over the unwanted node.  Then we make the back pointer of the succeeding node point to the preceding node.

3 August August August 2015 Anna University, Chennai Doubly Linked List - Deletion

3 August August August 2015 Anna University, Chennai Special Cases of Deletion  We do, however, have to be careful about the end cases: If location->back is NULL, we are deleting the first node if location->next is NULL, we are deleting the last node. If both location->back and location->next are NULL, we are deleting the only node.

3 August August August 2015 Anna University, Chennai Interaction