Professional Documents
Culture Documents
Data Structures and Algorithms with C Lecture Notes by Eng. Ahmed Mohy El-Din, M.Sc.
Lecture #4
LINKED LISTS
A variable Length, Random Access List Structure
Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 2
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
Informati on 0
Informati on
Informati on 0
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
Doubly-Linked List : a doubly-linked list contains link to both next and the previous element in the list.
End Node Pointer
Informati on 0
Informati on
Informati on 0
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
Doubly-Linked List
Doubly-Linked
Lists consist of data and links to both the next item and the preceding item.
End Node Pointer
Informati on 0
Informati on
Informati on 0
Information : is usually complex data type containing a group of information (e.g., Employee data, Student Record, etc.)
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 6
Draft Copy
list that has two links instead of just one has two major advantages:
First , the list can be read in either direction. This not only simplifies sorting the list but also, the case of a database, allows a user to scan the list in either direction. n Second , because either a forward link or a backward link can read the entire list, if one link becomes invalid, the list can be reconstructed using the other link. This is meaningful only in the case of equipment failure.
n
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
primary operations than can performed on a doubly linked list are: Inserting New Nodes
Case 1: insert new first node. n Case 2: insert a new middle node. n Case 3: insert a new last node.
n
be
Deleting
n
Nodes
Case 1: delete a node. n Case 2: deleting all nodes (removing the list).
Searching
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
following example shows how to build a Doubly-Linked List, in C, using nodes of Employee Structure.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
n Node Element Defi nition struct Employee { int Code; char Name[20]; char Address[40]; int Age; float Salary; float OTime; float Deduct; struct Employee *pNext; struct Employee *pPrevious; }; struct Employee *pStart; struct Employee *pLast;
Draft Copy
Node Element
pStart pLast
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
n Node Element Defi nition /* Initializing the Empty List */ pStart = pLast = NULL;
Node Element
pStart pLast
Null
Null
Structure Employee as the basic data item, the AddList() function builds a doubly linked list. This function places each new entry on the end of the list (i.e., append the element to the end of the list).
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
12
AddList() Function
/* AddList() function builds a doubly linked list */ void AddList(struct Employee *pItem){ if (!pStart) /* Case of empty list Add the first node */ { pItem->pNext = NULL; pItem->pPrevious = NULL; pStart = pLast = pItem; } else /* Case of existing list nodes Append Node */ { pLast->pNext = pItem; pItem->pPrevious = pLast; pItem->pNext = NULL; pLast = pItem; } }
Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 13
Structure Employee as the basic data item, The SearchList() function searches for a specified employee code.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
14
SearchList() Function
/* SearchList() function searches for a specified employee code */ struct Employee * SearchList(int Code) { struct Employee * pItem; pItem = pStar t; while(pItem && pItem->Code != Code) { pItem = pItem ->pNext; } return pItem; }
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
15
are three cases to consider when deleting an element from a Doubly-Linked List:
Case1: Deleting the first item, n Case2: Deleting a middle item, and n Case3: Deleting the last item.
n
The
following figure shows how the links are rearranged in each case.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
16
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
17
Structure Employee as the basic data item, The DeleteList() function will delete an item of type Employee from a Doubly-Linked List.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
18
DeleteList() Function
/* DeleteList() function deletes a node from the list */ int DeleteList(int Code) { struct Employee *pItem; int RetFlag = 1; pItem = SearchList(Code); if (!pItem) { RetFlag = 0; } else { (cont )
Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 19
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
20
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
21
Structure Employee as the basic data item, The FreeList() function will remove the entire Doubly-Linked List of Employees from memory.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
22
FreeList() Function
/* FreeList() function will remove the entire list from memory */ void FreeList(void) { struct Employee * pItem; while(pStart) { pItem = pStar t; pStart = pStart->pNext; free(pItem); } pLast = NULL; }
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
23
Lab #4
LINKED LISTS
A variable Length, Random Access List Structure
Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 24
Assignments
1. 2.
Implement Linked List with all functionalities. Add the case of Inserting a Node to the array in the following Cases
1. 2. 3.
Adding new node at the start of the list. Adding new node at specif ied location. Adding new node at the end of the list.
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
25
Assignment 2 Hint:
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
26
Draft Copy
DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.
27