Professional Documents
Culture Documents
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
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 …..)
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;
6. Write algorithm and C-function that will delete the node pointed by a pointer
from a linear linked list implemented dynamically.
8. Write an algorithm which deletes the second last node from list L.
return head;
if((head->next->next)==NULL)
List *temp=head;
head=head->next;
free(temp);
return head;
prev = curr;
current = current->next;
prev->next = current->next;
free(current);
return head;