70
LINKED LIST C#ODE Studio || codstudio.wordpress.com

Linked List Lect-4

Embed Size (px)

DESCRIPTION

Files For C#ode Studio.Linked ListLovely Professional University

Citation preview

  • LINKED LISTC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Linked List- A linked list or one-way list is a linear collection of data elements, called nodes , where linear order is given by means of pointers. Each node is divided into two parts:The first part contains the information of the element.Second part, called the linked field or next pointer field, contains the address of the next node in the list.Representation of Linked list in memoryLinked list is maintained in memory by two linear arrays: INFO and LINK such that INFO[K] and LINK[K] contain respectively the information part and the next pointer field of node of LIST. LIST also requires a variable name such as START which contains the location of the beginning of the list and the next pointer denoted by NULL which indicates the end of the LIST.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • C#ODE Studio || codstudio.wordpress.com5START

    BED NUMBERPATIENT1Kirk23Dean4Maxwell5Adams67Lane8Green9Samuels1011Fields12Nelson

    NEXT7

    11123

    410

    89

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: (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. Variable PTR points to the node currently being processed.Step 1: Set PTR:= STARTStep 2: Repeat while PTR NULL: Apply PROCESS to INFO[PTR] Set PTR:= LINK[PTR] [PTR now points to next node] [End of Step 2 Loop]Step 3: Exit

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Searching an unsorted Linked ListAlgorithm: SEARCH( INFO,LINK,START,ITEM,LOC) LIST is a linked list in memory. This algorithm finds the location of LOC of the node where ITEM first appears in LIST , or sets LOC=NULLStep 1: Set PTR:=STARTStep 2: Repeat while PTR NULL If ITEM = INFO[PTR], then: Set LOC := PTR Return Else: Set PTR:= LINK[PTR] [End of If structure] [End of Step 2 Loop]Step 3: [Search is unsuccessful] Set LOC:=NULLStep 4: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Complexity of this algorithm is same as that of linear search algorithm for linear array. Worst case running time is proportional to number n of elements in LIST and the average case running time is proportional to n/2 with condition that ITEM appears once in LIST but with equal probability in any node of LIST.

    Binary search cannot be applied to linked list as there is no provision to locate the middle element of LIST. This property is one of the main drawback of using a linked list as a data structure.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Search in sorted listAlgorithm: SRCHSL(INFO,LINK,START,ITEM,LOC) LIST is sorted list (Sorted in ascending order) in memory. This algorithm finds the location LOC of the node where ITEM first appears in LIST or sets LOC=NULLStep 1: Set PTR:= STARTStep 2:Repeat while PTR NULL If ITEM > INFO[PTR], then: Set PTR := LINK[PTR] Else If ITEM = INFO[PTR], then: Set LOC := PTR Return Else: Set LOC:= NULL Return [End of If structure] C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • [End of step 2 Loop]Step 3: Set LOC:= NULLStep 4: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • C#ODE Studio || codstudio.wordpress.comSTARTINFO[PTR]LINK[PTR]

    C#ODE Studio || codstudio.wordpress.com

  • Insertion into a Linked List- Together with the linked list , a special list is maintained in memory which consist of unused memory cells. This list, which has its own pointer, is called the list of available space or the free storage list or the free pool. This list is called the Avail List. During insertion operation, new nodes are taken from this avail list which is maintained just like normal data linked list using its own pointer. Similar to START, Avail list also has its own start pointer named as AVAIL which stores the address of the first free node of avail list.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • C#ODE Studio || codstudio.wordpress.comSTARTAVAIL510

    Kirk76Dean11Maxwell12Adams30Lane4Green1Samuels02Fields8Nelson9

    123456789101112

    C#ODE Studio || codstudio.wordpress.com

  • Insertion in a Linked List- Algorithms which insert nodes into linked lists come up in various situations. Three main cases to be discussed are:Inserting node at the beginning of the ListInserting node after the node with a given locationInserting node into a sorted list.For all algorithms, variable ITEM contains the new information to be added to the list. All cases follow some common steps:Checking to see if free space is available in AVAIL list. If AVAIL=NULL, algorithm will print overflowRemoving first node from AVAIL list. Using variable NEW to keep track of location of new node, this step can be implemented by the pair of assignments (in this order) NEW:= AVAIL and AVAIL:=LINK[AVAIL]Copying new information into new node INFO[NEW]:=ITEM

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • C#ODE Studio || codstudio.wordpress.comAVAILNEWITEMSTARTINSERTION AT THE BEGINNING OF THE LINKED LIST

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING AT THE BEGINNING OF THE LISTAlgorithm: INSFIRST (INFO, LINK,START,AVAIL,ITEM) This algorithm inserts ITEM as the first node in the listStep 1: [OVERFLOW ?] If AVAIL=NULL, then Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list ] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL].Step 3: Set INFO[NEW]:=ITEM [Copies new data into new node]Step 4: Set LINK[NEW]:= START [New node now points to original first node] Step 5: Set START:=NEW [Changes START so it points to new node]Step 6: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING AFTER A GIVEN NODEAlgorithm: INSLOC(INFO, LINK,START,AVAIL, LOC, ITEM) This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts ITEM as the first node when LOC =NULLStep 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]Step 4: If LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW [End of If structure] Step 5: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • C#ODE Studio || codstudio.wordpress.comAVAILSTARTNEWLOC 18ITEM

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING BEFORE A GIVEN NODEAlgorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, ITEM1) This algorithm inserts ITEM so that ITEM PRECEDES the node with data item ITEM1Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]Step 4: Set PTR:=START and SAVE:=NULLStep 5: Repeat while INFO[PTR] ITEM1 Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop]Step 6:Set LOC:=SAVEStep 7: If LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW [End of If structure] Step 8: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING BEFORE A GIVEN NODE WITH LOCATION GIVENAlgorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, LOC) This algorithm inserts ITEM so that ITEM PRECEDES the node with location LOCStep 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]Step 4: Set PTR:=START and SAVE:=NULLStep 5: Repeat while PTR LOC Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop]Step 6:Set LOC:=SAVEStep 7: If LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW [End of If structure] Step 8: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING BEFORE NTH NODEAlgorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, N) This algorithm inserts ITEM so that ITEM PRECEDES the Nth nodeStep 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]Step 4: Set PTR:=START , SAVE:=NULL and K:=1Step 5: Repeat while K N SAVE:=PTR and PTR:=LINK[PTR] Set K:=K+1 [End of Loop]Step 6:Set LOC:=SAVEStep 7: If LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW [End of If structure] Step 8: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • For insertion in a sorted list, we use two alogorithms. One algorithm finds the location of the node after which the new node has to be inserted. This algorithm returns the location to main algorithm which finally do the insertion.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • INSERTING INTO A SORTED LISTAlgorithm: INSSRT(INFO,LINK,START,AVAIL,ITEM)Step 1: CALL FINDA(INFO,LINK, START,ITEM,LOC)Step 2: CALL INSLOC(INFO,LINK,START,AVAIL,LOC,ITEM)Step 3: ExitAlgorithm: FINDA(INFO,LINK,START,ITEM,LOC) This algorithm finds the location LOC of the last node in a sorted list such that INFO[LOC ]
  • Step 3: Set SAVE:=START and PTR:=LINK[START]Step 4: Repeat while PTRNULL: If ITEM< INFO[PTR], then: Set LOC:=SAVE Return [End of If Structure] Set SAVE:=PTR and PTR:=LINK[PTR] [End of Step 4 Loop]Step 5: Set LOC:=SAVEStep 6: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: INSLOC(INFO, LINK,START,AVAIL, LOC, ITEM) This algorithm inserts ITEM so that ITEM follows the node with location LOC or inserts ITEM as the first node when LOC =NULLStep 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: [Remove first node from AVAIL list] Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:= ITEM [Copies new data into new node]Step 4: If LOC=NULL, then: Set LINK[NEW]:=START and START:=NEW Else: Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW [End of If structure] Step 5: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Deletion of a node from a linked listCase 1: Deletion of node following a given nodeAlgorithm: DEL(INFO,LINK,START,AVAIL,LOC,LOCP) This algorithm deletes node N with location LOC. LOCP is location of node which precedes N or when N is first node, LOCP=NULL.Step 1: If LOC=NULL, then: Write: UNDERFLOW ExitStep 2: If LOCP=NULL, then: Set START:=LINK[START] [Deletes first node] Else: Set LINK[LOCP]:=LINK[LOC] [End of if structure]Step 3: [Return deleted node to AVAIL list] Set LINK[LOC]:=AVAIL and AVAIL:=LOCStep 4: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Deleting the node with a given item of informationFor deleting with given item of information, we first need to know the location of item to be deleted and also the location preceding the item to be deleted. For this, one algorithm will be called from the main algorithm to search for the two locations. Two variables, SAVE and PTR will be used to save the location of preceding and current node at every comparison respectively. Once , locations are found, deletion will be done in main algorithm.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: DELETE(INFO, LINK, START, AVAIL, ITEM)Step 1: CALL FINDB(INFO, LINK,START, ITEM, LOC, LOCP)Step 2: If LOC=NULL, then: Write: Underflow ExitStep 3: If LOCP=NULL, then: Set START:= LINK[START] Else: Set LINK[LOCP]:=LINK[LOC] [End of if structure]Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOCStep 5: Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: FINDB(INFO,LINK,START,ITEM,LOC,LOCP) This algorithm finds the location LOC of first node N which contains ITEM and location LOCP of node preceding N. if ITEM does not appear in the list, procedure sets LOC=NULL and if ITEM appears in first node, then it sets LOCP=NULLStep 1: If START=NULL, then: Set LOC:=NULL and LOCP:=NULL ReturnStep 2: If INFO[START]=ITEM, then: Set LOC:=START and LOCP:=NULL ReturnStep 3: Set SAVE:=START and PTR:=LINK[START]Step 4: Repeat while PTRNULL:Step 5: If INFO[PTR]=ITEM, then: Set LOC:=PTR and LOCP:=SAVE Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Step 6: Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop]Step 7:[Item to be deleted not in list] Set LOC:=NULLStep 8: Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Concatenating two linear linked listsAlgorithm: Concatenate(INFO,LINK,START1,START2) This algorithm concatenates two linked lists with start pointers START1 and START2Step 1: Set PTR:=START1Step 2: Repeat while LINK[PTR]NULL: Set PTR:=LINK[PTR] [End of Step 2 Loop]Step 3: Set LINK[PTR]:=START2Step 4: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Circular Linked List- A circular linked list is a linked list in which last element or node of the list points to first node. For non-empty circular linked list, there are no NULL pointers. The memory declarations for representing the circular linked lists are the same as for linear linked lists. All operations performed on linear linked lists can be easily extended to circular linked lists with following exceptions:While inserting new node at the end of the list, its next pointer field is made to point to the first node.While testing for end of list, we compare the next pointer field with address of the first nodeCircular linked list is usually implemented using header linkedlist. Header linked list is a linked list which always contains a special node called the header node, at the beginning of the list. This header node usually contains vital information about the linked list such as number of nodes in lists, whether list is sorted or not etc. Circular header lists are frequently used instead of ordinary linked lists as many operations are much easier to state and implement using header listsC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • This comes from the following two properties of circular header linked lists:The null pointer is not used, and hence all pointers contain valid addressesEvery (ordinary ) node has a predecessor, so the first node may not require a special case.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: (Traversing a circular header linked list) This algorithm traverses a circular header linked list with START pointer storing the address of the header node. Step 1: Set PTR:=LINK[START]Step 2: Repeat while PTRSTART: Apply PROCESS to INFO[PTR] Set PTR:=LINK[PTR] [End of Loop]Step 3: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Searching a circular header linked listAlgorithm: SRCHHL(INFO,LINK,START,ITEM,LOC) This algorithm searches a circular header linked listStep 1: Set PTR:=LINK[START]Step 2: Repeat while INFO[PTR]ITEM and PTRSTART: Set PTR:=LINK[PTR] [End of Loop]Step 3: If INFO[PTR]=ITEM, then: Set LOC:=PTR Else: Set LOC:=NULL [End of If structure]Step 4: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Deletion from a circular header linked list Algorithm: DELLOCHL(INFO,LINK,START,AVAIL,ITEM) This algorithm deletes an item from a circular header linked list.Step 1: CALL FINDBHL(INFO,LINK,START,ITEM,LOC,LOCP)Step 2: If LOC=NULL, then: Write: item not in the list ExitStep 3: Set LINK[LOCP]:=LINK[LOC] [Node deleted]Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOC [Memory retuned to Avail list]Step 5: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: FINDBHL(NFO,LINK,START,ITEM,LOC,LOCP) This algorithm finds the location of the node to be deleted and the location of the node preceding the node to be deleted Step 1: Set SAVE:=START and PTR:=LINK[START]Step 2: Repeat while INFO[PTR]ITEM and PTRSTART Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop]Step 3: If INFO[PTR]=ITEM, then: Set LOC:=PTR and LOCP:=SAVE Else: Set LOC:=NULL and LOCP:=SAVE [End of If Structure]Step 4: Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Insertion in a circular header linked listAlgorithm: INSRT(INFO,LINK,START,AVAIL,ITEM,LOC) This algorithm inserts item in a circular header linked list after the location LOCStep 1:If AVAIL=NULL, then Write: OVERFLOW Exit Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 3: Set INFO[NEW]:=ITEMStep 4: Set LINK[NEW]:=LINK[LOC] Set LINK[LOC]:=NEWStep 5: Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Insertion in a sorted circular header linked listAlgorithm: INSSRT(INFO,LINK,START,AVAIL,ITEM) This algorithm inserts an element in a sorted circular header linked listStep 1: CALL FINDA(INFO,LINK,START,ITEM,LOC)Step 2: If AVAIL=NULL, then Write: OVERFLOW Return Step 3: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL]Step 4: Set INFO[NEW]:=ITEMStep 5: Set LINK[NEW]:=LINK[LOC] Set LINK[LOC]:=NEWStep 6: Return C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: FINDA(INFO,LINK,ITEM,LOC,START) This algorithm finds the location LOC after which to insertStep 1: Set PTR:=STARTStep 2: Set SAVE:=PTR and PTR:=LINK[PTR]Step 3: Repeat while PTRSTART If INFO[PTR]>ITEM, then Set LOC:=SAVE Return Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop]Step 4: Set LOC:=SAVEStep 5: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • One of the most important application of Linked List is representation of a polynomial in memory. Although, polynomial can be represented using a linear linked list but common and preferred way of representing polynomial is using circular linked list with a header node. Polynomial Representation: Header linked list are frequently used for maintaining polynomials in memory. The header node plays an important part in this representation since it is needed to represent the zero polynomial.Specifically, the information part of node is divided into two fields representing respectively, the coefficient and the exponent of corresponding polynomial term and nodes are linked according to decreasing degree. List pointer variable POLY points to header node whose exponent field is assigned a negative number, in this case -1. The array representation of List will require three linear arrays as COEFF, EXP and LINK. For example: P(x)= 2x8-5x7-3x2+4 can be represented as:

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • POLYC#ODE Studio || codstudio.wordpress.com0-128-57-3240CIRCULAR HEADER LINEKD LIST REPRESENTATION OF 2x8-5x7-3x2+4HEADER NODE WITH EXP -1 AND COEFF 0

    C#ODE Studio || codstudio.wordpress.com

  • Addition of polynomials using linear linked list representation for a polynomialC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm:ADDPOLY( COEFF, POWER, LINK, POLY1, POLY2, SUMPOLY, AVAIL) This algorithm adds the two polynomials implemented using linear linked list and stores the sum in another linear linked list. POLY1 and POLY2 are the two variables that point to the starting nodes of the two polynomials. Step 1: Set SUMPOLY:=AVAIL and AVAIL:=LINK[AVAIL]Step 2: Repeat while POLY1 NULL and POLY2NULL: If POWER[POLY1]>POWER[POLY2],then: Set COEFF[SUMPOLY]:=COEFF[POLY1] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] Else If POWER[POLY2]>POWER[POLY1], then: Set COEFF[SUMPOLY]:=COEFF[POLY2]Set POWER[SUMPOLY]:=POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY]

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Else: Set COEFF[SUMPOLY]:=COEFF[POLY1]+COEFF[POLY2] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set POLY2=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of If structure] [End of Loop]Step3: If POLY1=NULL , then: Repeat while POLY2NULL Set COEFF[SUMPOLY]:=COEFF[POLY2] Set POWER[SUMPOLY]:=POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of Loop] [End of If Structure]

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Step 4: If POLY2=NULL, then: Repeat while POLY1NULL Set COEFF[SUMPOLY]:=COEFF[POLY1] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of Loop] [End of If Structure] Step 5: Set LINK[SUMPOLY]:=NULL Set SUMPOLY:=LINK[SUMPOLY]Step 6: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Addition of polynomials using circular header linked list representation for polynomialsC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: ADDPOLY( COEFF, POWER, LINK, POLY1,POLY2, SUMPOLY, AVAIL) This algorithm finds the sum of two polynomials implemented using header circular linked lists. POLY1 and POLY2 contain the addresses of header nodes of two polynomials and SUMPOLY is the circular header linked list storing the terms of sum of two polynomials Step 1: Set HEADER:=AVAIL and AVAIL:=LINK[AVAIL]Step 2: Set SUMPOLY:=HEADER Set LINK[SUMPOLY ]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY]Step 3: Set START1:=POLY1 and START2:=POLY2 Set POLY1:=LINK[START1] and POLY2:=LINK[START2]Step 4: Repeat while POLY1 START1 and POLY2START2: If POWER[POLY1]>POWER[POLY2],then: Set COEFF[SUMPOLY]:=COEFF[POLY1] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] Else If POWER[POLY2]>POWER[POLY1], then: Set COEFF[SUMPOLY]:=COEFF[POLY2]Set POWER[SUMPOLY]:=POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY]

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Else: Set COEFF[SUMPOLY]:=COEFF[POLY1]+COEFF[POLY2] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set POLY2=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of If structure] [End of Loop]Step5: If POLY1=START1 , then: Repeat while POLY2 START2 Set COEFF[SUMPOLY]:=COEFF[POLY2] Set POWER[SUMPOLY]:=POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of Loop] [End of If Structure]

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Step 6: If POLY2=START2, then: Repeat while POLY1START1 Set COEFF[SUMPOLY]:=COEFF[POLY1] Set POWER[SUMPOLY]:=POWER[POLY1] Set POLY1:=LINK[POLY1] Set LINK[SUMPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set SUMPOLY:=LINK[SUMPOLY] [End of Loop] [End of If Structure] Step 7: Set LINK[SUMPOLY]:=HEADER and SUMPOLY:=LINK[SUMPOLY]Step 8: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Multiplication of Polynomials using linear linked list representation for polynomialsC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: MULPOLY( COEFF, POWER, LINK, POLY1, POLY2, PRODPOLY, AVAIL) This algorithm multiplies two polynomials implemented using linear linked list. POLY1 and POLY2 contain the addresses of starting nodes of two polynomials. The result of multiplication is stored in another linked list whose starting node is PRODPOLY Step 1: Set PRODPOLY:=AVAIL and AVAIL:=LINK[AVAIL] Set START:=PRODPOLY Step 2: Repeat while POLY1 NULLStep 3:Repeat while POLY2NULL: Set COEFF[PRODPOLY]:=COEFF[POLY1]*COEFF[POLY2] Set POWER[PRODPOLY]:=POWER[POLY1]+POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[PRODPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set PRODPOLY:=LINK[PRODPOLY] [End of step 4 loop] Set POLY1:=LINK[POLY1] [End of step 3 loop]Step 4 Set LINK[PRODPOLY]:=NULL and PRODPOLY:=LINK[PRODPOLY]Step 5: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: MULPOLY( COEFF, POWER, LINK, POLY1, POLY2, PRODPOLY, AVAIL) This algorithm finds the product of two polynomials implemented using header circular linked list. POLY1 and POLY2 contain addresses of header nodes of two polynomials. The result of multiplication is stored in another header circular linked list. Step 1: Set HEADER:=AVAIL and AVAIL:=LINK[AVAIL]Step 2: Set PRODPOLY :=HEADER Set LINK[PRODPOLY ]:=AVAIL and AVAIL:=LINK[AVAIL] Set PRODPOLY:=LINK[PRODPOLY]Step 3: Set START1:=POLY1 and START2:=POLY2 Set POLY1:=LINK[START1] and POLY2:=LINK[START2]Step 4: Repeat while POLY1 START1 Step 5: Repeat while POLY2 START2 Set COEFF[PRODPOLY]:=COEFF[POLY1]*COEFF[POLY2] Set POWER[PRODPOLY]:=POWER[POLY1]+POWER[POLY2] Set POLY2:=LINK[POLY2] Set LINK[PRODPOLY]:=AVAIL and AVAIL:=LINK[AVAIL] Set PRODPOLY:=LINK[PRODPOLY] [End of Step 5 Loop] Set POLY1:=LINK[POLY1] [End of Step 4 Loop]Step 6: Set LINK[PRODPOLY]:=HEADER and PRODPOLY:=LINK[PRODPOLY]Step 7: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Doubly Linked List: Two-way ListA two-way list is a linear linked list which can be traversed in two directions: in usual forward direction from beginning of the list to end and in backward direction from end of list to the beginning. Thus, given the location LOC of a node N in list, one has immediate access to both the next node and the preceding node in the list.Each node of a two-way list is divided into three parts:An information field INFO which contains data of NA pointer field FORW which contains the location of next node in the listA pointer field BACK which contains the location of preceding node. The list also requires two list pointer variables FIRST which points to first node in the list and LAST which points to the last node in the list. Thus null pointer will appear in FORW field of last node in list and also in BACK field of first node in list.

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Two way lists are maintained in memory by means of linear arrays in same way as one way list except that two pointer arrays , FORW and BACK , are required instead of one list pointer variable. The list AVAIL will still be maintained as a one-way list.C#ODE Studio || codstudio.wordpress.comFIRSTLASTFORW[PTR]BACK[PTR]

    C#ODE Studio || codstudio.wordpress.com

  • Operations on a Two-way listTraversalAlgorithm: Traversal This algorithm traverses a two-way list. FORW and BACK are the two address parts of each node containing the address of next node and previous node respectively. INFO is the information part of each node. START contains the address of the first nodeStep 1: Set PTR:=STARTStep 2: Repeat while PTR NULL Apply PROCESS to INFO[PTR]Step 3: Set PTR:=FORW[PTR] [End of Step 2 Loop]Step 4: Exit

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: SEARCH(INFO,FORW,BACK,ITEM,START,LOC) This algorithm searches the location LOC of ITEM in a two- way list and sets LOC=NULL if ITEM is not found in the listStep 1: Set PTR:=STARTStep 2: Repeat while PTRNULL and INFO[PTR]ITEM Set PTR:=FORW[PTR] [End of Loop]Step 3: If INFO[PTR]=ITEM, then: Set LOC:=PTR Else: Set LOC:=NULLStep 4: ReturnC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: DELETE(INFO,FROW,BACK,START,AVAIL,LOC) This algorithm deletes a node from a two-way listStep 1: If LOC=START , then: START:=FORW[START] BACK[START]:=NULL ReturnStep 2: [Delete node] Set FORW[BACK[LOC]]:=FORW[LOC] Set BACK[FORW[LOC]]:=BACK[LOC]Step 3: [Returning node to AVAIL] Set FORW[LOC]:=AVAIL and AVAIL:=LOCStep 4: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm:INSRT (INFO,FORW,BACK,START,AVAIL,LOCA, LOCB, ITEM) This algorithm inserts an item in a doubly linked list. LOCA and LOCB location of adjacent nodes A and BStep 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW ReturnStep 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Set INFO[NEW]:=ITEMStep 3: Set FORW[LOCA]:=NEW and BACK[NEW]:=LOCA Set FORW[NEW]:=LOCB and BACK[LOCB]:=NEWStep 4: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • BACK[PTR]C#ODE Studio || codstudio.wordpress.comFORW[PTR] NEW

    C#ODE Studio || codstudio.wordpress.com

  • TEST QUESTIONSC#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm to copy the contents of one linked list to anotherAlgorithm: Copy (INFO,LINK,START,AVAIL) This algorithm copies the contents of one linked list to another.Step 1: If AVAIL=NULL,then Write: Overflow ReturnStep 2: Set PTR:=STARTStep 3: Set NEW:=AVAIL , START1:=NEW and AVAIL:=LINK[AVAIL]Step 4: Repeat while PTR NULL INFO[NEW]:=INFO[PTR] LINK[NEW]:=AVAIL and AVAIL:=LINK[AVAIL] NEW:=LINK[NEW] [End of Loop]Step 5: Set LINK[NEW]:=NULLStep 6: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm: Copy (INFO,LINK,START,START1) This algorithm copies the contents of one linked list to another. START AND START1 are the start pointers of two listsStep 1: Set PTR:=START and PTR1:=START1Step 2: Repeat while PTR NULL and PTR1 NULL INFO[PTR1]:=INFO[PTR] PTR1:=LINK[PTR1] PTR:=LINK[PTR] [End of Loop]Step 3: If PTR=NULL and PTR1=NULL ReturnStep 4:[Case when the list to be copied is still left] If PTR1=NULL,then PTR1: =AVAIL and AVAIL:=LINK[AVAIL] Repeat while PTR NULL INFO[PTR1]:=INFO[PTR] LINK[PTR1]:=AVAIL and AVAIL:=LINK[AVAIL] PTR1:=LINK[PTR1] [End of Loop] Step 5: [Case when list to be copied is finished, Truncate the extra nodes] If PTR1 NULL, then Set PTR1:=NULL [End of If structure]Step 6: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm to insert a node after the kth node in the circular linked listAlgorithm: INSRT (INFO,LINK,START,AVAIL,K,ITEM) This algorithm inserts in a circular linked list after the kth node Step 1: If AVAIL:=NULL, then Write: OVERFLOW ReturnStep 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Set INFO[NEW]:=ITEMStep 3: Set PTR:=START and N:=1 Step 4: If N=K, then Set LINK[NEW]:=LINK[PTR] Set LINK[PTR]:=NEW ReturnStep 5: Set PTR:=LINK[PTR] and Step 6: Repeat while N K Set PTR:=LINK[PTR] and N:=N+1 [End of if structure]Step 7: Set LINK[NEW]:=LINK[PTR] Set LINK[PTR]:=NEWStep 8: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Algorithm to delete the kth node from a doubly linked listAlgorithm: Del (INFO,FORW,BACK,FIRST,LAST,AVAIL,K) This algorithm deletes the kth nodeStep 1: Set N:=1 and PTR:=FIRSTStep 2: Set SAVE:=PTR and PTR:=FORW[PTR]Step 3: Repeat while N K Set SAVE:=PTR and PTR:=FORW[PTR] Set N:=N+1 [End of If structure]Step 4: If N=K, then FORW[SAVE]:=FORW[PTR] BACK[FORW[PTR]]:=BACK[PTR] FORW[PTR]:=AVAIL and AVAIL:=PTR [End of If structure]Step 5: Return

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • GARBAGE COLLECTION

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • In computer science, garbage collection (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory used by objects that will never be accessed or mutated again by the application. Garbage collection is often portrayed as the opposite of manual memory management, which requires the programmer to specify which objects to deallocate and return to the memory system.The basic principle of how a garbage collector works is:Determine what data objects in a program will not be accessed in the future Reclaim the resources used by those objects . Reachability of an objectInformally, a reachable object can be defined as an object for which there exists some variable in the program environment that leads to it, either directly or through references from other reachable objects. More precisely, objects can be reachable in only two ways:A distinguished set of objects are assumed to be reachablethese are known as the roots. Typically, these include all the objects referenced from anywhere in the call stack (that is, all local variables and parameters in the functions currently being invoked), and any global variables. Anything referenced from a reachable object is itself reachable; more formally, reachability is a transitive closure

    C#ODE Studio || codstudio.wordpress.com.

    C#ODE Studio || codstudio.wordpress.com

  • The memory is traced for garbage collection using Tracing collectors OR SIMPLY COLLECTORS. Tracing collectors are called that way because they trace through the working set of memory. These garbage collectors perform collection in cycles. A cycle is started when the collector decides (or is notified) that it needs to reclaim storage, which in particular happens when the system is low on memory. The original method involves a naive mark-and-sweep in which the entire memory set is touched several timesIn this method, each object in memory has a flag (typically a single bit) reserved for garbage collection use only. This flag is always cleared (counter-intuitively), except during the collection cycle. The first stage of collection sweeps the entire 'root set', marking each accessible object as being 'in-use'. All objects transitively accessible from the root set are marked, as well. Finally, each object in memory is again examined; those with the in-use flag still cleared are not reachable by any program or data, and their memory is freed. (For objects which are marked in-use, the in-use flag is cleared again, preparing for the next cycle.).C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Moving vs. non-moving garbage CollectionOnce the unreachable set has been determined, the garbage collector may simply release the unreachable objects and leave everything else as it is, or it may copy some or all of the reachable objects into a new area of memory, updating all references to those objects as needed. These are called "non-moving" and "moving" garbage collectors, respectively.At first, a moving garbage collection strategy may seem inefficient and costly compared to the non-moving approach, since much more work would appear to be required on each cycle. In fact, however, the moving garbage collection strategy leads to several performance advantages, both during the garbage collection cycle itself and during actual program execution

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Memory Allocation: Garbage CollectionThe maintenance of linked list in memory assumes the possibility of inserting new nodes into the linked lists and hence requires some mechanism which provides unused memory space for new nodes.. Analogously, some mechanism is required whereby memory space of deleted nodes becomes available for future use. Together with linked list, a special list is maintained in memory which consists of unused memory cells. This list, which has its own pointer is called the list of available space or the free-storage list or the free pool. During insertions and deletions in a linked list, these unused memory cells will also be linked together to form a linked list using AVAIL as its list pointer variable.Garbage Collection: The operating system of a computer may periodically collect all deleted space onto the free-storage list. Any technique which does this collection is called garbage collection. Garbage collection is mainly used when a node is deleted from a list or an entire list is deleted from a program.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • Garbage collection usually takes place in two steps:First the computer runs through the whole list tagging those cells which are currently in use, The computer then runs through the memory collecting all untagged spaces onto the free storage list. Garbage collection may take place when there is only some minimum amount of space or no space at all left in free storage list or when CPU is idle and has time to do the collection.C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com

  • ReferencesDinesh Mehta and Sartaj Sahni Handbook of Data Structures and Applications, Chapman and Hall/CRC Press, 2007. Niklaus Wirth, Algorithms and Data Structures, Prentice Hall, 1985. Diane Zak, Introduction to programming with c++, copyright 2011 Cengage Learning Asia Pte LtdSchaumm Series ,McGraw Hill.

    C#ODE Studio || codstudio.wordpress.com

    C#ODE Studio || codstudio.wordpress.com