0% found this document useful (0 votes)
18 views16 pages

Lab Programs

rooms

Uploaded by

mohitmgmt1619
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)
18 views16 pages

Lab Programs

rooms

Uploaded by

mohitmgmt1619
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/ 16

Program-1

Write a program for traversing an array

#include <stdio.h>
int main() {
int a[20], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);

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


for(i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("The traversed array elements are: \n");
for(i = 0; i < n; i++) {
printf("Element number %d and value is %d\n", i + 1, a[i]);
}
return 0;
}

Output:
Program-2
Write a program for inserting an element in array

#include<stdio.h>
void main() {
int a[20];
int size, i, pos, new_element;
printf("enter the number of element: ");
scanf("%d", &size);
printf("enter %d elements: ", size);

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


scanf("%d", &a[i]);
printf("enter the new element to be inserted: ");
scanf("%d", &new_element);
printf("where %d is to be inserted: ", new_element);
scanf("%d", &pos);

for(i=size; i>=pos; i--)


a[i+1]= a[i];
a[pos]= new_element;
printf("array after insertion is \n");
for(i=0; i<=size; i++)
printf("%d", a[i]);
}

Output:
Program-3
Write a program for deleting an element in array

#include<stdio.h>
void main() {
int a[20];
int size, i, pos, del;
printf("enter the number of element: ");
scanf("%d", &size);
printf("enter %d elements: ", size);

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


scanf("%d", &a[i]);
printf("enter the new element to be deleted: ");
scanf("%d", &pos);

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


a[i]= a[i+1];

size = size-1;
printf("array after deletion is: \n");
for(i=0; i<size; i++)
printf("%d", a[i]);
}

Output:
Program-4
Write a program for searching an element in array

#include<stdio.h>
void main() {
int arr[20];
int size, i, toSearch, found;
printf("enter size of array: ");
scanf("%d", &size);
printf("enter elements in array: ");

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


scanf("%d", &arr[i]);
printf("enter element to be searched: ");
scanf("%d", &toSearch);
found = -1;
for(i=0; i<size; i++)
if(arr[i]== toSearch) {
found = i;
break;
}
if(found == -1)
printf("element not found \n");
else {
printf("%d is found at %d", toSearch, found);
}
Output:
Program-5
Write a program to insert a new node at the
beginning of single linked list

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
} *head;
void createList(int n) {
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL) {
printf("Unable to allocate memory!\n");
return;
}

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!\n");
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 insertNodeAtBeginning(int data) {

struct node *newNode;


newNode = (struct node*)malloc(sizeof(struct node));
if (newNode == NULL) {
printf("Unable to allocate memory!\n");
return;
} else {
newNode->data = data;
newNode->next = head;
head = newNode;
printf("Data inserted successfully at the beginning!\n");
}
}

void displayList() { struct node *temp;

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

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

createList(n);
displayList();
printf("\nEnter data to insert at the beginning of the list: ");
scanf("%d", &data);

insertNodeAtBeginning(data);
printf("\nData in the list after insertion:\n");
displayList();

return 0;
}

Output:
Program-6
Write a program to delete a new node at the
beginning of single linked list

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
} *head;
void createList(int n) {
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL) {
printf("Unable to allocate memory!\n");
return;
}
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!\n");
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 deleteFirstNode() {
struct node *toDelete;
if (head == NULL) {
printf("List is already empty!\n");
} else {
toDelete = head;
head = head->next;
printf("\nData deleted = %d\n", toDelete->data);
free(toDelete);
printf("Successfully deleted first node from lust!\n");
}
}
void displayList() {
struct node *temp;
if (head == NULL)
printf("List is empty.\n");
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();
printf("\nPress 1 to delete first node: ");
scanf("%d", &choice);

if(choice == 1)
deleteFirstNode();
printf("\nData in the list\n");
displayList();
return 0;
}
Output:
Program-7
Write a program to perform stack operations

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
struct Stack {
int top;
unsigned capacity;
int *array;
};
struct Stack* createStack(unsigned capacity) {
struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->capacity = capacity;
stack->top = -1;
stack->array = (int*)malloc(stack->capacity * sizeof(int));
return stack;
}
int isFull(struct Stack* stack) {
return stack->top == stack->capacity - 1; }
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
void push(struct Stack* stack, int item) {
if (isFull(stack)) {
printf("Stack overflow! Cannot push %d\n", item);
return;
}
stack->array[++stack->top] = item;
printf("%d pushed to stack\n", item);
}
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack underflow! Cannot pop from empty stack\n");
return INT_MIN;
}
return stack->array[stack->top--]; }
int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty! Cannot peek\n");
return INT_MIN;
}
return stack->array[stack->top];
}
int main() {
struct Stack* stack = createStack(100);
push(stack, 10);
push(stack, 20);
push(stack, 30);

printf("%d popped from stack\n", pop(stack));


printf("Top element is %d\n", peek(stack));
return 0;
}
Output:
Program-8
Write a program to perform queue operations

#include<stdio.h>
#include<stdlib.h>
struct QNode {
int key;
struct QNode* next;
};
struct Queue{
struct QNode *front, *rear;
};
struct QNode* newNode(int k){
struct QNode* temp
= (struct QNode*)malloc(sizeof(struct QNode));
temp->key = k;
return temp;
}
struct Queue* createQueue(){
struct Queue*q = (struct Queue)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue*q, int k) {
struct QNode temp = newNode(k);
if(q->rear == NULL) {
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q) {
if(q->front == NULL)
return;
struct QNode* temp = q->front;
q->front = q->front->next;
if(q->front == NULL)
q->rear = NULL;

free(temp);

}
void main() {
struct Queue* q = createQueue();
enQueue(q,10);
enQueue(q,20);
deQueue(q);
deQueue(q);
enQueue(q,30);
enQueue(q,40);
enQueue(q,50);
deQueue(q);
printf("Queue front: %d\n", q->front->key);
printf("Queue rear: %d\n", q->rear->key);
}
Output:
Program-9
Write a program to traverse a tree

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *left, *right;
};
void printCurrentLevel(struct node* root, int level) {
if(root == NULL)
return;
if(level == 1)
printf("%d", root->data);
else if(level>1) {
printCurrentLevel(root->left, level-1);
printCurrentLevel(root->right, level-1);
}
}
int height(struct node* node) {
if(node == NULL)
return 0;
else {
int lheight = height(node->left);
int rheight = height(node->right);
if(lheight>rheight)
return(lheight+1);
else
return(rheight+1);
}
}
struct node* newNode(int data) {
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
void printLevelOrder(struct node* root) {
int h = height(root);
int i;
for (i=1; i<=h; i++)
printCurrentLevel(root,i);
}
void main(){
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Level Order traversal of binary tree is \n");
printLevelOrder(root);
}
Output:

You might also like