0% found this document useful (0 votes)
15 views12 pages

Dsa Assignment

The document contains multiple C programs demonstrating basic data structure operations including inserting and deleting elements in an array, multiplying matrices using pointers, finding the transpose of a matrix, searching for an element in an array, and implementing a singly linked list with various operations. Each program includes user input prompts and outputs the results of the operations performed. The document serves as a comprehensive guide for fundamental programming concepts related to arrays and linked lists.

Uploaded by

Shivam Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Dsa Assignment

The document contains multiple C programs demonstrating basic data structure operations including inserting and deleting elements in an array, multiplying matrices using pointers, finding the transpose of a matrix, searching for an element in an array, and implementing a singly linked list with various operations. Each program includes user input prompts and outputs the results of the operations performed. The document serves as a comprehensive guide for fundamental programming concepts related to arrays and linked lists.

Uploaded by

Shivam Rathore
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1.

​ Write a program to insert an element at specific position in array

Program -
#include <stdio.h>
int main() {
int arr[100];
int n, i, pos, element;
printf("Enter number of elements: ");
scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to insert: ");


scanf("%d", &element);

printf("Enter the position to insert at (0-based index): ");


scanf("%d", &pos);

if (pos < 0 || pos > n) {


printf("Invalid position!\n");
return 1;
}

// Shift elements right from the end to the pos


for (i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}

arr[pos] = element;
n++; // updated size
printf("Array after insertion:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}
Output -
enter the size of array : 5
enter the position between 0 and 4 only : 4
enter the value : 34
value 34 inserted at 4 index successfully
2.​ Write a program to delete element at specific position in array
program -
#include<stdio.h>
void main() {
int arr[100];
int n;
printf("enter the size of array : ");
scanf("%d",&n);

printf("enter the elmenets of array : ");


for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}

int pos;
do {
printf("enter the position of element you want to delete
between 0 and %d only : ",n-1);
scanf("%d",&pos);
}while(pos<0 || pos>n-1);

for(int i= pos;i<n-1;i++){
arr[i] =arr[i+1];
}
n--;
printf("element at position %d deleted successfully\n",pos);

printf("updated elements of array are : ");


for(int i=0;i<n;i++) {
printf(" %d",arr[i]);
}

output-

enter the size of array : 5


enter the elmenets of array : 1
2
3
4
5
enter the position of element you want to delete between 0 and 4
only : 2
element at position 2 deleted successfully
updated elements of array are : 1 2 4 5
3.​Multiply two matrix using pointer
Program-

#include<stdio.h>

void main() {
int matrix1[100][100];
int matrix2[100][100];
int matrix3[100][100];
int r,c;
int *p1 = &matrix1[0][0];
int *p2 = &matrix2[0][0];
int *p3 = &matrix3[0][0];

printf("enter the size of row and column of both matrix : ");


scanf("%d%d",&r,&c);

printf("enter the elements of matrix1 : ");


for(int i=0;i<r;i++){
for(int j=0;j<c;j++) {
scanf("%d",&matrix1[i][j]);
}
}

printf("enter the elements of matrix2 : ");


for(int i=0;i<r;i++){
for(int j=0;j<c;j++) {
scanf("%d",&matrix2[i][j]);
}
}

//multiply
for(int i=0;i<r;i++){
for(int j=0;j<c;j++) {
*(p3 + i * 100 + j) = (*(p1 + i * 100 + j)) * (*(p2 +
i * 100 + j));
}
}

printf("elements of matrix3 after multipication are : ");


for(int i=0;i<r;i++){
for(int j=0;j<c;j++) {
printf(" %d",matrix3[i][j]);
}
}
}

Output -
enter the size of row and column of both matrix : 2
2
enter the elements of matrix1 : 1
2
3
4
enter the elements of matrix2 : 1
2
3
4
elements of matrix3 after multipication are : 1 4 9 16

4.​Find the transpose of given array


Program -

#include <stdio.h>

int main() {
int matrix[100][100], transpose[100][100];
int r, c;

printf("Enter number of rows and columns: ");


scanf("%d%d", &r, &c);

printf("Enter elements of the matrix:\n");


for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
scanf("%d", &matrix[i][j]);

// Transposing the matrix


for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++)
transpose[j][i] = matrix[i][j];

printf("Transpose of the matrix is:\n");


for (int i = 0; i < c; i++) {
for (int j = 0; j < r; j++)
printf("%d ", transpose[i][j]);
printf("\n");
}

return 0;
}

Output -
Enter number of rows and columns: 2
2
Enter elements of the matrix:
1
2
3
4
Transpose of the matrix is:
1 3
2 4

5.​Write a program to search an element in given array


Program -
#include <stdio.h>

int main() {
int arr[100], n, key, found = 0;

printf("Enter the size of the array: ");


scanf("%d", &n);

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter the element to search: ");


scanf("%d", &key);

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


if (arr[i] == key) {
printf("Element %d found at index %d (position
%d)\n", key, i, i + 1);
found = 1;
break; // remove this line if you want to find all
occurrences
}
}

if (!found) {
printf("Element %d not found in the array.\n", key);
}

return 0;
}

Output -
Enter the size of the array: 5
Enter 5 elements:
1
2
3
4
5
Enter the element to search: 3
Element 3 found at index 2 (position 3)

6.​Write a program for singly linked list


Program -

# include <stdio.h>
# include <stdlib.h>

struct link
{
​ int info;
​ struct link *next;
};

struct link *start, *new1;

void insertion_at_begin();
void insertion_at_end();
void insertion_at_position();
void delete_by_position();
void delete_by_value();
void search();
void reverse();
void traverse ();
void short_ascending();

/* Function main */
void main()
{
​ int choice;
​ char ch;

​ start = NULL; /* Empty list */


​ traverse();

​ do
​ {
​ ​ printf("\n\nInsert\n1 for insert_at_beginning\n2 for
insert_at_end\n3 for insert_at_position\n4 delete_by_position\n5
delete_by_value\n6 search\n7 reverse\n8 for short_ascending");

​ ​ printf("\n Insert choice: ");


​ ​ scanf("%d", &choice);
​ ​ getchar();

​ ​ switch(choice)
​ ​ {
​ ​ ​ case 1:
​ ​ ​ ​ insertion_at_begin ();
​ ​ ​ ​ traverse ();
​ ​ ​ ​ break;

​ ​ ​ case 2:
​ ​ ​ ​ insertion_at_end ();
​ ​ ​ ​ traverse ();
​ ​ ​ ​ break;

​ ​ ​ case 3:
​ ​ ​ ​ insertion_at_position();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ case 4:
​ ​ ​ ​ delete_by_position();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ case 5:
​ ​ ​ ​ delete_by_value();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ case 6:
​ ​ ​ ​ search();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ case 7:
​ ​ ​ ​ reverse();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ case 8 :
​ ​ ​ ​ short_ascending();
​ ​ ​ ​ traverse();
​ ​ ​ ​ break;

​ ​ ​ default:
​ ​ ​ ​ exit(1);
​ ​ }
​ ​ printf("\n\n Do you want to insert an another choice?
");
​ ​ printf("\n Insert y for yes or n for no. ");
​ ​ //fflush(stdin);
​ ​ scanf("%c",&ch);
​ }while(ch == 'y' || ch == 'Y');
}
/* Function to insert a node at the beginning of SLL */
void insertion_at_begin()
{
​ new1 = (struct link* ) malloc(sizeof(struct link));
​ printf("\n Input the node value: ");
​ scanf("%d", &new1->info);
​ getchar();

​ new1->next = start ;
​ start = new1;
}

/* Function to insert a node at the end of SLL */


void insertion_at_end()
{
​ struct link *temp=start; /* Point to the first node
of the list */

​ new1 = (struct link* ) malloc(sizeof(struct link));

​ printf("\n Input the node value: ");


​ scanf("%d", &new1->info);
​ getchar();

​ if(start == NULL)
​ {
​ ​ new1->next = start ;
​ ​ start = new1;
​ }
​ else
​ {
​ ​ while (temp->next != NULL) { // To reach before the
desired position
​ ​ ​ temp = temp->next;
​ ​ }

​ ​ new1->next = temp->next ;
​ ​ temp->next = new1;
​ }
}

/* Function to traverse the SLL */


void traverse()
{
​ struct link *temp=start; /* Point to the first node of
the list */

​ if(start == NULL)
​ ​ printf("\n Empty linked list");
​ else
​ {
​ ​ printf("\n List is as follows:\n");
​ ​ while (temp)
​ ​ {
​ ​ ​ printf(" %d", temp->info);
​ ​ ​ temp = temp->next;
​ ​ }
​ }
}

void insertion_at_position() {
​ int pos=0;
​ int count=0;
​ struct link *temp =start;

​ while(temp) {
​ ​ temp=temp->next;
​ ​ count++;
​ }

​ if(count<2) {
​ ​ printf("there should be atleast 2 nodes to use
insertion_at_choice");
​ }else{
​ ​ printf("total node exist : %d\n",count);
​ ​ while(pos<2||pos>count) {
​ ​ ​ printf("enter the position only betweem 2 and
%d: ",count);
​ ​ ​ scanf("%d",&pos);
​ ​ }
​ new1=(struct link*)malloc(sizeof(struct link));
​ if(new1==NULL) {
​ ​ printf("memory allocation failed!");
​ ​ ​ exit(0);
​ ​ }
​ printf("enter value of new node : ");
​ scanf("%d",&new1->info);
​ getchar();

​ ​ temp=start;
​ ​ for(int i=1;i<pos-1;i++) {
​ ​ ​ temp =temp->next;
​ ​ }
​ new1->next=temp->next;
​ temp->next=new1;
​ }

void delete_by_position() {
​ int pos=0;
​ int count=0;
​ struct link *temp=start;
​ struct link *prev_temp;
​ while(temp) {
​ ​ temp=temp->next;
​ ​ count++;
​ }

​ if(count==0) {
​ ​ printf("no node exist! first create one ");

​ }else if(count==1) {
​ ​ free(start);
​ ​ printf("one node got deleted");

​ }else {

​ ​ printf("total node : %d\n",count);

​ ​ do{
​ ​ ​ printf("enter the position of node to be
deleted(only between 1 and %d) : ",count);
​ ​ scanf("%d",&pos);
​ ​ getchar();
​ ​ }while(pos<1 || pos>count);

​ ​ temp=start;
​ ​ for(int i=1;i<pos;i++) {
​ ​ ​ if(i==pos-1) {
​ ​ ​ ​ prev_temp=temp;
​ ​ ​ }
​ ​ ​ temp=temp->next;
​ ​ }

​ ​ if(pos==1) {
​ ​ ​ start=temp->next;
​ ​ ​ free(temp);
​ ​ }else {
​ ​ ​ prev_temp->next=temp->next;
​ ​ free(temp);
​ ​ }
​ ​

​ }
}

void delete_by_value() {
​ int value;
​ int found=0;
​ struct link *temp = start;
​ struct link *prev_temp;
​ if(temp==NULL) {
​ ​ printf("no node exist");
​ }else {

​ ​ printf("enter the value of node to be deleted : ");


​ ​ scanf("%d",&value);
​ ​ getchar();

​ ​ if(value==temp->info) { //for first node


​ ​ found=1;
​ ​ ​ start = temp->next;
​ ​ ​ free(temp);
​ ​ ​ printf("node deleted");

​ ​ }else if(temp->next!=NULL ){
​ ​ ​ while(temp) { //for other nodes
​ ​ ​ ​ prev_temp=temp;
​ ​ ​ ​ temp=temp->next;
​ ​ ​ ​ if(temp!=NULL && value==temp->info) {
​ ​ ​ ​ ​ found=1;
​ ​ ​ ​ ​ prev_temp->next=temp->next;
​ ​ ​ ​ ​ free(temp);
​ ​ ​ ​ ​ printf("node deleted");
​ ​ ​ ​ ​ break;
​ ​ ​ ​ }
​ ​ }
​ ​ }
​ ​ if(found ==0) {
​ ​ ​ printf("value %d doesn't exist in list",value);
​ ​ }

​ ​
​ }
}

void search() {
​ int value;
​ struct link *temp=start;

​ if(temp==NULL) {
​ ​ printf("no node exist");
​ }else {
​ ​ printf("enter the value for searching : ");
​ ​ scanf("%d",&value);
​ ​ getchar();
​ ​
​ ​ while(temp) {
​ ​ if(value==temp->info) {
​ ​ ​ printf("ys value %d exist",value);
​ ​ ​ break;
​ ​ }
​ ​ temp=temp->next;
​ ​ if(temp==NULL) {
​ ​ ​ printf("value %d does not exist\n",value);
​ ​ }
​ }
​ }

void reverse() {
​ struct link *past=NULL;
​ struct link *curr=start;
​ struct link *next=NULL;

​ if(curr==NULL) {
​ ​ printf("no linked list exist\n");
​ }else {
​ ​ while(curr) {
​ ​ ​ next = curr->next;
​ ​ ​ curr->next=past;
​ ​ ​ past=curr;
​ ​ ​ curr=next;
​ ​ }
​ ​ start = past;
​ }

​ printf("reverse the list completed");


}

void short_ascending() {
​ if(start== NULL) {
​ ​ printf("no linked list");
​ }else{
​ ​ struct link *temp = start;
​ ​ struct link *temp1;
​ ​ int store;
​ ​ while(temp!=NULL) {
​ ​ ​ temp1= start;
​ ​ ​ while(temp1!= NULL) {
​ ​ ​ ​ if(temp->info<temp1->info) {
​ ​ ​ ​ ​ store = temp->info;
​ ​ ​ ​ ​ temp->info= temp1->info;
​ ​ ​ ​ ​ temp1->info= store;
​ ​ ​ ​ }
​ ​ ​ ​ temp1= temp1->next;
​ ​ ​ }
​ ​ ​ temp = temp->next;
​ ​ }
​ }
}

You might also like