DS Unit 1 2020 12 July 2022
DS Unit 1 2020 12 July 2022
Krishnakaarthik/AP/IT/Data Structures/Unit 1 1
Unit 1
Linear Data Structure – List
Abstract Data Type - ADT:
Definition 1:
Abstract Data type is a type (or class) for objects whose behaviour is defined by a set of value and a
set of operations. ADT is a data type, where only behaviour is defined but not implementation.
ADT as a black box which hides the inner structure and design of the data type.
ADT only mentions what operations are to be performed but not how these operations will be
implemented.
Process of providing only the essentials and hiding the details is known as abstraction.
Example: Lists, Graphs and Sets along with their operations.
Definition 2:
Abstract Data Types is the collection of data types and their associated operations.
ADT is basically a special kind of data type created by programmer to a handle a complex data
structure.
It specifies the following information:
1. The storage of data values in memory
2. The operations that are performed on the data types
3. The behaviour of the operations
Advantages of ADT:
1. It is reusable, robust and reduces the efforts in coding.
2. It provides encapsulation that protects data from being corrupted.
3. It provides robust data structure.
4. Modularity
5. Code is easier to understand
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 2
Data Structure:
Data Structure is a way of organizing the data, which includes several operations to perform on that data.
Example: List, Stack, Queue, Tree and Graphs
---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 3
Applications of arrays:
1. Arrays are particularly used in programs that require storing large collection of similar type data
elements
1. Declaration of Array:
#define maxsize 10
int list[maxsize], n ;
20 10 30 40 50 60
nd
If data 15 is to be inserted in the 2 position then 50 has to be moved to next index
position, 40 has to be moved to 50 position, 30 has to be moved to 40 position and 20 has
to be moved to 30 position.
nd
After this four data movement, 15 is inserted in the 2 position of the array.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 7
If data 20 is to be deleted from the array, then 30 has to be moved to data 20 position, 40
has to be moved to data 30 position and 50 has to be moved to data 40 position.
nd
After this 3 data movements, data 20 is deleted from the 2 position of the array.
}
Output
---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 11
SLL
Advantages of SLL
1. The elements can be accessed using the next link
2. Occupies less memory than DLL as it has only one next field.
Disadvantages of SLL
1. Traversal in the backwards is not possible
2. Less efficient to for insertion and deletion.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 13
This routine creates a list by getting the number of nodes from the user. Assume n=4 for this
example.
void create()
{ int i,n;
L=NULL;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the number of nodes to be inserted\n");
scanf("%d",&n);
printf("\n Enter the data\n");
scanf("%d",&newnode->data);
newnode->next=NULL;
L=newnode;
p=L;
for(i=2;i<=n;i++)
{ newnode=(struct node *)malloc(sizeof(struct node));
scanf("%d",&newnode->data);
newnode->next=NULL;
p->next=newnode;
p=newnode;
} }
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 14
Insert(10,List L):
A new node with data 10 is inserted and the next field is updated to NULL.
The next field of previous node is updated to store the address of new node.
Insert(20,L):
A new node with data 20 is inserted and the next field is updated to NULL.
The next field of previous node is updated to store the address of new node.
Insert(30,L):
A new node with data 30 is inserted and the next field is updated to NULL.
The next field of previous node is updated to store the address of new node.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 15
2. Insertion
}
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 17
Routine to Find the Element in the List: This routine returns the position of X in the list L
position find(List L, int X)
{ position p;
p=L->next;
while(p!=NULL && p->data!=X)
p=p->next;
return(p);
}
Find(List L, 20):
To find an element X traverse from the first node of the list and move to the next with the
help of the address stored in the next field until data is equal to X or till the end of the list.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 18
Routine to find next Element in the List: It returns the position of successor.
void FindNext(int X, List L)
{ position P;
P=L->next;
while(P!=NULL && P->data!=X)
P = P->next;
return P->next;
}
Step 3: Insert 48
Step 4: insert 57
Step 4: insert 57
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 23
Insert: 8
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 24
A doubly linked list is a linked list in which each node has three fields namely Data, Next, Prev.
o Data This field stores the value of the element
o Next This field points to the successor node in the list
o Previous This field points to the predecessor node in the list
Doubly-Linked List
Advantage
1. Deletion operation is easier.
2. Finding the predecessor & Successor of a node is easier.
Disadvantage
1. More Memory Space is required since it has two pointers.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 25
Initially the list is empty. Then assign the first node as head.
newnode->data=X;
newnode->next=NULL;
newnode->prev=NULL;
L=newnode;
If we add one more node in the list,then create a newnode and attach that
node to the end of the list
L->next=newnode;
newnode->prev=L;
----------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 32
Circular Linked list is a linked list in which the pointer of the last node points to the first node.
Types of Circularly Linked List:
CLL can be implemented as circular singly linked list and circular doubly linked list.
1. Singly linked circular list:
A Singly linked circular list is a linked list in which the last node of the list points to the first node.
Applications:
1. Widely used in Operating Systems for task maintenance.
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 33
1. Declaration of Node:
Routine to insert an element in the Routine to insert an element in the Routine to insert an element in the
beginning middle last
void insert_beg(int X,List L) void insert_mid(int X,List L,Position p) void insert_last(int X, List L)
{ { {
position Newnode; position Newnode; position Newnode;
Newnode=(struct node*)malloc Newnode=(struct node*)malloc Newnode=(struct node*)malloc
(sizeof(struct node)); (sizeof(struct node)); (sizeof(struct node));
if(Newnode!=NULL) if(Newnode!=NULL) if(Newnode!=NULL)
{ { {
Newnode->data=X; Newnode->data=X; P=L;
Newnode->next=L->next; Newnode->next=p->next; while(P->next!=L) P=P->next;
L->next=Newnode; p->next=Newnode; Newnode->data=X;
} } P->next=Newnode;
} } Newnode->next=L;
}
}
3. Deletion:
1. Delete an element in the beginning (Delete 10)
Before Delete 10
After Delete 10
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 35
Before Delete 30
After Delete 30
3. Delete an element in the last (Delete 30)
Before Delete 30
After Delete 3 0
Routine to delete an element in the Routine to delete an element in the Routine to delete an element in
beginning middle the last
void del_first(List L) void del_mid(int X,List L) void del_last(List L)
{ { {
position temp; position p, temp; position p, temp;
temp=L->next; p=findprevious(X,L); if(!Islast(P,L)) p=L;
L->next=temp->next; { while(p->next->next!=L)
free(temp); temp=p->next; p=p->next;
} p->next=temp->next; temp=p->next;
free(temp); p->next=L
} free(temp);
} }
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 36
1. Declaration of Node:
typedef struct node *position;
struct node
{
int data;
position next;
position prev;
};
2. Insertion:
1. Insert an element in the beginning (Insert 5)
Before Insert 5
After Insert 5
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 37
Before Insert 25
After Insert 25
3. Insert an element in the last
Before Insert 40
After Insert 40
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 38
Routine to insert an element in the Routine to insert an element in the Routine to insert an element in the
beginning middle last
void insert_beg(int X,List L) void insert_mid(int X,List L,Position p) void insert_last(int X, List L)
{ { {
position Newnode; position Newnode; position Newnode,p;
Newnode=(struct Newnode=(struct Newnode=(struct
node*)malloc(sizeof(struct node)); node*)malloc(sizeof(struct node)); node*)malloc(sizeof(struct node));
if(Newnode!=NULL) if(Newnode!=NULL) if(Newnode!=NULL)
{ { { p=L;
Newnode->data=X; Newnode->data=X; while(p->next!=L)
Newnode->next=L->next; Newnode->next=p->next; p=p->next;
L->next->prev=Newnode; p->next->prev=Newnode; Newnode->data=X;
L->next=Newnode; p->next=Newnode; p->next =Newnode;
Newnode->prev=L; Newnode->prev=p; Newnode->next=L;
} } Newnode->prev=p;
} } L->prev=newnode;
}
}
3. Deletion:
1. Delete an element in the beginning (Delete 10)
Before Delete 10
After Delete 10
2. Delete an element in the middle (Delete 20)
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 39
Before Delete 20
After Delete 20
3. Delete an element in the last (Delete 30)
Before Delete 20
After Delete 2 0
Routine to delete an element in the Routine to delete an element in the Routine to delete an element in
beginning middle the last
void del_mid(int X,List L) void del_last(List L)
void del_first(List L) { {
{ Position p, temp; position p, temp;
position temp; p=FindPrevious(X); p=L;
if(L->next!=NULL) if(!IsLast(p,L)) while(p->next!=L)
{ { p=p->next;
temp=L->next; temp=p->next; temp=p;
L->next=temp->next; p->next=temp-next; p->next->prev=L;
temp->next->prev=L; temp->next->prev=p; L->prev=p->prev;
free(temp); free(temp); free(temp);
} } }
} }
----------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 40
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 41
The list L
o 5 10 1 9 0
o a bc
The list M
o 3 7820
o c df
---------------------------------------------------------------------------------------------------------------------------------------------------
T.Krishnakaarthik/AP/IT/Data Structures/Unit 1 44