0% found this document useful (0 votes)
15 views21 pages

Data Structures1

The document contains multiple C programs demonstrating various sorting algorithms (Merge Sort, Quick Sort, Heap Sort), data structures (Linked Lists, Stacks, Queues, Trees), and their traversal methods. Each section includes the program aim, code implementation, and prompts for user input. The programs are designed for educational purposes at BVRIT Hyderabad College of Engineering for Women.

Uploaded by

babita260203
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)
15 views21 pages

Data Structures1

The document contains multiple C programs demonstrating various sorting algorithms (Merge Sort, Quick Sort, Heap Sort), data structures (Linked Lists, Stacks, Queues, Trees), and their traversal methods. Each section includes the program aim, code implementation, and prompts for user input. The programs are designed for educational purposes at BVRIT Hyderabad College of Engineering for Women.

Uploaded by

babita260203
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/ 21

BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program for MERGE SORT


Program:
#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[20],n,i;
printf("Enter size of array");
scanf("%d",&n);
printf("Enter elements of array");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("Array after Sorting");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

void mergesort(int a[],int low,int high)


{
int mid;
if(low<high)
{
mid = (low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,mid+1,high);
}

1
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

void merge(int a[],int low,int highl,int lowr,int high )


{
int c[20];
int i,j,k;
i = low;
j = lowr;
k = 0;
while(i<=highl && j<=high)
{
if(a[i]<a[j])
c[k++] = a[i++];
else
c[k++] = a[j++];
}
while(i<=highl)

c[k++] = a[i++];
while (j<=high)
c[k++] = a[j++];
for(i = low,j = 0;i<=high;i++,j++)
a[i] = c[j];
}
OUTPUT:

2
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program for QUICKSORT


Program:
#include<stdio.h>
void quicksort(int a[100],int first,int last)
{
int i,j,pivot,temp;
if(first<last)
{
pivot = first;
i = first;
j = last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp = a[i];
a[i]=a[j];
a[j] = temp;
}
}
temp = a[pivot];
a[pivot] = a[j];
a[j] = temp;
quicksort(a,0,pivot-1);
quicksort(a,pivot+1,last);
}

}
int main()
{
int a[40],n,i;
printf("Enter size of array");
scanf("%d",&n);
printf("Enter elemnts");

3
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quicksort(a,0,n-1);
printf("Elements after sorting");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

4
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program for printing elements in reverse order using single linked list
Program:
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node *next;
}*first=NULL,*temp,*last;

void insertion_node()
{
struct node *p;
struct node *n;
p=(struct node*)malloc(sizeof(struct node));
printf("Enter the data");
scanf("%d",&p->data);
p->next=NULL;
n=p;
if(first==NULL)
{
first=n;
temp=n;
}
else
{

n->next=first;
first=n;

}
}
void display_node()
{
struct node *i;
for(i=first;i!=NULL;i=i->next)
printf("%d ",i->data);

5
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

}
int main()
{
int i;
for(i=0;i<5;i++)
{
insertion_node();
}
display_node();
}
OUTPUT:

6
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program to implement stacks using linked list


Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;

}*top = NULL;

void push(int val)


{
struct node*p;
p = (struct node*)malloc(sizeof(struct node));
p->data = val;
p->next =NULL;
if(top == NULL)
{
top = p;
}
else
{
p->next = top;
top = p;
}
}

int pop()
{
struct node*temp;
int b;
if(top == NULL)
{
printf("Stack underflow\n");
return -1;
}
else
{
7
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

temp = top;
b = temp->data;
top = top->next;
free(temp);
return b;
}
}
void display()
{
struct node*i;
if(top == NULL)
{
printf("Stack is Empty");
}
else
{

for(i= top;i!=NULL;i=i->next)
{
printf(" %d ",i->data);
}
}
}

int main()
{
int ch,d,p,i;
while(1)
{
printf("Enter your choice ");
printf("\n 1 for push \n 2 for pop \n 3 for display \n 4 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the elemnet you want to push");
scanf("%d",&d);
push(d);
}
break;
case 2:p=pop();
{
8
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

if(p>=0)
printf("popped element = %d\n",p);
else
printf("%d",p);
}
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Enter between 1 to 4\n");

}
}
}
OUTPUT:

9
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

10
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program to implement Queues using linked list


Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;

}*front = NULL,*rear = NULL;

void enqueue(int val)


