Вы находитесь на странице: 1из 70

LINKED LIST

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 memory Linked 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

START 5

BED NUMBER 1 2 3 4 5 6 7 8 9 10 11 12

PATIENT Kirk Dean Maxwell Adams Lane Green Samuels Fields Nelson

NEXT 7 11 12 3 4 1 0 8 9

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:= START Step 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

Searching an unsorted Linked List Algorithm: 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=NULL Step 1: Set PTR:=START Step 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:=NULL Step 4: Return
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

Search in sorted list Algorithm: 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=NULL Step 1: Set PTR:= START Step 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 C#ODE Studio || codstudio.wordpress.com [End of If structure]

[End of step 2 Loop] Step 3: Set LOC:= NULL Step 4: Return

C#ODE Studio || codstudio.wordpress.com

START

INFO[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

1
START 5

Kirk Dean Maxwell Adams Lane Green Samuels

2
3 4 5

6
11 12 3

6
7
AVAIL 10

0
4 1 0

8 9

10
11 Fields 12 Nelson
C#ODE Studio || codstudio.wordpress.com

2
8 9

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 List Inserting node after the node with a given location Inserting 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 overflow Removing 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 C#ODE Studio || codstudio.wordpress.com INFO[NEW]:=ITEM

START

AVAIL

ITEM NEW

INSERTION AT THE BEGINNING OF THE LINKED LIST

C#ODE Studio || codstudio.wordpress.com

INSERTING AT THE BEGINNING OF THE LIST Algorithm: INSFIRST (INFO, LINK,START,AVAIL,ITEM) This algorithm inserts ITEM as the first node in the list Step 1: [OVERFLOW ?] If AVAIL=NULL, then Write: OVERFLOW Return Step 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 C#ODE Studio || codstudio.wordpress.com node]

INSERTING AFTER A GIVEN NODE 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 =NULL Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 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: C#ODE Studio || codstudio.wordpress.com Set LINK[NEW]:=LINK[LOC] and LINK[LOC]:= NEW

START

LOC

AVAIL

NEW

18

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

INSERTING BEFORE A GIVEN NODE Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, ITEM1) This algorithm inserts ITEM so that ITEM PRECEDES the node with data item ITEM1 Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 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:=NULL Step 5: Repeat while INFO[PTR] ITEM1 Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop] Step 6:Set LOC:=SAVE Step 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: Return
C#ODE Studio || codstudio.wordpress.com

INSERTING BEFORE A GIVEN NODE WITH LOCATION GIVEN Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, LOC) This algorithm inserts ITEM so that ITEM PRECEDES the node with location LOC Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 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:=NULL Step 5: Repeat while PTR LOC Set SAVE:=PTR and PTR:=LINK[PTR] [End of Loop] Step 6:Set LOC:=SAVE Step 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: Return
C#ODE Studio || codstudio.wordpress.com

INSERTING BEFORE NTH NODE Algorithm: INSLOC(INFO, LINK,START,AVAIL, ITEM, N) This algorithm inserts ITEM so that ITEM PRECEDES the Nth node Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 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:=1 Step 5: Repeat while K N SAVE:=PTR and PTR:=LINK[PTR] Set K:=K+1 [End of Loop] Step 6:Set LOC:=SAVE Step 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: Return
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

INSERTING INTO A SORTED LIST Algorithm: 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: Exit Algorithm: FINDA(INFO,LINK,START,ITEM,LOC) This algorithm finds the location LOC of the last node in a sorted list such that INFO[LOC ]<ITEM, or sets LOC=NULL Step 1: [List Empty ?] If START=NULL, then: Set LOC:=NULL Return [End of If structure] Step 2: [Special case] If ITEM<INFO[START], then: Set LOC:=NULL Return C#ODE Studio || codstudio.wordpress.com

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:=SAVE Step 6: Return

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 =NULL Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 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 C#ODE Studio || codstudio.wordpress.com [End of If structure]

Deletion of a node from a linked list Case 1: Deletion of node following a given node Algorithm: 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 Exit Step 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:=LOC Step 4: Return C#ODE Studio || codstudio.wordpress.com

Deleting the node with a given item of information For 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

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 Exit Step 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:=LOC Step 5: Return
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=NULL Step 1: If START=NULL, then: Set LOC:=NULL and LOCP:=NULL Return Step 2: If INFO[START]=ITEM, then: Set LOC:=START and LOCP:=NULL Return Step 3: Set SAVE:=START and PTR:=LINK[START] Step 4: Repeat while PTRNULL: Step 5: If INFO[PTR]=ITEM, then: 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:=NULL Step 8: Return

C#ODE Studio || codstudio.wordpress.com

Concatenating two linear linked lists Algorithm: Concatenate(INFO,LINK,START1,START2) This algorithm concatenates two linked lists with start pointers START1 and START2 Step 1: Set PTR:=START1 Step 2: Repeat while LINK[PTR]NULL: Set PTR:=LINK[PTR] [End of Step 2 Loop] Step 3: Set LINK[PTR]:=START2 Step 4: Return

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 nonempty 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 node Circular linked list is usually implemented using header linked list. 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 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 addresses Every (ordinary ) node has a predecessor, so the first node may not require a special case.

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

