Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

14. Linked List

Linked list

  • Login to see the comments

14. Linked List

  1. 1. Linked List By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Java and DataJava and Data StructuresStructures
  2. 2. Linked List • Linear collection of data elements called nodes. • Linear order is given by means of pointers. • Each node is divided into two parts, first part contains info and second part, called link. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). NULLNULL Start Node A Node B Node C End 10 20 30 40 Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  3. 3. Representation of Linked List • Let LIST be a linked list, • LIST requires two linear array: – INFO[k] – information part – LINK [k] – next pointer field • Also requires variable Name – such as START- which contains the location of the beginning of the list Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  4. 4. Representation of Linked List Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). O J L W N START 1 2 3 4 5 6 7 8 9 10 6 7 5 3 INFO LINK 9 START 9 INFO[9] N LINK[9] 3 INFO[3] O LINK[3] 6 INFO[6] L LINK[6] 5 INFO[5] J LINK[5] 7 INFO[7] W
  5. 5. Traversing a Linked List Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). NULLNULL Start Node A Node B Node C End 20 30 4010 PTR Algorithm TraverseList(INFO, LINK, START) { PTR := START; while (PTR != NULL) { Process --> INFO[PTR]; PTR := LINK[PTR]; } }
  6. 6. Traversing a Linked List • Write algorithm to – Print elements – Count elements Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  7. 7. Searching a Linked List Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm SearchList(INFO, LINK, START, ITEM) { PTR := START; while (PTR != NULL) { if (ITEM = INFO[PTR])THEN LOC := PTR; else PTR := LINK[PTR]; } }
  8. 8. Insertion into Linked List:@beginning Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm InsertFirst(INFO, LINK, START, AVAIL, ITEM) { if(AVAIL = NULL) THEN write("Overflow!"); else NEW := AVAIL; AVAIL := LINK [AVAIL]; INFO[NEW] := ITEM; LINK[NEW]:=START; START:=NEW; } NULLNULL Start Node A Node B Node C End 20 30 4010 NULLNULL AVAIL NEW 50
  9. 9. Inserting after a given node Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm InsertAtLoc(INFO, LINK, START, AVAIL, LOC, ITEM) { if(AVAIL = NULL) write("Overflow"); else NEW := AVAIL; AVAIL := LINK [AVAIL]; INFO [NEW] := ITEM; if(LOC = NULL) then { LINK [NEW] := START; START := NEW; } else { LINK [NEW] := LINK[LOC]; LINK[LOC] :=NEW; } }
  10. 10. Inserting @end Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm InsertAtEnd(INFO, LINK, START, AVAIL, ITEM) { if(AVAIL = NULL) write("Overflow"); else NEW := AVAIL; AVAIL := LINK [AVAIL]; INFO [NEW] := ITEM; PTR := START; while (LINK[PTR] != NULL) { PTR := LINK[PTR]; } LINK [PTR] := NEW; }
  11. 11. Deleting first node Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm DeleteFirst(INFO, LINK, START, AVAIL) { PTR := LINK [START]; if(PTR = NULL) write("Underflow!"); else PTR1 := LINK [PTR]; LINK [START] := PTR1; //Returns deleted node to the AVAIL list. LINK [PTR] := AVAIL; AVAIL := PTR; } NULLNULL Start Node A Node B Node C End 20 30 4010 NULLNULL AVAIL PTR PTR1
  12. 12. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Deleting End node Algorithm DeleteEnd(INFO, LINK, START, AVAIL) { PTR := START; if(LINK[PTR] = NULL) write("Underflow!"); else while(LINK [PTR]!=NULL) { PTR1 := PTR; PTR := LINK [PTR]; } LINK [PTR1] := NULL; //Returns deleted node to the AVAIL list. LINK [PTR] := AVAIL; AVAIL := PTR; } NULLNULL Start Node A Node B Node C End 20 30 4010 NULLNULL AVAIL PTR PTR1 NULL
  13. 13. Deleting specific node Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Algorithm DeleteSpecific(INFO, LINK, START, AVAIL, KEY) { PTR1 := START; PTR := LINK [PTR1]; while(PTR != NULL) { if (INFO [PTR] != KEY) then { PTR1 := PTR; PTR := LINK[PTR]; } else { LINK [PTR1] := LINK [PTR]; } //Returns deleted node to the AVAIL list. LINK [PTR] := AVAIL; AVAIL := PTR; } if(PTR = NULL)then write("NODE with KEY does not exist"); }
  14. 14. Applications • Polynomial Representation – Polynomial having single variable is, Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  15. 15. Q & A

×