Array implementation and linked list as datat structure

drkredsight1 27,502 views 33 slides Sep 16, 2012
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

No description available for this slideshow.


Slide Content

ARRAY IMPLEMENTATION AND LINKED LIST AS A DATA STRUCTURE PRESENTED BY: AKSHAY WADALKAR

An array is a collection of elements of similar datatype. Contiguous memory allocation takes place. An array is a DS in which we can access every element directly using position variable . It is rather an organizational concept. Array elements can be accessed individually. Syntax: datatype nameofarray [dimension]; ARRAY

Two types of array- Single dimensional single for loop. Multidimensional nesting of for loop. Types of array

Array can be of integer ,character and string. Integer and character array can be implemented by same logic Implementation of string array is quiet different from the two. We can study the array implementation using integer array. Single dimensional array

7 14 32 58 5 8 16 9 23 a[0] i =0 a[1] i =1 a[2] i =2 a[3] i =3 a[4] i =4 a[5] i =5 a[6] i =6 a[7] i =7 a[8] i =8 a[9] i =9 int a[10]={7,1,32,58,0,5,8,16,9,23}; Integer array “a”. It is of dimension 10 (from 0 to 9). Take positing variable i . Its storage will be continuous 20 bytes(2 bytes each). Creation of integer array

ALGORITHM FOR INSERTION (1 diamension ) DECLARATION N SIZE OPERATION Repeat for i = 0 to (size-1) arr [ i ]= num end repeat OUTPUT RETURN( arr [ i ])

DECLARATION i rows j coloumn OPERATION Repeat for i =0 to (rows-1) Repeat for j=0 to (coloumn-1) Array[ i ][j]=num End repeat End repeat OUTPUT Return(Array[ i ][j]) ALGORITHM FOR INSERTION (2 diamension )

No need to declare large number of variables individually. Variables are not scattered in memory , they are stored in contiguous memory. Ease the handling of large no of variables of same datatype. Advantages

Rigid structure. Can be hard to add/remove elements. Cannot be dynamically resized in most languages. Memory loss. LIMITATION

LINKED LIST AS A DATA STRUCTURE

Each element (node) inside a linked list is linked to the previous node and successor (next) node. This allows for more efficient insertion and deletion of nodes . WHAT IS A LINKED LIST? 5 3 14 2 continued

Each item has a data part (one or more data members), and a link that points to the next item. One natural way to implement the link is as a pointer; that is, the link is the address of the next item in the list. It makes good sense to view each item as an object, that is, as an instance of a class . We call that class: Node The last item does not point to anything. We set its link member to NULL . This is denoted graphically by a self-loop Definition Details

Insert a new item At the head of the list, or At the tail of the list, or Inside the list, in some designated position Search for an item in the list The item can be specified by position, or by some value Delete an item from the list Search for and locate the item, then remove the item, and finally adjust the surrounding pointers Operations on Linked Lists

Suppose you want to find the item whose data value is A You have to search sequentially starting from the head item rightward until the first item whose data member is equal to A is found . At each item searched, a comparison between the data member and A is performed. Searching for an Item

Since nodes in a linked list have no names, we use two pointers, pre (for previous) and cur (for current). At the beginning of the search, the pre pointer is null and the cur pointer points to the first node. The search algorithm moves the two pointers together towards the end of the list. LOGIC FOR SEARCHING A LINKED LIST

ALGORITHM Declaration Current 0 Searching for (current = first; current != NULL; current = current->next ) if ( searchItem == current(data)) return (current); Break Output return (NULL);

Example

Insertion of an Element at the Head :

Node x = new Node(); x.setElement(new String(“Baltimore”)); The following statement is not correct: x.element = new String(“Baltimore”));

x.setNext(head); head = x;

Deleting an Element at the Head :

head = head.getNext();

Insertion of an Element at the Tail :

Node x = new Node( ); x.setElement(new String(“Baltimore”)); x.setNext(null); tail.setNext(x); tail = x;

Deleting an Element at the Tail :

should be removed

Singly Linked Lists and Arrays

CS314 Linked Lists 31 Linked lists are dynamic, they can grow or shrink as necessary Linked lists are non-contiguous; the logical sequence of items in the structure is decoupled from any physical ordering in memory Advantages of linked lists

A linked list is a very efficient data structure for sorted list that will go through many insertions and deletions. A linked list is a dynamic data structure in which the list can start with no nodes and then grow as new nodes are needed. A node can be easily deleted without moving other nodes, as would be the case with an array. For example, a linked list could be used to hold the records of students in a school. Each quarter or semester, new students enroll in the school and some students leave or graduate. Applications of linked lists

THANK YOU…………….!!!