0% found this document useful (0 votes)
27 views7 pages

DS Lab

Lab manual consist of set of questions It help students to understand the subject better
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

DS Lab

Lab manual consist of set of questions It help students to understand the subject better
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DS LAB EXTERNAL

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;
}

struct node* InsertEndS(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;
}
struct node *temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
return head;
}
struct node *deleteAtBeginS(struct node* head)
{
if(head==NULL)
{
printf("list is empty");
}
if(head->next==NULL)
{
free(head);
return NULL;
}
struct node *temp=head;
head=head->next;
free(temp);
return head;
}
struct node *deleteAtEndS(struct node* head)
{
if(head==NULL)
{
printf("the list is empty");
return head;
}
if(head->next==NULL)
{
free(head);
return head;
}
struct node* temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
free(temp->next);
temp->next=NULL;
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);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy