DS Programs
DS Programs
int main()
{
int num;
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);
int main()
{
int array[100],n,c,d,swap;
return 0;
}
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(){
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);
}
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
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
{
#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.