0% found this document useful (0 votes)
141 views47 pages

Data Structures and Algorithms LAB File

This document contains a lab practical record for the subject of Data Structures and Algorithms. It includes 9 programs with objectives, code, and output for various concepts like sorting an array, implementing a sparse matrix, searching in arrays, matrix multiplication, bubble sort, implementing stacks, and converting infix to postfix notation. The programs demonstrate fundamental data structures and algorithms.

Uploaded by

mayank
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
141 views47 pages

Data Structures and Algorithms LAB File

This document contains a lab practical record for the subject of Data Structures and Algorithms. It includes 9 programs with objectives, code, and output for various concepts like sorting an array, implementing a sparse matrix, searching in arrays, matrix multiplication, bubble sort, implementing stacks, and converting infix to postfix notation. The programs demonstrate fundamental data structures and algorithms.

Uploaded by

mayank
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 47

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS

LAB PRACTICALS RECORD

INSTRUMENTATION AND CONTROL ENGINEERING

DEPARTMENT OF INSTRUMENTATION AND CONTROL ENGINEERING


Dr. B R AMBEDKAR NATIONAL INSTITUTE OF TECHNOLOGY
JALANDHAR 144011, PUNJAB (INDIA)
January-June, 2016

Submitted To:

Submitted By:

Jagreeti Kaur

Mayank Gupta

Asst. Professor

13106030
1

DATA STRUCTURE AND ALGORITHMS

Dept. Of CSE

ICE,6th Semester , 13106030

ICE , 3rd Year

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

1. Objective : Sort an Array in Ascending Order

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

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


3

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

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


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

Output:

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

2. Objective : Implement Sparsh Matrix Using Array

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("\n%d row and %d column: ",i,j);


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

}
}
printf("The given matrix is:\n");
for(i=0;i<m;i++)
5

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

}
getch();
}

Output:

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

3. Objective : To Search An Element In Array using Linear Search

