0% found this document useful (0 votes)
31 views

DS Programs

The document contains descriptions and code examples for several algorithms and data structures: 1. It includes code to find the greatest common divisor (GCD) of two integers using recursion. 2. It shows code for solving the Tower of Hanoi puzzle using recursion to calculate and print the sequence of moves. 3. It provides a program to read city names from input and sort them alphabetically using string comparison and swapping. 4. Additional examples demonstrate sorting algorithms like selection sort, bubble sort, and insertion sort by sorting arrays of integers in ascending order. 5. Other examples implement classic data structures - stacks using arrays, linear queues using arrays, and circular queues using arrays.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

DS Programs

The document contains descriptions and code examples for several algorithms and data structures: 1. It includes code to find the greatest common divisor (GCD) of two integers using recursion. 2. It shows code for solving the Tower of Hanoi puzzle using recursion to calculate and print the sequence of moves. 3. It provides a program to read city names from input and sort them alphabetically using string comparison and swapping. 4. Additional examples demonstrate sorting algorithms like selection sort, bubble sort, and insertion sort by sorting arrays of integers in ascending order. 5. Other examples implement classic data structures - stacks using arrays, linear queues using arrays, and circular queues using arrays.

Uploaded by

Sahana M.k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DS Programs

1. GCD using Recursion


#include<stdio.h>
#include<math.h>
int GCD(int i, int j);
int main(){
int a,b;
printf("Enter the two integers: \n");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d is %d\n",a,b,GCD(a,b));
return 0;
}
/* Recursive Function*/
int GCD(int i,int j){
if(j>i)
return GCD(j,i);
if(j==0)
return i;
else
return GCD(j,i%j);
}

3. Tower of Hanoi using Recursion


#include <stdio.h>

void towers(int, char, char, char);

int main()
{
int num;

printf("Enter the number of disks : ");


scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

4. Program to read the names of cities and arrange them alphabetically.


#include<stdio.h>
#include<string.h>
main(){
int i,j,n;
char str[100][100],s[100];

printf("Enter the number of cities :\n");


scanf("%d",&n);

printf("Enter names of %d cities:\n",n);


for(i=0;i<n;i++){
scanf("%s",str[i]);
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("\nAlphabetical order of cities:\n");
for(i=0;i<n;i++){
printf("%s\n",str[i]);
}
}

Output:
Enter the number of cities :
5
Enter names of 5 cities:
Hubli
Bangalore
Mangalore
Davanagere
Mandya
Alphabetical order of cities:
Bangalore
Davanagere
Hubli
Mandya
Mangalore
5. C program on Selection Sort
#include <stdio.h>
void main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);

printf("Enter %d Numbers\n", n);


for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position]=swap;
}
}
printf("Sorted Array:\n");
for(i = 0; i < n; i++)
printf("%d\n", a[i]);
}

6. Program on Bubble Sort


#include <stdio.h>

void bubble_sort(int [],int);

int main()
{
int array[100],n,c,d,swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d Elementss\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);
bubble_sort(array, n);

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf("%d\n", array[c]);

return 0;
}

void bubble_sort(int list[],int n)


{
int c,d,temp;

for (c = 0 ; c < ( n - 1 ); c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
temp=list[d];
list[d]=list[d+1];
list[d+1]=temp;
}
}
}
}

Output
Enter number of elements
5
Enter 5 Elements
5
8
1
75
36
Sorted list in ascending order:
1
5
8
36
75
7. Program on Insertion Sort
#include<stdio.h>
int main(){

/* Here i & j for loop counters, temp for swapping,


* count for total number of elements, number[] to
* store the input numbers in array. You can increase
* or decrease the size of number array as per requirement
*/
int i, j, count, temp, number[25];

printf("Enter the number of elements: ");


scanf("%d",&count);

printf("Enter %d elements: ", count);


// This loop would store the input numbers in array
for(i=0;i<count;i++)
scanf("%d",&number[i]);

// Implementation of insertion sort algorithm


for(i=1;i<count;i++){
temp=number[i];
j=i-1;
while((temp<number[j])&&(j>=0)){
number[j+1]=number[j];
j=j-1;
}
number[j+1]=temp;
}

printf("Order of Sorted elements: ");


for(i=0;i<count;i++)
printf(" %d",number[i]);

return 0;
}

