0% found this document useful (0 votes)
132 views7 pages

DSA Lab 7 - Saurabh - Saini - 2005690 PDF

The document contains 3 questions related to data structures and algorithms: 1. The first question provides code to recursively reverse a singly linked list by changing the next pointers. 2. The second question asks to write code to delete nodes from a doubly linked list where the node data is greater than all previous nodes and less than all next nodes. 3. The third question asks to write code to replace 0 elements in a 2D matrix with 1 if all surrounding elements are 1.

Uploaded by

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

DSA Lab 7 - Saurabh - Saini - 2005690 PDF

The document contains 3 questions related to data structures and algorithms: 1. The first question provides code to recursively reverse a singly linked list by changing the next pointers. 2. The second question asks to write code to delete nodes from a doubly linked list where the node data is greater than all previous nodes and less than all next nodes. 3. The third question asks to write code to replace 0 elements in a 2D matrix with 1 if all surrounding elements are 1.

Uploaded by

uinsta a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME- SAURABH SAINI

ROLL NO.-2005690
BRANCH - CSE

Q1. Reverse a singly linked list using recursion.

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

struct Node {
int data;
struct Node *next;
}*head;
void createList(int n);
void reverseList();
void displayList();

void createList(int n)
{
struct Node *newNode, *temp;
int data, i;

if(n <= 0)
{
printf("List size must be greater than zero.\n");
return;
}

head = (struct Node *)malloc(sizeof(struct Node));


if(head == NULL)
{
printf("Unable to allocate memory.");
}
else
{
printf("Enter the data of Node 1: ");
scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct Node *)malloc(sizeof(struct Node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
else
{
printf("Enter the data of Node %d: ", i);
scanf("%d", &data);

newNode->data = data;
newNode->next = NULL;

temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
}
}

void recursiveReverse(struct Node* head, struct Node** headRef)


{
struct Node* first;
struct Node* rest;
if (head == NULL) {
return;
}

first = head;
rest = first->next;
if (rest == NULL)
{
*headRef = first;
return;
}
recursiveReverse(rest, headRef);
rest->next = first;
first->next = NULL;
}
void reverse(struct Node** head) {
recursiveReverse(*head, head);
}

void displayList()
{
struct Node *temp;
if(head == NULL)
{
printf("List is empty.");
}
else
{
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
}
}

int main()
{
int n, choice;
printf("Enter the total number of Nodes: ");
scanf("%d", &n);
createList(n);

printf("\nData in the list \n");


displayList();
reverse(&head);

printf("\nData in the list\n");


displayList();

return 0;
}
Q2. Write the code to delete all the nodes in a doubly linked list, where the
data element of the node is greater than data element of all its previous nodes
and is less than data element of all the next nodes.

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void deleteNode(struct Node** head_ref, struct Node* del)
{
if (*head_ref == NULL || del == NULL)
return;
if (*head_ref == del)
*head_ref = del->next;
if (del->next != NULL)
del->next->prev = del->prev;
if (del->prev != NULL)
del->prev->next = del->next;
free(del);
return;
}
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->prev = NULL;
new_node->next = (*head_ref);
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
(*head_ref) = new_node;
}
void printList(struct Node* node)
{
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
void deleteGreaterNodes(struct Node** head_ref, int x)
{
struct Node* ptr = *head_ref;
struct Node* next;
while (ptr != NULL) {
next = ptr->next;
if (ptr->data > x)
deleteNode(head_ref, ptr);
ptr = next;
}
}
void deleteLesserNodes(struct Node** head_ref, int x)
{
struct Node* ptr = *head_ref;
struct Node* next;
while (ptr != NULL) {
next = ptr->next;
if (ptr->data < x)
deleteNode(head_ref, ptr);
ptr = next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 2);
push(&head, 4);
push(&head, 8);
push(&head, 10);
push(&head, 12);
push(&head, 14);
push(&head, 16);
push(&head, 18);
printf("\n Original Linked list ");
printList(head);
int x = 12;
deleteGreaterNodes(&head, x);
printf("\n Modified Linked list - Greater ");
printList(head);
struct Node* head2 = NULL;
push(&head2, 2);
push(&head2, 4);
push(&head2, 8);
push(&head2, 10);
push(&head2, 12);
push(&head2, 14);
push(&head2, 16);
push(&head2, 18);
deleteLesserNodes(&head2, x);
printf("\n Modified Linked list - Lesser ");
printList(head2);
getchar();
}
Q3. Let a matrix of size m X n contains elements either 0 or 1. Write a code to
replace an element 0 with 1, if all its surrounding elements are 1.

#include<stdio.h>
main()
{
int row, column,i,j;
printf("Enter number of rows: ");scanf("%d",&row);
printf("Enter number of columns: ");scanf("%d",&column);
int a[row][column];
printf("Enter Elements: (only 0's and 1's)\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nORIGINAL MATRIX\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=1;i<row-1;i++)
{
for(j=1;j<column-1;j++)
{
if(a[i][j]==0)
{
if(a[i][j-1]==1 && a[i][j+1]==1 && a[i-1][j]==1 && a[i+1][j]==1)
a[i][j]=1;
}
}
}
printf("\nOUTPUT MATRIX\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}

You might also like