0% found this document useful (0 votes)
22 views31 pages

DS Practicels

The document discusses programs written to perform various operations on arrays and linked lists. It contains 5 questions related to arrays and their operations and 4 questions related to linked lists and their operations. For each question, it provides the code written to solve it and the corresponding output.

Uploaded by

ayesha sheikh
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)
22 views31 pages

DS Practicels

The document discusses programs written to perform various operations on arrays and linked lists. It contains 5 questions related to arrays and their operations and 4 questions related to linked lists and their operations. For each question, it provides the code written to solve it and the corresponding output.

Uploaded by

ayesha sheikh
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
You are on page 1/ 31

DATA STRUCTURES LAB-1

Date: /08/23

Q.1) WAP to display 1-D array of 10 no.


// Divya Mathankar
#include <stdio.h>
int main() {
int numbers[10];
printf("You have to Enter 10 integers \n");
// Taking input from the user and storing it in the array
for (int i = 0; i < 10; ++i) {
printf("Enter: ");
scanf("%d", &numbers[i]);
}
printf("Displaying integers: ");
// Printing the elements of the array
for (int i = 0; i < 10; ++i) {
printf("%d ", numbers[i]);
}
return 0;
}

OUTPUT:-
You have to Enter 10 integers
Enter: 2
Enter: 5
Enter: 6
Enter: 8
Enter: 5
Enter: 3
Enter: 1
Enter: 5
Enter: 6
Enter: 9
Displaying integers: 2 5 6 8 5 1 5 6 9

Q.2) WAP to show an array displaying your name


in reverse order.
// Divya Mathankar
#include <stdio.h>
#include <string.h>
int main() {
char name[] = "Divya Mathankar";
int length = strlen(name);
printf("Original array: %s\n", name);
printf("Array in reverse order: ");
for (int i = length - 1; i >= 0; --i) {
printf("%c", name[i]);
}
return 0;
}

OUTPUT:-
Original array: Divya Mathankar
Array in reverse order: raknahtaM ayviD

Q.3) WAP to insert a no. at a specific position in a


given array.
// Divya Mathankar
#include <stdio.h>

int main()
{
int array[50], position, c, n, value;
printf("Enter number of elements in the array\n");
scanf("%d", &n);

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

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


scanf("%d", &array[c]);

printf("Please enter the location where you want to insert an new


element\n");
scanf("%d", &position);

printf("Please enter the value\n");


scanf("%d", &value);

for (c = n - 1; c >= position - 1; c--)


array[c+1] = array[c];

array[position-1] = value;

printf("Resultant array is\n");

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


printf("%d\n", array[c]);

return 0;
}

OUTPUT:-
Enter number of elements in the array
5
Enter 5 elements
6
7
8
9
10
Please enter the location where you want to insert an new element
3
Please enter the value
5
Resultant array is
6
7
5
8
9
10

Q.4) WAP to showing deletion of an element at a


specific position in a given array.
// Divya Mathankar
#include <stdio.h>
#include <conio.h>

int main ()
{
int arr[50];
int pos, i, num;
printf (" \n Enter the number of elements in an array: \n ");
scanf (" %d", &num);

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

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


{ printf (" arr[%d] = ", i);
scanf (" %d", &arr[i]);
}
printf( " Define the position of the array element where you want to delete:
\n ");
scanf (" %d", &pos);

if (pos >= num+1)


{
printf (" \n Deletion is not possible in the array.");
}
else
{
for (i = pos - 1; i < num -1; i++)
{
arr[i] = arr[i+1];
}

printf (" \n The resultant array is: \n");

for (i = 0; i< num - 1; i++)


{
printf (" arr[%d] = ", i);
printf (" %d \n", arr[i]);
}
}
return 0;
}
OUTPUT:-
Enter 5 elements in array:
arr[0] = 3
arr[1] = 4
arr[2] = 5
arr[3] = 6
arr[4] = 7
Define the position of the array element where you want to delete:
3

The resultant array is:


arr[0] = 3
arr[1] = 4
arr[2] = 6
arr[3] = 7

Q.5) WAP to display an array of 5 integers & also


display the sum of elements of the array.
// Divya Mathankar
#include<stdio.h>

int main()
{

int arr[100], size, i, sum = 0;


printf("Enter array size\n");
scanf("%d",&size);

printf("Enter array elements\n");


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

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


sum = sum + arr[i];

printf("Sum of the array = %d\n",sum);

return 0;
}

OUTPUT:-

