Data Structure File 1
Data Structure File 1
INSTITUTIONS
#include<stdio.h>
#include<conio.h>
void main()
int a[10][10],i,j,r,C=0,n=3,A,b,c,d,e,f,g,h,I;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
A=a[0][0];
b=a[0][1];
c=a[0][2];
d=a[1][0];
e=a[1][1];
f=a[1][2];
g=a[2][0];
h=a[2][1];
I=a[2][2];
C=(A*e*I)-(A*f*h)-(b*d*I)+(b*f*g)+(c*d*h)-(c*e*g);
}
}
#include<conio.h>
void main()
int a[10],n,i,temp[10];
scanf("%d",&n);
for(i=0;i<n;i++)
temp[i]=a[i];
for(i=0;i<n;i++)
printf("%d\t ",a[i]);
printf("\n");
for(i=0;i<n;i++)
a[i]=temp[(n-1)-i];
printf("%d\t",a[i]);
}
Q3. WAP to find the sum of row & column and show them in matrix.
#CODE
#include<stdio.h>
#include<conio.h>
void main()
int a[10][10],i,j,r=0,c=0,n,k,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
for(i=0;i<n;i++)
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
r+=a[i][j];
printf("%d",r);
r=0;
printf("\n\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
c+=a[j][i];
printf("%d\t",c);
c=0;
}
Q4. WAP to arrange an array in ascending order using sorting.
#CODE
#include <stdio.h>
void main()
int i, j, a, n, number[30];
scanf("%d", &n);
scanf("%d", &number[i]);
a = number[i];
number[i] = number[j];
number[j] = a;
printf("%d\n", number[i]);
#include<conio.h>
#include<stdlib.h>
#define MAX 5
typedef struct
int sp;
int a[MAX];
}Stack;
if(s->sp==-1)
return(TRUE);
else
return(FALSE);
if(s->sp==MAX-1)
return(TRUE);
else
return(FALSE);
int item;
if(IsFull(s))
printf("\n OVERFLOW\n");
else
scanf("%d",&item);
s->sp++;
s->a[s->sp]=item;
}
if(IsEmpty(s))
else
s->sp--;
if(IsEmpty(s))
printf("\n EMPTY\n");
else
int i;
if(IsEmpty(s))
for(i=0;i<=s->sp;i++)
printf("%d\t ",s->a[i]);
void main()
int choice;
Stack s;
while(1)
scanf("%d",&choice);
switch(choice)
case 1:PUSH(&s);
break;
case 2:POP(&s);
break;
case 3:PEEK(&s);
break;
case 4:DISPLAY(&s);
break;
case 5:exit(1);
}
Q6. WAP to show all the operations of multi stack in single array.
#CODE
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
# define max 10
typedef struct
int a[max];
int sp1,sp2;
}stack;
typedef enum{FALSE,TRUE}Boolean;
s->sp1=-1;
s->sp2=max;
if(s->sp1==-1)
return(TRUE);
else
return(FALSE);
if(s->sp2==max)
return(TRUE);
else
return(FALSE);
if((s->sp1+1)==s->sp2)
return(TRUE);
else
return(FALSE);
int item;
if(isfull(s))
else
scanf("%d",&item);
s->sp1++;
s->a[s->sp1]=item;
int item;
if(isfull(s))
else
scanf("%d",&item);
s->sp2--;
s->a[s->sp2]=item;
if(isempty1(s))
printf("\n UNDERFLOW\n");
else
s->sp1--;
}
}
if(isempty2(s))
printf("\n UNDERFLOW\n");
else
s->sp2++;
if(isempty1(s))
else
if(isempty2(s))
else
{
printf("\n Peeked item is = %d",s->a[s->sp2]);
int i;
for(i=0;i<=s->sp1;i++)
printf(" %d ",s->a[i]);
for(i=s->sp1+1;i<=s->sp2-1;i++)
printf(" _ ");
for(i=s->sp2;i<max;i++)
printf(" %d ",s->a[i]);
void main()
int choice;
stack s;
createemptystack(&s);
while(1)
scanf("%d",&choice);
switch(choice)
case 1: PUSH1(&s);
break;
case 2: PUSH2(&s);
break;
case 3: POP1(&s);
break;
case 4: POP2(&s);
break;
case 5: PEEK1(&s);
break;
case 6: PEEK2(&s);
break;
case 7: DISPLAY(&s);
break;
case 8: exit(1);
break;
}
}
}
Q7. WAP to show all the operations of linear queue.
#CODE
#include<stdio.h>
#include<stdlib.h>
#define MAX 6
typedef struct
int F, R;
int a[MAX];
}Queue;
typedef enum{FALSE,TRUE}Boolean;
lq->F=-1;
lq->R=-1;
if(lq->F == -1)
return(TRUE);
else
return(FALSE);
}
Boolean IsFull(Queue *lq)
if((lq->F==0)&&(lq->R==MAX-1))
return(TRUE);
else
return(FALSE);
int item,i;
if(IsFull(lq))
printf("\n Overflow");
else
lq->R=lq->F=0;
else if(lq->F>0 && lq->R==MAX-1) //when vacant cells available in left side, do the left shift
for(i= lq->F;i<=lq->R;i++)
lq->a[i-lq->F]=lq->a[i];
lq->R=lq->R-lq->F;
lq->F=0;
else
lq->R++;
scanf("%d",&item);
lq->a[lq->R]=item;
int temp;
if(IsEmpty(lq))
printf("\n UnderFlow");
else
temp=lq->a[lq->F];
if(lq->F == lq->R)
lq->R=lq->F=-1;
else
lq->F++;
int temp;
if(IsEmpty(lq))
printf("\n UnderFlow");
else
temp=lq->a[lq->F];
int i;
if(IsEmpty(lq))
printf("\n Underflow");
else
for(i=lq->F;i<=lq->R;i++)
printf("\t %d",lq->a[i]);
void main()
Queue lq;
int choice;
CreateEmptyQueue(&lq);
while(1)
scanf("%d",&choice);
switch(choice)
case 1: Enqueue(&lq);
break;
case 2: Dequeue(&lq);
break;
case 3:Peek(&lq);
break;
case 4:Display(&lq);
break;
case 5:exit(1);
break;
}
Q8.WAP to convert infix expression to postfix expression.
#CODE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Stack
int top;
unsigned capacity;
int* array;
};
malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
return stack;
return stack->top == -1 ;
return stack->array[stack->top];
if (!isEmpty(stack))
return stack->array[stack->top--] ;
return '$';
stack->array[++stack->top] = op;
switch (ch)
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
return -1;
int i, k;
if(!stack)
return -1 ;
if (isOperand(exp[i]))
exp[++k] = exp[i];
push(stack, exp[i]);
exp[++k] = pop(stack);
return -1;
else
pop(stack);
}
else
exp[++k] = pop(stack);
push(stack, exp[i]);
while (!isEmpty(stack))
exp[++k] = pop(stack );
exp[++k] = '\0';
int main()
char exp[20] ;
gets(exp);
infixToPostfix(exp);
return 0;
}
Q9. WAP to show operation of circular queue.
#CODE
#include <stdio.h>
# define max 6
int queue[max];
int front=-1;
int rear=-1;
front=0;
rear=0;
queue[rear]=element;
else if((rear+1)%max==front)
printf("Queue is overflow..");
else
rear=(rear+1)%max;
queue[rear]=element;
}
int dequeue()
printf("\nQueue is underflow..");
else if(front==rear)
front=-1;
rear=-1;
else
front=(front+1)%max;
void display()
int i=front;
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
printf("%d,", queue[i]);
i=(i+1)%max;
int main()
int choice=1,x;
scanf("%d", &choice);
switch(choice)
case 1:
scanf("%d", &x);
enqueue(x);
break;
case 2:
dequeue();
break;
case 3:
display();
}}
return 0;
}
Q10. WAP to show operation of linked list.
#CODE
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
while(choice != 9)
printf("\n\n*********Main Menu*********\n");
printf("\n===============================================\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete from
Beginning\n5.Delete from last\n6.Delete node after specified location\n7.Search for an
element\n8.Show\n9.Exit\n");
scanf("\n%d",&choice);
switch(choice)
case 1:
beginsert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
void beginsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}
}
void lastinsert()
int item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
head = ptr;
printf("\nNode inserted");
else
temp = head;
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");
void randominsert()
int i,loc,item;
if(ptr == NULL)
printf("\nOVERFLOW");
else
scanf("%d",&item);
ptr->data = item;
scanf("\n%d",&loc);
temp=head;
for(i=0;i<loc;i++)
temp = temp->next;
if(temp == NULL)
printf("\ncan't insert\n");
return;
printf("\nNode inserted");
void begin_delete()
if(head == NULL)
printf("\nList is empty\n");
else
ptr = head;
head = ptr->next;
free(ptr);
void last_delete()
if(head == NULL)
{
printf("\nlist is empty");
head = NULL;
free(head);
else
ptr = head;
while(ptr->next != NULL)
ptr1 = ptr;
ptr1->next = NULL;
free(ptr);
void random_delete()
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=0;i<loc;i++)
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
printf("\nCan't delete");
return;
free(ptr);
void search()
int item,i=0,flag;
ptr = head;
if(ptr == NULL)
printf("\nEmpty List\n");
else
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
flag=0;
else
flag=1;
i++;
if(flag==1)
void display()
ptr = head;
if(ptr == NULL)
printf("Nothing to print");
}
else
while (ptr!=NULL)
printf("\n%d",ptr->data);