Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linked List: by Mrs. Preeti S. Patil

Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 52

Chapter 2 Linked List

By Mrs. Preeti S. Patil

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Linked List
Non-sequential collection of data items For every data item, there is an associated pointer that would give memory location of the next data item in the linked list. Data items are not in a consecutive memory locations.
Start 10
Preeti S. Patil

20

30

40 Null

Linked List MPSTME, SHIRPUR

Advantages (Linked List)


Are dynamic data structures Efficient memory utilization Insertion and deletion are easier and efficient. Complex applications can be carried out using linked list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Components of linked list.


Non sequential collection of data items called nodes. Nodes are structures containing fields. Each node in linked list basically contains two fields.
Data Field Link Field
Data Field: contains actual value to be stored and processed. Link Field: contains address of the next data item (or node). The address used to access a particular node is known as pointer.
Preeti S. Patil Linked List MPSTME, SHIRPUR

Linked list with logical links


The data values stored in the data fields of these four nodes are 10,20,30,40 respectively. First node is pointed by start. Last node is set to NULL(i.e zero). Assume the starting address of the 1st,2nd,3rd,4th nodes are 1000,2000,3000& 4000 respectively.
Start
Data Link
2000

Data

Link
3000

Data

Link
4000

Data

Link
0

10

20

30

40

1000
Preeti S. Patil

2000

3000

4000

Linked List MPSTME, SHIRPUR

Null Pointer: Link field of last node contains zero rather than valid address.it is a null pointer and indicates the end of the list. External pointer(start): it is a pointer to the very first node in the linked list. Enables to access the entire list. Empty List/Null list: if the nodes are not present in the linked list. Start=Null;
Preeti S. Patil Linked List MPSTME, SHIRPUR

Every node in the linked list is a structure containing two fields such as data and link field. Physical view:
Data Link
Node

Representation of Linked List

Logical view: Struct node


{ int data; struct node *link; };

Node has two fields.

First is an integer data item & second is a link (pointer) to the next node of the same type.
Preeti S. Patil Linked List MPSTME, SHIRPUR

Structure for a node having many fields can be defined as


struct node { data_type1 member1; data_type2 member2; data_type3 member3; . . data type n membern; struct node *link; }; Pictorial Representation:
member 2 member 3 member n link

member1

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Example
Node with the four fields:
Roll No Stud _Name Percentage link

The corresponding representation.


struct student_node {

int Roll_no; char stud_name[20]; float percentage; struct student_node *link;


};

Preeti S. Patil

Linked List MPSTME, SHIRPUR

struct student_node snode1,snode2; // reserves memory for two nodes.


Shown pictorially as follows:
Snode1.Rollno Snode1.stud_name Snode2.Rollno Snode2.stud_name

Snode1.percentage
Snode1.link

Snode2.percentage
Snode2.link

Establishing link between two node is possible with following stn. Snode1.link=&snode2; // link field of snode1 holds the address of snode2
Rollno stud_name percentage 2000 Rollno stud_name percentage

Note::2000 is the address of snode2 stored in the link field of snode1.


Preeti S. Patil Linked List MPSTME, SHIRPUR

Snode1 information
Snode1.Roll_no=123; Snode1.stud_name=kumar; Snode1.percentage=85.00;

Snode2 information
Snode2.Roll_no=147; Snode2.stud_name=saraswati; Snode2.percentage=89.00;

123 kumar 85.00 2000

147 saraswati 89.00

Note: link field of snode2 will be set to zero to signal end of the list. Snode2.link=0;
Preeti S. Patil Linked List MPSTME, SHIRPUR

Basic Operations on Linked List


Creation: to create a linked list. Insertion: insert a new node in linked list. Deletion: to delete an item(a node). Traversing: going thru all the nodes. Searching: accessing the desired node. Concatenation: appending the second list to the
end of first list.

Display: to print each and every nodes information


Preeti S. Patil Linked List MPSTME, SHIRPUR

Singly Linked List


Dynamic data structure. In C Created using structures, pointers and dynamic memory allocation using malloc. External pointer head helps in creating and accessing other nodes in the linked list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

