SlideShare a Scribd company logo
1
Unit IV & Unit V
Lists
 A list(linear linked list) is a collection of variable number of
data items .
 A list is a set of items organized sequentially
 For example, an array is a list where the sequential
organization is provided implicitly by its index.
2
Implementation of a List
 A list can be represented in two ways
1) Contigious List (Static implementation)
2) Linked List (Dynamic implementation)
Contiguous List
 A contiguous list is a list in array implementation.
 In this representation, each item is contiguous to next item.
 The operations of insertion and deletion of items can be
done to/from anywhere within the list.
3
Implementation of Contiguous List
 To implement a contiguous list, we need to define
a large enough array to hold all the items of the
list.
 The first item of the list is
Placed at 0th position of array
and successive items in successive
positions of the array.
The last item of the list
is indicated by “last index”.
 In the figure, Last index=7.
4
Implementation of Contiguous List…
Assumptions
MAXLIST is defined.
 CList[MAXLIST] is defined as an array to hold the items of the list.
 last_index is defined to indicate index the last element.
 Initially, last_index=-1.
Insertion:
1.Read item x to insert.
2.If last_index==MAXLIST-1, declare list full and return.
3.If last_index==-1, increment last_index and perform Clist[0]=x and return.
4.Read position pos to insert (pos=0 means CList[0]).
5.If pos>last_index+1, declare invalid and return.
i=last_index;
while(i>=pos)
CList[i+1]=CList[i];
i=i-1;
Clist[pos]=x;
Increment last_index
Return 5
Implementation of Contiguous List…
Assumptions
Deletion:
1. If last_index==-1, declare list empty and return.
2. Read position pos to delete.
3. If pos>last_index, declare invalid and return.
4. x=CList[pos];
5.i=pos;
6. while(i<last_index)
CList[i]=CList[i+1];
i=i+1;
7. Decrement last_index
8. Return x as deleted item
6
Linked List (Linear/Singly Linked List)
A linked list is defined as a collection of nodes in
which each node has two parts:
(i) information part and
(ii) link part
To store a set of items in linked list representation, the
information part of a node contains the actual item of
the list while the link part of the node contains the
address of the next node in the list.
The address of the last node of a linked list is NULL.
7
C Implementation of Linked List
 In C, a linked list template is created using a self-referential
structure and nodes of the linked list are created and removed
using dynamic memory allocation functions like malloc() and
free(). We consider startnode as an external pointer. This
pointer helps in creating and accessing other nodes in the linked
list.
 The following code defines the structure for linked list and
creates a startnode.
struct linkedlist
{
int info;
struct linkedlist *link;
};
struct linkedlist *startnode;
startnode=NULL;
Note: The pointer member variable link points to (i.e. contains
address of ) a structure variable of the structure type linkedlist.
8
9
Creating the empty list:
void createEmptyList(NodeType *head)
{
head=NULL;
}
10
11
12
13
14
15
16
17
18
19
20
21
22
Prepared by : Narayan Dhamala 23
24
25
26
27
28
29
30
31
32
33
34
35
Assignment #2
 Write the advantages and disadvantage of different
types of linked list.
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Advantages and disadvantages of singly linked list
Advantages of Singly linked list
 Singly linked list is probably the most easiest data structure to implement.
 Insertion and deletion of element can be done easily.
 Insertion and deletion of elements doesn't requires movement of all elements
when compared to an array.
 Requires less memory when compared to doubly, circular or doubly circular
linked list.
 Can allocate or deallocate memory easily when required during its execution.
 It is one of most efficient data structure to implement when traversing in one
direction is required.
Disadvantages of Singly linked list
 It uses more memory when compared to an array.
 Since elements are not stored sequentially hence requires more time to access
each elements of list.
 Traversing in reverse is not possible in case of Singly linked list when compared
to Doubly linked list.
 Requires O(n) time on appending a new node to end. Which is relatively very
high when compared to array or other linked list.
51
Advantage and disadvantage of doubly linked list
Advantages of doubly linked list
Bidirectional traversing is possible.
Easy to insert and delete a node to/from a linked list
(as there is no need to keep track of the previous node
during traversal or no need to traverse to find the
previous node).
Disadvantages of doubly linked list
Complex to handle double pointer.
More memory space is needed.
52
53

