Monday, 9 December 2024

Functions

 Solve :- insert a node at the start of a DLL

// Function to insert a node at the beginning of the doubly linked list

 struct Node* insertAtBeginning(struct Node* head, int value)

 { 

// Step 1: Create a new node with the given value

 struct Node* newNode = createNode(value);

 // Step 2: If the list is not empty, update the current head's previous pointer 

if (head != NULL) 

head->prev = newNode;

 } 

// Step 3: Make the new node's next pointer point to the current head

 newNode->next = head; 

// Step 4: Update the head to point to the new node

 head = newNode;

 // Return the new head of the list 

return head; 

    Write a function to sort a DLL

sort(struct node **head)

// pointer to the pointer of the head node of the linked list.
{
struct node *n, *c;

// declares two pointers to nodes in a linked list.
int x;

for (c = *head; c-> next ! = NULL, c = c->next)

//   c = *head: Start at the first node of the list.

 // c->next != NULL: Check if the current node has a next node; continue if true.

//  c = c->next: Move to the next node in the list.

for(n = c->next; n!=NULL; n=n->next)

{

//compare (swap logic)

   If(c->data > n->data)

{

X = c->data;

c->data = n->data;

n->data = x;

}

}
}


Reverse print a DLL without using recursion

d_rdisplay( struct dnode *q ) 

Struct dnode *temp=*q;


while ( temp->next != NULL )

 

//checks if the current node (temp) has a next node (i.e., temp->next is not NULL).


temp = temp->next;


//temp = temp->next;: This moves the pointer temp to the next node in the list.


while(temp!=NULL)

{

printf ( "%d ", temp -> data ) ; 

temp = temp -> next ; 

 The code loops through the linked list, printing the data of each node, and moves to the next node until it reaches the end (when temp is NULL).


Write a program to implement a Dynamic Queue(Use pointer,linked list)

#include <stdio.h>

#include <stdlib.h>


struct node {

    int data;

    struct node *next;  // Pointer to the next node

};


// Function declarations

void create(struct node **front);

void enqueue(struct node **front, int val);

int dequeue(struct node **front);

void display(struct node **front);


int main() {

    struct node *front;  // Front pointer for the queue

    int choice, val;

    

    create(&front);  // Initialize the queue

    

    while(1) {

        printf("\n\n<<< MENU >>>");

        printf("\n1. Create\n2. Enqueue\n3. Dequeue\n4. Display\n5. Exit");

        printf("\nEnter your choice: ");

        scanf("%d", &choice);

        

        switch(choice) {

            case 1:

                create(&front);

                printf("\nQueue created successfully!");

                break;

            case 2:

                printf("\nEnter value to insert: ");

                scanf("%d", &val);

                enqueue(&front, val);

                printf("\nValue inserted successfully!");

                break;

            case 3:

                val = dequeue(&front);

                if(val == -999)

                    printf("\nQueue is empty!");

                else

                    printf("\n%d removed successfully!", val);

                break;

            case 4:

                display(&front);

                break;

            case 5:

                exit(0);

            default:

                printf("\nInvalid choice!");

        }

    }

    return 0;

}


// Initialize the queue (set front to NULL)

void create(struct node **front) {

    *front = NULL;

}


// Add an element to the queue (enqueue)

void enqueue(struct node **front, int val) {

    struct node *temp = (struct node *)malloc(sizeof(struct node));

    struct node *p;

    

    temp->data = val;

    temp->next = NULL;

    

    if (*front == NULL) {

        *front = temp;  // If queue is empty, make temp the first node

    } else {

        p = *front;

        while (p->next != NULL) {

            p = p->next;  // Traverse to the last node

        }

        p->next = temp;  // Add the new node to the end

    }

}


// Remove an element from the queue (dequeue)

int dequeue(struct node **front) {

    struct node *temp = *front;

    int x;

    

    if (temp == NULL) {

        return -999;  // Return -999 if the queue is empty

    } else {

        x = temp->data;  // Get the data of the front node

        *front = temp->next;  // Move the front pointer to the next node

        free(temp);  // Free the memory of the removed node

        return x;  // Return the data of the removed node

    }

}


Write a program to implement a dynamic stack(Use pointer,linked list)

#include <stdio.h>

#include <stdlib.h>

// Define the structure of a stack node

struct Node {

    int data;

    struct Node* next;

};

// Function prototypes

void create(struct Node** top);

void push(struct Node** top, int val);

int pop(struct Node** top);

int stackTop(struct Node** top);


int main() {

    struct Node* top = NULL; // Initialize the stack as empty

    int choice, val;

    printf("\n<<< DYNAMIC STACK IMPLEMENTATION >>>\n");

    while (1) {

        printf("\n\n<<<< MENU >>>>");

        printf("\n1. Create Stack");

        printf("\n2. Push");

        printf("\n3. Pop");

        printf("\n4. Stack Top");

        printf("\n5. Exit");

        printf("\nEnter your choice: ");

        scanf("%d", &choice);


        switch (choice) {

            case 1:

                create(&top);

                printf("\nStack created successfully!");

                break;

            case 2:

                printf("\nEnter a value to push: ");

                scanf("%d", &val);

                push(&top, val);

                printf("\nValue pushed successfully!");

                break;

            case 3:

                val = pop(&top);

                if (val == -999)

                    printf("\nStack is empty!");

                else

                    printf("\n%d popped successfully!", val);

                break;

            case 4:

                val = stackTop(&top);

                if (val == -999)

                    printf("\nStack is empty!");

                else

                    printf("\nStack top is %d", val);

                break;

            case 5:

                printf("\nExiting program.");

                exit(0);

            default:

                printf("\nInvalid choice! Please try again.");

        }

    }

    return 0;

}


// Function to create an empty stack

void create(struct Node** top) {

    *top = NULL; // Initialize the stack top as NULL

}

The single pointer (*top) represents the pointer to the top of the stack.

The double pointer (**top) is used because we want to modify the original pointer (top) in the main function directly.

// Function to push an element onto the stack

void push(struct Node** top, int val) {

 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    if (!newNode) {

        printf("\nMemory allocation failed.");

        return;

    }


    newNode->data = val;

//Assigns the value val to the data field of the new node

    newNode->next = *top;

//Links the new node to the current top of the stack by pointing its next to the current top.

    *top = newNode;

//Updates the top pointer to make the new node the top of the stack.

}


// Function to pop an element from the stack

int pop(struct Node** top) {

    if (*top == NULL) {

        return -999; // Return -999 if the stack is empty

    }

    struct Node* temp = *top;




//Store the current top node in temp for later removal.

    int val = temp->data;

//Extract the data from the top node.

    *top = (*top)->next;

//Move the top to the next node in the stack.

    free(temp);

//Delete the old top node to release memory

    return val;

}

// Function to get the top element of the stack

int stackTop(struct Node** top) {

    if (*top == NULL) {

        return -999; // Return -999 if the stack is empty

    }

    return (*top)->data;

//It accesses the data of the node pointed to by *top (the top node of the stack).

}