Qu 1:
#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]);
file = fopen("array.txt", "w");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
for (i = 0; i < size; i++) {
fprintf(file, "%d ", arr[i]);
}
fclose(file);
file = fopen("array.txt", "r");
if (file == NULL) {
printf("Error opening file.\n");
return 1;
}
printf("Enter element to find: ");
scanf("%d", &elementToFind);
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;
typedef struct Queue {
node* front;
node* rear;
} queue;
node* createNode(int data)
{
node* newNode = (node*)malloc(sizeof(node));
if (newNode == NULL)
return NULL;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
queue* createQueue()
{
queue* newQueue = (queue*)malloc(sizeof(queue));
newQueue->front = newQueue->rear = NULL;
return newQueue;
}
int isEmpty(queue* q)
{
return q->front == NULL;
}
void enqueue(queue* q, int data)
{
node* newNode = createNode(data);
if (!newNode) {
printf("Queue Overflow!\n");
return;
}
if (q->rear == NULL) {
q->front = q->rear = newNode;
return;
}
q->rear->next = newNode;
q->rear = newNode;
}
int dequeue(queue* q)
{
if (isEmpty(q)) {
printf("Queue Underflow\n");
return -1;
}
node* temp = q->front;
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
int data = temp->data;
free(temp);
return data;
}
int peek(queue* q)
{
if (isEmpty(q))
return -1;
return q->front->data;
}
void printQueue(queue* q)
{
node* temp = q->front;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
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);
printf("Delete 3 items from the queue:");
dequeue(q);
dequeue(q);
dequeue(q);
printf("\nQueue after deletion of 3 elements:");
printQueue(q);
getch();
return 0;
}
Qu 4:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct Linklist
{
int info;
struct Linklist *link;
};
struct Linklist *start=NULL,*ptr,*cur,*prev,*nw;
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);
}
printf("\nThe list is=");
display();
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);
}