You are on page 1of 12

ASSIGNMENT 1

Array:
1. Algorithm to Rotate an array by K position to Left:
123456 : Rotate By 3: 456123
• Insert an Array from User
• Rotate the whole array: 654321
• Rotate the array from starting index to last-given index, from 0 To
4 :456123
• Then Rotate the remaining last elements: 456123

2. Algorithm to merge two sorted arrays into a single sorted array.


• Insert 2 sorted arrays and initialize an array result.
• Count no. of elements n1 and n2 in both the arrays.
• Set 2 counters i and j to zero “0”one for each array.
• Start a while loop : while(i<n1 && j<n2) and Compare
the elements of both arrays :
ith index of first array and jth index of second array
• Check the smaller element and add it in to the result array
and increase its counter by one.
• Check whether any one of these array’s elements are left
and add them to the end of the array.
• Print the sorted array.
3. Compute the length of array P find the effective indices of the
element.
P[-5:5,6:16],BA =250; W=2
Length of array=? ; effective index of P[-2,15]=?
L=UB-LB+1
L1=5+5+1=11
L2=16-6+1=11
Length of array=11*11=121
Effective index=ki-lbi
Effective index of P[-2,15]:
E1=-2-(-5)=3
E2=15-6=9

LINKED LIST
1. To count no. of nodes.
int count(List *head)
{
if(head==NULL)
{
return 0;
}
int c=0;
while(head!=NULL)
{
head=head->next;
c++;
}
return c;
}

b. To reverse a list.
List reverse (List *head)
{
if(head==NULL || head->next=NULL)
{
return head;
}
List prev=NULL;
List curr=head;
List last=NULL;
while(curr!=NULL)
{
last=curr->next;
curr->next=prev;
prev=curr;
curr=last;
}
head=prev;
return head;
}
c. to delete alternate nodes(1 st,3rd …..)

List delete_alternate(List *head)