Output:
Enter the number of elements:
5
Enter 5 elements:
87
67
8
34
29
Order of Sorted elements: 8 29 34 67 87
8. Stack Operations
/* C program to demonstrate the working of stack */
#include<stdio.h>
#include<conio.h>
#define MAX_SIZE 5
int STACK[MAX_SIZE];
int top=-1,ch;
int pop();
int push();
int display();
main()
{
do
{
printf("enter 1 for push\n");
printf("enter 2 for pop\n");
printf("enter 3 for display\n");
printf("enter 4 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:
printf("Wrong choice");

}
}while(ch!=4);
getch();
}

push()
{
int item;
if(top==MAX_SIZE)
printf("Stack is full");
else
{
printf("Enter the element\n");
scanf("%d",&item);
top=top+1;
STACK[top]=item;
}
}

pop()
{
int item;
if(top==-1)
printf("Stack is empty");
else
{
item=STACK[top];
top=top-1;
printf("The element poped is %d\n",item);
}
}

display()
{
int i;
if(top==-1)
printf("Stack is empty");
else
{
printf("stack elements are\n");
for(i=top;i>=0;i--)
printf("%d\n",STACK[i]);
}
}

9. Linear Queue
/* Write a C program to simulate the working of an ordinary Queue using an array. */
#include<stdio.h>
#define MAXSIZE 5
int queue[10];
int front=-1,rear=-1,ch;
main()
{
do
{
printf("\nEnter your choice");
printf("\n1:Insert\n2:Delete\n3:Display\n4:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: Qinsert();
break;
case 2: Qdelete();
break;
case 3: Qdisplay();
break;
case 4: return(0);
default:
printf("Wrong choice");
}
}while(ch!=4);

Qinsert()
{
int num;
if(rear== (MAXSIZE-1))
{
printf("Queue is full\n");
return;
}
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&num);
rear=rear+1;
queue[rear]=num;
if(front==-1)
front++;
}
}

Qdelete()
{
int num;
if(front== -1)
{
printf("Queue is Empty");
return;
}
else
{
if(front==rear)
{
printf("Deleted element is =%d\n",queue[front]);
front=rear=-1;
}
else
{
num=queue[front];
printf("Deleted element is =%d\n",queue[front]);
front++;
}
}
return(num);
}

