0% found this document useful (0 votes)
2 views6 pages

Java Questions Solution

programs for java

Uploaded by

shwetaadixit25
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)
2 views6 pages

Java Questions Solution

programs for java

Uploaded by

shwetaadixit25
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/ 6

1.

WAP A PROGRAM TO PRINT NTH NODE IN FROM GIVEN NODES IN


SINGULAR LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

void addNode(struct Node** head, int data) {


struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

void printNthNodeFromGivenNode(struct Node* givenNode, int n) {


struct Node* current = givenNode;
int count = 0;

while (current != NULL && count < n) {


current = current->next;
count++;
}

if (current != NULL) {
printf("The %dth node from the given node is: %d\n", n, current->data);
} else {
printf("The given node doesn't have %d nodes ahead.\n", n);
}
}

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

addNode(&head, 10);
addNode(&head, 20);
addNode(&head, 30);
addNode(&head, 40);
addNode(&head, 50);
addNode(&head, 60);
addNode(&head, 70);

printNthNodeFromGivenNode(head, 6);

return 0;
}

2 .Wap to delete node with given value in single linked list


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

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

struct Node* createNode(int data) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void addNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}

void deleteNode(struct Node** head, int value) {


struct Node* current = *head;
struct Node* previous = NULL;
if (current != NULL && current->data == value) {
*head = current->next;
free(current);
return;
}
while (current != NULL && current->data != value) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Value %d not found in the list.\n", value);
return;
}
previous->next = current->next;

free(current);
}
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
addNode(&head, 10);
addNode(&head, 20);
addNode(&head, 30);
addNode(&head, 40);
addNode(&head, 50);
addNode(&head, 60);
addNode(&head, 70);
printf("Original list: ");
printList(head);
deleteNode(&head, 30);

printf("List after deleting node with value 30: ");


printList(head);

return 0;
}

3. Wap to take a character from user and print its pattern using *
#include <stdio.h>

void printA() {
printf(" * \n");
printf(" * * \n");
printf("*****\n");
printf("* *\n");
printf("* *\n");
}

void printB() {
printf("**** \n");
printf("* *\n");
printf("**** \n");
printf("* *\n");
printf("**** \n");
}

void printC() {
printf(" ****\n");
printf("* \n");
printf("* \n");
printf("* \n");
printf(" ****\n");
}
void printD() {
printf("**** \n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("**** \n");
}
void printE() {
printf("*****\n");
printf("* \n");
printf("*****\n");
printf("* \n");
printf("*****\n");
}
void printPattern(char ch) {
switch (ch) {
case 'A':
printA();
break;
case 'B':
printB();
break;
case 'C':
printC();
break;
case 'D':
printD();
break;
case 'E':
printE();
break;
default:
printf("Pattern not available for the given character.\n");
}
}

int main() {
char ch;

printf("Enter a character (A-E): ");


scanf("%c", &ch);
if (ch >= 'a' && ch <= 'e') {
ch = ch - 'a' + 'A';
}

printPattern(ch);

return 0;
}

You might also like