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 to...
double or triple the size of an arraywhen additional space is required.Another way of storing a list inmemory is to have e...
Thus successive elements in the listneed not occupy adjacent space inmemory. This will make it easier toinsert and delete ...
A linked list, or one-way list, is alinear collection of data elements,called nodes, where the linear orderis given by mea...
address of the next node in the list.Figure below is a schematic diagramof a linked list with 6 nodes. Eachnode is picture...
The right part represents the nextpointer field of the node, and thereis an arrow drawn from it to the nextnode in the lis...
the null pointer, which is any invalidaddress.
(In actual practice, 0 or a negativenumber is used for the null pointer.)The null pointer, denoted by x in thediagram, sig...
the first node. Clearly, we need onlythis address in START to tracethrough the list. A special case is thelist that has no...
EXAMPLEA hospital ward contains 12 beds, ofwhich 9 are occupied as shown in Figbelow. Suppose we want analphabetical listi...
Hence START contains 5, since thefirst patient, Adams, occupies bed 5.Also, Adamss pointer is equal to 3,since Dean, the n...
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 s...
respectively, the information partand the next pointer field of a nodeof LIST. As noted above, LIST alsorequires a variabl...
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 ac...
LINK[9] = 3, so INFO[3] = 0 is thesecond character.LINK[3] = 6, so INFO[6] = 0 (blank) isthe third character.LINK[6] = 11,...
LINK[7] = 10, so INFO 0] = I is thesixth character.LlNK[10] = 4, so INFO] = T is theseventh character.LlNK[4] = 0, the NUL...
We require two tests. First we haveto check to see whether we havereached the end of the list; i.e., firstwe check to see ...
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 currentl...
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 operat...
1. Set PTR := START. [Initializes pointerPTR].2.Repeat Steps 3 and 4 while PTR #NULL.3.Apply PROCESS to INFO[PTR].4.Set PT...
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. Re...
4.     Set PTR := LINK[PTR]. [Updatespointer.]     [End of Step 2 loop.]5. Return.This procedure finds elements in alinked...
Procedure: COUNT (INFO, LINK,START, NUM)1. Set NUM := 0. [Initializescounter.]2. Set PTR := START. Initializespointer.]3. ...
4.     Set NUM := NUM+1.[Increases NUM by 1]5.     Set PTR:=LINK[PTR]. [Updatespointer.]    [End of Step 3 loop.]6. Return.
Linked lists 1
Linked lists 1
Upcoming SlideShare
Loading in …5
×

Linked lists 1

1,259 views

Published on

CSC 391

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,259
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Linked lists 1

  1. 1. Mr. Forkan Karim Mazumder FacultyDept. of Computer Science and Engineering
  2. 2. 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
  3. 3. 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.
  4. 4. 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.
  5. 5. 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
  6. 6. 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 . . .).
  7. 7. 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
  8. 8. the null pointer, which is any invalidaddress.
  9. 9. (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
  10. 10. 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.
  11. 11. 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.
  12. 12. 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
  13. 13. O. (Some arrows have been drawn to indicate the listing of the first few patients.)
  14. 14. 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,
  15. 15. 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.
  16. 16. Since the subscripts of the arraysINFO and LINK will usually bepositive, we will choose NULL = 0,unless otherwise stated.
  17. 17. 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[9] = N is the firstcharacter.
  18. 18. LINK[9] = 3, so INFO[3] = 0 is thesecond character.LINK[3] = 6, so INFO[6] = 0 (blank) isthe third character.LINK[6] = 11, so INFO[11] = E is thefourth character.LINK[11] = 7, so INFO[7]= X is thefifth character.
  19. 19. LINK[7] = 10, so INFO 0] = I is thesixth character.LlNK[10] = 4, so INFO] = T is theseventh character.LlNK[4] = 0, the NULL value, so thelist has ended. In other words, NO EXIT is thecharacter string.
  20. 20. 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
  21. 21. The two tests cannot be performedat the same time, since INFO[PTR] isnot defined when PTR = NULL.
  22. 22. 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]
  23. 23. moves the pointer to the next nodein the list.
  24. 24. 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.
  25. 25. 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.
  26. 26. The following procedure presentsabout how traversing a linked list.They are similar to the abovealgorithm.
  27. 27. 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].
  28. 28. 4. Set PTR := LINK[PTR]. [Updatespointer.] [End of Step 2 loop.]5. Return.This procedure finds elements in alinked list.
  29. 29. 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.
  30. 30. 4. Set NUM := NUM+1.[Increases NUM by 1]5. Set PTR:=LINK[PTR]. [Updatespointer.] [End of Step 3 loop.]6. Return.

×