You are on page 1of 27

P

Please consider the environment before printing this presentation

Data Structures and Algorithms with C Lecture Notes by Eng. Ahmed Mohy El-Din, M.Sc.

Lecture #4 Linked Lists a Variable Length, Radom Access Li st Structure


Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

Data Structures & Algorithms with C

Please consider the environment before printing this presentation

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

Data Structures & Algorithms with C

Purposes of Linked Lists


Linked
n

lists are used for two main purpose:

To create arrays of unknown size in memory.


If you know the amount of storage in advance, you can use an array; but if you do not know the actual size of a. list, you must use a linked list. The linked list allows you to insert and delete items quickly and easily without rearranging- the entire disk file. For these reasons, linked lists are used extensively in database managers.

To store data in disk-file storage of databases .

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

Data Structures & Algorithms with C

Types of Linked Lists


Linked
n

lists can be either Singly-Linked or Doubly-Linked ,


Singly Linked List : a singly-linked list contains a link to the next data item.

Start 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.

Data Structures & Algorithms with C

Types of Linked Lists (cont )


n

Doubly-Linked List : a doubly-linked list contains link to both next and the previous element in the list.
End Node Pointer

Start 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.

Data Structures & Algorithms with C

Doubly-Linked List
Doubly-Linked

Lists consist of data and links to both the next item and the preceding item.
End Node Pointer

Start 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

Data Structures & Algorithms with C

Advantages of Doubly-Linked List


A

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.

Data Structures & Algorithms with C

Operations of Doubly-Linked List


The

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

for a particular Node.


8

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

Data Structures & Algorithms with C

Building a Doubly-Linked List


The

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.

Data Structures & Algorithms with C

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

Employee Data pPrevious pNext

Node Element
pStart pLast

Start & End Nodes Pointers


10

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

Data Structures & Algorithms with C

n Node Element Defi nition /* Initializing the Empty List */ pStart = pLast = NULL;

Employee Data pPrevious pNext

Node Element
pStart pLast

Null

Null

Start & End Nodes Pointers


Draft Copy DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc. 11

Data Structures & Algorithms with C

Adding a New Node to the Li st


Using

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

Data Structures & Algorithms with C

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

Data Structures & Algorithms with C

Searching for a Node in the List


Using

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

Data Structures & Algorithms with C

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

Data Structures & Algorithms with C

Deleting a Node from the List


There

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

Data Structures & Algorithms with C

Deleting a Node from the List (cont )

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

17

Data Structures & Algorithms with C

Deleting a Node from the List (cont )


Using

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

Data Structures & Algorithms with C

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

Data Structures & Algorithms with C

DeleteList() Function (cont )


if (pStart == pLast) /* Case of one node */ { pStart = pLast = NULL; } else if(pItem == pStart) /* Case of first node */ { pStart = pStart->pNext; pStart->pPrevious = NULL; } else if(pItem == pLast) /* Case of last node */ { pLast = pLast->pPrevious; pLast->pNext = NULL; } (cont)

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

20

Data Structures & Algorithms with C

DeleteList() Function (cont )


else { } free(pItem); } return RetFlag; /* Case of intermediate node */ pItem->pPrevious->pNext = pItem->pNext; pItem->pNext->pPrevious = pItem->pPrevious;

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

21

Data Structures & Algorithms with C

Removing the Entire List


Using

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

Data Structures & Algorithms with C

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

Data Structures & Algorithms with C

Please consider the environment before printing this email

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

Lab #8: Searching Algorithms

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

Data Structures & Algorithms with C

Assignment 2 Hint:

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

26

Data Structures & Algorithms with C

Please consider the environment before printing this presentation

Draft Copy

DSA with C - Lecture Notes, Copyr ight 2010 by Eng. Ahmed Mohy El-Din, M.Sc.

27

You might also like