Posts

Showing posts from August, 2025

Insertion deletion ds lab

 Insert at front #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* next; struct Node* prev; } Node; Node* createNode(int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = NULL; newNode->prev = NULL; return newNode; } void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("n"); } void insertOne(Node** head, int data, int index) { Node* newNode = createNode(data); if (index == 0) { newNode->next = *head; if (*head != NULL) { (*head)->prev = newNode; } *head = newNode; return; } Node* temp = *head; int currentIndex = 0;   while (temp != NULL && currentIndex < index - 1) { temp = temp->next; currentIndex++; }   if (temp == NULL) { free(newNode); printf("Index out of bounds. Inserting at the end instead.n"); return; } newNode->next = temp->next; newNode->prev = t...

Week4

 #include<stdio.h> #include<conio.h> #define size 10 void push(int); void pop(); void display(); int stack[size], top = -1; void main() { int ele, choice; clrscr(); while(1){ printf("\n\n***** MENU *****\n"); printf("1. Push\n2. Pop\n3. Display\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice){ case 1: printf("Enter the value to be insert: "); scanf("%d",&ele); push(ele); break; case 2: pop(); break; case 3: display(); break; case 4: exit(0); default: printf("\nWrong selection!!! Try again!!!"); } } } void push(int ele) { if(top == size-1) printf("\nStack is Full!!! Insertion is not possible!!!"); else { top++; stack[top] = ele; printf("\nElement Inserted!!!"); } } void pop() { if(top == -1) printf("\nStack is Empty!!! Deletion is not possible!!!"); else { printf("\nDeleted : %d", stack[top]); top--; } } void display() { if(top...

Create and Insert data in circular linked list

  #include <stdio.h> #include <stdlib.h> // Create a node struct Node {   int data;   struct Node* next; }; // Insert at the beginning void insertAtBeginning(struct Node** head_ref, int new_data) {   // Allocate memory to a node   struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));   // insert the data   new_node->data = new_data;   new_node->next = (*head_ref);   // Move head to new node   (*head_ref) = new_node; } // Insert a node after a node void insertAfter(struct Node* prev_node, int new_data) {   if (prev_node == NULL) {   printf("the given previous node cannot be NULL");   return;   }   struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));   new_node->data = new_data;   new_node->next = prev_node->next;   prev_node->next = new_node; } // Insert the the end void insertAtEnd(struct Node** head_ref, int new_data) {   struct Node* n...

Create RandomGenerator.java

  Create RandomGenerator.java import java.util.*; class RandomGenerator extends Thread {  public String tname;  public Random r; public Thread t1, t2;  public RandomGenerator(String s) {  tname = s; } public void run() {  int num = 0;  r = new Random(); try { for (int i = 0; i< 50; i++) { num = r.nextInt(100); System.out.println("Main Thread and Generated Number is " + num);  if (num % 2 == 0) {  t1 = new Thread(new Even(num));   t1.start();  } else {  t2 = new Thread(new Odd(num));  t2.start(); } Thread.sleep(1000);  System.out.println ("--------------------------------------------"); } } catch (Exception ex) {  System.out.println(ex.getMessage()); } } } public class Mthread { public static void main(String[] args) {  RandomGenerator a = new RandomGenerator("One");  a.start(); } } import java.util.*; class Even implements Runnable { public int x; public Even(int x) { this.x = x; } public void r...