You are on page 1of 37
6.1.1 Comparison between Array and Linked Lists ‘Array data structure is simple to use and it is supported by almost all programming languages. It is very simple to understand and time to access any element from an array is constant - — Simple to use = Simple to define Properties of array = Constant access time data structure = Mapping by compiler. ‘An array element can de accessed by afi], where « is the name of the array and i is the index. Compiler maps afi] to its physical location in memory. Address of afi] is given by starting adress of a + ix size of array element in bytes. This mapping is carried out in constant time, inrespective of which element is accessed. Army data structure suffers from some severe limitations : = Sizeof an array is defined atthe time of programming. — Insertion and deletion is time consuming. = Requescotgnons m0. Fi esi an ary as tobe fond ese prog: hing we se cle ing om 0 the progen. Ths mews ‘fl te pogenmer bs 1 te & dein ging hau ot of at, Ihe etal ano a sted es tn emia i oa enw vate nn be ead sl du cuot be ane. To ao the La of inven ad et, ve ed to en at he it sired contiguous, swore ei pats OT wind oe nove. Advantages of linked lists Linked list is an example of ‘dynamic data structure. ‘They can grow and shrink during execution of the program. Representation of linear data structure. (linear data like polynomial, stack and queue can easily be represented using linked list. Efficient memory utilization. Memory is not pré-allocated like static data structure. Memory is allocated as per the need. Memory is deallocated when it is no longer needed. Insertion and deletions are easier and efficient. Insertion and deletion of a given data can be carried out in constant time. 5.1.2 Representation > (MSBTE : W-09, W-12, S-17) The linked list consists of a series of structures. They are not ‘scuired to be stored in adjacent memory locations, Each structure sexsists of a data field and address field. Address field contains the ‘sizes of its successors. Fig. 5.1.1 shows the actual representation Se stmcture, A variable of the above structure type is ct =n as a node, Fig, 5.1.2 gives a representation of a linked list p Sree nodes. node A ode C Fig. 5.1.2: Linked list 4 list consisting of three data x,, x), x; is represented using @ 4 list. Node A stores the data x, and the address of the (next) node B. Node B stores the data x, and the address ssccessor node C. Node C contains the data x, and its address = sounded (NULL pointer), indicating it does not have a ce B neo Cc Est oo +— Memory address Memory representation of a linked list .3 gives a memory represeniation of the linked list 1.2, Nodes A, B and C happen to reside at memory }, 1000 and 2000 respectively. x, = 5, %, = 10 and Node A resides at the memory location 500, its data field = = value 3 and its address field contains 1000, which is the ss successor node, Address field of node C contains 0 as maccessor. 5.1.3 Implementation Ibis a self referential structure in “C”.Itis assumed thatthe > (MSBTE : W-10, 5-12, W-12,$-14, type of data to be stored in cach node is predefined to be of integer SA5,WA6,WAT) type, Next field ofthe siructure is 8 pointer type variable, wood for Q.5:1.3 Dati inked ist terminotogy-node. =| storing address of its successor node. Nodes are manipulated -(Reler section 6.1.3) z BEBE) during run-time. A programming language must provide following |B.41.4 Define te term adskess, and noxt pote for | facilities forrun time manipslation of nodes -_ linked lst, (Refer section 5.1.2) 5.1.5 Define no erm nex pint oflinked lst. eter sectons3.3) — need forthe acquized memory is over, 2.54.8 Define the wire potter and NULL painar (Ans.: Refer sectons 5.1.9.and5.14) [Eid — Acquiring memory for storage during run-time — Freeing memory during execution of the program, once the Structures in “C” can be used to define a node. Address of the successor aode an be stored in 2 pointer type variable, ‘ppedef simiet node. iat data; etme tee (es Te *C” programming language, memory can be acquired. ugh the ust of standard library functions malloe( ) and : oe oe). Memory aoqied during anime can be freed though a Ee Plaats In the program segment above, the line | declares a variable (2a) Pe(aode#) mallo(eoote)) P with the storage class node *, It can store the address of a node (3a) Pdata= S$; that has been created dynamically. sizeof(node) in line 2 is storage foga) Pynext=NULL requirement in number of bytes to store a node malloc(sizeof{node)) returns the address of the allocated memor; block and it is assigned to variable P. The address returned by malloc(sizeotinode)) is type casted using type casting operator(node *) before assigning it to the pointer P P— data = 5, in line 3 stores a value of 5 in the data field 0 node whose address is stored in pointer P. P —> next = NULL stores a value of NULL in the next field of the node whose addres: is stored in the pointer P. © A linked list with the address of the starting node head ee Oe + A linked list with the address of the head node in array is referenced by its starting address, a linked list is known by the address of its starting node. Address of the starting, node is stored in a pointer variable (node * head) head. All manipulations on the linked list can be performed through the address of the starting node. Through the variable “head”, first node can be accessed and through the address of the second node stored in the next field of the first node, second node can be accessed and so on. head [nsertion into a linked lst requires obtaining @ new node then changing values of two pointers. The general idea is shown Fig 51.6, Te dashed line represents the old pointer [tis chang to point fo new node, Deletion of a node can be performed in o pointer change. Fig. 5.1.7 shows the result of deleting the containing X,. The next field of the node containing 2, i chang to point to the node containing x,, Node containing x, is fi using the brary function free ) subsequently. Linked list is used to represent lst of daia using dy cite structure, > Definition of dynamic memory allocation "2.5.1.8 Define dynamic memory allocation State its ‘C’ programming also uses dynamic memory allocation. can grow and shrink during execution of the program. Linked + used to represent a list of data using dynamic data structure. s standard library function malloc( ), calloc( ) realloc( ) and Importance of dynamic memory allocation ‘Memory gets assigned at run time hence it is called as Synamic memory allocation. Memory is not allocated at the time of loading the program, it % allocated whenever required and can be de-allocated whenever not needed. Distinguish between singly linked list and doubly inked list. (4 points) 519 Distinguish between singly linked (lst and doubly linked list. (4 points) (Reter section 51.3) om = Singly linked list | Doubly linked tist Dak & singly linked list we ¢ in one direction. In Doubly linked list we traverse in two directions. 4. | Applieation Stack canbe | implemented using SLL Application : Stock and bash table can be imiplemented using DLL. node holds one fields. Each node holds two pointer fields. S singly linked list each sede contains data and In doubly linked list, each node contains data link to next and link tp previous one, 5.1.4 Types of Linked List => (MSBTE : 5-08, S-17) 5.1.10 List types of linked list. (Refer secton5.14) : Ear 1.11 Draw representation of linear linked list, circular linked list and doubly Inked fist. {Refer section 5.1.4) — = S17 Types of Linked List 4. Singly Linked List 2, Doubly Linked List 3. A Ciroular Linked List Fig. 5.1.8 : Types of linked list 5.1.4(A) Singly Linked List In this type of linked list two successive nodes of the linked list ere linked with each other in sequential linear manner. ‘Movement in forward direction is possible 5.1.4(B) Doubly Linked List In this type of linked list each node holds »wo-pointer fields In doubly linked list addresses of next as well as preceding clemenis are linked with current node. Fig. 5.1.10 : Doubly linked list

You might also like