0% found this document useful (0 votes)
15 views

Data Structure File 1

The document contains 10 programming questions and their solutions involving data structures and algorithms. It includes programs to find the determinant of a 2D array, inverse of a 1D array, sorting an array, and operations on stacks, queues, and linked lists. The programs demonstrate various operations and uses of common data structures.

Uploaded by

Abhiuday Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Data Structure File 1

The document contains 10 programming questions and their solutions involving data structures and algorithms. It includes programs to find the determinant of a 2D array, inverse of a 1D array, sorting an array, and operations on stacks, queues, and linked lists. The programs demonstrate various operations and uses of common data structures.

Uploaded by

Abhiuday Rajput
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

DEVBHOOMI GROUP OF

INSTITUTIONS

ACADEMIC YEAR : 2020-2021


PRACTICAL FILE :
Data Structures and File Organisation

SUBMITTED TO- SUBMITTED BY-


SIR DIGVIJAY SINGH HARSHIT AGARWAL
20BCA0001
INDEX
S.NO. DESCRIPTION REMARKS
1. Program to find the determinant of an 2D
array.
2. Program WAP to print the inverse of 1D array.

3. Program to find the sum of row & column and


show them in matrix.

4. Program to arrange an array in ascending


order using sorting.
5. Program to show all the operations of stack.

6. Program to show all the operations of multi


stack in single array.

7. Program to show all the operations of linear


queue.

8. Program to convert infix expression to postfix


expression.
9. Program to show operation of circular queue.

10. Program to show operation of linked list.


Q1. WAP to find the determinant of an 2D array.
#CODE:-

#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++)

printf("\n\n Enter element in row no %d",i);

for(j=0;j<n;j++)

printf("\n Enter a[%d][%d]= ",i,j);

scanf("%d",&a[i][j]);

printf("\n You entered =");

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);

printf("\n\n Determinant = %d",C);

}
}

Q2. WAP to print the inverse of 1D array.


#CODE
#include<stdio.h>

#include<conio.h>

void main()

int a[10],n,i,temp[10];

printf("\n Enter no of elements= ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter a[%d]= ",i);


scanf("%d",&a[i]);

temp[i]=a[i];

printf("\n\n You Entered:- ");

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;

printf("\n Enter the size of matrix= ");

scanf("%d",&n);

for(i=0;i<n;i++)

printf("\n Enter elements of row no %d",i);

for(j=0;j<n;j++)

printf("\n Enter element in a[%d][%d]= ",i,j);

scanf("%d",&a[i][j]);

printf("\n You entered below matrix= ");

for(i=0;i<n;i++)

{
printf("\n");

for(j=0;j<n;j++)

printf("%d\t",a[i][j]);

printf("\n Sum of rows & columns= ");

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];

printf("Enter the value of N \n");

scanf("%d", &n);

printf("Enter the numbers \n");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)

for (j = i + 1; j < n; ++j)

if (number[i] > number[j])

a = number[i];

number[i] = number[j];

number[j] = a;

printf("The numbers arranged in ascending order are given below \n");


for (i = 0; i < n; ++i)

printf("%d\n", number[i]);

Q5. WAP to show all the operations of stack.


#CODE
#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5

typedef struct

int sp;

int a[MAX];

}Stack;

typedef enum {FALSE,TRUE}Boolean;


Boolean IsEmpty(Stack *s)

if(s->sp==-1)

return(TRUE);

else

return(FALSE);

Boolean IsFull(Stack *s)

if(s->sp==MAX-1)

return(TRUE);

else

return(FALSE);

void PUSH(Stack *s)

int item;

if(IsFull(s))

printf("\n OVERFLOW\n");

else

printf("\n Enter item for PUSH= ");

scanf("%d",&item);

s->sp++;

s->a[s->sp]=item;
}

void POP(Stack *s)

if(IsEmpty(s))

printf("\n UNDERFLOW \n");

else

printf("\n Popped item is= %d",s->a[s->sp]);

s->sp--;

void PEEK(Stack *s)

if(IsEmpty(s))

printf("\n EMPTY\n");

else

printf("\n Peeked element is= %d",s->a[0]);

void DISPLAY(Stack *s)

int i;

if(IsEmpty(s))

printf("\n EMPTY \n");


else

for(i=0;i<=s->sp;i++)

printf("%d\t ",s->a[i]);

void main()

int choice;

Stack s;

s.sp=-1; // for creating empty stack

while(1)

printf("\n Enter 1 for PUSH ");

printf("\n Enter 2 for POP");

printf("\n Enter 3 for PEEK");

printf("\n Enter 4 for DISPLAY");

