Ds Unit 1
Ds Unit 1
Sparse Matrix
Time-Space Trade-Off in Algorithms
It is matrix in which most of the elements of the matrix have
It is a problem solving technique in which we solve the zero value .
problem: Only we stored non-zero elements with triples- (Row,
Either in less me and using more space, or Column, value).
In very li le space by spending more me. Array representa on of Sparse Matrix –
The best algorithm is that which helps to solve a problem
that requires less space in memory as well as takes less me
to generate the output.
it is not always possible to achieve both of these condi ons
at the same me.
struct Node* addAtPos(int data , int pos , struct Node* head) A DLL is a complex version of a SLL .
{ A DLL has each node pointed to next node as well as previous node.
if(pos==1){
return addAtBeg(data,head); }
struct Node* temp=head;
for (int i = 1; i <=pos; i++)
{ if(i!=pos && temp==NULL ){
return head; //invalid position
}
if(i==pos-1){ Representa on Node of DLL :
struct Node * newNode=Node(data);
newNode->next=temp->next; struct node
temp->next=newNode; {
return head; int data; //data item for storing value of the node
} struct node *next; //address of the next node
temp=temp->next; struct node *prev; //address of the previous node
}
};
return head;
} create Node of DLL:
Delete at beginning : struct Node* Node(int data){
struct Node* newNode=(struct Node*)malloc(sizeof(struct
struct Node * deleteAtBeg( struct Node* head){
Node ));
if(head==NULL){
newNode->prev= NULL;
return NULL;
newNode->data=data;
}
newNode->next=NULL;
return head->next;
return newNode;
}
}
Delete at End :
Operations on DLL
struct Node * deleteAtEnd( struct Node* head){
if( head==NULL || head->next == NULL){ Insert At beginning :
return NULL;
} struct Node * addAtBeg(int data, struct Node* head){
struct Node* temp=head; struct Node* newNode=Node(data);
while (temp->next->next !=NULL){ if(head==NULL){
temp=temp->next; return newNode;
} }
temp->next=NULL; newNode->next=head;
return head; head->prev=newNode;
} return newNode;
}
Insert At End : Circular Linked List (CLL)
struct Node * addAtEnd(int data, struct Node* head){ All nodes are connected to form a circle.
struct Node* newNode=Node(data);
the first node and the last node are connected to each other
if(head==NULL){
return newNode; which forms a circle.
} There is no NULL at the end.
struct Node* temp=head;
while ( temp->next !=NULL)
{
temp=temp->next;
}
temp->next=newNode;
newNode->prev=temp;
return head;
Operations on CLL
}
Delete At beginning: Insert at beginning
struct Node * deleteAtBeg( struct Node* head){ Insert at specific Posi on
if(head==NULL || head->next==NULL){ Insert at end
return NULL; delete at beginning
} delete at specific posi on
head ->next->prev = NULL; delete at end
return head->next;
} Advantage & disadvantage of CLL
delete At End :
Advantages:
struct Node * deleteAtEnd( struct Node* head){ No need for a NULL pointer
if( head==NULL || head->next == NULL){ Efficient inser on and dele on
return NULL;
Flexibility
}
struct Node* temp=head; Disadvantages :
while (temp->next->next !=NULL){ Traversal can be more complex
temp=temp->next; Reversing of circular list is a complex as SLL.
}
temp->next=NULL; Row major order & Column major order
return head;
} int arr[2][3]=
Traverse DLL : { {1,2,3},
{4,5,6} } //2D array
void traverse(struct Node* head){
while (head!=NULL){ //row major order
printf("%d ", head->data); {1,2,3,4,5,6}
head=head->next; } } //column major order
ReverseTraverse DLL: {1,4,2,5,3,6}
Address of any element in 1D array
void traverseRev(struct Node* head){
if(head==NULL) Address of A[I] = B + W * (I – LB)
return;
while (head->next!=NULL) I =element, B = Base address, LB = Lower Bound
head=head->next;
W = size of element in any array(in byte),
while (head!=NULL){
printf("%d ", head->data); Example: Given the base address of an array A[1300 ………… 1900] as
head=head->prev; 1020 and the size of each element is 2 bytes in the memory, find the
} address of A[1700].
}
Advantage & disadvantage of DLL Solution :
= 100 + 1 * (110)=210
Solution: