59
Unit 4 LINKED LIST

Linked List

Embed Size (px)

Citation preview

Page 1: Linked List

Unit 4LINKED LIST

Page 2: Linked List

Ashim Lamichhane 2

Introduction to List and Linked Lists• List is a term used to refer to a linear collection of data items. A List

can be implemented either by using arrays or linked lists.

• Usually, a large block of memory is occupied by an array which may not be in use and it is difficult to increase the size of an array.

• Another way of storing a list is to have each element in a list contain a field called a link or pointer, which contains the address of the next element in the list.

• The successive elements in the list need not occupy adjacent space in memory. This type of data structure is called a linked list.

Page 3: Linked List

Ashim Lamichhane 3

Linked List• It is the most commonly used data structure used to

store similar type of data in memory.

• The elements of a linked list are not stored in adjacent memory locations as in arrays.

• It is a linear collection of data elements, called nodes, where the linear order is implemented by means of pointers.

Page 4: Linked List

Ashim Lamichhane 4

Linked List• In a linear or single-linked list, a node is connected to

the next node by a single link.

• A node in this type of linked list contains two types of fields • data: which holds a list element• next: which stores a link (i.e. pointer) to the next node in the

list.

Page 5: Linked List

Ashim Lamichhane 5

Linked List• The structure defined for a single linked list is

implemented as follows:

• The structure declared for linear linked list holds two members• An integer type variable ‘data’ which holds the elements and• Another type ‘node’, which has next, which stores the

address of the next node in the list.

struct Node{ int info; struct Node * next;}

Page 6: Linked List

Ashim Lamichhane 6

Figurative Representation

Page 7: Linked List

Ashim Lamichhane 7

Properties of Linked list• The nodes in a linked list are not stored contiguously in

the memory

• You don’t have to shift any element in the list

• Memory for each node can be allocated dynamically whenever the need arises.

• The size of a linked list can grow or shrink dynamically

Page 8: Linked List

Ashim Lamichhane 8

Operations on Linked List• Creation:

• This operation is used to create a linked list• Insertion / Deletion

• At/From the beginning of the linked list• At/From the end of the linked list• At/From the specified position in a linked list

• Traversing:• Traversing may be either forward or backward

• Searching:• Finding an element in a linked list

• Concatenation:• The process of appending second list to the end of the first list

Page 9: Linked List

Ashim Lamichhane 9

Types of Linked List• Singly Linked List• Doubly linked list• Circular linked list• Circular doubly linked list

Page 10: Linked List

Ashim Lamichhane 10

Singly Linked List• A singly linked list is a dynamic data structure which

may grow or shrink, and growing and shrinking depends on the operation made.

• In this type of linked list each node contains two fields one is data field which is used to store the data items and another is next field that is used to point the next node in the list.

5 3 8 null

next next nextinfo info info

Page 11: Linked List

Ashim Lamichhane 11

Creating a Linked List• The head pointer is used to create and access unnamed

nodes.

• The above statement obtains memory to store a node and assigns its address to head which is a pointer variable.

struct Node{ int info; struct Node* next;};typedef struct Node NodeType;NodeType* head; head=(NodeType *) malloc (sizeof( NodeType ) );

Page 12: Linked List

Ashim Lamichhane 12

Creating a Node• To create a new node, we use the malloc function to

dynamically allocate memory for the new node.

• After creating the node, we can store the new item in the node using a pointer to that node.

• Note that p is not a node; instead it is a pointer to a node.

Nodetype *p;p=(NodeType *) malloc (sizeof( NodeType ) );p->info=50;p->next = NULL;

Page 13: Linked List

Ashim Lamichhane 13

Creating an empty list

void createEmptyList(NodeType *head){

head=NULL;}

OR SIMPLY

NodeType *head =Null;

Page 14: Linked List

Ashim Lamichhane 14

Inserting an Element• While inserting an element or a node in a linked list, we

have to do following things:• Allocate a node• Assign a data to info field of the node.• Adjust a pointer

• We can insert an element in following places• At the beginning of the linked list• At the end of the linked list• At the specified position in a linked list

Page 15: Linked List

Ashim Lamichhane 15

An algorithm to insert a node at the beginning of the singly linked listLet *head be the pointer to first node in the current list 1. Create a new node using malloc function

NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node

NewNode->info=newItem; 3. Set next of new node to head

NewNode->next=head; 4. Set the head pointer to the new node

head=NewNode; 5. End

Page 16: Linked List

Ashim Lamichhane 16

Inserting a node at the beginning of the singly linked list

Page 17: Linked List

Ashim Lamichhane 17

An algorithm to insert a node at the end of the singly linked listlet *head be the pointer to first node in the current list 1. Create a new node using malloc function

NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node

NewNode->info=newItem; 3. Set next of new node to NULL

NewNode->next=NULL; 4. if (head ==NULL) then

Set head =NewNode.and exit. 5. Set temp=head; 6. while(temp->next!=NULL)

temp=temp->next; //increment temp 7. Set temp->next=NewNode; 8. End

Page 18: Linked List

Ashim Lamichhane 18

Page 19: Linked List

Ashim Lamichhane 19

An algorithm to insert a node after the given node in singly linked listlet *head be the pointer to first node in the current list and *p be the pointer to the node after which we want to insert a new node. 1. Create a new node using malloc function

NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node

NewNode->info=newItem;3. Set next of new node to next of p

NewNode->next=p->next; 4. Set next of p to NewNode

p->next =NewNode 5. End

Page 20: Linked List

Ashim Lamichhane 20

Page 21: Linked List

Ashim Lamichhane 21

