Ce diaporama a bien été signalé.
Le téléchargement de votre SlideShare est en cours. ×

data structure3.pptx

Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Publicité
Prochain SlideShare
Data structure.pptx
Data structure.pptx
Chargement dans…3
×

Consultez-les par la suite

1 sur 32 Publicité
Publicité

Plus De Contenu Connexe

Plus récents (20)

Publicité

data structure3.pptx

  1. 1. CS261 DATA STRUCTURES & ALGORITHMS (WEEK-3) LECTURE-5 & 6 INTRODUCTION TO DATA STRUCTURES & ALGORITHMS Lecturer Azka Aziz Azka.a@scocs.edu.pk
  2. 2. Data structures & Algorithms Lecture#05 Link List(continue..)
  3. 3. Course contents Link List Data Structure (Declaration, Initialization, Updating) Insert After a specific location Insert a value before a specific location Insert a value as a first item of list Delete a value from a link list
  4. 4. Insert After a specific Location Insert_after (position , value) 1. If (position <= 0); then Write(“invalid position ”); [End of if structure] 2. Temp = ptfirst; 3. Repeat for I = 1,2,3…. < position 3.1.Temp = temp -> link ; 3.2.If (temp == NULL) 3.2.1. Write(“invalid position ”); 3.2.2. Return; [End of if structre of 3.2] [End of loop of step number 3] 4. Q = new node ; 5. Q -> data = value; 6. Q -> link temp -> link; 7. Temp -> link = Q; 8. Return;
  5. 5. Insert After a Specific Location Insert_after (position , value) 1. If (position <= 0); then Write(“invalid position ”); [End of if structure] 2. Temp = ptfirst; 3. Repeat for I = 1,2,3…. < position 3.1. Temp = temp -> link ; 3.2. If (temp == NULL) 3.2.1. Write(“invalid position ”); 3.2.2. Return; [End of if structure of 3.2] [End of loop of step number 3] 4. Q = new node ; 5. Q -> data = value; 6. Q -> link temp -> link; 7. Temp -> link = Q; 8. Return;
  6. 6. Insert a Value Before a Specific Location Insert_before (value , position ) 1. Insert position <= 0 ; then Write(“Invalid position”) Return; [ End of if structure] 2. Temp = ptfirst; 2.1. Repeat for I = 1,2,3….. <= position-1; 2.1.1. Previous = temp; 2.1.2. Temp = temp -> link; 2.1.3. If (temp == NULL) Write(“invalid position ’) Return [End if structure of step 2.1.3] [End of loop of step 2.1] 3. Temp = new node ; 4. Temp -> data = value; 5. Temp -> link = previous -> link; 6. Previous -> link = temp; 7. Return
  7. 7. Insert a Value as a First List Item Insert_First(value) 1. [create a new node] temp= new node 2. temp->data = value 3. Temp-> link = ptfirst 4. Ptfirst = temp 5. Return
  8. 8. Delete a Value from Link List Delete(value) 1. [inilization] Temp = ptfist ; Previous = temp; 2. Repeat while (temp != NULL) 2.1. If (temp == ptfist) then 2.1.1. If (temp == ptfirst) then Delete temp Else Previous -> link = temp -> link ; Delete -> temp; Return; [end of if-else structure of 2.1.1. ] Else Previous = temp; Temp = temp -> link; [end of if-else structure of step 2.1] [end of loop of step of 2] 3. Write(“value not found”); 4. Return;
  9. 9. Data structures & Algorithms Lecture#06 Double Link List
  10. 10. Course contents Doubly Link List ADT Operations Sample Code Structure of a DLL Insert value Insert first
  11. 11. Insert First Insert_first(value) 1. [create new node ] Temp = new value; 2. [insert value] Temp -> data = value; 3. Temp -> prev = NULL; 4. Temp -> next = ptfirst ; 5. Ptfirst = NULL; 6. If (ptfirst -> next != NULL) Ptfirst -> next -> prev = ptfirst; [End of if-structure] 7. Return
  12. 12. Double Link list NULL NULL Doubly linked list (or two way linked list) is a type of linked list with every node containing two node pointers (next, prev) instead of only one (next). Start Diagram represents a Doubly Linked List with yellow pointers representing next-link and green pointers representing previous-link.
  13. 13. Double Link list … Node ADT
  14. 14. Double Link list … Doubly Linked List ADT NULL NULL Start End
  15. 15. Doubly Linked List … Operations Operation Description Pseudocode int size(); Counts and returns the number of nodes in Doubly Linked List  Create a Node Pointer ‘temp’ and point it to ‘start’  Create an integer variable ‘count’ and assign 0 to it  Iterate temp through all nodes of list till the end of list is reached  During every iteration add one to ‘count’  Return ‘count’ when iterations are over
  16. 16. Doubly Linked List … Operations Operation Description Pseudocode void print_all(); Prints all nodes’ data values  Create a node pointer ‘temp’ and point it to ‘start’ of list  Traverse through the list until ‘temp’ reaches to the end of list (NULL)  During every iteration print the ‘data’ value
  17. 17. Doubly Linked List … Operations Operation Description Pseudocode void print_all_backward(); Print all nodes’ ‘data’ values in backward direction  Create a node pointer ‘temp’ and point it to ‘end’ of list  Traverse back through the list until ‘temp’ reaches to the node before the start of list (NULL)  During every iteration print the ‘data’ value
  18. 18. Doubly Linked List … Operations Operation Description Pseudocode Node* search(int); Searches through the list for a Node with given value and returns its reference  Create a node pointer ‘temp’ and point it to ‘start’  Traverse through the list until either ‘temp’ reaches to the Node with given value or it reaches to the end of list (NULL)  Return the node pointer ‘temp’
  19. 19. Doubly Linked List … add_node()
  20. 20. Doubly Linked List … Operations Operation Description Pseudocode void append_node(int); Create a node with given value and append (add to end) it to the list  Create a node with given ‘key’ as its ‘data’, NULL as ‘next’, NULL as ‘prev’  Point the newly created node with pointer ‘temp’  If size() of list is ZERO, assign ‘temp’ to ‘start’, assign ‘temp’ to ‘end’  Otherwise  Assign ‘temp’ to ‘next’ of ‘end’  Assign ‘end’ to ‘prev’ of ‘temp’  Assign ‘temp’ to ‘end’
  21. 21. Doubly Linked List … append_node()
  22. 22. Doubly Linked List … Operations Operation Description Pseudocode void delete_node(int); Delete the node with given value from the list  Search node with given value as key (say it is ‘temp’)  If ‘temp’ is equl to NULL then Exit  Else If ‘temp’ is equal to ‘start’ and it is equal to ‘end’  Assign NULL to ‘start’ and assign NULL to ‘end’  Exit  Else if ‘temp’ is equal to ‘start’  Assign ‘start’ to ‘next’ of ‘start’  Assign NULL to ‘prev’ of ‘start’  Delete ‘temp’, Exit  Else if ‘temp’ is equal to ‘end’  Assign ‘end’ to ‘prev’ of ‘end’  Assign NULL to ‘next’ of ‘end’  Delete ‘temp’, Exit  Otherwise,  Create node pointers ‘n’ and ‘p’ and assign them ‘next’ of ‘temp’ and ‘prev’ of ‘temp’ respectively  Assign ‘n’ to the ‘next’ of ‘p’  Assign ‘p’ to the ‘prev’ of ‘n’  Delete ‘temp’, Exit
  23. 23. Doubly Linked List … delete_node
  24. 24. Doubly Linked List … insert_after
  25. 25. Doubly Linked List … Operations Operation Description Pseudocode void insert_before(int before, int key); Insert a node with given value in linked list immediately before a specific node already present in the list  Search the node with data is equal to ‘before’, say it is ‘temp’  If ‘temp’ is pointing to NULL,  exit  Else,  Create a new node in memory and point it be a pointer ‘new_node’  Set ‘key’ as data of ‘new_node’  Assign ‘prev’ of ‘temp’ to the ‘prev’ of ‘new_node’  Assign ‘temp’ to ‘next’ of ‘new_node’  Assign ‘new_node’ to the ‘prev’ of ‘temp’  If ‘prev’ of ‘new_node’ is not NULL, assign ‘new_node’ to the ‘next’ of ‘prev’ of ‘new_node’  Exit
  26. 26. Doubly Linked List … insert_before
  27. 27. Double Link list … sample code Sample code is provided during class
  28. 28. Structure for node in DLL struct node { struct node * prev; struct node * next; int data ; struct node *next; }; struct node *ptfirst = NULL; (global variable)
  29. 29. Main Algorithm for DLL opertions Start Repeat (loop) while true (true = 1) [menu option] Write(cout) (“1: Insert ”); Write (“2: display ”); Write (“3: Insert first ”); Write (“4: Insert after ”); Write (“5: Insert before ”); Write (“6: Delete ”); Write (“7: Exite ”); Write (“Enter option number ”); Read (cin) option number (option_number) Select option_number
  30. 30. Select option Option 1 Write (“ Insert value ”); Read value Insert (value) Option 2 Display(); Option 3 …… Option 4 Exit(); [Exit of selection structure] [End of loop structure] End
  31. 31. Insert a value in DLL Insert(value) { 1. [check for empty list] If(ptfirst == NULL) then Ptfirst = new node ; Ptfirst -> prev = Null; Temp = ptfirst; Else 2. Temp = ptfirst ; 3. Repeat while (temp -> next != NULL) Temp = temp -> next; [ end of loop] Temp -> next = new node ; Temp -> next -> prev = temp; Temp = temp -> next ; [end of if-else ] 4. Temp -> data = value; 5. Temp -> next = NULL; 6. Return ; }
  32. 32. Display/Traverse a DLL: Display() { If(ptfirst == NULL) then Write (“list is empty ”); Return ; [End of if-structure] temp = ptfirst; Repeat while(temp != NULL); write(temp -> data); Temp = temp -> next; [ end of loop] Return }

×