DS Programs
DS Programs
Output
Output
Found at location:14
******************************************************************************
Output
4. Append 2 Arrays
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int arr1[50],arr2[50],size1,size2,i,k,merge[100];
printf(“\nEnter array 1 size:”);
scanf(“%d”,&size1);
printf(“\nEnter array 1 elements:”);
for(i=0;i<size1;i++)
{
scanf(“%d”,&arr1[i]);
merge[i]=arr1[i];
}
k=i;
printf(“\nEnter array 2 size:”);
scanf(“%d”,&size2);
printf(“\nEnter array 2 elements:”);
for(i=0;i<size2;i++)
{
scanf(“%d”,&arr2[i]);
merge[k]=arr2[i];
k++;
}
printf(“\nThe new array after merging is :\n”);
for(i=0;i<k;i++)
printf(“%d”,merge[i]);
getch();
return 0;
}
Output
Output
Enter 4 elements:
5
6
7
8
7 found at location: 3
******************************************************************************
6. Read a sparse matrix and display its triplet representation using array
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][3],m,n,i,j,s=0;
clrscr();
printf(“\nEnter the order of mxn of the sparse matrix: “);
scanf(“%d%d”,&m,&n);
printf(“\nEnter the elements in the sparse matrix(mostly zeros): “);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“\n%d rows and %d columns: “,i,j);
scanf(“%d”,&A[i][j]);
}
}
printf(“\nThe given matrix is: \n“);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(“%5d”,A[i][j]);
}
printf(“\n”);
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
If(A[i][j]!=0)
{
B[s][0]= A[i][j]);
B[s][1]=i;
B[s][2]=j;
s++;
}
}
}
printf(“\nThe sparse matrix is given by: \n“);
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf(“%5d”,B[i][j]);
}
printf(“\n”);
}
getch();
}
Output
Output:
List Operations:
1.Insert a New Node.
2.Display.
3.Exit.
Choose an option: 1
Enter value to be inserted:7
List Operations:
1.Insert a New Node.
2.Display.
3.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
******************************************************************************
Output:
List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
8
9
List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 3
Enter number to delete: 8
8 deleted successfully!
List Operations:
1.Insert a New Node.
2.Display.
3.Delete a node.
4.Exit.
Choose an option: 2
Nodes of Singly Linked List:
7
9
******************************************************************************
9. Creating a doubly linked list of integers and display in forward and backward direction
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *head,*tail=NULL;
void addNode(int data)
{
struct node *newNode=(struct node*)malloc(sizeof(struct node));
newNode->data=data;
if(head==NULL)
{
head=tail=newNode;
head->prev=NULL;
tail->next=NULL;
}
else
{
tail->next=newNode;
newNode->prev=tail;
tail=newNode;
tail->next=NULL;
}
}
void display()
{
struct node *current=head;
if(head==NULL)
{
printf(“\nList is empty!”);
return;
}
printf(“\nNodes of doubly linked list :\n”);
while(current!=NULL)
{
printf(“%d”,current->data);
current=current->next;
printf(“\t”);
}
}
void reverse()
{
struct node *current=head,*temp;
while(current!=NULL)
{
temp=current->next;
current->next=current->prev;
current->prev=temp;
current=current->prev;
}
temp=head;
head=tail;
tail=temp;
display();
}
int main()
{
int i,num;
while(1)
{
printf(“\nList Operations”);
printf(“\n1.Insert a node.”);
printf(“\n2Display.”);
printf(“\n3.Display in reverse order.”);
printf(“\n4.Exit.”);
printf(“\n\nEnter your choice:”);
scanf(“%d”,&i);
if(i<=0) printf(“\nInvalid Option”);
else switch(i)
{
case 1: {
printf(“\nEnter the value to be inserted:”);
scanf(“%d”,&num);
addNode(num);
break;
}
case 2:display();break;
case 3:printf(“\nIn reverse order”);reverse();break;
case 4:return 0;
default: printf(“\nInvalid option !”);
}
}
}
Output:
List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:1
List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:1
List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:2
List Operations:
1.Insert a node.
2.Display.
3.Display in reverse order.
4.Exit.
Enter your choice:3
***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1
***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1
***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:1
***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:3
***MENU**
1.Push.
2.Pop.
3.Display.
4.Exit.
Enter your choice:2
Deleted 8
******************************************************************************
Output
Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1
Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1
Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:1
Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:3
Choose an Option
1.Push.
2.Pop.
3.Show.
4.Exit.
Enter your choice:2
Item Popped.
******************************************************************************
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
clrscr();
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :");
scanf("%s",&exp);
e=exp;
while(*e!='\0')
{
if(isdigit(*e))
{
num = *e - 48;
push(num);
}
else
{
n1 = pop();
n2 = pop();
switch(*e)
{
case '+' :n3 = n1 + n2;break;
case '-' :n3 = n2 - n1; break;
case '*' :n3 = n1 * n2;break;
case '/' :n3 = n2 / n1;break;
case '^' :n3 = n2 ^ n1; break;
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
getch();
return 0;
}
Output
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define n 5
void main()
{
int queue[n],ch=1,front=0,rear=0,i,j=0,x=n;
printf(“\nQueue using Array”);
printf(“\n1.Insertion.\n2.Deletion.\n3.Display.\n4.Exit.”);
while(ch)
{
printf(“\nEnter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:{
if(rear==x) printf(“\nQueue is full.”);
else
{
printf(“\nEnter no %d”,j++);
scanf(“%d”,&queue[rear++]);
}
break;
}
case 2:
{
if(front==rear) printf(“\nQueue is empty.”);
else
{
printf(“\nDeleted elements is %d”,queue[front++]);
x++;
}
break;
}
case 3:
{
printf(“\nQueue elements are:\n”);
if(front==rear) printf(“\nQueue is empty.”);
else
{
for(i=front;i<rear;i++)
{
printf(“%d”,queue[i]);
printf(“\n”);
}
}
break;
}
case 4:exit(0);
default: printf(“\nWrong Choice.Please see the options.”);
}
}
getch();
}
Output
Enter no 1:10
Enter no 2:54
Enter no 4:234
Deleted elements is 10
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
}*rear, *front;
void dequeue()
{
struct node *var=front;
if(front==NULL)
printf("\nQueue is empty");
else
{
rear->next;
front=front->next;
free(var);
}
}
void enqueue(int value)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
if(rear==NULL)
{
rear=temp;
rear->next=NULL;
front=rear;
}
else
{
rear->next=temp;
rear=temp;
rear->next=NULL;
}
}
void display()
{
struct node *var=front;
if(var!=NULL)
{
printf("\nElements are:");
while(var!=NULL)
{
printf("\t%d",var->data);
var=var->next;
}
printf("\n");
}
else printf("\nQueue is empty.");
}
void main()
{
int i=0;
clrscr();
front=NULL;
while(1)
{
printf("\n1.Enqueue.\n2.Dequeue. \n3.Display.\n4.Exit.");
printf("\nChoose an option:");
scanf("%d", &i);
switch(i)
{
case 1:{
int value;
printf("\nEnter a value to enqueue:");
scanf("%d", &value);
enqueue(value);
display();
break;
}
case 2:dequeue();display();break;
case 3:display();break;
case 4:exit(0);
default:printf("\nInvalid Option!");
}
}
}
Output
1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1
1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1
1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:1
1.Enqueue.
2.Dequeue.
3.Display.
4.Exit.
Choose an option:2
Elements are: 6 7
******************************************************************************
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode *l,*r;
};
struct tnode *root=NULL;
struct tnode *create(int data)
{
struct tnode *newnode;
newnode=(struct tnode*)malloc(sizeof(struct tnode));
newnode->data=data;
newnode->l=NULL;
newnode->r=NULL;
return(newnode);
}
Output
1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:1
Enter your data:9
1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:4
Inorder Traversal
479
1.Insertion.
2.Deletion.
3.Search.
4.Traversal.
5.Exit
Enter your choice:3
Data found 7.
******************************************************************************
16. Implement Quick sort
#include<stdio.h>
#include<conio.h>
void quicksort(int number[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(number[i]<=number[pivot]&&i<last) i++;
while(number[j]>number[pivot]) j--;
if(i<j)
{
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
void main()
{
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
getch();
}
Output:
How many elements are u going to enter?: 5
Enter 5 elements: 88 0 -9 98 27
#include<stdio.h>
#include<conio.h>
void main()
{
int array[100],n,c,d,position,swap;
printf("\n Enter no of elements:");
scanf("%d",&n);
printf("\n Enter %d integers: \n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
for(c=0;c<(n-1);c++)
{
position=c;
for(d=c+1;d<n;d++)
{
if(array[position]>array[d])
position=d;
}
if(position!=c)
{
swap=array[c];
array[c]=array[position];
array[position]=swap;
}
}
printf("\n Sorted list in ascending order:\n");
for(c=0;c<n;c++)
printf("%d\n",array[c];
getch();
}
Output:
Enter no of elements:4
Enter 4 integers:
4
2
7
1
**************************************************************************************
Output:
Enter no of elements:
5
Enter 5 integers:
4
3
-1
2
1