Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures & Algorithms

Similar presentations


Presentation on theme: "Data Structures & Algorithms"— Presentation transcript:

1 Data Structures & Algorithms
Instructor Name: Lecture-06

2 Today’s Lecture Arrays Background Knowledge Basic Operation on Arrays
List Data Structure List Implementation Analysis of Array List List Using Linked Memory Linked List 2

3 Arrays Data Structure What is an Array?
Array is a container which can hold fix number of items and these items should be of same type. Most of the data structure make use of array to implement their algorithms. Following are important terms to understand the concepts of Array. Element − Each item stored in an array is called an element. Index − Each location of an element in an array has a numerical index which is used to identify the element. Consider following program for Array declaration and its representation in memory main( ) { int x[10]={35,33,42,10,14,19,27,44,26,31}; int j; for(j = 0; j < 6; j++) x[j] = 2 * j; } 3

4 Arrays Data Structure Array Representation
As per above shown illustration, following are the important points to be considered. Index starts with 0. Array length is 10 which means it can store 10 elements. Each element can be accessed via its index. For example, we can fetch element at index 6 as 27 (Array Manipulation). Array occupies contiguous memory area in the computer In case of the above example, if some location is assigned to x[0], the next location can not contain data other than x[1]. 4

5 Arrays Data Structure Array Data Structure Issues
Want to use an array data structure but may lack the information about the size of the array at compile time For example you have to store telephone directory or store the names of total population of a city or a country If you initially assign a very large chunk of memory and store very few information – what will happen? If you initially store very small chunk of memory and your requirement increases with the passage of time – what will happen? Misuse of resource What are the possible solutions? 5

6 Arrays Data Structure Array creation at run time int* y = new int[20];
Suppose you need an integer array of size n after the execution of the program. If it is known at the execution of the program that an array of size 20 or 30 is needed, it is allocated dynamically (cf Dynamic Array in Programming Fundamental, Lecture 19) . The programming statement is as follows: int* y = new int[20]; Computer will allocate twenty memory location at the time of execution. Again memory locations are contiguous. the new key word returns the memory address of first of twenty locations and store that address into y. Use following statement to release memory because y is allocated memory using new delete[] y; 6

7 Arrays Data Structure Operations performed o Array Data Structure
Following are the basic operations supported by an array. Traverse − print all the array elements one by one. Insertion − add an element at given index. Deletion − delete an element at given index. Search − search an element using given index or by value. Update − update an element at given index. 7

8 List Data Structure What is List Data Structure?
The List data structure is among the most generic of data structure Have you ever used List in your daily life? – List of groceries A list is the collection of items of the same type (grocery items, integers, names). The same is try for Array – then what is the difference? List store data in some order and this order will be reserved when data is entered into the list. The property of List is the sequence used to store data. Data may be inserted at various location and may be removed from different locations but in all these operation execution the ordered in which data is stored must retain. Some times order is due to sorting and some time due to importance of data items. 8

9 List Data Structure Operations on List Data Structure
Following are the operations that can be performed on List data structure 9

10 List Data Structure Understanding Particular Position (?)
Two possibilities Use the actual index of element: i.e. insert it after element 3, get element number 6. This approach is used with arrays ƒUse a “current” marker or pointer to refer to a particular position in the list. In the second option we do not use first, second etc for position but say wherever is the current pointer. Just think of a pointer in the list that we can move forward or backward. When we say get, insert or update while using the current pointer, it means that wherever is the current pointer, get data from that position, insert data after that position or update the data at that position. In this case, we need not to use numbers. But it is our responsibility that current pointer is used in a proper way. 10

11 List Data Structure Methods used with “current” Pointer
Following are the four methods useful if we use “current” marker 11

12 List Data Structure Implementation
Lets Start implementation of List of Integers (2,6,8,7,1) The methods of list can be implemented with the use of an array inside add Method Suppose there is call to add an element in the list , say add(9) Where the element 9 will be inserted in the list? – current position What will be the new list after adding 9 in the list? New List (2,6,8,9,7,1) 12

