Mr. Forkan Karim Mazumder FacultyDept. of Computer Science and Engineering
Linked Lists One way to store data is in arrays.But arrays have certain disadvantagessuch as it is relatively expensive toinsert and delete elements in anarray. Also, since an array usuallyoccupies a block of memory space,one cannot simply
double or triple the size of an arraywhen additional space is required.Another way of storing a list inmemory is to have each element inthe list contain a field, called a link orpointer, which contains the addressof the next element in the list.
Thus successive elements in the listneed not occupy adjacent space inmemory. This will make it easier toinsert and delete elements in the list.So for inserting, deleting, searchingdata, it is better to use list withpointers rather than array. This typeof data structure is called linked list.
A linked list, or one-way list, is alinear collection of data elements,called nodes, where the linear orderis given by means of pointers. Thatis, each node is divided into twoparts: the first part contains theinformation of the element, and thesecond part, called the link field ornext pointer field, contains the
address of the next node in the list.Figure below is a schematic diagramof a linked list with 6 nodes. Eachnode is pictured with two parts. Theleft part represents the informationpart of the node, which may containan entire record of data items (e.g.NAME, ADDRESS . . .).
The right part represents the nextpointer field of the node, and thereis an arrow drawn from it to the nextnode in the list. This follows theusual practice of drawing an arrowfrom a field to a node when theaddress of the node appears in thegiven field. The pointer of the lastnode contains a special value, called
(In actual practice, 0 or a negativenumber is used for the null pointer.)The null pointer, denoted by x in thediagram, signals the end of the list.The linked list also contains a listpointer variable-called START orNAME-which contains the address ofthe first node in the list; hence thereis an arrow drawn from START to
the first node. Clearly, we need onlythis address in START to tracethrough the list. A special case is thelist that has no nodes. Such a list iscalled the null list or empty list andis denoted by the null pointer in thevariable START.
EXAMPLEA hospital ward contains 12 beds, ofwhich 9 are occupied as shown in Figbelow. Suppose we want analphabetical listing of the patients.This listing may be given by thepointer field, called Next in thefigure. We use the variable START topoint to the first patient.
Hence START contains 5, since thefirst patient, Adams, occupies bed 5.Also, Adamss pointer is equal to 3,since Dean, the next patient,occupies bed 3; Deans pointer is 11,since Fields, the next patient,occupies bed 11; and so on. Theentry for the last patient (Samuels)contains the null pointer, denoted by
O. (Some arrows have been drawn to indicate the listing of the first few patients.)
REPRESENTATION OF LINKED LISTS INMEMORYLet LIST be a linked list. Then LISTwill be maintained in memory,unless otherwise specified orimplied, as follows. First of all, LISTrequires two linear arrays-we willcall them here INFO and LINK-suchthat INFO[K] and LINK[K] contain,
respectively, the information partand the next pointer field of a nodeof LIST. As noted above, LIST alsorequires a variable name-such asSTART-which contains the location ofthe beginning of the list, and a nextpointer denoted by NULL-whichindicates the end of the list.
Since the subscripts of the arraysINFO and LINK will usually bepositive, we will choose NULL = 0,unless otherwise stated.
EXAMPLE:Figure pictures a linked list inmemory where each node of the listcontains a single character. We canobtain the actual list ofcharacters, or, in other words, thestring, as follows:START = 9, so INFO = N is the firstcharacter.
LINK = 3, so INFO = 0 is thesecond character.LINK = 6, so INFO = 0 (blank) isthe third character.LINK = 11, so INFO = E is thefourth character.LINK = 7, so INFO= X is thefifth character.
LINK = 10, so INFO 0] = I is thesixth character.LlNK = 4, so INFO] = T is theseventh character.LlNK = 0, the NULL value, so thelist has ended. In other words, NO EXIT is thecharacter string.
We require two tests. First we haveto check to see whether we havereached the end of the list; i.e., firstwe check to see whetherPTR= NULLIf not, then we check to see whetherINFO[PTR] = ITEM
The two tests cannot be performedat the same time, since INFO[PTR] isnot defined when PTR = NULL.
TRAVERSING A LINKED LIST Our traversing algorithm usesa pointer variable PTR which pointsto the node that is currently beingprocessed. Accordingly, LINK[PTR]points to the next node to beprocessed. The assignmentPTR := LINK[PTR]
moves the pointer to the next nodein the list.
Algorithm :(Traversing a Linked List) Let LIST be alinked list in memory. This algorithmtraverses LIST, applying an operationPROCESS to each element of LIST.The variable PTR points to the nodecurrently being processed.
1. Set PTR := START. [Initializes pointerPTR].2.Repeat Steps 3 and 4 while PTR #NULL.3.Apply PROCESS to INFO[PTR].4.Set PTR:=LINK[PTR]. [PTR now pointsto the next node.] [End of Step 2 loop.]5.Exit.
The following procedure presentsabout how traversing a linked list.They are similar to the abovealgorithm.
Procedure : PRINT(INFO, LINK,START)This procedure prints theinformation at each node of the list.1. Set PTR := START.2. Repeat Steps 3 and 4 while PTR #NULL:3. Write: INFO[PTR].
4. Set PTR := LINK[PTR]. [Updatespointer.] [End of Step 2 loop.]5. Return.This procedure finds elements in alinked list.
Procedure: COUNT (INFO, LINK,START, NUM)1. Set NUM := 0. [Initializescounter.]2. Set PTR := START. Initializespointer.]3. Repeat Steps 4 and 5 while PTR #NULL.
4. Set NUM := NUM+1.[Increases NUM by 1]5. Set PTR:=LINK[PTR]. [Updatespointer.] [End of Step 3 loop.]6. Return.