An algorithm to insert a node at the specified position in a linked list let *head be the pointer to first node in the current list 1. Create a new node using malloc function

NewNode=(NodeType*)malloc(sizeof(NodeType)); 2. Assign data to the info field of new node

NewNode->info=newItem; 3. Enter position of a node at which you want to insert a new node. Let

this position is pos. 4. Set temp=head;5. if (head ==NULL)then

printf(“void insertion”); and exit(1). 6. for(i=1; i<pos; i++)

temp=temp->next;7. Set NewNode->next=temp->next;

set temp->next =NewNode.. 8. End

Page 22: Linked List

Ashim Lamichhane 22

Deleting Nodes

• A node may be deleted:• From the beginning of the linked list• From the end of the linked list• From the specified position in a linked list

Page 23: Linked List

Ashim Lamichhane 23

Page 24: Linked List

Ashim Lamichhane 24

Deleting first node of the linked list

Page 25: Linked List

Ashim Lamichhane 25

Page 26: Linked List

Ashim Lamichhane 26

Page 27: Linked List

Ashim Lamichhane 27

Page 28: Linked List

Ashim Lamichhane 28

Searching an item in a linked list• Let *head be the pointer to first node in the current list1. If head==Null

Print “Empty List”2. Else, enter an item to be searched as key3. Set temp==head 4. While temp!=Null

If (temp->info == key)Print “search success”

temp=temp->next;

5. If temp==NullPrint “Unsuccessful search”

Page 29: Linked List

Ashim Lamichhane 29

Page 30: Linked List

Ashim Lamichhane 30

Linked list implementation of Stack

Page 31: Linked List

Ashim Lamichhane 31

Page 32: Linked List

Ashim Lamichhane 32

Linked list implementation of queue

Page 33: Linked List

Ashim Lamichhane 33

Page 34: Linked List

Ashim Lamichhane 34

Circular Linked List• A circular linked list is a list where the link field of last

node points to the very first node of the list.

• Complicated linked data structure.

• A circular list is very similar to the linear list where in the circular list pointer of the last node points not Null but the first node.

Page 35: Linked List

Ashim Lamichhane 35

C representation of circular linked list• We declare structure for the circular linked list in the

same way as linear linked list.

Page 36: Linked List

Ashim Lamichhane 36

Algorithms to insert a node in a circular linked list

Page 37: Linked List

Ashim Lamichhane 37

Algorithm to insert a node at the end of a circular linked list

Page 38: Linked List

Ashim Lamichhane 38

Algorithms to delete a node from a circular linked list

Page 39: Linked List

Ashim Lamichhane 39

Page 40: Linked List

Ashim Lamichhane 40

Stack as a circular List • To implement a stack in a circular linked list, let pstack

be a pointer to the last node of a circular list.

• There is no any end of a list but for convention let us assume that the first node(rightmost node of a list) is the top of the stack.

• An empty stack is represented by a null list.

Page 41: Linked List

Ashim Lamichhane 41

Stack as a circular List

Page 42: Linked List

Ashim Lamichhane 42

Page 43: Linked List

Ashim Lamichhane 43

Page 44: Linked List

Ashim Lamichhane 44

Page 45: Linked List

Ashim Lamichhane 45

Queue as a circular List • It is easier to represent a queue as a circular list than as

a linear list.

• As a linear list a queue is specified by two pointers, one to the front of the list and the other to its rear.

• However, by using a circular list, a queue may be specified by a single pointer pq to that list.

• node(pq) is the rear of the queue and the following node is its front.

Page 46: Linked List

Ashim Lamichhane 46

Queue as a circular List

Page 47: Linked List

Ashim Lamichhane 47

Queue as a circular List

Page 48: Linked List

Ashim Lamichhane 48

Queue as a circular List : Deletion function

Page 49: Linked List

Ashim Lamichhane 49

Doubly Linked List • A linked list in which all nodes are linked together by

multiple number of links i.e. each node contains three fields (two pointer fields and one data field) rather than two fields is called doubly linked list.

• It provides bidirectional traversal.

Page 50: Linked List

Ashim Lamichhane 50

Page 51: Linked List

Ashim Lamichhane 51

Page 52: Linked List

Ashim Lamichhane 52

Page 53: Linked List

Ashim Lamichhane 53

Page 54: Linked List

Ashim Lamichhane 54

Page 55: Linked List

Ashim Lamichhane 55

Circular Doubly Linked List • A circular doubly linked list is one which has the successor and

predecessor pointer in circular manner.

• It is a doubly linked list where the next link of last node points to the first node and previous link of first node points to last node of the list.

• The main objective of considering circular doubly linked list is to simplify the insertion and deletion operations performed on doubly linked list.

Page 56: Linked List

Ashim Lamichhane 56

Page 57: Linked List

Ashim Lamichhane 57

Page 58: Linked List

Ashim Lamichhane 58

Assignments• Slides at Slidesharehttp://www.slideshare.net/AshimLamichhane/linked-list-63790316

• Assignments at githubhttps://github.com/ashim888/dataStructureAndAlgorithm

Page 59: Linked List

Ashim Lamichhane 59

Reference• http://www.learn-c.org/en/Linked_lists• http://

www.tutorialspoint.com/data_structures_algorithms/linked_list_algorithms.htm• http://

www.studytonight.com/data-structures/introduction-to-linked-list• http://

www.tutorialspoint.com/data_structures_algorithms/circular_linked_list_algorithm.htm• http://

www.studytonight.com/data-structures/circular-linked-list• http://

geekswithblogs.net/MarkPearl/archive/2010/02/20/linked-lists-in-c.aspx