Program:
#include<stdio.h>
int main()
{

int array[100], search, c, n;


printf("Enter the number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integer(s)\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to search\n");
scanf("%d", &search);
for (c = 0; c < n; c++)
{

if (array[c] == search)

/* if required element found */


8

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

{
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.

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

4. Objective: To Search An Element In Array using Binary Search

Program:
#include <stdio.h>
int main()
{

int c, first, last, middle, n, search, array[100];


printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
10

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

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


scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.\n", search);
return(0);
}
11

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output:

12

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

5. Objective : Matrix Multiplication 3*3

Program:
#include <stdio.h>
int main()
{
13

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

for (k = 0; k < p; k++)


{
sum = sum + first[c][k]*second[k][d]; }
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

Output:

15

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

16

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

6. Objective : To perform Bubble Sort In Array


Program:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap
= array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}

Output:

17

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

7. Objective : To Perform Push and pop Operation on Stack


Program:
#include <stdio.h>
18

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

#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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

{ 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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

else
printf ("\nStack overflow on Push");
break;
case 2:
item = pop (stack);
if (status)
{

printf ("\nThe Popped item is %d. After Popping: ");


display (stack);

}
else
printf ("\nStack underflow on Pop");
break;
default:
printf ("\nEND OF EXECUTION");
}
}while (ch != 3);
getch();
}

22

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output:

23

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

8. Objective : Implement two Stacks In Single Array


Program:
#include <stdio.h>
#define SIZE 10
int ar[SIZE]; int top1 = -1; int top2 = SIZE;
void push_stack1 (int data)
{

if (top1 < top2 - 1)


{ ar[++top1] = data; }
else
{ printf ("Stack Full! Cannot Push\n");

}
void push_stack2 (int data)
{

if (top1 < top2 - 1)


{

ar[--top2] = data; }

printf ("Stack Full! Cannot Push\n"); }

else

}
void pop_stack1 ()
{

if (top1 >= 0)
{ int popped_value = ar[top1--];
24

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

printf ("%d is being popped from Stack 1\n", popped_value); }


else

{ printf ("Stack Empty! Cannot Pop\n"); }

}
void pop_stack2 ()
{ if (top2 < SIZE)
{

int popped_value = ar[top2++];


printf ("%d is being popped from Stack 2\n", popped_value); }

else
{

printf ("Stack Empty! Cannot Pop\n"); }

}
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

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

int main()
{

int ar[SIZE]; int i; int num_of_ele;


printf ("We can push a total of 10 values\n");
for (i = 1; i <= 6; ++i)
{

push_stack1 (i);
printf ("Value Pushed in Stack 1 is %d\n", i); }

for (i = 1; i <= 4; ++i)


{

push_stack2 (i);
printf ("Value Pushed in Stack 2 is %d\n", i); }

print_stack1 ();

print_stack2 ();

printf ("Pushing Value in Stack 1 is %d\n", 11);


push_stack1 (11);
num_of_ele = top1 + 1;
while (num_of_ele)
{

pop_stack1 ();

--num_of_ele; }

pop_stack1 ();
}

26

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output:

27

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

9. Objective : To change Expression Infix to Postfix


Program:

#include<stdio.h>

#include<conio.h>

#define SIZE 100


int top = -1;

char stack[SIZE];

void push(char item);

char pop();

int is_operator(char symbol);


int precedence(char symbol);
void main()
{

int i,j;

char infix_exp[SIZE], postfix_exp[SIZE];


char item,x;
printf("\nEnter Infix expression in parentheses: \n");
gets(infix_exp);
i=0; j=0; item=infix_exp[i++];
28

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

while(item != '\0')
{

if(item == '(')
{

push(item);

else if((item >= 'A' && item <= 'Z') ||


(item >= 'a' && item <= 'z'))
{

postfix_exp[j++] = item;

else if(is_operator(item) == 1)
{

x=pop();

while(is_operator(x) == 1 && precedence(x)


>= precedence(item))
{

postfix_exp[j++] = x;

x = pop();
push(x);

}
push(item);

}
else if(item == ')')
{

x = pop();

while(x != '(')
{

postfix_exp[j++] = x;

x = pop();

}
else
{

printf("\nInvalid Arithmetic Expression.\n");


29

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

getch();

exit(0);

item = infix_exp[i++];
}
postfix_exp[j++] = '\0';
printf("\nArithmetic expression in Postfix notation: ");
puts(postfix_exp);
}
void push(char item)
{

if(top >= SIZE-1)


{

printf("\nStack Overflow. Push not possible.\n");

else
{

top = top+1;

stack[top] = item; }

}
char pop()
{

char item = NULL;

if(top <= -1)


{

printf("\nStack Underflow. Pop not possible.\n"); }

else
{

item = stack[top]; stack[top] = NULL;

top = top-1;

return(item);
}
30

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

int is_operator(char symbol)


{

if(symbol == '^' || symbol == '*' || symbol == '/' ||


symbol == '+' || symbol == '-')

return( 1);

return 0;

else
{
}
int precedence(char symbol)
{

if(symbol == '^')

return(3);

else if(symbol == '*' || symbol == '/')


{

return(2);

else if(symbol == '+' || symbol == '-')


{

return(1);

return(0);

else

31

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output:

32

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

10. Objective : To Create a Linked List


Program:

#include<stdio.h>
void create();

#include<stdlib.h>
void display();

struct node
{

int info;
struct node*next;

};

struct node *first=NULL;


int main()
{

create();
printf("linklist is \n");
33

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

display();

void create()
{

int ch,n;

struct node*p,*q;

printf("Enter nodes to be created\t");


scanf("%d",&ch);
while(ch>0)
{

printf("Enter the info :");

scanf("%d",&n);

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


p->info=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

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output:

35

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

11. Objective : To Delete a Node From a Linked List


Program:
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
struct node
{

int data;
struct node* next;

};

void push(struct node** head_ref, int new_data)


36

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

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


new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref)

= new_node;

}
void printList(struct node *head)
{

struct node *temp = head;


while(temp != NULL)
{

printf("%d ", temp->data);


temp = temp->next;

}
void deleteNode(struct node *node_ptr)
{

struct node *temp = node_ptr->next;


node_ptr->data

= temp->data;

node_ptr->next

= temp->next;

free(temp);

int main()
{

struct node* head = NULL;


push(&head, 1);

push(&head, 4);

push(&head, 1);

push(&head, 12);

push(&head, 1);
printf("Before deleting \n");
37

DATA STRUCTURE AND ALGORITHMS

printList(head);

ICE,6th Semester , 13106030

deleteNode(head);

printf("\nAfter deleting \n");


printList(head);
getchar();
return 0;
}

Output:

38

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

39

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

12. Objective : Add an Element in Linked List.


Program:
#include<stdio.h> #include<conio.h>
struct node
{

int data;

struct node *link; };

struct node *header, *ptr, *temp;


void insert_front();

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

printf("\n1. Insert at front\n");


printf("\n2. Insert at end\n");
printf("\n3. Insert at any position\n");
printf("\n4. Display linked list\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);

switch(choice)
{

case 1: insert_front(); break;


case 2: insert_end(); break;
40

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

case 3: insert_any(); break;


case 4: display();

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

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

ptr = ptr->link;

temp->data = data_value;

}
temp->link = ptr->link;

ptr->link = temp;
}
void insert_any()
{

int data_value, key;


printf("\nEnter data of the node: "); scanf("%d", &data_value);
printf("\nEnter data of the node after which new node is to be inserted: ");
scanf("%d", &key);
temp = (struct node *) malloc(sizeof(struct node));
ptr = header;
while(ptr->link != NULL && ptr->data != key)
{ ptr = ptr->link;

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

printf("\nContents of the linked list are: \n");

ptr = header;

while(ptr->link != NULL)
42

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

{ ptr = ptr->link;

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

Output:

43

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

13. Objective : Perform Operation Push and Pop in Queue


Program:
#include <stdio.h>
#include <conio.h>

#include <process.h>
#define QUEUE_SIZE 5

void main()
{

void insert_rear(int, int *, int *);


void delete_front(int *, int *, int *);
void display(int *, int, int);
int choice, item, f, r, q[10];
f = 0; /* Front end of queue*/
r = -1; /* Rear end of queue*/
for (;;)
{

printf("\t Ordinary Queue Operation\n\n");


printf("\t 1 . Push / Insert\n");
printf("\t 2 . Pop / Delete\n");
printf("\t 3 . View / Display\n");
printf("\t 4 . Exit\n\n\n");
printf("\t Enter the choice : "); scanf("%d", &choice);
44

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

switch (choice)
{

case 1: printf("Enter the item to be inserted : "); scanf("%d", &item);


insert_rear(item, q, &r); continue;
case 2: delete_front(q, &f, &r);
case 3: display(q, f, r);

break;

break;

case 4: exit(0);
default:

printf("\t\t\tInvalid Input Try Again");

} getch();
}
}
void insert_rear(int item, int q[], int *r)
{

if (qfull(*r)) /* Is queue full ? */


{

printf("\t\t\tQueue overflow\n"); return; }


q[++(*r)] = item;

}
void delete_front(int q[], int *f, int *r)
{

if (qempty(*f, *r))
{

printf("\t\t\tQueue underflow\n"); return; }


printf(" Pop Successfull, element deleted = %d ",q[(*f)++]);

if(*f> *r)
{

*f=0,*r=-1; }

}
45

ICE,6th Semester , 13106030

DATA STRUCTURE AND ALGORITHMS

void display(int q[], int f, int r)


{ int i;
if (qempty(f,r))
{

printf("Queue is empty\n");

return; }

printf("\t\t\t Queue Container\n\n");


for(i=f;i<=r; i++)
printf("\t\t\t| %5d |\n",q[i]);
}
int qempty(int f, int r)
{

return (f>r)?1:0; }
int qfull(int r)

return (r==QUEUE_SIZE-1)?1:0;

46

DATA STRUCTURE AND ALGORITHMS

ICE,6th Semester , 13106030

Output :

47

You might also like