Data Structures and Algorithms LAB File
Data Structures and Algorithms LAB File
Submitted To:
Submitted By:
Jagreeti Kaur
Mayank Gupta
Asst. Professor
13106030
1
Dept. Of CSE
Program:
#include <stdio.h>
int 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)
{
a = number[i];
number[i] = number[j];
number[j] = a;
}
}
Output:
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
int A[10][10],B[10][3],m,n,s=0,i,j;
printf("\nEnter the order m x n of the sparse matrix\n");
scanf("%d%d",&m,&n);
printf("\nEnter the elements in the sparse matrix(mostly zeroes)\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
}
}
printf("The given matrix is:\n");
for(i=0;i<m;i++)
5
for(j=0;j<n;j++)
{
printf("%d ",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");
printf("\n");
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
6
}
getch();
}
Output:
Program:
#include<stdio.h>
int main()
{
if (array[c] == search)
{
printf("%d is present at location %d.\n", search, c+1);
break;
}
if (c == n)
printf("%d is not present in array.\n", search);
return 0;
}
Output:
Case1.
Case2.
Program:
#include <stdio.h>
int main()
{
Output:
12
Program:
#include <stdio.h>
int main()
{
13
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");
for (c = 0; c < p; c++)
for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);
for (c = 0; c < m; c++)
{
for (d = 0; d < q; d++)
{
14
Output:
15
16
Output:
17
#include<conio.h>
#define MAX 5
int top, status;
void push (int stack[], int item)
{ if (top == (MAX-1))
status = 0;
else
{ status = 1;
++top;
stack [top] = item;
}
}
int pop (int stack[])
{
int ret;
if (top == -1)
{ ret = 0;
status = 0;
}
else
{ status = 1;
ret = stack [top];
19
--top;
}
return ret;
}
void display (int stack[])
{ int i;
printf ("\nThe Stack is: ");
if (top == -1)
printf ("empty");
else
{ for (i=top; i>=0; --i)
printf ("\n--------\n|%3d |\n--------",stack[i]);
}
printf ("\n");
}
void main()
{
int stack [MAX], item;
int ch;
top = -1;
do
20
{ do
{ printf ("\n MAIN MENU");
printf ("\n1.PUSH (Insert) in the Stack");
printf ("\n2.POP (Delete) from the Stack");
printf ("\n3.Exit (End the Execution)");
printf ("\nEnter Your Choice: ");
scanf ("%d", &ch);
if (ch<1 || ch>3)
printf ("\nInvalid Choice, Please try again");
} while (ch<1 || ch>3);
switch (ch)
{case 1:
printf ("\nEnter the Element to be pushed : ");
scanf ("%d", &item);
printf (" %d", item);
push (stack, item);
if (status)
{ printf ("\nAfter Pushing ");
display (stack);
if (top == (MAX-1))
printf ("\nThe Stack is Full");
}
21
else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{
}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}
22
Output:
23
}
void push_stack2 (int data)
{
ar[--top2] = data; }
else
}
void pop_stack1 ()
{
if (top1 >= 0)
{ int popped_value = ar[top1--];
24
}
void pop_stack2 ()
{ if (top2 < SIZE)
{
else
{
}
void print_stack1 ()
{
int i;
for (i = top1; i >= 0; --i)
{ printf ("%d ", ar[i]); }
printf ("\n");
}
void print_stack2 ()
{
int i;
for (i = top2; i < SIZE; ++i)
{ printf ("%d ", ar[i]); }
printf ("\n");
}
25
int main()
{
push_stack1 (i);
printf ("Value Pushed in Stack 1 is %d\n", i); }
push_stack2 (i);
printf ("Value Pushed in Stack 2 is %d\n", i); }
print_stack1 ();
print_stack2 ();
pop_stack1 ();
--num_of_ele; }
pop_stack1 ();
}
26
Output:
27
#include<stdio.h>
#include<conio.h>
char stack[SIZE];
char pop();
int i,j;
while(item != '\0')
{
if(item == '(')
{
push(item);
postfix_exp[j++] = item;
else if(is_operator(item) == 1)
{
x=pop();
postfix_exp[j++] = x;
x = pop();
push(x);
}
push(item);
}
else if(item == ')')
{
x = pop();
while(x != '(')
{
postfix_exp[j++] = x;
x = pop();
}
else
{
getch();
exit(0);
item = infix_exp[i++];
}
postfix_exp[j++] = '\0';
printf("\nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
}
void push(char item)
{
else
{
top = top+1;
stack[top] = item; }
}
char pop()
{
else
{
top = top-1;
return(item);
}
30
return( 1);
return 0;
else
{
}
int precedence(char symbol)
{
if(symbol == '^')
return(3);
return(2);
return(1);
return(0);
else
31
Output:
32
#include<stdio.h>
void create();
#include<stdlib.h>
void display();
struct node
{
int info;
struct node*next;
};
create();
printf("linklist is \n");
33
display();
void create()
{
int ch,n;
struct node*p,*q;
scanf("%d",&n);
p->next=NULL;
if(first==NULL)
first=p;
else
{
q=first;
while(q->next!=NULL)
q=q->next;
q->next=p;
ch--; }
void display()
{
struct node*p;
p=first;
while(p!=NULL)
{
printf("%d\n",p->info); p=p->next;
}
}
34
Output:
35
int data;
struct node* next;
};
= new_node;
}
void printList(struct node *head)
{
}
void deleteNode(struct node *node_ptr)
{
= temp->data;
node_ptr->next
= temp->next;
free(temp);
int main()
{
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
printf("Before deleting \n");
37
printList(head);
deleteNode(head);
Output:
38
39
int data;
void insert_end();
void insert_any();
void display();
void main()
{ int choice; int cont = 1;
header = (struct node *) malloc(sizeof(struct node));
header->data = NULL;
header->link = NULL;
while(cont == 1)
{
switch(choice)
{
break;
}
printf("\n\nDo you want to continue? (1 / 0): ");
scanf("%d", &cont);
}
}
void insert_front()
{
int data_value;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));
temp->data = data_value;
temp->link = header->link;
header->link = temp;
}
void insert_end()
{
int data_value;
printf("\nEnter data of the node: ");
scanf("%d", &data_value);
temp = (struct node *) malloc(sizeof(struct node));
ptr = header;
while(ptr->link != NULL)
41
ptr = ptr->link;
temp->data = data_value;
}
temp->link = ptr->link;
ptr->link = temp;
}
void insert_any()
{
if(ptr->data == key)
{ temp->data = data_value;
temp->link = ptr->link;
ptr->link = temp; }
else
{ printf("\nValue %d not found\n",key); }
}
void display()
{
ptr = header;
while(ptr->link != NULL)
42
{ ptr = ptr->link;
Output:
43
#include <process.h>
#define QUEUE_SIZE 5
void main()
{
switch (choice)
{
break;
break;
case 4: exit(0);
default:
} getch();
}
}
void insert_rear(int item, int q[], int *r)
{
}
void delete_front(int q[], int *f, int *r)
{
if (qempty(*f, *r))
{
if(*f> *r)
{
*f=0,*r=-1; }
}
45
printf("Queue is empty\n");
return; }
return (f>r)?1:0; }
int qfull(int r)
return (r==QUEUE_SIZE-1)?1:0;
46
Output :
47