27
Linked Lists in Data Structure Department of Information Technology University of The Punjab, Jhelum Campus By Mohd. Umair Hassan (BCS-F13-23)

Linked Lists

Embed Size (px)

Citation preview

Linked Lists in Data Structure

Department of Information TechnologyUniversity of The Punjab,

Jhelum Campus

By Mohd. Umair Hassan (BCS-F13-23)

Linked Lists in Data Structures 2

Why Lists

• Most simple and effective way to store things• Shopping lists

• Works very well for the following• Small size of list items• Does not require any ordering of items• Typically, no searching is required• Processing is typically done in the order items are added to list

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 3

Defining Lists

• Linked List is an ordered collection of elements called nodes which has two parts• The nodes are connected by pointers

• The Data part and Next part• The data contains elements • Next contains address of another node

Data Next

Information part : store element of the list

Address part : pointer that indicates the location of next node

Data Next Data \Head

If no next node, the pointer contains a NULL value

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 4

Array vs. Linked ListAspect Array Linked List

Size • Fixed number• Size need to be specific during

declaration

• Grow and contract since of insertions and deletions

• Maximum size depends on heap

Storage capacity • Static: It’s location is allocated during compile time

• Dynamic: It’s node is located during run time

Order and sorting • Stored consecutively • Stored randomly

Accessing the element • Direct or random access method• Specify the array index or

subscript

• Sequential access method• Traverse starting from the first

node in the list by pointer

Searching • Binary search and linear search • Linear search

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 5

List: Technical Terms

• Empty List• When there are no elements in the list

• List Size:• Total number of elements in the list

• Head: Identifies beginning of the list• May refer to first element of the list

• Tail: Identifies last element of the list• Typically, refer to last element of the list

• Node:• Implementation of list data item

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 6

List: Basic Requirements

• List should be able to grow• Provides ability to add or insert elements

• List should be able to shrink• Provides ability to remove or delete elements

• Accessing list item• Provides ability to access (or read) any element• Provides ability to modify items (contents of node)

• Initialize• Ability to clear or re-initialize list• Create an empty list

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 7

List: Basic Operations

• The basic operations on liked lists are• Creation • Insertion• Deletion• Traversing• Searching• Concatenation• Display

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 8

List: Basic Operations

• Creation• The creation operation is used to create a linked list.

• Insertion• The insertion operation is used to insert a new node in the linked list at the

specified position. A new node may be inserted at the beginning of a linked list , at the end of the linked list , at the specified position in a linked list. If the list itself is empty , then the new node is inserted as a first node.

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 9

List: Basic Operations

• Deletion• The deletion operation is used to delete an item from the linked list. It may be

deleted from the beginning of a linked list , specified position in the list.

• Traversing• The traversing operation is a process of going through all the nodes of a linked

list from one end to the another end. If we start traversing from the very first node towards the last node, it is called forward traversing.• If the traversal start from the last node towards the first node , it is called

back word traversing.

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 10

List: Basic Operations

• Searching• The searching operation is a process of accessing the desired node in the list.

We start searching node –by-node and compare the data of the node with the key.

• Concatenation• The concatenation operation is the process of appending the second list to

the end of the first list. When we concatenate two lists , the resultant list becomes larger in size.

• Display• The display operation is used to print each and every node’s information.

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 11

List: Basic Operations

• Create a dynamic node• Create a list• Traversing a list• Insert node• Delete node

• Example:

struct NumNode

{ int value;

NumNode * Next;

};

NumNode *head, *current, *temp, *Nptr;

head=NULL; //the list is empty

To hold the list

To traverse the list

To mark a node in the list

To allocate new node

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 12

Create a Dynamic Node

• Using new operator.• Example :

Nptr = new NumNode; cout<<“Enter a number: “; Nptr NumNode

cin>>Nptr->value;Nptr->Next=NULL;

10

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 13

Delete a Dynamic Node

• Using delete operator.

delete Nptr; //delete the node pointed by NptrNptr=Null; //set the pointer to Null• If the link list more than one node: Need to change pointer of record before the one

being deleted.

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 14

Create a Linked List

• Using new operator.

Example :

// create a node

Nptr = new NumNode;cout<<“Enter a number: “;cin>>Nptr->value;Nptr->Next=NULL; //test if the list empty if (head==NULL) head=Nptr; //head pointer to //the new node

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 15

Traversing a Linked List

• There are purposes:• To process every node in the list.

• To find the last node in the list.while (current->Next != NULL)current=current->Next;University of The Punjab, Jhelum

Campus

Linked Lists in Data Structures 16

//create a new listNptr=new NumNode;cout<<“Enter a new number: “;cin>>Nptr->value;Nptr-Next=NULL;//test if the list is emptyif (head==NULL)head=Nptr; //head pointer points to the new nodeelse //head pointer points to new node{ Nptr->Next=head; head=Nptr;}

10

head

15

head=Nptr

Inserting Node at the Beginning of the List

Process

Nptr->Next=Head

Nptr

15

head

10

Nptr

Result

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 17

Linked List (continued)

struct listItem {type payload;struct listItem *next;

};struct listItem *head;

payload

nextpayloadnextpayload

next

payload

next

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 18

Adding an Item to a List

struct listItem *p, *q;• Add an item pointed to by q after item pointed to by p

• Neither p nor q is NULL

payload

nextpayload

nextpayload

next

payload

next

payload

next

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 19

Adding an Item to a List

listItem *addAfter(listItem *p, listItem *q){q -> next = p -> next;p -> next = q;return p;

}

payload

nextpayload

nextpayload

next

payload

next

payload

next

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 20

Adding an Item to a ListlistItem *addAfter(listItem *p, listItem *q){

q -> next = p -> next;p -> next = q;return p;

}

payload

nextpayload

nextpayload

next

payload

next

payload

next

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 21

Adding an Item to a ListlistItem *addAfter(listItem *p, listItem *q){

q -> next = p -> next;p -> next = q;return p;

}

payload

nextpayload

nextpayload

next

payload

next

payload

next

Question: What to do if we cannotguarantee that p and q are non-NULL?

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 22

Adding an Item to a List (continued)

listItem *addAfter(listItem *p, listItem *q){if (p && q) {

q -> next = p -> next;p -> next = q;

}return p;

}

payload

nextpayload

nextpayload

next

payload

next

payload

next

Note test for non-null p and q

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 23

What about Adding an Item before another Item?struct listItem *p;• Add an item before item pointed to by p (p != NULL)

payload

nextpayload

nextpayload

next

payload

next

payload

next

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 24

Advantages of Linked Lists

• We can dynamically allocate memory space as needed.• We can release the unused space in the situation where the allocated

space seems to be more.• Operation related to data elements like insertions or deletion are

more simplified.• Operation like insertion or deletion are less time consuming.• Linked lists provide flexibility in allowing the items to be arranged

efficiently.

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 25

Applications

• Linked lists are used in many other data structures.• Linked lists are used in

polynomial manipulation.• Representation of trees, stacks,

queues. etc.• The cache in your browser that

allows you to hit the BACK button (a linked list of URLs).

University of The Punjab, Jhelum Campus

• Real life example

Linked Lists in Data Structures 26

Applications

More practical applications would be:• A list of images that need to be burned to a CD in any imaging application• A list of users of a website that need to be emailed some notification• A list of objects in a 3D game that need to be rendered to the screen

University of The Punjab, Jhelum Campus

Linked Lists in Data Structures 27

Topic Finished…!!!

University of The Punjab, Jhelum Campus