Professional Documents
Culture Documents
DS Practicals 1-10
DS Practicals 1-10
#include<stdio.h>
int main()
{
int a[10],n,i;
printf("Enter the size of the array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nArray element in proper order : ");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\nArray element in reverse order : ");
for(i=n-1 ;i>=0; i--)
{
printf("%d",a[i]);
}
return 0;
Output
12002040701170 Page 1
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
1(b): To insert and delete a particular element from given position in the
array.
#include<stdio.h>
int a[20],i,n,pos,val;
void insertinarray(int position,int value)
{
i=n;
while(i>=position)
{
a[i+1]=a[i];
i--;
}
n++;
a[pos]=value;
}
void deleteinarray(int positon,int n)
{
i=positon;
while(i<=n-1)
{
a[i]=a[i+1];
i++;
}
n--;
}
int main()
{
int c;
printf("how much element you want to enter : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("elments are\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
do
{
printf("enter the case : ");
scanf("%d",&c);
switch (c)
{
12002040701170 Page 2
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
case 1:
printf("enter the position : ");
scanf("%d",&pos);
printf("enter the value : ");
scanf("%d",&val);
insertinarray(pos,val);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
break;
case 2:
printf("enter the position : ");
scanf("%d",&pos);
deleteinarray(pos,val);
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
printf("\n");
break;
case 3:
printf("exit");
}
}while(c!=3);
return 0;
Output
12002040701170 Page 3
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
1(c): To search a particular element from the list and print its position in
the array.
#include<stdio.h>
int main()
{
int a[10];
int i,n,element;
printf("Enter the size of the array : ");
scanf("%d",&n);
printf("\nEnter the element you want to search\n");
for (int i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the element you want to search : ");
scanf("%d",&element);
for (i = 0; i < n; i++)
{
if(element==a[i])
{
printf("\nThe position of the element is : %d ",i+1);
break;
}
if(i==n-1)
{
printf("\n Element not found ");
}
}
return 0;
}
12002040701170 Page 4
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Output
12002040701170 Page 5
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
1(d): To merge two sorted arrays and sort the merged array
#include<stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,j,k;
int m,n,q;
q=m+n;
printf("Enter the size of array 1 : ");
scanf("%d",&n);
printf("\nEnter the size of array 2 : ");
scanf("%d",&m);
printf("\nElements of array 1 \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements of array 2 \n");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
// Merged the Sorted array
i=0;
j=0;
k=0;
while (i<n && j<m)
{
if (b[j]>a[i])
{
c[k]=a[i];
i++,k++;
}
else
{
c[k]=b[j];
k++,j++;
}
}
while (i < 3)
{
c[k] = a[i];
k++,i++;
}
while (j < 2)
12002040701170 Page 6
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
{
c[k] = b[j];
k++,j++;
}
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);
}
return 0;
}
// To merge two sorted arrays and sort the merged array. (part 2)
#include<stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,j,k=0;
int m,n,q,p,temp;
q=m+n;
printf("Enter the size of array 1 : ");
scanf("%d",&n);
printf("\nEnter the size of array 2 : ");
scanf("%d",&m);
printf("\nElements of array 1 \n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nElements of array 2 \n");
for(j=0;j<m;j++)
{
scanf("%d",&b[j]);
}
for(i=0;i<n;i++)
{
c[k]=a[i];
k++;
}
k=n;
for(j=0;j<m;j++)
{
c[k]=b[j];
k++;
}
printf("\nMerged array is : ");
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);
12002040701170 Page 7
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
}
printf("\nSorted merged array is : ");
for(k=0;k<m+n-1;k++)
{
for(p=k+1;p<m+n;p++)
{
if(c[p]<c[k])
{
temp = c[k];
c[k]=c[p];
c[p]=temp;
}
}
}
for(k=0;k<m+n;k++)
{
printf("%d",c[k]);
}
}
Output
12002040701170 Page 8
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 9
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
}
}
int peek (int stack[])
{
if(top==-1)
{
printf("\nEmpty Stack\n");
}
else
{
return (stack[top]);
}
}
int main()
{
int c;
do
{
printf("Enter the case : ");
scanf("%d",&c);
switch (c)
{
case 1:
printf("Enter the value : ");
scanf("%d",&value);
push(stack,value);
break;
case 2:
value=pop(stack);
if (value!=-1)
{
printf("\nthe value deleted from stack is : %d ",value);
}
break;
case 3: value=peek(stack);
if (value!=-1)
{
printf("\nthe value deleted from stack is : %d ",value);
}
case 4:
display(stack);
}
} while (c!=5);
return 0;
}
12002040701170 Page 10
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Output
12002040701170 Page 11
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
char stack[SIZE];
int top = -1;
char pop()
{
char item ;
if(top <0)
{
printf("stack under flow: invalid infix expression");
getchar();
/* underflow may occur for invalid expression */
/* where ( and ) are not matched */
exit(1);
}
else
{
item = stack[top];
top = top-1;
return(item);
}
12002040701170 Page 12
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
push('(');
strcat(infix_exp,")");
i=0;
j=0;
item=infix_exp[i];
while(item != '\0')
{
if(item == '(')
12002040701170 Page 13
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
{
push(item);
}
else if( isdigit(item) || isalpha(item))
{
postfix_exp[j] = item;
j++;
}
else if(is_operator(item) == 1)
{
x=pop();
while(is_operator(x) == 1 && precedence(x)>= precedence(item))
{
postfix_exp[j] = x;
j++;
x = pop();
}
push(x);
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j] = x;
j++;
x = pop();
}
}
else
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
i++;
item = infix_exp[i];
}
if(top>0)
{
printf("\nInvalid infix Expression.\n");
getchar();
exit(1);
}
if(top>0)
{
12002040701170 Page 14
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
postfix_exp[j] = '\0';
int main()
{
char infix[SIZE], postfix[SIZE];
printf("ASSUMPTION: The infix expression contains single letter variables and single digit
constants only.\n");
printf("\nEnter Infix expression : ");
gets(infix);
InfixToPostfix(infix,postfix);
printf("Postfix Expression: ");
puts(postfix);
return 0;
}
Output
12002040701170 Page 15
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
#include<stdio.h>
int stack[20];
int top = -1;
void push(int x)
{
stack[++top] = x;
}
int pop()
{
return stack[top--];
}
int main()
{
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))
{
12002040701170 Page 16
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
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;
}
}
12002040701170 Page 17
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
OUTPUT
12002040701170 Page 18
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
#include<stdio.h>
#define n 5
int main()
{
int queue[n],ch=1,front=0,rear=0,i,j=1,x=n;
printf("Queue using Array");
printf("\n1.Insertion \n2.Deletion \n3.Display \n4.Exit");
while(ch)
{
printf("\nEnter the Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(rear==x)
printf("\n Queue is Full");
else
{
printf("\n Enter no %d:",j++);
scanf("%d",&queue[rear++]);
}
break;
case 2:
12002040701170 Page 19
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
if(front==rear)
{
printf("\n Queue is empty");
}
else
{
printf("\n Deleted Element is %d",queue[front++]);
x++;
}
break;
case 3:
printf("\nQueue Elements are:\n ");
if(front==rear)
printf("\n Queue is Empty");
else
{
for(i=front; i<rear; i++)
{
printf("%d",queue[i]);
printf("\n");
}
break;
case 4:
exit(0);
default:
printf("Wrong Choice: please see the options");
}
}
}
return 0;}
12002040701170 Page 20
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
OUTPUT
12002040701170 Page 21
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
#include<stdlib.h>
#include<stdio.h>
#define max 5
void insert();
int delete();
void display();
int main()
{
int w,no;
for(;;)
{
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Display");
printf("\n4. EXIT");
printf("\nEnter what you want :");
scanf("%d",&w);
switch(w)
12002040701170 Page 22
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
{
case 1:
insert();
break;
case 2:
no=delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("\nInvalid Choice !!\n");
}
}
}
void insert()
{
int no;
if((front ==0 && rear == max-1) || front == rear+1)
{
printf("\nCircular Queue Is Full !\n");
return;
}
printf("\nEnter a number to Insert :");
scanf("%d",&no);
if(front==-1)
front=front+1;
12002040701170 Page 23
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
if(rear==max-1)
rear=0;
else rear=rear+1;
CQueue[rear]=no;
}
int delete()
{
int e;
if(front==-1)
{
printf("\nThe Circular Queue is Empty !!\n");
}
e=CQueue[front];
if(front==max-1)
front=0;
else if(front==rear)
{
front=-1;
rear=-1;
}
else front=front+1;
printf("\n%d was deleted !\n",e);
return e;
}
void display()
{
int i;
12002040701170 Page 24
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
if(front==-1)
{
printf("\nThe Circular Queue is Empty ! Nothing To Display !!\n");
return;
}
i=front;
if(front<=rear)
{
printf("\n\n");
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
else
{
printf("\n\n");
while(i<=max-1)
printf("%d ",CQueue[i++]) ;
i=0;
while(i<=rear)
printf("%d ",CQueue[i++]);
printf("\n");
}
}
12002040701170 Page 25
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
OUTPUT
12002040701170 Page 26
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 28
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
void deleteEnd()
{
struct node *temp, *prevnode;
if (start == NULL)
printf("\nList is Empty\n");
else
{
temp = start;
while (temp->link != 0)
{
prevnode = temp;
temp = temp->link;
}
free(temp);
prevnode->link = 0;
}
}
void deletePosition()
{
struct node *temp, *position;
int i = 1, pos;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter index : ");
scanf("%d", &pos);
position = malloc(sizeof(struct node));
temp = start;
while (i < pos - 1)
{
temp = temp->link;
i++;
}
position = temp->link;
temp->link = position->link;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\t1 To see data\n");
printf("\t2 For insertion at starting\n");
printf("\t3 For insertion at end\n");
printf("\t4 To sort\n");
printf("\t5 For deletion of first element\n");
printf("\t6 For deletion of last element\n");
12002040701170 Page 29
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 30
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Output:-
12002040701170 Page 31
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 32
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 33
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
int data;
struct node *temp, *trav;
temp = (struct node*)malloc(sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\nEnter number to be inserted: ");
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
if (start == NULL)
{
start = temp;
}
else
{
while (trav->next != NULL)
trav = trav->next;
temp->prev = trav;
trav->next = temp;
}
}
void deleteEnd()
{
struct node* temp;
if (start == NULL)
printf("\nList is empty\n");
temp = start;
while (temp->next != NULL)
temp = temp->next;
if (start->next == NULL)
start = NULL;
else
{
temp->prev->next = NULL;
free(temp);
}
}
void deletePosition()
{
int pos, i = 1;
struct node *temp, *position;
temp = start;
if (start == NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter position : ");
scanf("%d", &pos);
12002040701170 Page 34
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
if (pos == 1)
{
deleteEnd();
if (start != NULL)
{
start->prev = NULL;
}
free(position);
return;
}
while (i < pos - 1)
{
temp = temp->next;
i++;
}
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
free(position);
}
}
int main()
{
int choice;
while (1)
{
printf("\n\t1 To see list\n");
printf("\t2 For insertion at starting\n");
printf("\t3 For insertion at end\n");
printf("\t4 For deletion of last element\n");
printf("\t5 For deletion of element at any position\n");
printf("\t6 To exit\n");
printf("\nEnter Choice :\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;;
case 4:
deleteEnd();
break;
12002040701170 Page 35
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
case 5:
deletePosition();
break;
case 6:
exit(1);
break;
default:
printf("Incorrect Choice.\n");
continue;
}
}
return 0;
}
12002040701170 Page 36
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Output
12002040701170 Page 37
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 38
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
12002040701170 Page 39
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Practical no. 9: Write a program to implement linear search and the binary
search.
#include<stdio.h>
void main()
{
int a[25],beg,item,last,n,num,i,ch,mid,f=0;
printf("menu\n");
printf("\n 1.linear search");
printf("\n 2.binary search");
printf("\n enter the choice");
scanf("%d",&ch);
if(ch==1)
{
printf("\n enter the number of elements in the array");
scanf("%d",&n);
printf("\n enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the item to be searched");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(a[i]==item)
{
printf("\n item found at position%d",i+1);
break;
}
}
12002040701170 Page 40
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
if(i==n)
printf("\n item not found");
}
if(ch==2)
{
printf("\nenter the number of elements in the array");
scanf("%d",&n);
printf("enter the sorted array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("item to be searched");
scanf("%d",&item);
last=n-1;
mid=(beg+last)/2;
while(beg<=last)
{
if(item==a[mid])
{
printf("\n item found at position %d",mid+1);
break;
}
else if(a[mid]>item)
last=mid-1;
else beg=mid+1;
mid=(beg+last)/2;
}
}
}
12002040701170 Page 41
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Output
12002040701170 Page 42
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
Practical no. 10: Write a program to implement the Bubble Sort and
selection sort.
#include<stdio.h>
#include<stdlib.h>
void display(int a[],int n);
void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);
//-----------------Main Function----------------------
int main()
{
int n,choice,i;
char ch[20];
for(i=0;i<n;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&arr[i]);
}
while(1)
12002040701170 Page 43
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
{
printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Display Array.\n5. Exit
the Program.\n");
printf("\nEnter your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
case 3:
display(arr,n);
break;
case 4:
return 0;
default:
printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}
12002040701170 Page 44
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
//-------------------Display Function-----------------
for(int i=0;i<n;i++)
{
printf(" %d ",arr[i]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
12002040701170 Page 45
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
}
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
12002040701170 Page 46
Udaypratapsingh Rajput Data Structure [102040301] Division: 3CE3
OUTPUT
12002040701170 Page 47