Data Structure Lab
Data Structure Lab
1. To create a two-dimensional array of numbers and calculate & display the row & column, sum and
the grand total.
Source Code.
/*To create a two-dimensional array of numbers and calculate & display the row & column sum and the
grand total*/
#include <stdio.h>
int main()
{
int rows, cols, sumRow, sumCol;
//Initialize matrix a
int a[][3] =
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
//Calculates number of rows and columns present in given matrix
rows = (sizeof(a)/sizeof(a[0]));
cols = (sizeof(a)/sizeof(a[0][0]))/rows;
//Calculates sum of each row of given matrix
for(int i = 0; i < rows; i++)
{
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
printf("Sum of %d row: %d\n", (i+1), sumRow);
}
4. To write a program to insert (Push) an element and delete (Pop) an element from the stack using
pointer.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 3
7. To write a program to insert an element in the queue and delete an element from the queue using
pointer.
Source code:
#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n\n ******MAIN MENU******");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the Queue");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if (val != -1)
printf("\n The number deleted is: %d", val);
break;
case 3:
val = peek();
if (val != -1)
printf("\n The first value in queue is: %d", val);
break;
case 4:
display();
break;
}
}
while(option != 5);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue: ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
int peek()
{
if(front==-1 || front>rear)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front; i <= rear; i++)
printf("\t %d", queue[i]);
}
}
Output:
8. To create a circular queue and add an element and delete an element from a circular queue.
Source Code:
#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front=-1, rear=-1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n ******MAIN MENU******");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the queue");
printf("\n 5. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if(val!=-1)
printf("\n The number is deleted is: %d", val);
break;
case 3:
val = peek();
if(val!=-1)
printf("\n The first value in queue is: %d", val);
break;
case 4:
display();
break;
}
}
while(option!=5);
getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue: ");
scanf("%d", &num);
if(front==0 && rear==MAX-1)
printf("\n OVERFLOW");
else if(front==-1 && rear==-1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX-1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
int delete_element()
{
int val;
if(front==-1 && rear==-1)
{
printf("\n UNDERFLOW");
return -1;
}
val=queue[front];
if(front==-1 && rear==-1)
front=rear=-1;
else
{
if(front==MAX-1)
front=0;
else
front++;
}
return val;
}
int peek()
{
if(front==-1 && rear==-1)
{
printf("\n QUEUE IS EMPTY");
return -1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if(front==-1 && rear==-1)
printf("\n QUEUE IS EMPTY");
else
{
if(front<rear)
{
for(i=front; i<=rear; i++)
printf("\t %d", queue[i]);
}
else
{
for(i=front; i<MAX; i++)
printf("\t %d", queue[i]);
for(i=0; i<=rear; i++)
printf("\t %d", queue[i]);
}
}
}
Output:
9. To write a program of a structure containing an item name along with the unit price. The user
enters the item name and quantity to be purchased. Program print outs total price of item with name
using pointer in a structure or array in a structure.
Source Code:
#include <stdio.h>
void main()
{
struct date
{
int day;
int month;
int year;
};
struct details
{
char name[20];
int price;
int code;
int qty;
struct date mfg;
};
struct details item[50];
int n, i;
printf("Enter number of items:");
scanf("%d", &n);
fflush(stdin);
for (i = 0; i < n; i++)
{
fflush(stdin);
printf("Item name: \n");
scanf("%s", item[i].name);
fflush(stdin);
printf("Item code: \n");
scanf("%d", &item[i].code);
fflush(stdin);
printf("Quantity: \n");
scanf("%d", &item[i].qty);
fflush(stdin);
printf("price: \n");
scanf("%d", &item[i].price);
fflush(stdin);
printf("Manufacturing date(dd-mm-yyyy): \n");
scanf("%d-%d-%d", &item[i].mfg.day,
&item[i].mfg.month, &item[i].mfg.year);
}
printf(" ***** INVENTORY ***** \n");
printf("------------------------------------------------------------------\n");
printf("S.N.| NAME | CODE | QUANTITY | PRICE | MFG.DATE \n");
printf("------------------------------------------------------------------\n");
for (i = 0; i < n; i++)
printf("%d %-15s %-d %-5d %-5d %d/%d/%d \n", i + 1, item[i].name, item[i].code,
item[i].qty,
item[i].price, item[i].mfg.day, item[i].mfg.month, item[i].mfg.year);
printf("------------------------------------------------------------------\n");
}
Output:
12. To create a circular linked list and insert & delete an element from the list.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_cll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
int main()
{
int option;
do
{
printf("\n\n **********MAIN MENU**********");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Delete a node from the beginning");
printf("\n 6: Delete a node from the end");
printf("\n 7: Delete a node after a given node");
printf("\n 8: Delete the entire list");
printf("\n 9: EXIT");
printf("\n \n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_cll(start);
printf("\n CIRCULR LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = delete_beg(start);
break;
case 6: start = delete_end(start);
break;
case 7: start = delete_after(start);
break;
case 8: start = delete_list(start);
printf("\n CIRCULR LINKED LIST DELETED");
break;
}
}
while(option != 9);
getch();
return 0;
}
struct node *create_cll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data: ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = num;
if(start == NULL)
{
new_node->next = new_node;
start = new_node;
}
else
{
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
}
printf("\n Enter the data: ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr->next != start)
{
printf("\t %d", ptr->data);
ptr = ptr->next;
}
printf("\t %d", ptr->data);
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data: ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = num;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = new_node;
new_node->next = start;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr->next != start)
ptr = ptr->next;
ptr->next = start->next;
free(start);
start = ptr->next;
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
while(ptr->next != start)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
free(ptr);
return start;
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value after which the node has to deleted: ");
scanf("%d", &val);
ptr = start;
preptr = ptr;
while(preptr->data != val)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = ptr->next;
if(ptr == start)
{
start = preptr->next;
}
free(ptr);
return start;
}
struct node *delete_list(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr->next != start)
{
start = delete_end(start);
}
free(start);
return start;
}
Output:
13. Write a program to merge two shorted linked list.
Source Code:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *create(struct node *start);
struct node *insert_s(struct node *start,int data);
struct node *insert(struct node *start,int data);
void display(struct node *start );
void merge(struct node *p1,struct node *p2);
int main()
{
struct node *start1=NULL,*start2=NULL;
start1=create(start1);
start2=create(start2);
printf("List1 : ");
display(start1);
printf("List2 : ");
display(start2);
merge(start1, start2);
return 0;
}/*End of main()*/
void merge(struct node *p1,struct node *p2)
{
struct node *start3;
start3=NULL;
while(p1!=NULL && p2!=NULL)
{
if(p1->info < p2->info)
{
start3=insert(start3,p1->info);
p1=p1->link;
}
else if(p2->info < p1->info)
{
start3=insert(start3,p2->info);
p2=p2->link;
}
else if(p1->info==p2->info)
{
start3=insert(start3,p1->info);
p1=p1->link;
p2=p2->link;
}
}
/*If second list has finished and elements left in first list*/
while(p1!=NULL)
{
start3=insert(start3,p1->info);
p1=p1->link;
}
/*If first list has finished and elements left in second list*/
while(p2!=NULL)
{
start3=insert(start3,p2->info);
p2=p2->link;
}
printf("Merged list is : ");
display(start3);
}
struct node *create(struct node *start )
{
int i, n,data;
printf("Enter the number of nodes : ");
scanf("%d",&n);
start=NULL;
for(i=1;i<=n;i++)
{
printf("Enter the element to be inserted : ");
scanf("%d", &data);
start=insert_s(start, data);
}
return start;
}/*End of create_slist()*/
void release(struct
node **head)
{
struct node *temp =
*head;
*head = (*head)-
>next;
while ((*head) !=
NULL)
{
free(temp);
temp = *head;
(*head) =
(*head)- >next;
}
}
Output:
15. (a) To write a program to calculate the binomial co-efficient of nCr of two numbers using
recursive function.
Source Code:
#include<stdio.h>
int BC(int n, int k);
int main()
{
int n,k;
printf("Enter n and k
: ");
scanf("%d%d",&n,&k);
printf("Binomial coefficient \n",BC(n,k));
printf("%d\n",BC(n,k));
return 0;
}
int BC(int n, int k)
{
if(k==0 || k==n)
return 1;
return BC(n-1,k-1) + BC(n-1,k);
}
Output:
(b)To write a program to calculate the binomial co-efficient of nCr of two numbers using function in
non-recursive way.
Source Code:
#include<stdio.h>
void main() {
int i, j, n, k, min, c[20][20]={0};
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n Enter the value of k: ");
scanf("%d", &k);
if(n >= k) {
for(i=0; i<=n; i++) {
min = i<k? i:k;
for(j = 0; j <= min; j++) {
if(j==0 || j == i) {
c[i][j] = 1;
} else {
c[i][j] = c[i-1][j-1] + c[i-1][j];
}
}
}
printf("%d\t",c[n][k]);
printf("\n");
} else {
printf("\n Invalid input \n Enter value n>=k \n");
}
}
Output:
16. (a) To write a program to generate Fibonacci Series using recursive function.
Source Code:
#include <stdio.h>
int Fibonacci(int);
int main()
{
int n, i = 0, res;
printf("Enter the number of terms: \n");
scanf("%d", &n);
printf("Fibonacci Series
\n");
for(i = 0; i < n; i++)
{
res = Fibonacci(i);
printf("%d\t",res);
}
return 0;
}
int Fibonacci (int n)
{
if ( n == 0 )
return 0;
else if (n == 1)
return 1;
else
return (Fibonacci(n-1) + Fibonacci(n-2));
}
Output:
(b) To write a program to
generate Fibonacci
Series using function in
non-recursive way.
Source Code:
#include <stdio.h>
int main()
{
int n1 = 0, n2 = 1, n3, i, number;
printf("Enter the number of elements: \n");
scanf("%d", &number);
printf("\n %d%d \n \n", n1, n2); //printing 0 and n1
for(i = 2; i < number; ++i)
{
n3 = n1 + n2;
printf("%d \n", n3);
n1 = n2;
n2 = n3;
}
return 0;
}
Output:
17. To write a program to create a binary tree and transverse it in pre-order and post-order form.
Source Code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node* left;
struct node* right;
};
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root)
{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(value)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value)
{
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value)
{
root->right = createNode(value);
return root->right;
}
int main()
{
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
Output: