"Array and Linked List in Data Structures with Types, Operations, Implementations, and Applications in C Programming"
1. VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi
Department of Computer Science and
Engineering
UNIT 1 – ARRAY AND LINKED LIST
Dr. USHA M
Associate Professor
Dept of CSE
Velammal Engineering College
2. Introduction To Data Structures
And
List ADT
Mrs. G. Sumathi
Assistant Professor
Velammal Engineering College
Chennai
VELAMMAL ENGINEERING COLLEGE
An Autonomous Institution, Affiliated to Anna University Chennai, & Approved by AICTE Delhi
3. Dept of CSE, Velammal Engineering College,
Chennai
3
Agenda
• Introduction to Data Structures
• List ADT
• Issues in Array Implementation
• Linked List Representation
• Types
• Applications of Linked List
25/5/2020
4. Dept of CSE, Velammal Engineering College,
Chennai
4
Introduction to Data Structures
Data
Values or set of values
of different data types
such as int, float, etc.,
Way of organizing and
storing information so
that it is easy to use
Structure
Data + Structure
Way of organizing and
storing data on a computer
so that it can be used easily
and effectively
25/5/2020
5. Dept of CSE, Velammal Engineering College,
Chennai
5
Introduction to Data Structures
Definition:
Data Structure is a way of organizing,
storing and retrieving data and their relationships
with each other
25/5/2020
6. Dept of CSE, Velammal Engineering College,
Chennai
6
Classification of Data Structures
Data Structure
Primitive
int char float boolean
Non - Primitive
Linear
Array Linked List Stack Queue
Non - Linear
Tree Graph
25/5/2020
7. Dept of CSE, Velammal Engineering College,
Chennai
7
• It is a type for objects whose behavior is defined by set of
values and set of operations from user point of view
• Mentions only what operations are to be performed but not
how they will be implemented
• Thus, ADT is the specification of a data type within some
language, independent of implementation
ADT – Abstract Data Type
25/5/2020
8. Dept of CSE, Velammal Engineering College,
Chennai
8
Examples – List ADT, Stack ADT, Queue ADT, etc.,
Some operations on these ADT are:
List:
• insert(x)
• delete(x)
• find(x)
Stack:
• Push (x)
• Pop()
• isFull()
• isEmpty()
Queue:
• enqueue()
• dequeue()
• isFull()
• isEmpty()
ADT – Abstract Data Type
25/5/2020
9. Dept of CSE, Velammal Engineering College,
Chennai
9
• List ADT can be represented in two ways
– Array
– Linked List
List ADT
25/5/2020
10. Dept of CSE, Velammal Engineering College,
Chennai
10
• Collection of elements of same data type
• Elements in an array is stored in adjacent memory location
Syntax:
• datatype arrayname [size]
• Eg: int a [50];
Operations:
• insertion()
• deletion()
• search()
Array ADT
25/5/2020
11. Dept of CSE, Velammal Engineering College,
Chennai
11
• Fixed size: Resizing is expensive
• Requires continuous memory for allocation
– Difficult when array size is larger
• Insertions and deletions are inefficient
– Elements are usually shifted
• Wastage of memory space unless the array is
full
Issues in Array
25/5/2020
12. Dept of CSE, Velammal Engineering College,
Chennai
12
• Linked list is a linear data structure
• Made up of chain of nodes
• Each node contains a data and a pointer to the next
node in the chain
• Last node of the list is pointed to NULL
Linked List ADT
25/5/2020
13. Dept of CSE, Velammal Engineering College,
Chennai
13
• Each node contains two fields:
– Data field: Contains data element to be stored
– Pointer field: Contains the address of next node
Representation of Node in Linked List
Declaration of a node:
struct node
{
int data;
struct node *next;
};
25/5/2020
14. Dept of CSE, Velammal Engineering College,
Chennai
14
Statement to create a node:
n= (struct node*) malloc(sizeof(sturct node))
nnext =NULL;
Creating a Node
struct node
{
int data;
struct node *next;
} *n;
25/5/2020
15. Dept of CSE, Velammal Engineering College,
Chennai
15
• Singly linked list
• Doubly linked list
• Circular linked list
Types of Linked List
25/5/2020
16. Dept of CSE, Velammal Engineering College,
Chennai
16
• Type of linked list
• Each node contains only one pointer field that
contains the address of the next node in the list
Singly Linked List (SLL)
25/5/2020
17. Dept of CSE, Velammal Engineering College,
Chennai
17
Major operations are:
• Insertion
– At the beginning
– At the middle
– At the end
• Deletion
– At the beginning
– At the middle
– At the end
• Search
Operations on Singly Linked List
Global declaration of a
Node:
struct node
{
int data;
struct node *next;
} *head, *tail, *n, *t;
25/5/2020
18. Dept of CSE, Velammal Engineering College,
Chennai
18
Routine for Insertion at the Beginning
void ins_beg (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
nnext=head;
head=n;
}
}
10
25/5/2020
19. Dept of CSE, Velammal Engineering College,
Chennai
19
Routine for Insertion at the End
void ins_end (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
tail=n;
}
}
10
25/5/2020
20. Dept of CSE, Velammal Engineering College,
Chennai
20
Routine for Insertion at the Middle
void ins_end (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
83==9 ? NO 9==9 ? Yes
10
300
300
1500
300
25/5/2020
21. Dept of CSE, Velammal Engineering College,
Chennai
21
Routine for Deletion at the Beginning
void del_beg ()
{
t=head;
head=tnext;
free(t);
}
25/5/2020
22. Dept of CSE, Velammal Engineering College,
Chennai
22
Routine for Deletion at the End
void del_end ()
{
struct node *tp;
t=head;
while(tnext!=NULL)
{
tp=t;
t=tnext;
}
tail=tp;
tailnext=NULL;
free(t);
}
25/5/2020
23. Dept of CSE, Velammal Engineering College,
Chennai
23
Routine for Deletion at the Middle
void del_mid (int num) //Let num=70
{
struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
}
2800
83!=70? Yes 9!=70? Yes 70!=70? No
1500
25/5/2020
24. Dept of CSE, Velammal Engineering College,
Chennai
24
Routine for Search an Element
struct node* search (int num) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
t!=NULL? Yes
83!=9? Yes
t!=NULL? Yes
9!=9? No
Element Found!!
25/5/2020
25. Dept of CSE, Velammal Engineering College,
Chennai
25
Linked List – Advantages & Disadvantages
Advantages:
• No wastage of memory
– Memory allocated and deallocated as per
requirement
• Efficient insertion and deletion operations
Disadvantages:
• Occupies more space than array to store a data
– Due to the need of a pointer to the next node
• Does not support random or direct access
25/5/2020
26. Dept of CSE, Velammal Engineering College,
Chennai
26
Doubly Linked List (DLL)
• Type of linked list
• Each node contains two pointer fields that contains
the address of the next node and that of previous
node in the list
25/5/2020
27. Dept of CSE, Velammal Engineering College,
Chennai
27
• Each node contains three fields:
– Data: Contains data element to be stored
– next: Contains the address of next node
– prev: Contains address of previous node
Representation of Node in DLL
Declaration of a node:
struct node
{
int data;
struct node *next, *prev;
};
A Node in DLL
25/5/2020
28. Dept of CSE, Velammal Engineering College,
Chennai
28
Major operations are:
• Insertion
– At the beginning
– At the middle
– At the end
• Deletion
– At the beginning
– At the middle
– At the end
• Search
Operations on Doubly Linked List
Global declaration of a Node:
struct node
{
int data;
struct node *next, prev;
} *head, *tail, *n, *t;
25/5/2020
30. Dept of CSE, Velammal Engineering College,
Chennai
30
void ins_end_dll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
nprev=tail;
tail=n;
}
}
Routine for Insertion at the End
25/5/2020
31. Dept of CSE, Velammal Engineering College,
Chennai
31
void ins_end_dll (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
nprev=t;
tnextprev=n;
tnext=n;
}
Routine for Insertion at the Middle
25/5/2020
32. Dept of CSE, Velammal Engineering College,
Chennai
32
void del_beg_dll ()
{
t=head;
head=headnext;
headprev=NULL;
free(t);
}
Routine for Deletion at the Beginning
t is freed
25/5/2020
33. Dept of CSE, Velammal Engineering College,
Chennai
33
Routine for Deletion at the End
void del_end_dll ()
{
t=head;
while(tnext!=NULL)
{
t=tnext;
}
tail=tailprev;//tail=tprev;
tailnext=NULL;
free(t);
}
25/5/2020
34. Dept of CSE, Velammal Engineering College,
Chennai
34
Routine for Deletion at the Middle
void del_mid_dll (int num) //Let num=9
{
t=head;
while(tdata!=num)
{
t=tnext;
}
tprevnext=tnext;
tnextprev=tprev;
free(t);
}
25/5/2020
35. Dept of CSE, Velammal Engineering College,
Chennai
35
Routine for Search an Element
struct node* search_dll (int num) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
Element Found!!
t is returned
25/5/2020
36. Dept of CSE, Velammal Engineering College,
Chennai
36
Advantages:
• Can traverse in both directions
• Operations in the middle of the list can be done
efficiently
– No need of extra pointer (tp) to track the previous
node
• Reversing the list is easy
Disadvantage:
• Space required to store a data is even more higher
than SLL
– Due to the use of two pointers
DLL – Advantages & Disadvantages
25/5/2020
37. Dept of CSE, Velammal Engineering College,
Chennai
37
• Type of linked list
• The last node will be pointing to the first node
• It can be either singly or doubly linked
Circular Linked List (CLL)
Global declaration of a node:
struct node
{
int data;
struct node *next, *prev;
}*head,*n,*t;
100
25/5/2020
38. Dept of CSE, Velammal Engineering College,
Chennai
38
Routine for Insertion at the Beginning
void ins_beg_cll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{ head=n;
nnext=head;
}
else //case 2
{ t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
head=n;
}
}
10
10 100
25/5/2020
39. Dept of CSE, Velammal Engineering College,
Chennai
39
Routine for Insertion at the End
void ins_end_cll (int num) //Let num=10
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
}
10
1000
25/5/2020
40. Dept of CSE, Velammal Engineering College,
Chennai
40
Routine for Insertion at the Middle
void ins_end_cll (int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc (size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; tnext!=head; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
1000
25/5/2020
41. Dept of CSE, Velammal Engineering College,
Chennai
41
Routine for Deletion at the Beginning
void del_beg_cll ()
{
struct node *t1;
t=head;
t1=head;
while(tnext!=head)
t=tnext;
tnext=headnext;
head=tnext;
free(t1);
} 100
t1 is freed!!
25/5/2020
42. Dept of CSE, Velammal Engineering College,
Chennai
42
Routine for Deletion at the End
void del_end_cll ()
{
struct node *tp;
t=head;
while(tnext!=head)
{
tp=t;
t=tnext;
}
tpnext=head;
free(t);
}
100
t is freed !!
25/5/2020
43. Dept of CSE, Velammal Engineering College,
Chennai
43
Routine for Deletion at the Middle
void del_mid_cll (int num) //Let num=9
{
struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
} t is freed!!
25/5/2020
45. Dept of CSE, Velammal Engineering College,
Chennai
45
Advantage:
• Comparing to SLL, moving to any node from a node is
possible
Disadvantages:
• Reversing a list is complex compared to linear linked list
• If proper care is not taken in managing the pointers, it
leads to infinite loop
• Moving to previous node is difficult, as it is needed to
complete an entire circle
CLL – Advantages & Disadvantages
25/5/2020
46. Dept of CSE, Velammal Engineering College,
Chennai
46
Applications of List
Lists can be used to
• Sort elements
• Implement stack and queue
• Represent graph (adjacent list representation)
• Implement hash table
• Implement multiprocessing of applications
• Manipulate polynomial equations
25/5/2020
47. Dept of CSE, Velammal Engineering College,
Chennai
47
Polynomial ADT
struct node
{
int coeff;
int pow;
struct node *next;
}*n;
Examples of polynomial equations:
– 9x5
+ 7x3
– 4x2
+ 8
7x4
– 17x3
– 8x2
To store the data of polynomial equations in the linked
list, each node contains two data fields, namely
coefficient and power and one next pointer field
25/5/2020
48. Dept of CSE, Velammal Engineering College,
Chennai
48
Creating a Polynomial List
struct node* createpoly (int c, int p, struct node *t)
{ struct node *head, *tail;
head=t;
n=(struct node*) malloc(size of(struct node));
ncoeff=c;
npow=p;
nnext=NULL;
if (head==NULL)
{ head=n;
tail=n;
}
else
{ tailnext=n;
tail=n;
}
return head;
}
25/5/2020