Data Strcutures.

Slides:



Advertisements
Similar presentations
Linked Lists Linked Lists Representation Traversing a Linked List
Advertisements

CHP-5 LinkedList.
Chapter 19: Searching and Sorting Algorithms
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.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Chapter 8 Arrays and Strings
C o n f i d e n t i a l Developed By Nitendra HOME NEXT Subject Name: Data Structure Using C Unit Title: Searching Methods.
CHAPTER 71 TREE. Binary Tree A binary tree T is a finite set of one or more nodes such that: (a) T is empty or (b) There is a specially designated node.
ARRAYS, RECORDS AND POINTER
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Array.
Chapter 8 Arrays and Strings
 Data is always stored in a logical way so that it can be accessed efficiently. Ex:A telephone directory  The way data is stored is called the structure.
Chapter 2 ARRAYS.
Course Teacher: Moona Kanwal
Arrays.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
DATA STRUCTURE & ALGORITHMS (BCS 1223) CHAPTER 8 : SEARCHING.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
CSC 211 Data Structures Lecture 13
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 Structure Introduction.
Lecture -3 on Data structures Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Array Data structures are classified as either linear or nonlinear.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
NURUL HASLINDA NGAH BP-37 SEMESTER II 2013/2014.  At the end of this chapter you’ll be learnt about:  Binary tree  Binary search tree  Traversing.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Data Structure and Algorithms
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
INTRODUCTION OF ARRAY. Topics To Be Discussed………………………. Introduction Types of array One Dimensional Array Internal representation of one-dimensional array.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Two dimensional arrays A two dimensional m x n array A is a collection of m. n elements such that each element is specified by a pair of integers (such.
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.
1. Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Part 2. Deletion from a linked list Let LIST be a linked list with a node N between nodes A and B. suppose node N is to be deleted from the linked list.
LIST Unsorted 1.Set PTR := START 2.Repeat Step 3 while PTR=NULL 3.If ITEM = INFO[PTR], then : Set LOC := PTR and Exit else Set PTR:=LINK[PTR] [PTR points.
1 Linked list. 1 A linked list, or one-way list, is a linear collection of data elements, called nodes Each node is divided into two parts: * first part.
Data Structures I (CPCS-204)
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
Computer Programming BCT 1113
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Lecture - 6 On Data Structures
Operation performed by Linear Structure
Linked-list.
Data Structures Interview / VIVA Questions and Answers
Arrays.
Linear and Binary Search
Introduction to Data Structures
ARRAYS, RECORDS AND POINTER
Data Structures (CS212D) Week # 2: Arrays.
Data Structures: Searching
Arrays Week 2.
Presentation transcript:

Data Strcutures

Traversing a linear array Here A is a linear array with lower bound LB and upper bound UB. This algorithm traverses A applying an operation PROCESS to each element of A. [Initilize counter] Set K = LB. Repeat steps 3 and 4 while K ≤ UB. [Visit element] Apply PROCESS to A[K]. [Increase counter] Set K = K + 1. [End of step 2 loop] Exit

Inserting into a linear array INSERT (A, N, K, ITEM) Here A is a linear array with N elements and K is a positive integer such that K ≤ N. This algorithm inserts an element ITEM into the Kth position in A. [Initialize counter] Set J = N. Repeat steps 3 and 4 while J ≥ K. [Move Jth element downward] Set A[J + 1 ] = A[J] [Decrease counter] Set J = J – 1 [End of step 2 loop] [Insert element] Set A[K] == ITEM [Reset N] Set N = N + 1 Exit

Deleting from a linear array DELETE (A, N, K, ITEM) Here A is a linear array with N elements and K is a positive integer such that K ≤ N. This algorithm deletes the Kth element from A. Set ITEM = A[K] Repeat fro J = K to N – 1 [Move J + 1st element upward] Set A[J] = A[J + 1] [End of loop] [Reset the number N of elements in A] Set N = N – 1 Exit

Bubble Sort Exit BUBBLE(DATA, N) Here DATA is an array with N elements. This algorithm sorts the elements in DATA. Repeat steps 2 and 3 for K = 1 to N – 1 Set PTR = 1 [Initialize pass pointer PTR] Repeat while PTR ≤ N – K [Executes pass] If DATA[PTR] > DATA[PTR + 1], then Interchange DATA[PTR] and DATA[PTR + 1] [End of If structure] Set PTR = PTR + 1 [End of inner loop] [End of step 1 outer loop] Exit

Linear Search LINEAR(DATA, N, ITEM, LOC) Here DATA is a linear array with N elements, and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA, or sets LOC = 0 if the search is unsuccessful. [Insert ITEM at the end of DATA] Set DATA[N+1] = ITEM [Initialize counter] Set LOC = 1 [Search for ITEM] Repeat while DATA[LOC] ≠ ITEM Set LOC = LOC + 1 [End of loop] [Successful?] If LOC = N+1, then: Set Loc = 0 Exit

Binary Search BINARY(DATA, LB, UB, ITEM, LOC) Here DATA is a sorted array with lower bound LB and upper bound UB, and ITEM is a given item of information. The variables BEG, END and MID denote, respectively, the beginning, end and middle locations of a segment of elements of DATA. This algorithm finds the location LOC of ITEM in DATA or sets LOC = NULL. [Initialize segment variables] Set BEG = LB, END = UB, and MID = INT((BEG+END)/2). Repeat steps 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM If ITEM < DATA[MID], then: Set END = MID – 1 Else Set BEG = MID + 1 [End of If structure]

Binary Search (Contd.) Exit Set MID = INT((BEG + END)/2) [End of step 2 loop] If DATA[MID] = ITEM, then: Set LOC = MID Else Set LOC = NULL [End of If structure] Exit

Multidimensional Arrays Most programming languages allow two-dimensional and three-dimensional arrays, i.e., arrays where elements are referenced, respectively, by two and three subscripts. In fact, some programming languages allow the number of dimensions for an array to be as high as 7. A two dimensional m×n array A is a collection of m.n data elements such that each element is specified by a pair of integers (such as J, K), called subscripts, with the property that 1 ≤ J ≤ m and 1 ≤ K ≤ n

Multidimensional Arrays (Contd.) The element of A with first subscript j and second subscript k will be denoted by AJ,K or A[J,K] Two dimensional arrays are called matrices in mathematics and tables in business applications; hence two dimensional arrays are sometimes called matrix arrays.

Multidimensional Arrays (Contd.) There is a standard way of drawing a two-dimensional m×n array A where the elements of A form a rectangular array with m rows and n columns and where the element A[J, K] appears in row J and column K. Columns 1 2 3 4 A[1, 1] A[1, 2] A[1, 3] A[1, 4] A[2, 1] A[2, 2] A[2, 3] A[2, 4] A[3, 1] A[3, 2] A[3, 3] A[3, 4] Rows Two-dimensional 3×4 array A

Multidimensional Arrays (Specification) Suppose A is a two-dimensional m×n array. The first dimension of A contains the index set 1, …….., m, with lower bound 1 and upper bound m; and the second dimension of A contains the index set 1, 2, …….., n, with lower bound 1 and upper bound n. the length of a dimension is the number of integers in its index set. The pair of lengths m×n (read “m by n”) is called the size of the array.

Multidimensional Arrays (Length) The length of a given dimension (i.e., the number of integers in its index set) can be obtained from the formula Length = upper bound – lower bound + 1

Representation of Two-Dimensional Arrays in memory Let A be a two-dimensional m×n array. Although A is pictured as a rectangular array of elements with m rows and n columns, the array will be represented in memory by a block of m.n sequential memory locations. Specifically, the programming language will store the array A either Column by column, what is called column major order, or Row by row, in row major order. The figure shows these two ways when A is a two-dimensional 3×4 array. We emphasize that the particular representation used depends upon the programming language, not the user.

Representation of Two-Dimensional Arrays in memory Subscript (1, 1) (2, 1) Col 1 (3, 1) (1, 2) (2, 2) Col 2 (3, 2) (1, 3) (2, 3) Col 3 (3, 3) (1, 4) (2, 4) Col 4 (3, 4) A A Subscript (1, 1) (1, 2) Row 1 (1, 3) (1, 4) (2, 2) Row 2 (2, 3) (2, 4) (3, 1) (3, 2) Row 3 (3, 4) a) Column major order b) Row major order

Drawbacks of Array The address of an element in an array can be easily computed because it occupies successive memory locations. It is relatively expensive to insert and delete elements in an array. Since an array usually occupies a block of memory space, one cannot simply double or triple the size of an array when additional space is required. For this reason, arrays are called dense lists and are said to be static data structures.

Linked Lists Another way of storing a list in memory is to have each element in the list contain a field, called a link or pointer, which contains the address of the next element in the list. Thus successive elements in the list need not occupy adjacent space in memory. This will make it easier to insert and delete elements in the list. This type of data structure is called linked list.

Linked Lists (Contd.) A linked list, or one-way list, is a linear collection of data elements, called nodes, where the linear order is given by means of pointers. That is, each node is divided into two parts; the first part contains the information of the element, and the second part, called the link field or nestpointer field, contains the address of the nest node in the list. In particular, a linked list implementation associates with each list element Si a pointer LINKi to indicate the address at which Si+1 is stored.

Linked Lists (Contd.) Figure on slide 20 is a schematic diagram of a linked list with 6 nodes. Each node is pictured with two parts. The left part represents the information part of the node, which may contain an entire record of data items (e.g., NAME, ADDRESS, …..). The right part represents the nextpointer field of the node, and there is an arrow drawn from it to the next node in the list. The pointer of the last node contains a special value, called the null pointer, which is any invalid address, denoted by × which signals the end of the list. The linked list also contains a list pointer variable – called START or NAME – which contains the address of the first node in the list; hence there is an arrow drawn from START to the first node. A special case is the list that has no nodes. Such a list is called the null list or empty list and is denoted by the null pointer in the variable START.

Linked Lists (Contd.)

Linked Lists (Example) START Bed # Patient 1 Kirk 2 3 Dean 4 Maxwell 5 Adams 6 7 Lane 8 Green 9 Samuels 10 11 Fields 12 Nelson Next 7 11 12 3 4 1 8 9 5

Representation of Linked Lists in Memory Let LIST be a linked list. Then LIST will be maintained in memory, unless otherwise specified or implied, as follows. First of all, LIST requires two linear arrays – we will call them here INFO and LINK – such that INFO[K] and LINK[K] contain, respectively, the information part and the nextpointer field of a node of LIST. LIST also requires a variable name – such as START – which contains the location of the beginning of the list, and a nextpointer sentinel – denoted by NULL – which indicates the end of the list. The nodes of a list need not occupy adjacent elements in the arrays INFO and LINK, and that more than one list may be maintained in the same linear arrays INOF and LINK. However, each list must have its own pointer variable giving the location of its first node.

Linked Lists (Examples) START START O T X N I E 6 11 10 3 4 7 LINK 1 2 3 4 5 6 7 8 9 10 11 12 9

Linked Lists (Examples) TEST 74 82 84 78 100 88 62 93 14 12 8 13 2 7 6 4 LINK ALG 1 2 3 4 5 6 7 8 9 10 11 12 13 14 11 GEOM 5

Traversing a Linked List Let LIST be a linked list in memory. This algorithm traverses LIST, applying an operation PROCESS to each element of LIST. The variable PTR points to the node currently being processed. Set PTR = START [Initializes pointer PTR] Repeat steps 3 and 4 while PTR ≠ NULL Apply PROCESS to INFO[PTR] Set PTR = LINK[PTR] [ PTR now points to the nest node] [End of step 2 loop] Exit

Searching a linked list (List is unsorted) SEARCH(INFO, LINK, START, ITEM, LOC) LIST is a linked list in memory. This algorithm finds the location LOC of the node where ITEM first appears in LIST, or sets LOC = NULL. Set PTR = START Repeat step 3 while PTR ≠ NULL If ITEM = INFO[PTR], then Set LOC = PTR and Exit Else Set PTR = LINK[PTR] [ PTR now points to the next node] [Search is unsuccessful] Set LOC = NULL Exit

Searching a linked list (List is sorted) SRCHSL(INFO, LINK, START, ITEM, LOC) LIST is a sorted list in memory. This algorithm finds the location LOC of the node where ITEM first appears in LIST, or sets LOC = NULL. Set PTR =START Repeat step 3 while PTR ≠ NULL If ITEM > INFO[PTR], then Set PTR = LINK[PTR] [PTR now points to the next node] Else Set LOC = NULL and Exit [ITEM now exceeds INFO[PTR]] [End of If structure] [End of step 2 loop] Set LOC = NULL Exit