DS Program
DS Program
#include<stdio.h>
#include<conio.h>
void main()
int x;
int *ptr;
scanf("%d",&x);
ptr=&x;
getch();
}
Program 2: Calculate the Length of a String Using a Pointer
#include<stdio.h>
void main()
char str[10];
int i;
scanf("%s",str);
i=0;
while(*(str+i))
i++;
}
Program 3: Swap Numbers Using Pointer
#include<stdio.h>
#include<conio.h>
void main()
int a,b;
int *p1,*p2;
int temp;
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
temp=*p1;
*p1=*p2;
*p2=temp;
getch();
}
Program 4: Print All Permutations of a Given String Using
Pointers
#include<stdio.h>
#include<string.h>
char temp;
temp = *p;
*p = *q;
*q = temp;
int j;
if (i == n) {
printf("%s\n", a);
} else {
permute(a, i + 1, n);
int main() {
char a[20];
int n;
scanf("%s", a);
n = strlen(a);
printf("Permutations:\n");
permute(a, 0, n - 1);
return 0;
}
Program 5: Store n Students' Information Using Structure
#include<stdio.h>
#include<conio.h>
struct Student
int roll,m1,m2;
char name[20];
};
void main()
int total,n,i;
float avg;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&s[i].roll);
scanf("%s",s[i].name);
printf("Enter the marks : ");
scanf("%d%d",&s[i].m1,&s[i].m2);
printf("______\n");
printf("RollNo\tName\tM1\tM2\tTotal\tAvg\n");
printf("______\n");
for(i=0;i<n;i++)
total=s[i].m1+s[i].m2;
avg=total/2;
printf("%d\t%s\t%d\t%d\t%d\t%.2f\n",
s[i].roll,s[i].name,s[i].m1,s[i].m2,total,avg);
getch();
}
Program 6: Implement Push, Pop, and Traverse Operations
on Stack
#include<stdio.h>
#include<conio.h>
int a[50],top,i,ele,stack_size;
void push();
void pop();
void display();
void main()
int ch;
top=-1;
scanf("%d",&stack_size);
do
printf("---\n");
printf("1:Push\n2:Pop\n3:Display\n4:Exit\n");
printf("---\n");
printf("Enter the choice : ");
scanf("%d",&ch);
switch(ch)
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
}while(ch<4);
getch();
void push()
if(top==stack_size-1)
printf("\nStack is Overflow\n");
else
a[++top]=ele;
void pop()
if(top==-1)
printf("\nStack is Underflow\n");
else
void display()
if(top==-1)
printf("\nStack is empty\n");
else
for(i=0;i<=top;i++)
printf("%d\n",a[i]);
#include <stdio.h>
#include <string.h>
switch(symbol)
case '+':
case '*':
case '^':
default: return 8;
}
}
switch(symbol)
case '+':
case '*':
case '^':
default: return 7;
int top;
char s[30];
int j, n;
int i;
char symbol;
top = -1;
s[++top] = '#';
j =0;
n=strlen(infix);
symbol = infix[i];
while (F(s[top])>G(symbol))
postfix[j] = s[top--];
j++;
if (F(s[top])!=G(symbol))
s[++top] = symbol;
else
top--;
while(s[top]!='#')
postfix[j++] = s[top--];
}
postfix[j] = '\0';
void main()
char infix[20];
char postfix[20];
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("%s\n",postfix);
getch();
}
Program 8: Convert Infix Notation to Prefix Notation
#include <stdio.h>
#include <string.h>
switch(symbol)
case '+':
case '*':
case '^':
default: return 8;
}
int G(char symbol)
switch(symbol)
case '+':
case '*':
case '^':
default: return 7;
int top;
char s[30];
int j, n;
int i;
char symbol;
top = -1;
s[++top] = '#';
j =0;
strrev(infix);
n=strlen(infix);
symbol = infix[i];
while (F(s[top])>G(symbol))
prefix[j] = s[top--];
j++;
if (F(s[top])!=G(symbol))
s[++top] = symbol;
else
top--;
while(s[top]!='#')
{
prefix[j++] = s[top--];
prefix[j] = '\0';
void main()
char infix[20];
char prefix[20];
scanf("%s",infix);
infix_prefix(infix,prefix);
strrev(prefix);
printf("%s\n",prefix);
getch();
}
Program 9: Convert Prefix Notation to Postfix Notation
#include <stdio.h>
#include <conio.h>
#include <string.h>
*top=*top+1;
strcpy(s[*top],item);
char *item;
item=s[*top];
*top=*top-1;
return item;
int i,top;
char s[20][20],symbol,temp[2],*op1,*op2;
top=-1;
strrev(prefix);
for(i=0;i<strlen(prefix);i++)
symbol=prefix[i];
temp[0]=symbol;
temp[1]='\0';
switch(symbol)
case '+':
case '-':
case '*':
case '/':
case '^':
op1=pop(&top,s);
op2=pop(&top,s);
strcpy(postfix,op1);
strcat(postfix,op2);
strcat(postfix,temp);
push(postfix,&top,s);
break;
default:push(temp,&top,s);
}
}
void main()
char prefix[20],postfix[20];
scanf("%s",prefix);
prefix_postfix(prefix,postfix);
printf("%s",postfix);
getch();
}
Program 10: Perform Insert, Delete, and Display Operations on Queue
#include<stdio.h>
#include<conio.h>
void insert(int);
void qdelete();
void display();
int queue_size,q[10],f,r;
void main()
int choice,item;
f=0;
r=-1;
scanf("%d",&queue_size);
do
printf("1:Insert\n2:Delete\n3:Display\n4:Exit\n");
printf("---\n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&item);
insert(item);
break;
case 2:qdelete();
break;
case 3:display();
break;
break;
}while(choice<4);
getch();
if(r==queue_size-1)
{
printf("\nQueue is full\n");
else
q[++r]=item;
void qdelete()
int i;
if(r==-1)
printf("\nQueue is empty\n");
else
if(f>r)
r=-1;
f=0;
}
}
void display()
int i;
if(r==-1)
printf("\nQueue is empty\n");
else
for(i=f;i<=r;i++)
printf("%d\n",q[i]);
}
Program 11: Implement Circular Queue
#include<stdio.h>
void qinsert(int);
void qdelete();
void qdisplay();
int q_size;
int i,f,r,item,q[20],count;
void main()
int choice;
f=0;
r=-1;
count=0;
scanf("%d",&q_size);
do
{
printf("\n Queue Operations\n");
printf("\n1:Insert\n2:Delete\n3:Display\n4:Exit\n");
printf("---\n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&item);
qinsert(item);
break;
case 2:qdelete();
break;
case 3:qdisplay();
break;
default:printf("Exit");
}while(choice<4);
if(count==q_size)
printf("\nQueue overflow\n");
}
else
r=(r+1)%q_size;
q[r]=item;
count++;
void qdelete()
if(count==0)
printf("\nQueue underflow\n");
else
f=(f+1)%q_size;
count--;
void qdisplay()
int j;
if(count==0)
printf("\nQueue is Empty\n");
else
i=f;
for(j=1;j<=count;j++)
printf("%d\n",q[i]);
i=(i+1)%q_size;
}
Program 12: Implement Double-Ended Queue
#include<stdio.h>
#include<stdlib.h>
#define QUEUE_SIZE 5
int choice,item,f,r,q[10];
void insert_rear()
if (r==QUEUE_SIZE-1)
printf("Queue overflow");
return;
r=r+1;
q[r] = item;
void delete_front()
if(f>r)
{
printf("Queue underflow\n");
return;
if (f>r)
f=0;
r=-1;
void insert_front()
q[++(r)] =item;
return;
if(f!=0)
q[--(f)] = item;
return;
}
printf("Front insertion not possible\n");
void delete_rear()
if(f>r)
printf("Queue underflow\n");
return;
if (f> r)
f=0;
r=-1;
void display()
int i;
if(f>r)
{
printf("Queue is empty\n");
return;
printf("%d\n", q[i]);
void main()
f=0;r=-1;
for (;;)
printf("1:Insert_front\n2:Insert_rear\n");
printf("3:Delete_front\n4:Delete_rear\n");
printf("5:Display\n6:Exit\n");
scanf("%d",&choice);
switch (choice)
{
scanf("%d",&item);
insert_front();
break;
scanf("%d",&item);
insert_rear();
break;
case 3:delete_front();
break;
case 4: delete_rear();
break;
case 5:display();
break;
default: exit(0);
}
Program 13: Implement Priority Queue
#include<stdio.h>
void qinsert(int);
void qdelete();
void display();
int queue_size,q[10],f,r;
void main()
int choice,item;
f=0;
r=-1;
scanf("%d",&queue_size);
do
printf("---\n");
printf("1:Insert\n2:Delete\n3:Display\n4:Exit\n");
printf("---\n");
scanf("%d",&choice);
switch(choice)
scanf("%d",&item);
qinsert(item);
break;
case 2:qdelete();
break;
case 3:display();
break;
break;
}while(choice<4);
getch();
int j;
if(r==queue_size-1)
printf("\nQueue is full\n");
else
j=r;
q[j+1]=q[j];
j--;
q[j+1]=item;
r=r+1;
void qdelete()
int i;
if(r==-1)
printf("\nQueue is empty\n");
else
{
if(f>r)
r=-1;
f=0;
void display()
int i;
if(r==-1)
printf("\nQueue is empty\n");
else
for(i=f;i<=r;i++)
printf("%d",q[i]);
}
}
#include<stdio.h>
void main()
int a[10],n,i,key;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&key);
for(i=0;i<n;i++)
if(a[i]==key)
{
printf("The element is found at location %d",(i+1));
break;
if(i==n)
}
Program 15: Sort Given Array Using Insertion Sort Technique
#include <stdio.h>
int key, j, s;
key = array[s];
j = s - 1;
array[j + 1] = array[j];
--j;
array[j + 1] = key;
void main()
{
int a[10], n, i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insertionSort(a, n);
printf("%d\n", a[i]);
printf("\n");
}
Program 16: Sort Given Array Using Bubble Sort Technique
#include<stdio.h>
void main()
int a[20],i,j,n,temp;
clrscr();
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/*SWAPING PART*/
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Program 17: Sort Given Array Using Quick Sort Technique
#include<stdio.h>
int i,j,temp,key;
key=a[low];
i=low+1;
j=high;
while(1)
while(key<a[j]) j--;
if(i<j)
temp=a[i];
a[i]=a[j];
a[j]=temp;
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
int j;
if(low<high)
j=partition(a,low,high);
quicksort(a,low,j-1);
quicksort(a,j+1,high);
void main()
int i,n,a[20];
scanf("%d",&n);
printf("Enter the numbers to be sorted\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n-1);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Program 18: Sort Given Array Using Selection Sort Technique
#include <stdio.h>
pos = i;
pos = j;
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
void main()
{
int a[10], n, i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionSort(a, n);
printf("%d\n", a[i]);
printf("\n");
}
Program 19: Implement Singly Linked List
#include<stdio.h>
#include<process.h>
#include<alloc.h>
struct node
int info;
};
NODE getnode()
NODE x;
if(x==NULL)
printf("Out of Memory\n");
exit(0);
return x;
}
void freenode(NODE x)
free(x);
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
NODE temp;
if(first==NULL)
temp=first;
while(temp!=NULL)
printf("%d ",temp->info);
temp=temp->link;
printf("\n");
void main()
NODE first=NULL;
int ch,item;
do
printf("______\n");
printf("1)Insert\n2)Display\n3)Exit\n");
printf("______\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2: display(first);
break;
default : exit(0);
}while(1);
}
Program 20: Implement Double Linked List
#include<stdio.h>
#include<process.h>
#include<stdlib.h>
struct node
int info;
};
NODE getnode()
NODE x;
if(x==NULL)
printf("Out of memory\n");
exit(0);
}
return x;
void freenode(NODE x)
free(x);
temp = getnode();
temp->info = item;
cur = head->rlink;
head->rlink = temp;
temp->llink = head;
temp->rlink = cur;
cur->llink = temp;
temp = getnode();
temp->info = item;
cur = head->llink;
head->llink = temp;
temp->rlink = head;
temp->llink = cur;
cur->rlink = temp;
return head;
}
NODE delete_front(NODE head)
if(head->rlink == head)
printf("List is empty\n");
return head;
cur = head->rlink;
next = cur->rlink;
head->rlink = next;
next->llink = head;
freenode( cur);
return head;
{
NODE cur,prev;
if ( head->llink == head)
printf("List is empty\n");
return head;
cur = head->llink;
prev = cur->llink;
head->llink = prev;
prev->rlink = head;
freenode(cur);
return head;
NODE temp;
if ( head->rlink == head)
printf("Deque is empty\n");
return;
temp = head->rlink;
printf("%d ",temp->info );
temp = temp->rlink;
printf("\n");
void main()
NODE head;
head = getnode();
head->rlink = head;
head->llink = head;
for (;;)
printf("\n*************\n");
printf("5:Display\n6:Exit\n");
printf("\n*************\n");
scanf("%d",&choice);
switch( choice)
scanf("%d",&item);
head = insert_front(item,head);
break;
scanf("%d",&item);
head = insert_rear(item,head);
break;
break;
break;
case 5: display(head);
break;
default:printf("Exit");
exit(0);