Download presentation
Presentation is loading. Please wait.
Published byJemima York 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 6: Linked List(1)
2
Outline Self-Referential Classes Example What is a Linked List? Creating the Items of a List Implementing a Linked List Add item as the last of the linked list Emank X Mezank !!
3
Self-Referential Classes A self-referential class contains a reference member that refers to an object of the same class type. Self-referential objects can be linked together to form useful data structures, such as lists, queues, stacks and trees. 3 Presented & Prepared by: Mahmoud R. Alfarra Class example Example a;
4
Example 4 Presented & Prepared by: Mahmoud R. Alfarra class Node { private int data; private Node next; public Node( int d ) { }
5
Self-Referential Classes Operator new is essential to dynamic memory allocation. Operator new takes as an operand the type of the object being dynamically allocated and returns a reference to a newly created object of that type. For example, Node nodeToAdd = new Node( 10 ); 5 Presented & Prepared by: Mahmoud R. Alfarra
6
What is a Linked List? A linked list is a technique of creating a list with the ability to add, delete, or retrieve items. Additional operations can also be provided to a more elaborate list such as finding an item, deleting an item, etc 6 Presented & Prepared by: Mahmoud R. Alfarra
7
The Items of a List Before creating a list, you probably should first decide or define what the list would be made of. As different as they are, one list can be made of numbers. For example, a list that will be made of numbers can define the item's class as follows: 7 Presented & Prepared by: Mahmoud R. Alfarra
8
Implementing a Linked List After deciding what each item of the list would be made of, you can create a class that would manage the list. This class would be responsible for all operations that can be performed on the list. Here is an example: 8 Presented & Prepared by: Mahmoud R. Alfarra
9
Linked List Example using System; class CarPart { public long PartNumber; public string PartName; public double UnitPrice; public CarPart Next; } 9 Presented & Prepared by: Mahmoud R. Alfarra Self-referential class Self-referential class
10
Linked List Example class ListOfParts { private int size; public ListOfParts() { size = 0; Head = null; } public int Count { get { return size; } } public CarPart Head; } 10 Presented & Prepared by: Mahmoud R. Alfarra Managment class
11
Linked List Example class ListOfParts { private int size; public ListOfParts() { size = 0; Head = null; } public int Count { get { return size; } } public CarPart Head; } 11 Presented & Prepared by: Mahmoud R. Alfarra Managment class This class will be contained the linked list’s operations as Add Remove Item Retrieval Search sort
12
Add item as the last of the linked list 12 Presented & Prepared by: Mahmoud R. Alfarra Data 1 Front Data 2Data 3Data n ….. null New data Head 1 × 2 3 To add an item as the last of linked list, you must save its head then set the previous points to it.
13
Add item as the last of the linked list class ListOfParts {. public int Add(CarPart NewItem) { CarPart Sample = new CarPart(); Sample = NewItem; Sample.Next = Head; Head = Sample; return size++; } 13 Presented & Prepared by: Mahmoud R. Alfarra Managment class Operation of Add items
14
Linked List Example class Exercise { static int Main() { ListOfParts Parts = new ListOfParts(); CarPart Part = new CarPart(); Part.PartNumber = 9743; Part.PartName = "Air Filter"; Part.UnitPrice = 8.75; Parts.Add(Part); Console.WriteLine("Number of Parts ", Parts.Count);// will print 1 } 14 Presented & Prepared by: Mahmoud R. Alfarra
15
Emank X Mezank !! أخي أنت حرٌّ وراء السدود..... أخي أنت حرٌّ بتلك القيود إذا كنـت بالله مستعصمـا.... فماذا يضيرك كيد العبيد؟!!
16
Next Lecture Item Retrieval And Deletion
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.