Array implementation and linked list as datat structure
drkredsight1
27,502 views
33 slides
Sep 16, 2012
Slide 1 of 33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
About This Presentation
No description available for this slideshow.
Size: 309.18 KB
Language: en
Added: Sep 16, 2012
Slides: 33 pages
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