lab programs
lab programs
#include <stdio.h>
int main() {
int a[20], n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
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);
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);
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: ");
#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;
}
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;
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);
#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: