1 / 30

Data Strcutures

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.

hateya
Télécharger la présentation

Data Strcutures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Strcutures

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

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

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

  5. Bubble Sort 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

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

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

  8. Binary Search (Contd.) • 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

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

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

  11. 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 Rows Two-dimensional 3×4 array A

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

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

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

  15. Representation of Two-Dimensional Arrays in memory A A a) Column major order b) Row major order

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

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

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

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

  20. Linked Lists (Contd.)

  21. Linked Lists (Example) START 5

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

  23. Linked Lists (Examples) LINK START START 9

  24. Linked Lists (Examples) LINK TEST ALG 11 GEOM 5

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

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

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

More Related