More Related Content

PPT
ANOITO2341988888888888888888888885555.ppt
PPT
Link list using array in Data structure amd algorithms
PPTX
Datastucture-Unit 4-Linked List Presentation.pptx
PPT
Linked List
PPT
lecture four of data structures :Linked List-ds.ppt
PPTX
1.3 Linked List.pptx
PPTX
Linked lists linked lists vs Arrays.pptx
ANOITO2341988888888888888888888885555.ppt
Link list using array in Data structure amd algorithms
Datastucture-Unit 4-Linked List Presentation.pptx
Linked List
lecture four of data structures :Linked List-ds.ppt
1.3 Linked List.pptx
Linked lists linked lists vs Arrays.pptx

Similar to data structure and algorithm note for IT students (20)

PPTX
Linear Data Structures - List, Stack and Queue
PPTX
link list.pptx complete notes detailed ans
PPTX
Linked list in Data Structure and Algorithm
DOCX
Linked list.docx
PPT
Data Structures- Part7 linked lists
PPT
DS Unit 2.ppt
PDF
02. the linked lists (1)
PDF
4 chapter3 list_stackqueuepart1
PPTX
Linked List.pptx
PDF
DS Module 03.pdf
PPTX
linked list_MODULE 3.pptx ppt on the linked list
PPT
Lecture 2b lists
PPT
Lecture 3 List of Data Structures & Algorithms
PPTX
Linked List Representation of a Linked List.pptx
PPTX
VCE Unit 02 (1).pptx
PPTX
Engineering.CSE.DataStructure.Linkedlist.notes
PPTX
link listgyyfghhchgfvgggfshiskabaji.pptx
PPTX
module 3-.pptx
PPTX
CH4.pptx
PPTX
Data Structures(Part 1)
Linear Data Structures - List, Stack and Queue
link list.pptx complete notes detailed ans
Linked list in Data Structure and Algorithm
Linked list.docx
Data Structures- Part7 linked lists
DS Unit 2.ppt
02. the linked lists (1)
4 chapter3 list_stackqueuepart1
Linked List.pptx
DS Module 03.pdf
linked list_MODULE 3.pptx ppt on the linked list
Lecture 2b lists
Lecture 3 List of Data Structures & Algorithms
Linked List Representation of a Linked List.pptx
VCE Unit 02 (1).pptx
Engineering.CSE.DataStructure.Linkedlist.notes
link listgyyfghhchgfvgggfshiskabaji.pptx
module 3-.pptx
CH4.pptx
Data Structures(Part 1)
Ad

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Cardiovascular Pharmacology for pharmacy students.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
01-Introduction-to-Information-Management.pdf
Pharma ospi slides which help in ospi learning
Open Quiz Monsoon Mind Game Final Set.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Renaissance Architecture: A Journey from Faith to Humanism
FourierSeries-QuestionsWithAnswers(Part-A).pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Structure & Organelles in detailed.
Week 4 Term 3 Study Techniques revisited.pptx
Open folder Downloads.pdf yes yes ges yes
Cardiovascular Pharmacology for pharmacy students.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Basic Mud Logging Guide for educational purpose
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
NOI Hackathon - Summer Edition - GreenThumber.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Ad