Enter array size


3
Enter array elements
5
6
Sum of the array = 19
DATA STRUCTURES LAB-2
Date: /08/23

Q.1. WAP to print the Fibonacci series of the first


15 elements.
Q.2. WAP to print a matrix of order 3x3
Q.3. WAP to add two matrix of order 2x2 each and
print the result as
Q.4. WAP to print transpose of a given matrix of
order 3x3.
PROGRAMS:-

#include <stdio.h>
int main()
{// WAP to print the Fibonacci series of the first 15 elements.
int a = 0, b = 1, c;
printf(" Q1. by Deepanshi\n");
printf("The first 15 elements of the Fibonacci series are:\n");
printf("%d %d ", a, b);
for (int i = 0; i < 13; i++)
{
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
printf("\n");

// WAP to print a matrix of order 3x3.


int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Q2. by Divya Mathankar\n");
printf("The 3x3 matrix is:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// WAP to add two matrix of order 2x2 each and print the result as sum.
int matrix1[2][2] = {{1, 2}, {3, 4}};
int matrix2[2][2] = {{5, 6}, {7, 8}};
int sum[2][2];
// Add the two matrices
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
printf("Q3. by Divya Mathankar\n");
// Print the sum matrix
printf("The sum of the two matrices is:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", sum[i][j]);
}
printf("\n");
}
// WAP to print transpose of a given matrix of order 3x3.
int transpose[3][3];
printf("Q4. by Divya Mathankar\n");
// Calculate the transpose matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Print the transpose matrix
printf("The transpose of the given matrix is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
return 0;
}

OUTPUT:-
Q1. by Divya Mathankar
The first 15 elements of the Fibonacci series are:
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Q2. by Divya Mathankar
The 3x3 matrix is:
123
456
789
Q3. by Divya Mathankar
The sum of the two matrices is:
68
10 12
Q4. by Divya Mathankar
The transpose of the given matrix is:
147
258
369
DATA STRUCTURES LAB-3
DATE: /09/23

Q1)WAP to implement a linked list of 5 elements


and also display the entered elements:
// Divya Mathankar
#include <stdio.h>
#include <stdlib.h>
//Represent a node of singly linked list |
struct node{
int data;
struct node *next;
};

//Represent the head and tail of the singly linked list


struct node *head, *tail = NULL;

//addNode() will add a new node to the list


void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;

//Checks if the list is empty


if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}

//display() will display all the nodes present in the list


void display() {
//Node current will point to head
struct node *current = head;

if(head == NULL) {
printf("List is empty\n");
return;
}
printf("Nodes of singly linked list: \n");
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}

int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);

//Displays the nodes present in the list


display();
printf(" by Divya Mathankar\n");
return 0;
}
OUTPUT:-
Nodes of singly linked list:
1234
by Divya Mathankar

Q.2) WAP to insert an element at the beginning of a


linked list:
// Divya Mathankar
#include<stdio.h>
#include<stdlib.h>
void beginsert(int);
struct node
{
int data;
struct node *next;
};
struct node *head;
void main ()
{
int choice,item;
do
{ printf("by Divya Mathankar");
printf("\nEnter the item which you want to insert?\n");
scanf("%d",&item);
beginsert(item);
printf("\nPress 0 to insert more ?\n");
scanf("%d",&choice);
}while(choice == 0);
}
void beginsert(int item)
{
struct node *ptr = (struct node *)malloc(sizeof(struct node *));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted\n");

OUTPUT:-
by Divya Mathankar
Enter the item which you want to insert?
5

Node inserted

Press 0 to insert more ?


Q3)Wap to delete an element from beginning of the
linked list :
// By Divya Mathankar
#include <stdio.h>
#include <stdlib.h>

// Represent a node of the linked list |


struct node {
int data;
struct node* next;
};

// Function to delete the first node of the linked list


void deleteFromBeginning(struct node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}

struct node* temp = *head;


*head = (*head)->next;
free(temp);
printf("Node deleted from the beginning\n");
}

// Function to display all the nodes in the linked list


void display(struct node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}

printf("Nodes of the linked list: ");


while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("\n");
}

int main() {
struct node* head = NULL;

// Create a linked list with 5 elements


for (int i = 5; i >= 1; i--) {
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = i;
newNode->next = head;
head = newNode;
}

// Display the initial linked list


display(head);

// Delete the first node


deleteFromBeginning(&head);

// Display the updated linked list


display(head);

return 0;
}