13 List Data Structure add Method - Implementation How it will possible?
Making space by shifting all elements on the right of 8 to one place on right. After creating space put the element 9 at position 4 and set the current position to 4 Note the size of list is changed and current position is also changed. The size here shows the number of elements in the list, the actual size of list have defined already to fix the size which may be 50 or 100 or may be 1000. 13

14 List Data Structure next method Implementation
Next method simply moves the current position to next position in the list Two variables-current and size use to store the position of current pointer and the number of elements in the list. Values of these variable tells the state of the list. The method next is used to know about the boundary conditions of the list i.e. the array being used by us to implement the list. Suppose we use an array of size 100 to implement list. What happened if we try to add 101st element? Similarly if we move pointer backward, what happened if we try to move backward from first position? These situations are known as boundary conditions These boundary conditions must be handled carefully. 14

15 List Data Structure remove method Implementation
remove method removes the element from the current position Consider previous list (2,6,8,9,7,1) with six elements with current position 5 How blank space will fill? Fill blank space by moving all elements on the right of position 5 to one position left. Current pointer in this case will not be changed and it still points 5 15

16 List Data Structure find method Implementation
find method is used to find a specific element in the list Pass the element as an argument to method int find (int x) { int j ; for (j = 1; j < size + 1; j++ ) if (A[j] == x ) break ; if ( j < size + 1) // x is found current = j ; //current points to the position where x found return 1 ; // return true } return 0 ; //return false, x is not found 16

17 List Data Structure other methods Implementation
The other method implementation may be done in class or taken as non graded home assignment Think about the following methods as they will be required to implement in Lab or Assignment Get() Update(X) Length() Previous() Start() End() 17

18 Analysis of Array List Efficiency of any algorithm depends on the time consumption of CPU add Method Worst Case – add at start – involves loop Best Case – add at end remove Method Worst Case – remove from start – involves loop Best Case – remove from end Average case – moves half of the elements – it does not mean that every time you will move half elements - its just average. find Method Worst Case – Element to find is at the end of List and we start from beginning Best Case – Element to find is at the current position All other method perform their task only in one step. 18

19 Limitation of List using Array
Limitations / Drawback of List using Array Size must be known in advance During execution size can not be change and if required we need to recompile code with new size in case of static memory Suppose we are using dynamic memory and we initially created a list of size with the use of new operator. If we need 200 element at later we will release previous array and create new with size 200 Before releasing we need to copy data to avoid data loss These are very challenging problems faced by the programmer The solution to this kind of problem is List using linked Memory 19

20 List using Linked Memory
Main Features Memory is not contiguous Each cell of memory not only contains the data but also the information where the next element of the list is located in the memory. Next element not necessarily be at the very next location – it may be anywhere in memory Location of next cell can be maintained by holding the memory address of next element A programmer needs to keep track of these memory addresses 20

21 Linked List What is a Linked List How to form a Linked List
A linked-list is a sequence of data structures which are connected together via links. Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list the second most used data structure after array. How to form a Linked List To form a linked list, at first, we define a node. A node comprises two fields. i.e. the object field that holds the actual list element and the next that holds the starting location of the next node. 21

22 Linked List Methods of Linked List
The example we discuss with array will be represented as below when used with linked memory A chain of nodes where next part of each node holds the address of next node forms linked list. 22

23 Linked List Different pointers in linked list
As in Arrays we knew that index started from 1, similarly in linked list we need to know the starting point of the list For this purpose, we have a pointer head that points to the first node of the list. If we don’t use head, it will not be possible to know the starting position of the list. We also have a pointer current to point to the current node of the list. We need this pointer to add or remove current node from the list. The next field of the last node points to nothing .It is the end of the list. We place the memory address NULL in the last node. NULL is an invalid address and is inaccessible. 23

24 Linked List linked list in memory 24

25 Lab -01 Lab-01 regarding Arrays is uploaded. Before coming to lab it is expected that you have implemented the code. We will discuss the issues regarding the lab tasks. Lab exercises are very important so perform all the tasks We will continue with Linked List in the next lecture 25

26 26


Download ppt "Data Structures & Algorithms"

Similar presentations


Ads by Google