data structure and algorithm note for IT students

  • 1. 1
  • 2. Unit IV & Unit V Lists  A list(linear linked list) is a collection of variable number of data items .  A list is a set of items organized sequentially  For example, an array is a list where the sequential organization is provided implicitly by its index. 2
  • 3. Implementation of a List  A list can be represented in two ways 1) Contigious List (Static implementation) 2) Linked List (Dynamic implementation) Contiguous List  A contiguous list is a list in array implementation.  In this representation, each item is contiguous to next item.  The operations of insertion and deletion of items can be done to/from anywhere within the list. 3
  • 4. Implementation of Contiguous List  To implement a contiguous list, we need to define a large enough array to hold all the items of the list.  The first item of the list is Placed at 0th position of array and successive items in successive positions of the array. The last item of the list is indicated by “last index”.  In the figure, Last index=7. 4
  • 5. Implementation of Contiguous List… Assumptions MAXLIST is defined.  CList[MAXLIST] is defined as an array to hold the items of the list.  last_index is defined to indicate index the last element.  Initially, last_index=-1. Insertion: 1.Read item x to insert. 2.If last_index==MAXLIST-1, declare list full and return. 3.If last_index==-1, increment last_index and perform Clist[0]=x and return. 4.Read position pos to insert (pos=0 means CList[0]). 5.If pos>last_index+1, declare invalid and return. i=last_index; while(i>=pos) CList[i+1]=CList[i]; i=i-1; Clist[pos]=x; Increment last_index Return 5
  • 6. Implementation of Contiguous List… Assumptions Deletion: 1. If last_index==-1, declare list empty and return. 2. Read position pos to delete. 3. If pos>last_index, declare invalid and return. 4. x=CList[pos]; 5.i=pos; 6. while(i<last_index) CList[i]=CList[i+1]; i=i+1; 7. Decrement last_index 8. Return x as deleted item 6
  • 7. Linked List (Linear/Singly Linked List) A linked list is defined as a collection of nodes in which each node has two parts: (i) information part and (ii) link part To store a set of items in linked list representation, the information part of a node contains the actual item of the list while the link part of the node contains the address of the next node in the list. The address of the last node of a linked list is NULL. 7
  • 8. C Implementation of Linked List  In C, a linked list template is created using a self-referential structure and nodes of the linked list are created and removed using dynamic memory allocation functions like malloc() and free(). We consider startnode as an external pointer. This pointer helps in creating and accessing other nodes in the linked list.  The following code defines the structure for linked list and creates a startnode. struct linkedlist { int info; struct linkedlist *link; }; struct linkedlist *startnode; startnode=NULL; Note: The pointer member variable link points to (i.e. contains address of ) a structure variable of the structure type linkedlist. 8
  • 9. 9
  • 10. Creating the empty list: void createEmptyList(NodeType *head) { head=NULL; } 10
  • 11. 11
  • 12. 12
  • 13. 13
  • 14. 14
  • 15. 15
  • 16. 16
  • 17. 17
  • 18. 18
  • 19. 19
  • 20. 20
  • 21. 21
  • 22. 22
  • 23. Prepared by : Narayan Dhamala 23
  • 24. 24
  • 25. 25
  • 26. 26
  • 27. 27
  • 28. 28
  • 29. 29
  • 30. 30
  • 31. 31
  • 32. 32
  • 33. 33
  • 34. 34
  • 35. 35
  • 36. Assignment #2  Write the advantages and disadvantage of different types of linked list. 36
  • 37. 37
  • 38. 38
  • 39. 39
  • 40. 40
  • 41. 41
  • 42. 42
  • 43. 43
  • 44. 44
  • 45. 45
  • 46. 46
  • 47. 47
  • 48. 48
  • 49. 49
  • 50. 50
  • 51. Advantages and disadvantages of singly linked list Advantages of Singly linked list  Singly linked list is probably the most easiest data structure to implement.  Insertion and deletion of element can be done easily.  Insertion and deletion of elements doesn't requires movement of all elements when compared to an array.  Requires less memory when compared to doubly, circular or doubly circular linked list.  Can allocate or deallocate memory easily when required during its execution.  It is one of most efficient data structure to implement when traversing in one direction is required. Disadvantages of Singly linked list  It uses more memory when compared to an array.  Since elements are not stored sequentially hence requires more time to access each elements of list.  Traversing in reverse is not possible in case of Singly linked list when compared to Doubly linked list.  Requires O(n) time on appending a new node to end. Which is relatively very high when compared to array or other linked list. 51
  • 52. Advantage and disadvantage of doubly linked list Advantages of doubly linked list Bidirectional traversing is possible. Easy to insert and delete a node to/from a linked list (as there is no need to keep track of the previous node during traversal or no need to traverse to find the previous node). Disadvantages of doubly linked list Complex to handle double pointer. More memory space is needed. 52
  • 53. 53