data structures & algorithms linked list filelinked lists a linked list is a linear collection...
TRANSCRIPT
Data Structures &Algorithms
Linked List
Prepared By:-
Dinesh SharmaAsstt. Professor, CSE & IT Deptt. ITM Gurgaon
Linked Lists
A linked list is a linear collection of data elements, called nodes, where the linear order is given by means of pointers.
Each node is divided into two parts:Each node is divided into two parts: The first part contains the information of the
element and
The second part contains the address of the next node (link /next pointer field) in the list.
Advantages/Disadvantages ofLinked ListsAdvantages
1. Linked Lists are dynamic data structure.
2. Efficient memory utilization.
3. Insertion and deletion are easier and3. Insertion and deletion are easier andefficient.
Disadvantages:
1. More memory.
2. Access to an arbitrary element is little bitcumbersome and time consuming.
August 07, 2009 3
Linked Lists
info next
list
info next info next
null
Linear linked list
Adding an Element to the front ofa Linked List
5
info next
list
info next info next
3 8 null5list 3 8 null
Some Notations for use inalgorithm (Not in C programs)p: is a pointer
node(p): the node pointed to by p
info(p): the information portion of the node
next(p): the next address portion of the nodenext(p): the next address portion of the node
getnode(): obtains an empty node
freenode(p): makes node(p) available for reuse even if the value of the pointer p is changed.
Adding an Element to the front ofa Linked List
info next
p p = getnode()
5
info next
list
info next info next
3 8 null
Adding an Element to the front ofa Linked List
info next info next info next
info next
p 6 info(p) = 6
5
info next
list
info next info next
3 8 null
Adding an Element to the front ofa Linked List
info next info next info next
info next
p 6 next(p) = list
5
info next info next info next
3 8list
null
Adding an Element to the front ofa Linked List
info next info next info next
info next
6p
list list = p
5
info next info next info next
3 8 null
Adding an Element to the front ofa Linked List
5
info next info next info next
3 8
info next
list 6 null
Removing an Element from thefront of a Linked List
5
info next info next info next
3 8
info next
list 6 null
Removing an Element from thefront of a Linked List
info next info next info nextinfo next
p = list
5
info next info next info next
3 8
info next
6listp
null
Removing an Element from thefront of a Linked List
info next
6p list = next(p)
5
info next info next info next
3 8list
null
Removing an Element from thefront of a Linked List
info next info next info next
info next
6p x = info(p)
5
info next info next info next
3 8list
x = 6null
Removing an Element from thefront of a Linked List
info next info next info next
info next
p freenode(p)
5
info next info next info next
3 8x = 6
list null
Removing an Element from thefront of a Linked List
5
info next info next info next
3 8listx = 6 null
Linked List Implementation ofStacks – PUSH(S,X) The first node of the list is the top of the
stack. If an external pointer s points to such a linked list, the operation push(s,x) may be implemented by
p=getnode();info(p)=x;next(p)=s;s=p;
Linked List Implementation ofStack
stack_pointer.cstack_pointer.c
Linked List Implemantation ofQUEUES
Assignment for you people. WillAssignment for you people. Willcheck this in tutorial.
Linked List as a Data Structure
An item is accesses in a linked list bytraversing the list from its beginning.
An array implementation allows acccess tothe nth item in a group using single operation,whereas a list implementation requires nwhereas a list implementation requires noperations.
The advantage of a list over an array occurswhen it is necessary to insert or delete anelement in the middle of a group of otherelements.
Operations on Linked Lists
Creation
Insertion
DeletionDeletion
traversing
Searching
Concatenation
DisplayAugust 07, 2009 22
Types of Linked List
Singly linked List
Doubly linked list
Circular Linked listCircular Linked list
Circular doubly linked list
August 07, 2009 23
Structure of a Node
struct node
{
int info;
struct node *next;struct node *next;
} *start=NULL;
typedef struct node NODE;
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
August 07, 2009 24
Insertion into a Linked List
Insertion at the beginning
Insertion at the end
Insertion after a specific position orInsertion after a specific position orinsertion after a specific node
August 07, 2009 25
Insertion At the beginning of alinked list
A B C
Start
August 07, 2009 26
dP->info= itemP- >next= startStart= P
Algorithm for Insertion At the beginning
Algorithm : Insert_begin(start, item)
1. Create a new node ptr.
2. Set ptr->info= item
3. Set ptr->next= start3. Set ptr->next= start
4. Set start= ptr
5. Exit
August 07, 2009 27
Insertion At the end of a linked list
A B C
StartLOC
August 07, 2009 28
A B C
D
Insertion At the end of a linked list
A B C
Start LOC
August 07, 2009 29
A B C
D
Insertion At the end of a linked list
A B C
Start LOC
August 07, 2009 30
A B C
D
Insertion At the end of a linked list
A B C
Start LOCloc->next=p
August 07, 2009 31
A B C
D
Algorithm for Insertion At theendAlgorithm : Insert_end(start, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. Set ptr ->info=item
3. Set ptr->next=NULL
August 07, 2009 32
3. Set ptr->next=NULL
4. If start==NULL then Start=ptr and exit
5. Set loc= start
6. Repeat step 7 Untill loc->next!=NULL
7. Set loc=loc->next
8. Set loc->next=p
Element x is inserted between the third an fourth elements in an array
X0
X1
X2
X3
X4
X0
X1
X2
X3
X0
X1
X2
X3
x
X4
X5
X6
X3
X4
X5
X6
X3
X4
X5
X6
Inserting an item x into a list aftera node pointed to by p
X0 X1 X2 X3 X4 X5 X6 nulllist
p
p
X0 X1 X2 X3 X4 X5 X6 nulllist
xq
Inserting an item x into a list after anode pointed to by pAlgorithm : Insert_position(start, loc, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. Set ptr ->info=item3. If start==NULL then Start=ptr,ptr->next=NULL and exit
4. Initialize counter i and pointer temp3. i=0,temp=start
5. Repeat step 6 and 7 Untill i <loc
6. Set temp=temp->next,
7. Set i=i+1
8. Set ptr->next=temp->next
9. Set temp->next=p
10. Exit
Deletion from a linked list
Deletion from the beginning.
Deletion from the end.
Deletion of a node with specificDeletion of a node with specificposition.
August 07, 2009 36
Deletion from the start of a linked list
A B C
Start
August 07, 2009 37
Ptr=startStart=start->nextFree(ptr)
ptr
Deletion of first node from alinked listAlgorithm : delete_start(start)
1. [Check for underflow]1. If(start==NULL) then print underflow and
exit.
August 07, 2009 38
exit.
2. Set ptr =start
3. Set start=start->next
4. Print :element deleted is ptr->info
5. Free(ptr)
6. Exit
Deletion of last node from alinked list
A B C
Start ptr
August 07, 2009 39
loc
Deletion of last node from alinked list
A B C
Start ptr
August 07, 2009 40
loc
Deletion of last node from alinked list
A B C
Start ptr
August 07, 2009 41
loc
loc->next=NULLFree(ptr)
Deletion of last node from a linked list
Algorithm : delete_end(start)
1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. If start->next==NULL then
a. Set ptr=start, set start=NULL
b. Print element deleted is ptr->info
August 07, 2009 42
b. Print element deleted is ptr->info
c. Free(ptr), //end if
3. Set ptr=start
4. Repeat step 5 & 6 untill ptr->next!=NULL
5. Set loc=ptr
6. Set ptr=ptr->next
7. Set loc->next=NULL
8. Free ptr
9. Exit
Deleting an item x from a list aftera node pointed to by p
X0 X1 X2 X3 X4 X5 X6 nulllist
p q
X0 X1 X2 X4 X5 X6 nulllist
p
x =X3
X3
Deleting an item x from a list after a node pointed to by pAlgorithm : delete_specific(start,position)
1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. [initialize counter]
1. Set i=0, Set ptr=start
August 07, 2009 44
1. Set i=0, Set ptr=start
3. Repeat step 4 until i<=position
4. Set t = ptr, Ptr = ptr->next, I = i+1
5. T->next = ptr->next
6. Free(ptr)
7. Exit
Home Work!!!
WAP Delete a node whoseinfo part is equal to item frominfo part is equal to item froma linked list.
August 07, 2009 45
Circular Linked List
In linear linked lists if a list is traversed (all theelements visited) an external pointer to thelist must be preserved in order to be able toreference the list again.
Circular linked lists can be used to help the Circular linked lists can be used to help thetraverse the same list again and again ifneeded. A circular list is very similar to thelinear list where in the circular list the pointerof the last node points not NULL but the firstnode.
August 07, 2009 46
Circular Linked Lists
A Linear Linked List
Circular Linked Lists
Circular linked list
August 07, 2009 49
circular linked List
A B C
StartLast
August 07, 2009 50
A B C
Insertion at the beginning in acircular linked List
A B C
StartLast
August 07, 2009 51
dPtr -> info = itemPtr->next = startStart = ptrLast -> next= ptr
Insertion at the beginning of aCircular linked ListAlgorithm : insert_begin(start, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. If (start==NULL) then1. Set ptr- > info = item
Ptr -> next= ptr
August 07, 2009 52
2. Ptr -> next= ptr
3. Set start = ptr
4. Set last = ptr //end of if
3. Set ptr-> info = item
4. Ptr->next = start
5. Start = ptr
6. Last -> next= ptr
7. Exit
Insertion at the end of a circularlinked List
A B C
StartLast
August 07, 2009 53
dSet ptr-> info = item
last->next = ptr
Last = ptr
Last -> next= start
Insertion at the end of a Circularlinked ListAlgorithm : insert_end(start, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. If (start==NULL) then1. Set ptr- > info = item
Ptr -> next= ptr
August 07, 2009 54
2. Ptr -> next= ptr
3. Set last = ptr
4. Set start = ptr //end of if
3. Set ptr-> info = item
4. last->next = ptr
5. Last = ptr
6. Last -> next= start
7. Exit
Deletion from the beginning of aCircular linked List
A B C
StartLast
August 07, 2009 55
ptr = start
Start = start->next
Last ->next= start
Free(ptr)
Deletion from the beginning of aCircular linked ListAlgorithm : delete_first(start, item)1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. Set ptr = start
August 07, 2009 56
3. Start = start->next
4. Print element deleted is ptr -> info
5. Last ->next= start
6. Free(ptr)
7. Exit
Deletion from the end of a Circularlinked List
A B C
StartLast
August 07, 2009 57
ptr = start
Start = start->next
Last ->next= start
Free(ptr)
Deletion from the end of a Circularlinked ListAlgorithm : delete_last(start)1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. Set ptr = start
August 07, 2009 58
3. Repeat step 4 and 5 until ptr!=Last
4. Set t= ptr5. Set ptr=ptr -> next
6. Set t->next = ptr-> next
7. Last= t
8. Exit
WAP to implement various operations on circular linked listcircular linked list
August 07, 2009 59
Doubly Linked List
Doubly linked list is one in which all nodesare linked together by number of links, whichhelps in accessing both successor andpredecessor node from a given node position.
It provides bidirectional traversing. Everynode has two link fields.
August 07, 2009 60
Structure of a Node
struct node
{
int info;
struct node *next, *prev;struct node *next, *prev;
} *start=NULL;
typedef struct node NODE;
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
August 07, 2009 61
Insertion at the beginning of aDoubly linked ListAlgorithm : insert_begin(start, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. [ prepare a new node ]
1. Ptr - > info=item
2. Ptr->next= NULL
August 07, 2009 62
2. Ptr->next= NULL
3. Ptr -> prev=NULL
3. If (start==NULL) then
1. Start = ptr
Else goto step 4
4. [perform following] DOUBLY.C
1. Ptr->next=start
2. Ptr->prev=NULL
3. Start->prev=ptr
4. Start=ptr
5. [ Exit ]
Doubly Linked List Insertion at theBeginningvoid insert_beg(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr-> next=NULL;
ptr->prev=NULL;
if(start==NULL)
start=ptr;*)malloc(sizeof(NODE));
if(ptr==NULL)
{
printf("\n overflow");
return;
}
else
{
ptr->next=start;
ptr->prev=NULL;
start->prev=ptr;
start=ptr;
}
}August 07, 2009 63
Insertion at the __________
August 07, 2009 64
Insertion at the end of a Doublylinked List
Algorithm : insert_end(start, item)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. [ prepare a new node ]
4. Repeat while (temp->next!=NULL)
1. Temp=temp->next
5. Temp-next=ptr
Ptr->prev=temp2. [ prepare a new node ]
1. Ptr - > info=item
2. Ptr->next= NULL
3. Ptr -> prev=NULL
3. If (start==NULL) then
1. Start = ptr
Else temp= start
goto step 4
6. Ptr->prev=temp
7. Ptr->next=NULL6. [ Exit ]
August 07, 2009 65
Doubly Linked List Insertion at theEnd
void insert_end(int item)
{
NODE *ptr,*temp;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->prev=NULL;
if(start==NULL)
start=ptr;
else
{
temp=start;ptr->info=item;
ptr->next=NULL;
if(ptr==NULL)
{
printf("\n overflow");
return;
}
ptr->info=item;
ptr-> next=NULL;
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=ptr;
ptr->prev=temp;
ptr->next=NULL;
}
}
August 07, 2009 66
Insertion at a specific position in adoubly linked listAlgorithm : insert_position(start, item, pos)
1. [Check for overflow]
1. If(ptr==NULL) then print overflow and exit.
2. [ prepare a new node ]
1. Ptr - > info=item
2. Ptr->next= NULL
3. Ptr -> prev=NULL
3. If (start==NULL) then
August 07, 2009 67
3. If (start==NULL) then
1. Start = ptr
Else goto step 4
4. temp=start;
5. Repeat step 6 for I = 1 To POS-1
6. temp = temp - > next
7. Temp -> next-> prev = ptr
8. Ptr->next= temp->next
9. Temp->next=ptr
10. Ptr->prev= temp
11. [ Exit ]
Doubly Linked List Insertion at thespecific positionvoid insert_pos(int item,int pos)
{
NODE *ptr,*temp;
int i;
ptr=(NODE*)malloc(sizeof(NODE))
while(i<pos)
{
temp=temp->next;
if(temp==NULL)
{
printf("\nless nodes in the ptr=(NODE*)malloc(sizeof(NODE));
ptr->info=item;
temp=start;
i=1;
printf("\nless nodes in the list");
return;
}
i++;
}
temp->next->prev=ptr;
ptr->next=temp->next;
temp->next=ptr;
ptr->prev=temp;
}
August 07, 2009 68
Deleting a node from the begining
Algorithm : delete_start(start)
1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. [Deletion of the only node present]
1. If (start- >next= NULL) then
August 07, 2009 69
2. Ptr=start
3. Start=NULL
3. [ More than one node in the list]
1. Ptr= start
2. Start = start->next
3. Start -> prev=NULL
4. Free(ptr)
5. Exit
Deleting a node from the begining
void del_beg()
{
NODE *ptr;
if(start==NULL)
{
printf("\nlist is empty");printf("\nlist is empty");
return;
}
else
{
ptr=start;
start=start->next;
start->prev=NULL;
}
free(ptr);
}
August 07, 2009 70
Deleting a last node from the list
Algorithm : delete_last(start)
1. [Check for underflow]
1. If(start==NULL) then print underflow and exit.
2. [Deletion of the only node present]
1. If (start- >next= NULL) then
August 07, 2009 71
2. Ptr=start
3. Start=NULL
3. [ traverse to the last node]
1. Ptr= start
2. While( ptr -> next != NULL)1. Ptr=ptr->next
4. Ptr -> prev -> next = NULL
5. Free(ptr)
4. Exit
Deleting a last node from the list
void del_last()
{
NODE *ptr;
if(start==NULL)
{
if(start->next==NULL)
{
ptr=start;
start=NULL;
}
else
{printf("\n list is empty");
return;
}
else
{
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->prev->next=NULL;
}
}
}
August 07, 2009 72
Deleting a specific node from thelist
Assignment for U people.
August 07, 2009 73
Assignment for U people.
Circular Doubly Linked List
A circular linked list is one which has both the successor pointer and predecessor pointer in circular manner
August 07, 2009 74
Deleting an item x from a list aftera node pointed to by p
q=next(p);
x=info(q);
next(p)=next(q);
freenode(q);freenode(q);
LINKED LISTS USINGDYNAMIC VARIABLES In array implementation of the linked lists a fixed set of nodes
represented by an array is established at the beginning of the execution
A pointer to a node is represented by the relative position of the node within the array.
In array implementation, it is not possible to determine the number of nodes required for the linked list. Therefore;
Less number of nodes can be allocated which means that the program Less number of nodes can be allocated which means that the program will have overflow problem.
More number of nodes can be allocated which means that some amount of the memory storage will be wasted.
The solution to this problem is to allow nodes that are dynamic, rather than static.
When a node is required storage is reserved/allocated for it and when a node is no longerneeded, the memory storage is released/freed.
ALLOCATING AND FREEINGDYNAMIC VARIABLES C library function malloc() is used for
dynamically allocating a space to a pointer. Note that the malloc() is a library function in <stdlib.h> header file.
The following lines allocate an integer space from The following lines allocate an integer space from the memory pointed by the pointer p.
int *p;p = (int *) malloc(sizeof(int));
Note that sizeof() is another library function that returns the number of bytes required for the operand. In this example, 4 bytes for the int.
ALLOCATING AND FREEINGDYNAMIC VARIABLES Allocate floating point number space for a
float pointer f.
float *f;float *f;
f = (float *) malloc(sizeof(float));
Question:What is the output of thefollowing lines?int *p, *q;
int x;
p = (int *) malloc(sizeof(int));
*p = 3;pp 3*p = 3;
x = 6;
q = (int *) malloc(sizeof(int));
*q=x;
printf(“%d %d \n”, *p, *q);
The above lines will print 3 and 6.
p 3
6x
qq 6
malloc() and free()
The following lines and the proceeding figure shows the effectiveness of the free() function.int *p, *q;p = (int *) malloc(sizeof(int));*p = 5;q = (int *) malloc(sizeof(int));q = (int *) malloc(sizeof(int));*q = 8;free(p);p = q;q = (int *) malloc(sizeof(int));*q = 6;printf(“%d %d \n”, *p, *q);
Queries
August 07, 2009 81
August 07, 2009 82