Group A Solve
Group A Solve
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main() {
int arr[100],j,n;
int size = sizeof(arr) / sizeof(arr[0]);
int elementToFind, i;
FILE *file;
clrscr();
printf("Enter the number of element of the Array:");
scanf("%d",&n);
printf("Enter the elements:");
for(j=0;j<n;j++)
scanf("%d",&arr[j]);
int found = 0;
for (i = 0; i < size; i++) {
int num;
fscanf(file, "%d", &num);
if (num == elementToFind) {
printf("Element found at index %d.\n", i);
found = 1;
break;
}
}
if (!found) {
printf("Element not found.\n");
}
fclose(file);
getch();
return 0;
}
Qu 2:
#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} node;
node* createNode(int data)
{
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int insertBeforeHead(node** head, int data)
{
node* newNode = createNode(data);
if (!newNode)
return -1;
if (*head == NULL) {
*head = newNode;
return 0;
}
newNode->next = *head;
*head = newNode;
return 0;
}
int deleteHead(node** head)
{
node* temp = *head;
*head = (*head)->next;
free(temp);
return 0;
}
int isEmpty(node** stack) { return *stack == NULL; }
void push(node** stack, int data)
{
if (insertBeforeHead(stack, data)) {
printf("Stack Overflow!\n");
}
}
int pop(node** stack)
{
if (isEmpty(stack)) {
printf("Stack Underflow\n");
return -1;
}
deleteHead(stack);
}
int peek(node** stack)
{
if (!isEmpty(stack))
return (*stack)->data;
else
return -1;
}
void printStack(node** stack)
{
node* temp = *stack;
while (temp != NULL) {
printf("%d-> ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main()
{
clrscr();
node* stack = NULL;
push(&stack, 65);
push(&stack, 79);
push(&stack, 85);
push(&stack, 92);
push(&stack, 45);
printf("Stack: ");
printStack(&stack);
pop(&stack);
pop(&stack);
printf("\nStack: ");
printStack(&stack);
getch();
return 0;
}
Qu 3:
#include <stdio.h>
#include<stdlib.h>
#include <conio.h>
typedef struct Node {
int data;
struct Node* next;
} node;
int main()
{ int ele,i;
clrscr();
queue* q = createQueue();
for(i=1;i<=10;i++)
{
printf("Enter element:");
scanf("%d",&ele);
enqueue(q, ele);
}
printf("Queue: ");
printQueue(q);
struct Linklist
{
int info;
struct Linklist *link;
};
void create(int);
void display();
void del_last();
void main()
{
int i,n;
clrscr();
for(i=1;i<=5;i++)
{ printf("Enter item=");
scanf("%d",&n);
create(n);
}
del_last();
printf("\nThe list is(after delete)=");
display();
getch();
}
void create(int n)
{
cur=(struct Linklist *)malloc(sizeof(struct Linklist *));
cur->info=n;
cur->link=NULL;
if(start==NULL)
{
start=cur;
ptr=cur;
}
else
{
ptr ->link =cur;
ptr=ptr->link;
}
}
void display()
{
ptr=start;
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr=ptr->link;
}
void del_last()
{
ptr=start;
prev=NULL;
while(ptr->link!=NULL)
{
prev=ptr;
ptr=ptr->link;
}
prev->link=NULL;
printf("\n Deleted node=%d",ptr->info);
free(ptr);
}