1 / 21

Data structure

MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE. Using C#. Information Technology , 3’rd Semester. Data structure. Lecture 7: Linked List(2). Presented By: Mahmoud Rafeek Alfarra. Outline. Add item in the front of linked list

cili
Download Presentation

Data structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Using C# Information Technology , 3’rd Semester Data structure Lecture 7: Linked List(2) Presented By: Mahmoud Rafeek Alfarra

  2. Outline • Add item in the front of linked list • Add item in the n’th place of linked list • Print the data of linked list • Delete item from the back of the linked list • Delete item from the front of the linked list • Delete the n’th item from the linked list • Emank X Mezank !!

  3. Add item in the front of linked list • Add the new item in the front, mean that the new item will be pointed to the first one. 1 Head Back Data 1 Data 2 Data 3 Data n ….. null New data Presented & Prepared by: Mahmoud R. Alfarra

  4. Add item in the front of linked list New.Next = first; Front = new; Size ++ 1 Head Back Data 1 Data 2 Data 3 Data n ….. null New data Presented & Prepared by: Mahmoud R. Alfarra

  5. Add item in the n’th place of linked list • Add the new item in the n’th place, mean that the new item will be pointed to the (n)’th node and then the (n-1)’th node will be pointed to new node. Head 1 Back × Data 1 Data 2 Data 3 Data n ….. 0 1 2 n null 2 New data New node will be inserted at the 3’rd place Presented & Prepared by: Mahmoud R. Alfarra

  6. Add item in the n’th place of linked list New.Next = n’th_node; (n-1)’th_node.next = new; Size ++ Head 1 Back × Data 1 Data 2 Data 3 Data n ….. 0 1 2 n null 2 New data New node will be inserted at the 3’rd place Presented & Prepared by: Mahmoud R. Alfarra

  7. Print the data of the linked list 7 • Once a list exists, the user can explorer it. • One of the operations performed on linked List is to prints its data. • To do this, you can declare a method that examines the head of the list is not pointing to null. • Using for loop … Item Current = Head; for(int i = 0; Current != null; i++) Print current.data; Current = Current.Next; Presented & Prepared by: Mahmoud R. Alfarra

  8. Example (Linked List of cars) 8 Build the item list of car: Which is a class of car Contains the car’s data Contains a variable of car to point to the next node class Car { public string name; public Car next; } 1 Presented & Prepared by: Mahmoud R. Alfarra

  9. Example (Linked List of cars) 9 Build the list of car : Which is a management class contains Length of list which begins as 0 A variable of car as the pointer of each node and begins as null All the operations class LinkedCar { private int length ; public Car head; public LinkedCar() { lenght = 0; head = null; }} 2 Presented & Prepared by: Mahmoud R. Alfarra

  10. Example (operations on Linked List) 10 Add a new car to the linked list in the front In the class of management (LinkedCar), we declare all the operations as add, remove, sort, … 3 public void addAtFront(Car inserted) { Car newc = new Car (); newc = inserted; newc.next = head; lenght++; head = newc; } HW 7.1 Write the methods to add a new elements in the n’th position and as the last node Presented & Prepared by: Mahmoud R. Alfarra

  11. Example (operations on Linked List) 11 Print the information of the cars in the LinkedList 4 public void printCarData() { Car current = head; for (int i =0 ; current != null; i++) { Console.WriteLine("The name is "+ current.name); current = current.next; } } HW 7.2 Write the methods to print the data of i’th node Presented & Prepared by: Mahmoud R. Alfarra

  12. Example (create an object of LinkedCar) 12 Create a linkedlist of cars The class of management (LinkedCar) is just a class. so, need to create object to perform the operations … static void Main(string[ ] args) { LinkedCar list1 = new LinkedCar(); Car car1 = new Car(); car1.name = "Mahmoud"; list1.addAtFront(car1); Car car2 = new Car(); car2.name = "Ali"; list1.addAtFront(car2); list1.printCarData(); Console.Read(); } 5 Presented & Prepared by: Mahmoud R. Alfarra

  13. Home Work 13 HW 7.3 Re-write the previous example using the windows application, as the above figure Presented & Prepared by: Mahmoud R. Alfarra

  14. Retrieve data from the linked list 14 • Current = Head • For (int i = 0; Current != null ; i++) • Current = Current.Next; • return current.data Back Current 4 Current Current Head 2 Current 3 1 Data 1 Data k-1 Data k Data n Data k+1 null Presented & Prepared by: Mahmoud R. Alfarra

  15. Delete an item from the linked list 15 • Deleting an item consists of removing it from the list. • There are two main types of deletion you can perform on a list. • You can simply ask the class to delete an item. In this case, it is usually the item at the end that gets deleted. • Another technique used to delete an item consists of specifying the position of the item to be deleted. Presented & Prepared by: Mahmoud R. Alfarra

  16. Delete item from the back of the linked list 16 • (Last-1)node.next = null • Back = (Last-1)node × Back Head × 2 ….. Data 1 Data 2 Data n-1 Data n 1 null Presented & Prepared by: Mahmoud R. Alfarra

  17. Delete item from the front of the linked list 17 • Current = head.next • Head = current • Size -- Back Current Head × ….. Data 1 Data 2 Data n-1 Data n null Presented & Prepared by: Mahmoud R. Alfarra

  18. Delete the k’th item from the linked list 18 • current = position(k-1) • current.next = position(k+1) • Size -- 1 Back Head × × Data 1 Data k-1 Data k Data n Data k+1 null HW 7.4 Write the methods to perform the deletion operations Presented & Prepared by: Mahmoud R. Alfarra

  19. Search about data in the linked list 19 • Wanted data • Current = Head • For (int i = 0; Current != null && current.data != wanted ; i++) • If (current.data == wanted) • return i • break • Current = Current.Next; HW 7.4 Write the methods to perform the search operations Presented & Prepared by: Mahmoud R. Alfarra

  20. Emank X Mezank !! أخي أنت حرٌّ وراء السدود ..... أخي أنت حرٌّ بتلك القيود إذا كنـت بالله مستعصمـا .... فماذا يضيرك كيد العبيد؟!!

  21. Next Lecture Sorting& Searching

More Related