DS Lab
DS Lab
QUESTION-1
#include<stdio.h>
#include<stdlib.h>
struct node
{
char data;
struct node * next;
};
int main()
{
struct node *head,*node1,*node2,*node3,*node4,*node5;
node1=(struct node*) malloc(sizeof(struct node));
node2=(struct node*) malloc(sizeof(struct node));
node3=(struct node*) malloc(sizeof(struct node));
node4=(struct node*) malloc(sizeof(struct node));
node5=(struct node*) malloc(sizeof(struct node));
node1->data='G';
node2->data='U';
node3->data='K';
node4->data='T';
node5->data='R';
node1->next=node2;
node2->next=node3;
node3->next=node4;
node4->next=node5;
node5->next=node1;
head= node5;
printf("circular list :");
struct node *temp=head;
do{
printf("%c->",temp->data);
temp=temp->next;
}while(temp!=head);
printf("head\n");
}
QUESTION-2
#include<stdio.h>
void print(int arr[],int n)
{
for(int i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
void merge(int arr[],int low,int mid,int high)
{
int n1=(mid-low)+1;
int n2=high-mid;
int L[n1];
int R[n2];
for(int i=0;i<n1;i++)
{
L[i]=arr[low+i];
}
for(int j=0;j<n2;j++)
{
R[j]=arr[mid+1+j];
}
int i=0,j=0,k=low;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
}
void mergesort(int arr[],int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,high);
merge(arr,low,mid,high);
}
}
int main()
{
int arr[7]={50,30,80,20,10,15,90};
int n=sizeof(arr)/sizeof(arr[0]);
printf("Intial array elements : ");
print(arr,n);
mergesort(arr,0,n-1);
printf("After sortiong: ");
print(arr,n);
return 0;
}
QUESTION-3
#include<stdio.h>
#include<stdlib.h>
struct node{
char data;
struct node* next;
};
struct node *InsertatBeginS(struct node *head,char data)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=data;
newnode->next=NULL;
if(head==NULL)
{
head=newnode;
return head;
}
newnode->next=head;
head=newnode;
return head;
}
}
void print(struct node*head)
{
printf("\nlinked list:");
struct node *temp=head;
while(temp!=NULL)
{
printf("%c->",temp->data);
temp=temp->next;
}
printf("NULL\n");
}
int main()
{
struct node* head=NULL;
printf("the intial linked list");
print(head);
head=InsertatBeginS(head,'U');
head=InsertatBeginS(head,'G');
head=InsertatBeginS(head,'R');
printf("after insertion of three nodes at begin :");
print(head);
head=InsertEndS(head,'K');
head=InsertEndS(head,'T');
printf("after insertin of two elements at end :");
print(head);
head=deleteAtBeginS(head);
head=deleteAtBeginS(head);
printf("after deletion of two nodes at begin :");
print(head);
head=deleteAtEndS(head);
head=deleteAtEndS(head);
printf("after deletion of two elements at end :");
print(head);