struct node { int num; struct node *ptr; //Pointer to node }; typedef struct node NODE; //type definition making it abstract
data type

NODE * head; //pointer to the node of linked list head=(NODE *) malloc(sizeof(NODE)); //Dynamic
memory allocation

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Inserting nodes
Three things to be done
Allocating a node Assigning a data. Adjusting the pointers.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Instances for insertion


Inserting a new node into the linked list has three instances
Insertion at the beginning of the list. Insertion at the end of the list. Insertion at the specified position within the list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Inserting a Node at the Beginning


Algorithm 1. Allocate memory for the new node. 2. Assign the value to the data field of the new node. 3. Make the link field of the new node to point to the starting node of the linked list. 4. Then set the external pointer(which was pointing to the starting node)to point to the new node.
Preeti S. Patil Linked List MPSTME, SHIRPUR

Insert node at the beginning


head
10
Node 1

20
Node 2

30
Node 3

40
New node

Preeti S. Patil

Linked List MPSTME, SHIRPUR

C code for inserting node


Insert_beg(NODE *head,int item) { NODE *new_node; new_node=allocate_node(); new_node->num=item; new_node->ptr=head; head= new_node; return (new_node); } and allocate_node() has following definition

NODE *allocate_node() { return((NODE*)malloc(sizeof(NODE))); }


Preeti S. Patil Linked List MPSTME, SHIRPUR

Inserting a Node at the End


Algorithm 1. If the list is empty then create the new node. 2. If the list is not empty, then go to the last node and then insert the new node after last node.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Insert node at the end

head 10
Node 1

20
Node 2

30
Node 3

40
New node

Preeti S. Patil

Linked List MPSTME, SHIRPUR

C code for inserting node at the end


Insert_end(NODE *head,int item) { NODE *new_node, *temp; if(head==NULL) { head=allocate_node(); head->num=item; head->ptr=NULL; }

Preeti S. Patil

Linked List MPSTME, SHIRPUR

else { temp=head; while(temp->ptr!=NULL) { temp=temp->ptr; }

new_node=allocate_node(); new_node->num=item; new_node->ptr=NULL; temp->ptr=new_node; } }


Preeti S. Patil Linked List MPSTME, SHIRPUR

Inserting a new node at the specified location


Assume that the new node to be inserted between Node A and Node B. 1. Allocate memory for the new node. 2. Assign value to the data field of the new node. 3. Make the link field of the new node to point Node B. 4. Make the link field of the Node A to point new node.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Inserting a new node at the specified location

head 10 20
Node A

30
Node B

40
New node

Preeti S. Patil

Linked List MPSTME, SHIRPUR

C code for inserting node at the specified location


Insert_LOC(NODE *head,int item, int count) { NODE *new_node, *temp; int k; for(k=0, temp=head;k<count;k++) { temp=temp->ptr; if(temp=NULL) { printf(nodes in the list are less than count\n); return; } } new_node=allocate_node(); new_node->num=item; new_node->ptr=temp->ptr; temp->ptr=new_node; }
Preeti S. Patil Linked List MPSTME, SHIRPUR

Deleting Nodes
Three instances for deleting node
1. Deleting the first node of the linked list. 2. Deleting the last node of the linked list. 3. Deleting the specified node within the linked list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Deleting the First Node


Algorithm
1. 2. 3. If the list is not empty then check whether the element to be deleted belongs to the first node of the list. Move the head pointer to the second node. Free the first node.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Deleting the First Node

head
temp

10

20

30

Preeti S. Patil

Linked List MPSTME, SHIRPUR

C code for deleting the first node


Delete_beg(NODE *head) { NODE *temp; int item; if(head==NULL) { printf(Empty list\n); return; } else { printf(Enter the element to be deleted); scanf(%d, &item); if(head->num==item) { temp=head->ptr; free(head); //delete head=temp; //reset head } } }
Preeti S. Patil Linked List MPSTME, SHIRPUR

Deleting the Last node


Algorithm 1. if the linked list is not empty, go on traversing the list till the last but one node. 2. Set the link field of the last but one node to NULL 3. Free the last node.
Preeti S. Patil Linked List MPSTME, SHIRPUR

Deleting the last node


head 10 temp1 20 temp2 30

Preeti S. Patil

Linked List MPSTME, SHIRPUR

The c code for deleting the last node


Delete_end(NODE *head) { NODE *temp1,*temp2; if(head==NULL) {printf(Empty List); return; } else { temp1=NULL; temp2=head; while(temp2->ptr != NULL) { temp1= temp2; temp2=temp2->ptr; } } printf( Element deleted is = %d\n,temp2->num); free(temp2); if(temp1 != NULL) { temp1->ptr= NULL; } else head= NULL; } Preeti S. Patil Linked List
MPSTME, SHIRPUR

Deleting the node from the specified position


Algorithm for deleting the node in between two nodes NODE A and NODE B
1. If the linked list is not empty, then make the link field of NODE A to point to NODE B. 2. Free the node between NODE A and NODE B

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Deleting the node from the specified position


head Node A 10 20 Node deleted Node B 30

Preeti S. Patil

Linked List MPSTME, SHIRPUR

C code for deleting the node from the specified position


Delete_loc(NODE *head) { NODE *temp1,temp2; int item; printf( Enter the element to be deleted\n); scanf(%d, &item); temp1=head; if(head==NULL) { printf(List is empty); return; } else {

Preeti S. Patil

Linked List MPSTME, SHIRPUR

temp2=temp1; while(temp1 != NULL) { if(temp1->num ==item) { temp2->ptr = temp1->ptr; free(temp1); return; } temp2=temp1; temp1=temp1->ptr; } } }

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Advantages
Accessibility of a node in the forward direction is easier. Insertion and deletion of nodes is easier.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Disadvantages
Accessing the preceding node of a current node is not possible as there is no backward traversal. Accessing a node is time consuming.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Header node
Exists at the beginning of the linked list. Only node present in an empty list. Data field of header node hold the global info (like number of items in the linked list) about the entire linked list. Traversing the entire linked list to know the number of nodes in linked list is not required.
Preeti S. Patil Linked List MPSTME, SHIRPUR

Insertion in header linked list


As the number of nodes are inserted in linked list the data field of header node is also incremented. If the node is to be inserted at the beginning of linked list then it is to be inserted after the header node.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Deletion in a header linked list


As the number of nodes are deleted from the linked list, the data field of header node is also decremented. If the node is to be deleted at the beginning of linked list then it is to be deleted after the header node. Data field will hold NULL value if the list is empty
Preeti S. Patil Linked List MPSTME, SHIRPUR

Advantage of keeping Header Node


1. Traversing the entire list to find out the number of nodes in the list is not required. 2. The count of the nodes in the list can be directly found out from data field of the header node

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Linked Stacks
A linked Stack is a linear linked list in which all the insertion and deletions are always performed at the front of a list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Horizontal Representation
Top is an external pointer to the very first node of a linked list.

Top 40 30 20 10

Null

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Vertical representation
Top
Data Link

40 30

20

10 Null

Preeti S. Patil

Linked List MPSTME, SHIRPUR

PUSH Operation on Linked Stack


PUSH(NODE *top) { NODE *new_node; int item; new_node=allocate_node(); new_node->num=item; new_node->ptr=top; top = new_node; return(top); }
Preeti S. Patil Linked List MPSTME, SHIRPUR

POP Operation on Linked Stack


POP(NODE *top) { NODE *temp; int item; if(top==NULL) { printf(Empty stack\n); return; } else { printf(element deleted = %d,top->num); temp=top->ptr; free(top); top=temp; } } }
Preeti S. Patil Linked List MPSTME, SHIRPUR

Advantages and Disadvantages


Advantages
1.
2. 3.

No wastage of Memory as in sequential representation. Multiple stacks can be represented efficiently. PUSH & POP are easier and efficient.

DisAdvantages
1. A node in a linked requires more memory than a corresponding element in an array representation

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Linked Queues
Linked queue does not suffer the limitation of size. Insertion of a new node is done at the end of the linked list Deletion is done from the beginning of the linked list.

Preeti S. Patil

Linked List MPSTME, SHIRPUR

Front 40 30 20 Rear 10 Null

Front is the external pointer pointing to the very first node in the linked queue. Rear is the pointer pointing to the last node in the linked queue

Preeti S. Patil

Linked List MPSTME, SHIRPUR

End of Linked List

Preeti S. Patil

Linked List MPSTME, SHIRPUR

You might also like