printf("\n Enter 5 for EXIT");

printf("\n Enter choice = ");

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;

void createemptystack(stack *s)

s->sp1=-1;

s->sp2=max;

Boolean isempty1(stack *s)

if(s->sp1==-1)

return(TRUE);

else
return(FALSE);

Boolean isempty2(stack *s)

if(s->sp2==max)

return(TRUE);

else

return(FALSE);

Boolean isfull(stack *s)

if((s->sp1+1)==s->sp2)

return(TRUE);

else

return(FALSE);

void PUSH1(stack *s)

int item;

if(isfull(s))

printf("\n OVERFLOW \n");

else

printf("\n Enter item = ");

scanf("%d",&item);
s->sp1++;

s->a[s->sp1]=item;

void PUSH2(stack *s)

int item;

if(isfull(s))

printf("\n OVERFLOW \n");

else

printf("\n Enter item = ");

scanf("%d",&item);

s->sp2--;

s->a[s->sp2]=item;

void POP1(stack *s)

if(isempty1(s))

printf("\n UNDERFLOW\n");

else

printf("\n Popped item is = %d",s->a[s->sp1]);

s->sp1--;

}
}

void POP2(stack *s)

if(isempty2(s))

printf("\n UNDERFLOW\n");

else

printf("\n Popped item is = %d",s->a[s->sp2]);

s->sp2++;

void PEEK1(stack *s)

if(isempty1(s))

printf("\n Stack 1 UNDERFLOW\n");

else

printf("\n Peeked item is = %d",s->a[0]);

void PEEK2(stack *s)

if(isempty2(s))

printf("\n Stack 2 UNDERFLOW\n");

else

{
printf("\n Peeked item is = %d",s->a[s->sp2]);

void DISPLAY(stack *s)

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)

printf("\n Enter 1 for PUSH1");

printf("\n Enter 2 for PUSH2");

printf("\n Enter 3 for POP1");


printf("\n Enter 4 for POP2");

printf("\n Enter 5 for PEEK1 ");

printf("\n Enter 6 for PEEK2");

printf("\n Enter 7 for DISPLAY ");

printf("\n Enter 8 for EXIT");

printf("\n Enter your Choice= ");

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);

default: printf("\n Enter correct choice");

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;

void CreateEmptyQueue(Queue *lq)

lq->F=-1;

lq->R=-1;

Boolean IsEmpty(Queue *lq)

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);

void Enqueue(Queue *lq)

int item,i;

if(IsFull(lq))

printf("\n Overflow");

else

if(lq->R==-1) //For very First Item

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++;

printf("\n enter the value for Enqueue : ");

scanf("%d",&item);

lq->a[lq->R]=item;

void Dequeue(Queue *lq)

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++;

printf("\n Dequeue element : %d",temp);

void Peek(Queue *lq)


{

int temp;

if(IsEmpty(lq))

printf("\n UnderFlow");

else

temp=lq->a[lq->F];

printf("\n Peek element : %d",temp);

void Display(Queue *lq)

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)

printf("\n Press 1 - For Enqueue");

printf("\n Press 2 - For Dequeue");

printf("\n Press 3 - For Peek");

printf("\n Press 4 - For Display");

printf("\n Press 5 - For Exit");

printf("\n enter your choice : ");

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);

default:printf("\n enter only available choices!!!!!!!!!!!!");

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;

};

struct Stack* createStack( unsigned capacity )

struct Stack* stack = (struct Stack*)

malloc(sizeof(struct Stack));

if (!stack)

return NULL;

stack->top = -1;

stack->capacity = capacity;

stack->array = (int*) malloc(stack->capacity *sizeof(int));

return stack;

int isEmpty(struct Stack* stack)

return stack->top == -1 ;

char peek(struct Stack* stack)


{

return stack->array[stack->top];

char pop(struct Stack* stack)

if (!isEmpty(stack))

return stack->array[stack->top--] ;

return '$';

void push(struct Stack* stack, char op)

stack->array[++stack->top] = op;

int isOperand(char ch)

return (ch >= 'a' && ch <= 'z') ||

(ch >= 'A' && ch <= 'Z');

int Prec(char ch)

switch (ch)

case '+':

case '-':

return 1;

case '*':

case '/':

return 2;

case '^':
return 3;

return -1;

int infixToPostfix(char* exp)

int i, k;

struct Stack* stack = createStack(strlen(exp));

if(!stack)

return -1 ;

for (i = 0, k = -1; exp[i]; ++i)

if (isOperand(exp[i]))

exp[++k] = exp[i];

else if (exp[i] == '(')

push(stack, exp[i]);

else if (exp[i] == ')')

while (!isEmpty(stack) && peek(stack) != '(')

exp[++k] = pop(stack);

if (!isEmpty(stack) && peek(stack) != '(')

return -1;

else

pop(stack);
}

