DSA Unit II
DSA Unit II
Linear lists , Sequential and linked allocation, list ADT, array and linked
Implementations, Singly Linked List-Opertion-Insertion-Deletion, Doubly
Linked List-Operation-Insertion, Deletion.
Sequential Allocation
Sequential allocation refers to the arrays or a contiguous block of memory.
It is hard to Insert and delete files and requires movement to do so.
Space is wasted.
Expensive.
It requires less space as only information is stored.
It cannot be extended or reduced as per the requirements.
Similar amount of time is required to access every element.
Elements are stored in consecutive memory locations.
A direct route is present to reach element individually.
List ADT
A list is a ordered tuple of homogeneous elements
A0, A1, A2, …., An-1
where Ai is the i-th element of the list
The position of element Ai is i; positions range from 0 to n-1 inclusive
The size of a list is N (a list with no elements is called an “empty list”.
One-Dimensional Arrays:
A list of items can be given one variable name using only one subscript and such a variable
is called single-subscripted variable or one dimensional array. A one dimensional array can
be declared as follows:
10 20 30 40 50 60 70
10 20 30 40 50 60 70
Index 0 1 2 3 4 5 6
10 20 30 40 Null
Figure. 1.2 Singly Linked List containing four integer values
Basic Operations:
1. Insertion
a. At first
b. At last
c. At a given location or given data item (At middle)
2. Deletion
a. First Node
b. Last Node
c. Node in given location or given data item
Initial Condition
HEAD or START = NULL;
Address of the first node in the list is stored in HEAD/START. Initially there is no node in the
list. So, HEAD/START isinitialized to NULL (No address).
Insertion Operation
1. Insertion At First
void insert_at_beg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\n Enter the data to insert at begin: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(start==NULL)
start=newnode;
else
{
newnode->next=start;
Deletion Operation
1. Delete At First
void del_at_beg()
{
temp=start;
start=start->next; /*First element deleted*/
free(temp);
}
2. Delete At Mid
void del_at_mid()
{
int key;
printf("\n Enter element to delete..");
scanf("%d",&key);
p=start;
while(p->next!= NULL)
{
if(p->next->data==key) /*Element deleted in between*/
{
temp=p->next;
p->next=temp->next;
free(temp);
}
p=p->next;
}
}
3. Delete At End
void del_at_end()
{
temp=start;
while(temp->next!= NULL)
{
p=temp;
temp=temp->next;
}
free(p->next);
p->next=NULL;
}
10 20 30 40
****************
Doubly-Linked List
A more sophisticated kind of linked list is a doubly-linked list or two-way linked list,
in which each node has two links: one points to the previous node, or points to a null value or
empty list if it is the first node; and one points to the next, or points to a null value or empty list
if it is the final node.
*prev data *next
Null 15 56 27 Null
First Node Last Node
Figure : Doubly Linked List
Basic Operations:
1. Insertion
a. At first
b. At last
c. At a given location or given data item (At middle)
2. Deletion
a. First Node
b. Last Node
c. Node in given location or given data item
Insertion Operation
1. Insertion At First
void insert_at_beg()
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at begin: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
start=newnode;
2. Insertion At Mid
void insert_mid()
{
int key;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data to insert at middle: ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(start==NULL)
start=temp=newnode;
else
{
printf("\nEnter the element after which you want to insert: ");
scanf("%d",&key);
temp=start;
do
{
if(temp->data==key)
{
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
return;
}
else
temp=temp->next;
}while(temp!=NULL);
}
}
3. Delete At End
void del_at_end()
{
temp=start;
while(temp->next!= NULL)
{
p=temp;
temp=temp->next;
} /*Last element deleted*/
free(temp);
p->next=NULL;
}
Doubly-circular-linked list
In a doubly-circular-linked list, each node has two links, similar to a doubly-linked
list, except that the previous link of the first node points to the last node and the next link of
the last node points to the first node. As in doubly-linked lists, insertions and removals can be
done at any point with access to any nearby node.
***********************