Download presentation
Presentation is loading. Please wait.
Published byTyrone Osborne Modified over 9 years ago
1
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Information Technology, 3’rd Semester Lecture 7: Linked List(2)
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. 3 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2Data 3Data n ….. null New data Back 1
4
Add item in the front of linked list New.Next = first; Front = new; Size ++ 4 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2Data 3Data n ….. null New data Back 1
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. 5 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2Data 3Data n ….. null New data Back 1 New node will be inserted at the 3’rd place 012n 2 ×
6
Add item in the n’th place of linked list New.Next = n’th_node; (n-1)’th_node.next = new; Size ++ 6 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2Data 3Data n ….. null New data Back 1 New node will be inserted at the 3’rd place 012n 2 ×
7
Print the data of the linked list 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 … 7 Presented & Prepared by: Mahmoud R. Alfarra Item Current = Head; for(int i = 0; Current != null; i++) Print current.data; Current = Current.Next;
8
Example (Linked List of cars) 8 Presented & Prepared by: Mahmoud R. Alfarra class Car { public string name; public Car next; } Build the item list of car: 1.Which is a class of car 2.Contains the car’s data 3.Contains a variable of car to point to the next node 11
9
Example (Linked List of cars) 9 Presented & Prepared by: Mahmoud R. Alfarra class LinkedCar { private int length ; public Car head; public LinkedCar() { lenght = 0; head = null; }} Build the list of car : 1.Which is a management class contains 2.Length of list which begins as 0 3.A variable of car as the pointer of each node and begins as null 4.All the operations 22
10
Example (operations on Linked List) 10 Presented & Prepared by: Mahmoud R. Alfarra Add a new car to the linked list in the front public void addAtFront(Car inserted) { Car newc = new Car (); newc = inserted; newc.next = head; lenght++; head = newc; } In the class of management (LinkedCar), we declare all the operations as add, remove, sort, … 33 Write the methods to add a new elements in the n’th position and as the last node HW 7.1
11
Example (operations on Linked List) 11 Presented & Prepared by: Mahmoud R. Alfarra Print the information of the cars in the LinkedList 44 public void printCarData() { Car current = head; for (int i =0 ; current != null; i++) { Console.WriteLine("The name is "+ current.name); current = current.next; } Write the methods to print the data of i’th node HW 7.2
12
Example (create an object of LinkedCar) 12 Presented & Prepared by: Mahmoud R. Alfarra Create a linkedlist of cars The class of management (LinkedCar) is just a class. so, need to create object to perform the operations … 55 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(); }
13
Home Work 13 Presented & Prepared by: Mahmoud R. Alfarra Re-write the previous example using the windows application, as the above figure HW 7.3
14
Retrieve data from the linked list 14 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data k-1 Data k Data n null Back Data k+1 Current 1 2 3 4 Current = Head For (int i = 0; Current != null ; i++) Current = Current.Next; return current.data
15
Delete an item from the linked list Deleting an item consists of removing it from the list. There are two main types of deletion you can perform on a list. 1. You can simply ask the class to delete an item. In this case, it is usually the item at the end that gets deleted. 2.Another technique used to delete an item consists of specifying the position of the item to be deleted. 15 Presented & Prepared by: Mahmoud R. Alfarra
16
Delete item from the back of the linked list (Last-1)node.next = null Back = (Last-1)node 16 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2 Data n-1 Data n ….. null Back 1 × × 2
17
Delete item from the front of the linked list Current = head.next Head = current Size -- 17 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data 2 Data n-1 Data n ….. null Back × Current
18
Delete the k’th item from the linked list current = position(k-1) current.next = position(k+1) Size -- 18 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Head Data k-1 Data k Data n null Back Data k+1 1 ×× Write the methods to perform the deletion operations HW 7.4
19
Search about data in the linked list 19 Presented & Prepared by: Mahmoud R. Alfarra Write the methods to perform the search operations HW 7.4 Wanted data Current = Head For (int i = 0; Current != null && current.data != wanted ; i++) If (current.data == wanted) return i break Current = Current.Next;
20
Emank X Mezank !! أخي أنت حرٌّ وراء السدود..... أخي أنت حرٌّ بتلك القيود إذا كنـت بالله مستعصمـا.... فماذا يضيرك كيد العبيد؟!!
21
Next Lecture Sorting& Searching
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.