Searching a circular header linked list Algorithm: SRCHHL(INFO,LINK,START,ITEM,LOC) This algorithm searches a circular header linked list Step 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] 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 Exit Step 3: Set LINK[LOCP]:=LINK[LOC] [Node deleted] Step 4: Set LINK[LOC]:=AVAIL and AVAIL:=LOC [Memory retuned to Avail list] Step 5: Return 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 C#ODE Studio || codstudio.wordpress.com [End of If Structure]

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

Insertion in a sorted circular header linked list Algorithm: INSSRT(INFO,LINK,START,AVAIL,ITEM) This algorithm inserts an element in a sorted circular header linked list Step 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]:=ITEM Step 5: Set LINK[NEW]:=LINK[LOC] Set LINK[LOC]:=NEW C#ODE Studio || codstudio.wordpress.com

Algorithm: FINDA(INFO,LINK,ITEM,LOC,START) This algorithm finds the location LOC after which to insert Step 1: Set PTR:=START Step 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:=SAVE 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 C#ODE Studio || codstudio.wordpress.com representation of List will require three linear arrays as

POLY
HEADER NODE WITH EXP -1 AND COEFF 0

-1

-5

-3

CIRCULAR HEADER LINEKD LIST REPRESENTATION OF 2x8-5x7-3x2+4


C#ODE Studio || codstudio.wordpress.com

Addition of polynomials using linear linked list representation for a polynomial

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

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

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

Addition of polynomials using circular header linked list representation for polynomials

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

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

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

Multiplication of Polynomials using linear linked list representation for polynomials

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 NULL Step 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

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

Doubly Linked List: Two-way List


A 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 N A pointer field FORW which contains the location of next node in the list A pointer field BACK which contains the location of preceding node. The list also requires two list pointer variables FIRST which points to first node inC#ODE the list and LAST which points to the last 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 maintainedLAST as a FIRST one-way list.

BACK[PTR]

FORW[PTR]

C#ODE Studio || codstudio.wordpress.com

Operations on a Two-way list


Traversal Algorithm: 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 node Step 1: Set PTR:=START Step 2: Repeat while PTR NULL Apply PROCESS to INFO[PTR] Step 3: Set PTR:=FORW[PTR] [End of Step 2 Loop] C#ODE Studio || codstudio.wordpress.com Step 4: Exit

Algorithm: SEARCH(INFO,FORW,BACK,ITEM,START,LOC) This algorithm searches the location LOC of ITEM in a twoway list and sets LOC=NULL if ITEM is not found in the list Step 1: Set PTR:=START Step 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:=NULL
C#ODE Studio || codstudio.wordpress.com

Algorithm: DELETE(INFO,FROW,BACK,START,AVAIL,LOC) This algorithm deletes a node from a two-way list Step 1: If LOC=START , then: START:=FORW[START] BACK[START]:=NULL Return Step 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:=LOC Step 4: Return

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 B Step 1: [OVERFLOW] If AVAIL=NULL, then: Write: OVERFLOW Return Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Set INFO[NEW]:=ITEM Step 3: Set FORW[LOCA]:=NEW and BACK[NEW]:=LOCA Set FORW[NEW]:=LOCB and BACK[LOCB]:=NEW Step 4: Return

C#ODE Studio || codstudio.wordpress.com

FORW[PTR]

NEW

BACK[PTR]

C#ODE Studio || codstudio.wordpress.com

TEST QUESTIONS

C#ODE Studio || codstudio.wordpress.com

Algorithm to copy the contents of one linked list to another Algorithm: Copy (INFO,LINK,START,AVAIL) This algorithm copies the contents of one linked list to another. Step 1: If AVAIL=NULL,then Write: Overflow Return Step 2: Set PTR:=START Step 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]:=NULL Step 6: Return

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 lists Step 1: Set PTR:=START and PTR1:=START1 Step 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 Return Step 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

Algorithm to insert a node after the kth node in the circular linked list Algorithm: 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 Return Step 2: Set NEW:=AVAIL and AVAIL:=LINK[AVAIL] Set INFO[NEW]:=ITEM Step 3: Set PTR:=START and N:=1 Step 4: If N=K, then Set LINK[NEW]:=LINK[PTR] Set LINK[PTR]:=NEW Return Step 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]:=NEW Step 8: Return
C#ODE Studio || codstudio.wordpress.com

Algorithm to delete the kth node from a doubly linked list Algorithm: Del (INFO,FORW,BACK,FIRST,LAST,AVAIL,K) This algorithm deletes the kth node Step 1: Set N:=1 and PTR:=FIRST Step 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] C#ODE Studio || codstudio.wordpress.com

GARBAGE COLLECTION

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 object
Informally, 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, C#ODE Studio || codstudio.wordpress.com reachability is a transitive closure

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 times In 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 C#ODE Studio || codstudio.wordpress.com in-use, the in-use flag is cleared again, preparing for the next

Moving vs. non-moving garbage Collection Once 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

Memory Allocation: Garbage Collection The 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. 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

References
Dinesh 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 Ltd Schaumm Series ,McGraw Hill.

C#ODE Studio || codstudio.wordpress.com

Вам также может понравиться