else

while (!isEmpty(stack) &&

Prec(exp[i]) <= Prec(peek(stack)))

exp[++k] = pop(stack);

push(stack, exp[i]);

while (!isEmpty(stack))

exp[++k] = pop(stack );

exp[++k] = '\0';

printf( "%s", exp );

int main()

char exp[20] ;

printf("\n Enter expression= ");

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;

void enqueue(int element)

if(front==-1 && 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()

if((front==-1) && (rear==-1))

printf("\nQueue is underflow..");

else if(front==rear)

printf("\nThe dequeued element is %d", queue[front]);

front=-1;

rear=-1;

else

printf("\nThe dequeued element is %d", queue[front]);

front=(front+1)%max;

void display()

int i=front;

if(front==-1 && rear==-1)

printf("\n Queue is empty..");

else

{
printf("\nElements in a Queue are :");

while(i<=rear)

printf("%d,", queue[i]);

i=(i+1)%max;

int main()

int choice=1,x;

while(choice<4 && choice!=0)

printf("\n Press 1: Insert an element");

printf("\nPress 2: Delete an element");

printf("\nPress 3: Display the element");

printf("\nEnter your choice:- ");

scanf("%d", &choice);

switch(choice)

case 1:

printf("Enter the element which is to be inserted:- ");

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;

struct node *next;

};

struct node *head;

void beginsert ();

void lastinsert ();

void randominsert();

void begin_delete();

void last_delete();

void random_delete();

void display();

void search();

void main ()

int choice =0;

while(choice != 9)

printf("\n\n*********Main Menu*********\n");

printf("\nChoose one option from the following list ...\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");

printf("\nEnter your choice?\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:

printf("Please enter valid choice..");

void beginsert()

struct node *ptr;

int item;

ptr = (struct node *) malloc(sizeof(struct node *));

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()

struct node *ptr,*temp;

int item;

ptr = (struct node*)malloc(sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter value?\n");

scanf("%d",&item);

ptr->data = item;

if(head == NULL)

ptr -> next = NULL;

head = ptr;

printf("\nNode inserted");

else

temp = head;

while (temp -> next != NULL)

temp = temp -> next;

temp->next = ptr;
ptr->next = NULL;

printf("\nNode inserted");

void randominsert()

int i,loc,item;

struct node *ptr, *temp;

ptr = (struct node *) malloc (sizeof(struct node));

if(ptr == NULL)

printf("\nOVERFLOW");

else

printf("\nEnter element value");

scanf("%d",&item);

ptr->data = item;

printf("\nEnter the location after which you want to insert ");

scanf("\n%d",&loc);

temp=head;

for(i=0;i<loc;i++)

temp = temp->next;

if(temp == NULL)

printf("\ncan't insert\n");
return;

ptr ->next = temp ->next;

temp ->next = ptr;

printf("\nNode inserted");

void begin_delete()

struct node *ptr;

if(head == NULL)

printf("\nList is empty\n");

else

ptr = head;

head = ptr->next;

free(ptr);

printf("\nNode deleted from the begining ...\n");

void last_delete()

struct node *ptr,*ptr1;

if(head == NULL)

{
printf("\nlist is empty");

else if(head -> next == NULL)

head = NULL;

free(head);

printf("\nOnly node of the list deleted ...\n");

else

ptr = head;

while(ptr->next != NULL)

ptr1 = ptr;

ptr = ptr ->next;

ptr1->next = NULL;

free(ptr);

printf("\nDeleted Node from the last ...\n");

void random_delete()

struct node *ptr,*ptr1;

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;

ptr1 ->next = ptr ->next;

free(ptr);

printf("\nDeleted node %d ",loc+1);

void search()

struct node *ptr;

int item,i=0,flag;

ptr = head;

if(ptr == NULL)

printf("\nEmpty List\n");

else

printf("\nEnter item which you want to search?\n");

scanf("%d",&item);

while (ptr!=NULL)
{

if(ptr->data == item)

printf("item found at location %d ",i+1);

flag=0;

else

flag=1;

i++;

ptr = ptr -> next;

if(flag==1)

printf("Item not found\n");

void display()

struct node *ptr;

ptr = head;

if(ptr == NULL)

printf("Nothing to print");

}
else

printf("\nprinting values . . . . .\n");

while (ptr!=NULL)

printf("\n%d",ptr->data);

ptr = ptr -> next;

You might also like