{
if(head==NULL || head->next=NULL)
{
return NULL;
}
List start=head;
List curr=head->next;
head=curr;
List last=curr->next;
while(curr!=NULL && curr->next!=NULL
{ free(start);
curr->next=last->next;
start=last;
curr=last->next;
last=curr->next;
}
free(start);
return head;
}
2. POLYNOMIAL:
Representation:

Coefficient Power Address of next node

Program to add 2 linked list:


Struct node * Allocate(intc, inte) {
Struct node * nn=(struct node *) malloc(sizeof(struct node ));
if(nn!=NULL){
nn->coeff=c;
nn->exp=e;
nn->next=NULL;
}
return nn;
}
Struct node *insert(struct node *head3,int c, int e) {
Struct node *nn=Allocate(c,e);
if(head3==NULL){
head3=nn; }
curr=head3;
while(curr->next!=NULL){
curr=curr->next;
}
curr->next=nn;
return head3;
}
Struct node *add(struct node *head1,struct node *head2)
{
Struct node *t1,*t2, *head3=NULL;
t1=head1;
t2=head2;
while(t1!=NULL && t2!=NULL) {
if(t1->exp> t2->exp){
head3 = insert(head3,t1->coeff,t1->exp);
t1=t1->next;
}
else if(t2->exp> t1->exp){
head3 = insert(head3,t2->coeff,t2->exp);
t2=t2->next;
}
else{
head3 = insert(head3,t1->coeff+ t2->coeff,t1->exp);
t1=t1->next;
t2=t2->next;
}
}
while(t1!=NULL) {
head3 = insert(head3,t1->coeff,t1->exp);
t1=t1->next;
}
while(t2!=NULL) {
head3 = insert(head3,t2->coeff,t2->exp);
t2=t2->next; }
return head3;
}
3. DOUBLY LINKED LIST
Advantages:
• It provides more flexibility.
• Traversing of list become very easy.
• Insertion and Deletion of nodes also become easy
• Consumes less time in functioning comparatively to other linked
lists.
• It allows for easy traversal in both directions.

Disadvantages:
• It consumes extra memory space compared to other lists because it
stores address of next as well as previous element.
• We can only access elements sequentially, not at random position.
• More complex.

Applications:
• Used in applications where forward and backward traversing is
required
• Used in navigation systems
• Used in making games
• Text editors use doubly linked lists for internal data structures.
4. Advantages of 2 way list over 1 way list
i. Traversing the list to process each node: In a two-way list, we can
traverse both forward and backward, making it easier to process
each node.
ii. Searching an unsorted list for a given element: a two-way list does
not have a significant advantage over a one-way list because the
search process typically involves visiting each node one by one
iii. Searching a sorted list for a given item: In a sorted list, a two-way
list allows us to start searching from either end, reducing the
search time.
iv. Inserting a node before the node with a given location LOC: In a
two-way list, we can easily insert a node before a given location
by using address of previous node while it will be a little complex
in case of singly linked list.
v. Deleting a node whose location LOC is given: Similar to insertion,
in a two-way list, we can easily delete a node at a given location
by adjusting the pointers of the neighboring nodes.
5. Split list into two and make the second half as the first and vice versa.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int val;
struct node *next;
};
typedef struct node list;
list *allocate(int data)
{
list *nn=(list *)malloc(sizeof(list));
if(nn!=NULL)
{
nn->val=data;
nn->next=NULL;
}
return nn;
}
void display(list *head)
{
if(head==NULL)
{
printf("List is Empty\n");
return;
}
list *cur=head;
while(cur!=NULL)
{
printf("%d->",cur->val);
cur=cur->next;
}
printf("NULL\n");
}
list* insert (list *head,int data)
{
list *nn=allocate(data);
if(nn==NULL)
{
printf("Linked List Overflow\n");
return head;
}
if(head==NULL)
{
head=nn;
return head;
}
list *cur=head;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=nn;
return head;
}
List splitList(list *head) {
if(head==NULL || head->next==NULL)
{
display(head);
return head;
}
struct node *slow = head;
struct node *fast = head->next;
while (fast != NULL && fast->next != NULL)
{
slow = slow->next;
fast = fast->next->next;
}
struct node *head2 = slow->next;
slow->next = NULL;

struct node *curr = head2;


while (curr ->next!= NULL) {
curr = curr->next;
}
Curr->next=head;
head=head2;
display(head);
return head;
}

6. Write algorithm and C-function that will delete the node pointed by a pointer
from a linear linked list implemented dynamically.

List * delete(List *head, List *ptr)


{
if(head==ptr)
{
return head->next;
}
if(head==NULL)
{
printf(“list is empty”);
return head;
}
List *curr=head;
while(curr!=NULL && curr->next!=ptr)
{
curr=curr->next;
}
if(curr==NULL)
{
printf(“element not present in the list”);
return head;
}
List *temp=curr->next;
curr->next=temp->next;
free(temp);
return head;
}

7. Write algorithm for searching item in:


i) Sorted Circular Linked List
1. If the list is empty : print list is empty ;return NULL
2. Initialize a pointer "current" to the head of the list.
3. traverse the list:
a. If the data in the current node is equal to the target, return
current.
b. If the data in the current node is greater than the target,break
c. Move to the next node.
d. If we reach the head again, break
4. If the target is not found, print target not found: return NULL.
ii) Unsorted Circular Linked List

1. If the list is empty : print list is empty return NULL


2. Initialize a pointer "current" to the head of the list.
3. traverse the list:
a. If the data in the current node is equal to the target, return current
b. Move to the next node.
c. If we reach the head again, break
4. If the target is not found, print target not found: return NULL.

8. Write an algorithm which deletes the second last node from list L.

List deleteSecondLastNode(struct Node* head) {

if (head == NULL || (head)->next == NULL) {

printf(“there is no 2nd last element in the list”)

return head;

if((head->next->next)==NULL)

List *temp=head;

head=head->next;

free(temp);

return head;

List* prev =head;

List* current = head->next;

while (current->next ->next!= NULL) {

prev = curr;

current = current->next;

prev->next = current->next;
free(current);

return head;

You might also like