Dsuc Assignment
Dsuc Assignment
Dsuc Assignment
DATA
STRUCTURES
USING C
int main()
int i,j,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];
scanf("%d %d",&m,&n);
scanf("%d %d",&p,&q);
if(m==p&n==q)
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&arr1[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&arr2[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
res[i][j]=arr1[i][j]+arr2[i][j];
printf("Resultant matrix:\n");
for(i=0;i<p;++i)
{
for(j=0;j<q;++j)
printf("%d ",res[i][j]);
printf("\n");
else
return 0;
}
OUTPUT:
PROGRAM 2. WRITE A PROGRAM FOR SUBTRACTION OF TWO MATRICES.
#include<stdio.h>
int main()
int i,j,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];
scanf("%d %d",&m,&n);
scanf("%d %d",&p,&q);
if(m==p&n==q)
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&arr1[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&arr2[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
res[i][j]=arr1[i][j]-arr2[i][j];
printf("Resultant matrix:\n");
for(i=0;i<p;++i)
{
for(j=0;j<q;++j)
printf("%d ",res[i][j]);
printf("\n");
else
return 0;
}
OUTPUT:
PROGRAM 3. WRITE A PROGRAM FOR MULTIPLICATION OF TWO MATRICES.
#include<stdio.h>
int main()
int i,j,k,m,n,p,q,arr1[10][10],arr2[10][10],res[10][10];
scanf("%d %d",&m,&n);
scanf("%d %d",&p,&q);
if(n==p)
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&arr1[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&arr2[i][j]);
for(i=0;i<p;++i)
for(j=0;j<q;++j)
res[i][j]=0;
for(k=0;k<q;k++)
res[i][j]+=arr1[i][k] * arr2[k][j];
}
printf("Resultant matrix:\n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
printf("%d ",res[i][j]);
printf("\n");
else
return 0;
}
OUTPUT:
PROGRAM 4. WRITE A PROGRAM TO SUM DIAGONAL OF A MATRIX.
#include<stdio.h>
int main()
int i,j,m,arr[10][10],sum=0;
scanf("%d",&m);
for(i=0;i<m;++i)
for(j=0;j<m;++j)
scanf("%d",&arr[i][j]);
for(i=0;i<m;++i)
sum+=arr[i][i];
return 0;
}
OUTPUT:
PROGRAM 5. WRITE A PROGRAM TO STACK USING ARRAY
IMPLEMENTATION.
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int* array;
};
stack->capacity = capacity;
stack->top = -1;
return stack;
}
// Function to add an item to stack. It increases top by 1
if (isFull(stack))
return;
stack->array[++stack->top] = item;
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top--];
if (isEmpty(stack))
return INT_MIN;
return stack->array[stack->top];
int main()
push(stack, 10);
push(stack, 20);
return 0;
}
OUTPUT:
PROGRAM 6. WRITE A PROGRAM TO STACK USING LINKED LIST
IMPLEMETATION.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
};
if (top == NULL) {
newNode->next = NULL;
} else {
printf("Node is Inserted\n\n");
int pop() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
top = top->next;
free(temp);
return temp_data;
void display() {
if (top == NULL) {
printf("\nStack Underflow\n");
} else {
printf("%d--->", temp->data);
temp = temp->next;
printf("%d--->NULL\n\n", temp->data);
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\nEnter the value to insert: ");
scanf("%d", &value);
push(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
OUTPUT:
PROGRAM 7. WRITE A PROGRAM TO QUEUE USING ARRAY
IMPLEMENTATION.
#include<stdio.h>
#define SIZE 5
if(rear == SIZE-1){
else{
if(front == -1){
front = 0;
rear = rear + 1;
queue[rear] = item;
void dequeue(){
if(front == -1){
else{
printf("We have dequeued : %d\n", queue[front]);
front = front + 1;
front = -1;
rear = -1;
void printQueue(){
if(rear == -1)
else{
int i;
printf("\nThe queue after enqueue & dequeue ops looks like :");
printf("%d ",queue[i]);
int main()
enqueue(2);
enqueue(4);
enqueue(6);
enqueue(8);
//dequeue beings here
dequeue();
dequeue();
printQueue();
return 0;
OUTPUT:
PROGRAM 8. WRITE A PROGRAM TO QUEUE USING LINKED LIST
IMPLEMENTATION.
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
};
} else {
rear = ptr;
printf("Node is Inserted\n\n");
int dequeue() {
if (front == NULL) {
printf("\nUnderflow\n");
return -1;
} else {
free(temp);
return temp_data;
void display() {
printf("\nQueue is Empty\n");
} else {
temp = front;
while (temp) {
printf("\n\n\n");
int main() {
while (choice != 4) {
printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
switch (choice) {
case 1:
enqueue(value);
break;
case 2:
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nWrong Choice\n");
return 0;
}
OUTPUT:
PROGRAM 9. WRITE A PROGRAM TO CIRCULAR QUEUE USING ARRAY
IMPLEMENTATION.
#include <stdio.h>
#define capacity 6
int queue[capacity];
int checkFull(){
return 1;
return 0;
int checkEmpty(){
if (front == -1)
return 1;
return 0;
if (checkFull())
printf("Overflow condition\n");
else
{
if (front == -1)
front = 0;
queue[rear] = value;
int dequeue() {
int variable;
if (checkEmpty()) {
printf("Underflow condition\n");
return -1;
else
variable = queue[front];
if (front == rear) {
else {
return 1;
}
// Display the queue
void print(){
int i;
if (checkEmpty())
printf("Nothing to dequeue\n");
else
int main() {
dequeue();
enqueue(15);
enqueue(20);
enqueue(25);
enqueue(30);
enqueue(35);
print();
dequeue();
dequeue();
print();
enqueue(40);
enqueue(45);
enqueue(50);
enqueue(55);//Overflow condition
print();
return 0;
OUTPUT:
PROGRAM 10. WRITE A PROGRAM TO SINGLY LINKED LIST.
#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\nMain Menu\n");
printf("\n______________\n");
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()
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;
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;
ptr = ptr ->next;
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()
{
struct node *ptr;
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);
OUTPUT:
PROGRAM 11. WRITE A PROGRAM TO DOUBLY LINKED LIST.
#include <stdio.h>
#include <stdlib.h>
struct node {
int info;
};
void traverse()
// List is empty
if (start == NULL) {
printf("\nList is empty\n");
return;
temp = start;
temp = temp->next;
void insertAtFront()
{
int data;
scanf("%d", &data);
temp->info = data;
temp->prev = NULL;
// assigned to start
temp->next = start;
start = temp;
printf("\nNode inserted.");
void insertAtEnd()
int data;
temp->prev = NULL;
temp->next = NULL;
scanf("%d", &data);
temp->info = data;
temp->next = NULL;
trav = start;
// If start is NULL
if (start == NULL) {
start = temp;
// Changes Links
else {
trav = trav->next;
temp->prev = trav;
trav->next = temp;
printf("\nNode inserted.");
void insertAtPosition()
newnode->next = NULL;
newnode->prev = NULL;
scanf("%d", &pos);
// If start==NULL,
if (start == NULL) {
start = newnode;
newnode->prev = NULL;
newnode->next = NULL;
// If position==1,
else if (pos == 1) {
// this is author method its correct but we can simply call insertAtfront() function for this
special case
/* newnode->next = start;
newnode->next->prev = newnode;
newnode->prev = NULL;
start = newnode; */
insertAtFront();
// Change links
else {
scanf("%d", &data);
newnode->info = data;
temp = start;
temp = temp->next;
i++;
}
newnode->next = temp->next;
newnode->prev = temp;
temp->next = newnode;
temp->next->prev = newnode;
printf("\nNode inserted.");
void deleteFirst()
if (start == NULL)
printf("\nList is empty\n");
else {
temp = start;
start = start->next;
if (start != NULL)
start->prev = NULL;
free(temp);
printf("\nNode deleted.");
void deleteEnd()
if (start == NULL)
printf("\nList is empty\n");
temp = start;
temp = temp->next;
if (start->next == NULL)
start = NULL;
else {
temp->prev->next = NULL;
free(temp);
printf("\nNode deleted.");
void deletePosition()
int pos, i = 1;
temp = start;
// If DLL is empty
if (start == NULL)
printf("\nList is empty\n");
// Otherwise
else {
// Position to be deleted
scanf("%d", &pos);
// If the position is the first node
if (pos == 1) {
if (start != NULL) {
start->prev = NULL;
free(position);
return;
temp = temp->next;
i++;
// Change Links
position = temp->next;
if (position->next != NULL)
position->next->prev = temp;
temp->next = position->next;
// Free memory
free(position);
printf("\nNode deleted.");
// Driver Code
int main()
int choice;
while (1) {
printf("\n\n\tMain Menu\n");
printf("\n\t______________\n");
printf("\n\t1 Display\n");
" starting\n");
" end\n");
"any position\n");
"first element\n");
"last element\n");
printf("\t8 Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse();
break;
case 2:
insertAtFront();
break;
case 3:
insertAtEnd();
break;
case 4:
insertAtPosition();
break;
case 5:
deleteFirst();
break;
case 6:
deleteEnd();
break;
case 7:
deletePosition();
break;
case 8:
exit(1);
break;
default:
continue;
return 0;
OUTPUT:
PROGRAM 12. WRITE A PROGRAM TO CIRCULAR LINKED LIST.
#include<stdio.h>
#include<stdlib.h>
struct node
int info;
};
int main( )
int choice,data,item;
while(1)
printf("\n\n1.Create List\n");
printf("2.Display\n");
printf("4.Add at beginning\n");
printf("5.Add at end\n");
printf("8.Quit\n");
scanf("%d",&choice);
switch(choice)
case 1:
last=create_list(last);
break;
case 2:
display(last);
break;
case 3:
scanf("%d",&data);
last=addtoempty(last,data);
break;
case 4:
scanf("%d",&data);
last=addatbeg(last,data);
break;
case 5:
scanf("%d",&data);
last=addatend(last,data);
break;
case 6:
scanf("%d",&item);
last=addafter(last,data,item);
break;
case 7:
scanf("%d",&data);
last=del(last,data);
break;
case 8:
exit(1);
default:
printf("\nWrong choice\n");
}/*End of switch*/
}/*End of while*/
return 0;
int i,n,data;
scanf("%d",&n);
last=NULL;
if(n==0)
return last;
scanf("%d",&data);
last=addtoempty(last,data);
for(i=2;i<=n;i++)
scanf("%d",&data);
last=addatend(last,data);
return last;
}/*End of create_list()*/
tmp->info=data;
last=tmp;
last->link=last;
return last;
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
return last;
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
last=tmp;
return last;
p=last->link;
do
if(p->info==item)
tmp->info=data;
tmp->link=p->link;
p->link=tmp;
if(p==last)
last=tmp;
return last;
p=p->link;
}while(p!=last->link);
return last;
}/*End of addafter()*/
if(last==NULL)
printf("List is empty\n");
return last;
tmp=last;
last=NULL;
free(tmp);
return last;
if(last->link->info==data)
tmp=last->link;
last->link=tmp->link;
free(tmp);
return last;
/*Deletion in between*/
p=last->link;
while(p->link!=last)
if(p->link->info==data)
{
tmp=p->link;
p->link=tmp->link;
free(tmp);
return last;
p=p->link;
if(last->info==data)
tmp=last;
p->link=last->link;
last=p;
free(tmp);
return last;
return last;
if(last==NULL)
printf("\nList is empty\n");
return;
p=last->link;
do
{
printf("%d ",p->info);
p=p->link;
}while(p!=last->link);
printf("\n");