DATA STRUCTURES LAB 6 TA Nouf AlHarbi nouf
DATA STRUCTURES LAB 6 TA: Nouf Al-Harbi nouf 200@hotmail. com
DATA STRUCTURES LAB 6 Linked List
objectives 3 By the end of this lab you will be able to : � Implement a linked list class with all its operations Linked List
What’s Linked List. . ? 4 A B C Head A linked list is a series of connected nodes Each node contains at least �A piece of data (any type) � Pointer to the next node in the list Head: pointer to the first node The last node points to NULL Linked List
Linked List Operations 5 sets the list to an empty state � Is. Empty: determine whether or not the list is empty � Insert. Node: insert a new node at a particular position � Find. Node: find a node with a given value � Delete. Node: delete a node with a given value � Display. List: print all the nodes in the list � Make. Empty: Linked List
6 Linked List Example. . Implement a linked list of double numbers Linked List
Pointers Operator (*, ->, &) 7 *ptr_exp &lvalue ptr>member • Dereferences a pointer. • That is, finds the contents stored at the virtual address that ptr_exp holds • Finds the virtual address where the lvalue stored. • Dereferences a ptr to a structure or union where member is a member of that structure or union. Linked List
8 Implementing Linked. List Class 1 - class declaration Linked. List. Type. h Linked List
Linked. List. Type. h 9 Linked List
10 Implementing Linked. List. Type Class 2 - implementation of all the methods Linked. List. Type. cpp Linked List
Linked List Counstructor 11 Linked List
Linked List Destructor 12 Linked List
Make. Empty Method 13 Linked List
Is. Empty Method 14 Linked List
Inserting a new node 15 Node* Insert. Node(int index, double x) � � Insert a node with data equal to x after the index’th elements. (i. e. , when index = 0, insert the node as the first element; when index = 1, insert the node index’th after the first element, and so on) element If the insertion is successful, return the inserted node. Otherwise, return NULL. (If index is < 0 or > length of the list, the insertion will fail. ) Steps 1. 2. 3. 4. Locate index’th element Allocate memory for the new node Point the new node to its successor Point the new node’s predecessor to the new node Linked List new. Node
Inserting a new node 16 Possible cases of Insert. Node 1. 2. 3. 4. Insert into an empty list Insert in front Insert at back Insert in middle But, in fact, only need to handle two cases � � Insert as the first node (Case 1 and Case 2) Insert in the middle or at the end of the list (Case 3 and Case 4) Linked List
Insert. Node Method 17 Linked List
Find a Node 18 int Find. Node(double x) � Search for a node with the value equal to x in the list. � If such a node is found, return its position. Otherwise, return 0. Linked List
Find. Node Method 19 Linked List
Delete a Node 20 int Delete. Node(double x) � Delete a node with the value equal to x from the list. � Steps � Find the desirable node (similar to Find. Node) � Release the memory occupied by the found node Set the pointer of the predecessor of the found node to the successor of the found node � If such a node is found, return its position. Otherwise, return 0. Like Insert. Node, there are two special cases � � Delete first node Delete the node in middle or at the end of the list Linked List
Delete. Node Method 21 Linked List
Display. List Method 22 Linked List
23 Implementing Linked List Class 3 - Main function Linked. List. cpp Linked List
In Main function. . 24 Linked List
25 Linked List
26 Linked List
27 Linked List
28 Linked List
29 Linked List
- Slides: 29