{
struct node*p;
p = (struct node*)malloc(sizeof(struct node));
p->data = val;
p->next =NULL;
if(front == NULL&&rear == NULL)
{
front = p;
rear = p;
}
else
{
rear->next = p;
rear = p;
}
}

int dequeue()
{
struct node*temp;
int b;
if(front == NULL&&rear == NULL)
{
printf("Queue underflow\n");
return -1;
}
else if(front == rear)
11
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

{
temp = front;
b = temp->data;
front = NULL;
rear = NULL;
free(temp);
return b;
}
else
{
temp = front;
b = temp->data;
front = front->next;
free(temp);
return b;
}

}
void display()
{
struct node*i;
if(front ==NULL)
{
printf("Queue is empty");
}
else
{
for(i= front ;i!=NULL;i=i->next)
{
printf(" %d ",i->data);
}

}
}

int main()
{
int ch,d,p,i,n;
while(1)
{
printf("Enter your choice ");
printf("\n 1 for ENQUEUE \n 2 for DEQUEUE \n 3 for DISPLAY \n 4 for EXIT");
12
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the data");
scanf("%d",&d);
enqueue(d);
}
break;
case 2:p=dequeue();
{if(p>=0)
printf("deleted element = %d\n",p);
else
printf("%d\n",p);
}
break;
case 3:display();
break;
case 4:exit(0);
default:printf("Enter between 1 to 4");

}
}
}
OUTPUT:

13
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

14
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program to implement the tree traversal methods.


Program:
#include<stdio.h>
#include<stdlib.h>

struct node
{
int data;
struct node*left,*right;
}*root = NULL,*temp;

void inorder(struct node *root)


{

if(root!=NULL)
{
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
return ;
}

void preorder(struct node* root)


{
printf("PREORDER TREE TRAVERSAL");
if(root!=NULL)
{
printf("%d ",root->data);
preorder(root->left);
preorder(root->right);
}
return ;
}

void postorder(struct node* root)


{
printf("POSTORDER TREE TRAVERSAL");
15
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
return ;
}

void create_node()
{
struct node*n;
n = (struct node*)malloc(sizeof (struct node));
printf("Enter the data\n");
scanf("%d",&n->data);
n -> left = NULL;
n -> right = NULL;
if( root == NULL)
{
root = n;
temp = n;
}
else
{

int pos,ch;
printf("\nEnter your choice : 1 for left ,2 for right\n");
scanf("%d",&ch);
if(ch == 1)
{
printf("Enter position : 1 for left , 2 for right\n");
scanf("%d",&pos);
if(pos == 1)
{
while(temp->left!=NULL)
{
temp= temp->left;
}
temp->left = n;
}
else
16
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

{
while(temp->right!=NULL)
{
temp = temp->right;
}
temp->right = n;

}
}
else
{
printf("Enter position : 1 for left , 2 for right\n");
scanf("%d",&pos);
if(pos == 1)
{
while(temp->left!=NULL)
{
temp = temp->left;
}
temp->left = n;
}
else
{
while(temp->right!=NULL)
{
temp = temp->right;
}
temp->right = n;
}
}
}
}
int main()
{
int i,c;
for(i=0;i<6;i++)
create_node();
printf("--------------TREE TRAVERSAL---------------");
while(1)
{
printf("Enter 1 for Inorder \n 2 for Preorder \n 3 for Postorder\n 4 for exit \n");
scanf("%d",&c);
17
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

printf("/n");
switch(c)
{
case 1:inorder(root);
break;
case 2:preorder(root);
break;
case 3:postorder(root);
break;
case 4:exit(0);
default:printf("Enter valid choice");
break;
}
}
return 0;
}
OUTPUT:

18
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

19
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

Roll No. :20WH1A12A6 Date:

Aim: Write a C program to implement Heap sort


Program:
#include<stdio.h>

void create(int []);


void down_adjust(int [],int);

void main()
{
int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
//create a heap
heap[0]=n;
create(heap);
//sorting
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}

//print sorted data


printf("\nArray after sorting:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
}

void create(int heap[])


{
int i,n;
20
Department of Information Technology
BVRIT HYDERABAD College of Engineering for women

n=heap[0]; //no. of elements


for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}

void down_adjust(int heap[],int i)


{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{
j=2*i; //j points to left child
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}
OUTPUT:

21
Department of Information Technology

You might also like