OUTPUT:-
Nodes of the linked list: 1 2 3 4 5
Node deleted from the beginning
Nodes of the linked list: 2 3 4 5
DATA STRUCTURES LAB-4
DATE: /09/23

Q.1)WAP to implement a stack of 10 elements . also


show the push, pop,traverse and peak operations.
// Divya Mathankar
#include <stdio.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1;
int isEmpty() { // Function to check if the stack is empty
return top == -1;
}
int isFull() { // Function to check if the stack is full
return top == MAX_SIZE - 1;
}
void push(int data) { // Function to push an element onto the stack
if (isFull()) {
printf("Stack Overflow\n");
return;
}
stack[++top] = data;
printf("Element %d pushed onto the stack\n", data);
}
// Function to pop an element from the stack
int pop() {
if (isEmpty()) {
printf("Stack Underflow\n");
return -1;
}
int data = stack[top--];
printf("Element %d popped from the stack\n", data);
return data;
}
// Function to traverse and display the elements of the stack
void traverse() {
if (isEmpty()) {
printf("Stack is empty\n");
return;
}
printf("Elements of the stack: ");
for (int i = top; i >= 0; i--) {
printf("%d ", stack[i]);
}
printf("\n");
}
// Function to get the topmost element of the stack
int peek() {
if (isEmpty()) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
int main() {
push(1);
push(2);
push(3);
traverse();
int element = pop();
printf("Popped element: %d\n", element);
traverse();
int topElement = peek();
printf("Top element: %d\n", topElement);
return 0;
}

OUTPUT:-
Element 1 pushed onto the stack
Element 2 pushed onto the stack
Element 3 pushed onto the stack
Elements of the stack: 3 2 1
Element 3 popped from the stack
Popped element: 3
Elements of the stack: 2 1

Top element: 2
Q.2)WAP to print your name in reverse order using
pop operation of stack.
// Divya Mathankar
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct { // Structure to represent a stack
char data[MAX_SIZE];
int top;
} Stack;
// Function to initialize the stack
void initialize(Stack *stack) {
stack->top = -1;
}
// Function to push an element onto the stack
void push(Stack *stack, char element) {
if (stack->top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
stack->data[++stack->top] = element;
}
// Function to pop an element from the stack
char pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack Underflow\n");
return '\0';
}
return stack->data[stack->top--];
}
// Function to print a string in reverse order using a stack
void printReverseName(char *name) {
Stack stack;
initialize(&stack);
// Push each character of the name onto the stack
int length = strlen(name);
for (int i = 0; i < length; i++) {
push(&stack, name[i]);
}
// Pop and print each character from the stack
printf("Reversed name: ");
while (stack.top != -1) {
printf("%c", pop(&stack));
}
printf("\n");
}
int main() {
char name[100];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
// Remove the newline character from the input
name[strcspn(name, "\n")] = '\0';
printReverseName(name);
return 0;
}

OUTPUT:-
Enter your name: Divya Mathankar
Reversed name: raknahtaM ayviD
DATA STRUCTURES LAB-5
Date: /09/23

Q.1) WAP in c to create a queue of 5 elements using


arrays and write code for enqueue , dequeue ,
traverse operations.
// Divya Mathankar
#include <stdio.h>
int queue[5];
int front = -1;
int rear = -1;
void enqueue(int x){
if (rear == 4){
printf("queue is full");
}
else if (front == -1 && rear == -1){
front = rear = 0;
queue[rear] = x;
}
else{
rear++;
queue[rear] = x;
}
}
int dequeue(int queue[], int front, int rear){
int element = queue[0];

if (front == rear){
printf("Queue is empty\n");
return -1;
}

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


queue[i] = queue[i + 1];
}
queue[rear] = 0;

if (rear==1){
rear--; front--;
} else {
rear--;
}
return element;
}
void display_queue(int queue[], int front, int rear){
if (front == rear){
printf("Queue is empty \n");
return;
}
for (int i = front; i <= rear; i++){
printf("%d \n", queue[i]);
}

printf("\n");
}
int main(){
enqueue(7);
enqueue(8);
enqueue(9);
enqueue(6);
display_queue(queue, front, rear);
// Dequeue the front element.
int element = dequeue(queue, front, rear);
if (element != -1){
printf("Element dequeued: %d\n", element);
}
display_queue(queue, front, rear);
printf("by Divya Mathankar");
return 0;
}

OUTPUT:-
7
8

Element dequeued: 7

by Divya Mathankar

You might also like