Qdisplay()
{
int i;
if(front==-1)
{
printf("Queue is Empty");
return;
}
else
{
printf("\n The status of the queue\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
}

PART B

1. Quick Sort

2. Merge Sort

3.Linear Search

4.Binary Search

5.Infix to Postfix

/* Write a C program to convert and print a given valid fully parenthesized infix arithmetic
expression to post fix expression*/
#define SIZE 50 /* Size of Stack */
#include<stdio.h>
#include<conio.h>
char s[SIZE];
int top=-1; /* Global declarations */

push(char elem)
{ /* Function for PUSH operation */
top=top+1;
s[top]=elem;
}

char pop()
{ /* Function for POP operation */
char symbol;
symbol=s[top];
top=top-1;
return(symbol);
}

int pr(char elem)


{ /* Function for precedence */
switch(elem)
{
case '#': return -1;
case '(': return 0;
case '+':
case '-': return 2;
case '*':
case '/': return 4;
}
}

main()
{ /* Main Program */
char infx[50],pofx[50],ch,elem;
int i=0,k=0;
printf("Read the Infix Expression\n ");
scanf("%s",infx);
push('#');
while( (ch=infx[i++]) != '\0')
{
if( ch == '(')
push(ch);
else
if(isalnum(ch))
pofx[k++]=ch;
else
if( ch == ')')
{
while( s[top] != '(')
pofx[k++]=pop();
elem=pop(); /* Remove ( */
}
else
{ /* Operator */
while( pr(s[top]) >= pr(ch) )
pofx[k++]=pop();
push(ch);
}
}
while( s[top] != '#') /* Pop from stack till empty */
pofx[k++]=pop();
pofx[k]='\0'; /* Make pofix as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n",infx,pofx);
}

6.Circular Queue

/*Write a C program to simulate the working of a Circular Queue using an array. */


#include<stdio.h>
#include<conio.h>
#define MAXSIZE 5
int Cqueue[10],item;
int front=0,rear=-1,ch,count=0;
main()
{
do
{
printf("\nenter 1 for insert\n");
printf("enter 2 for delete\n");
printf("enter 3 for display\n");
printf("enter 4 for exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: CQinsert(Cqueue,&rear,&count);
break;
case 2: CQdelete(Cqueue,&front,&count);
break;
case 3: CQdisplay(Cqueue,front,count);
break;
case 4: exit(0);
default:
printf("Wrong choice");
}
}while(ch!=4);
getch();
}

CQinsert(int Cqueue[],int *rear,int *count)


{

if(*count==MAXSIZE)
{
printf("CQueue is full\n");
return;
}
else
{
printf("enter the element");
scanf("%d",&item);
*rear=(*rear+1)%MAXSIZE;
Cqueue[*rear]=item;
*count+=1;
}
}
CQdelete(int queue[],int *front,int *count)
{

if(*count==0)
{
printf("Queue is Empty");
return;
}
else
{

printf("Deleted element is =%d\n",Cqueue[*front]);


*front=(*front+1)%MAXSIZE;
*count-=1;
}
}

CQdisplay(int Cqueue[],int front,int count)


{
int i;
if(count==0)
{
printf("Queue is Empty");
return;
}
else
{
printf("COUNT=%d FRONT=%d",count,front);
printf("\n The status of the queue\n");
for(i=1;i<=count;i++)
{
printf("%d\n",Cqueue[front]);
front = (front+1)% MAXSIZE;
}
}
}

7.Singly Linked List


/* Using dynamic variables and pointers Write a C program to construct a singly linked
list.The operations to be supported are
Inserting a node in the front of the list
Deleting the node based on Roll- No
displaying all the nodes in the list */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int rollno;
char name[20];
struct node *ptr;
};
typedef struct node Node;
Node *head,*temp,*temp1,*temp2;
main()
{
int ch;
head=NULL;
do
{
printf("\n 1:linsert\n 2:ldelete\n 3: display\n4:exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: linsert();
break;
case 2: ldelete();
break;
case 3: ldisplay();
break;
case 4: exit(0);
default : printf("wrong choice");
}
}while(ch!=4);
getch();
}
linsert()
{
temp=(Node *) malloc(sizeof(Node));
printf("Enter the roll number\n");
scanf("%d",&temp->rollno);
printf("Enter the name\n");
scanf("%s",temp->name);
if(head==NULL)
{
head=temp;
temp->ptr=NULL;
}
else
{
temp->ptr=head;
head=temp;
}
}

ldelete()
{
int r;
if(head==NULL)
printf("singly linked list is empty\n");
else
{
printf("Enter the rollno which is to be deleted\n");
scanf("%d",&r);
temp1=NULL;
temp=head;
while(temp!=NULL && temp->rollno!=r)
{
temp1=temp;
temp=temp->ptr;
}

if(temp==NULL)
printf("A node with rollno=%d is not present in
list\n",r);
else if(head->rollno==r)
{
head=head->ptr;
free(temp);
}
else
{
temp2=temp->ptr;
temp1->ptr=temp2;
free(temp);
}
}
}
ldisplay()
{
if(head==NULL)
printf("Singly linked list is empty");
else
{

for(temp1=head;temp1->ptr!=NULL;temp1=temp1->ptr)
{

printf("Rollno=%d\tName=%s\n",temp1->rollno,temp1->name);
}
printf("Rollno=%d\t
Name=%s\n",temp1->rollno,temp1->name);
}
}

8.
9.

You might also like