Skip to main content

Featured

Solve LeetCode 3 Longest Substring Without Repeating Characters in C++ using the sliding window

Longest Substring Without Repeating Characters in C++ LeetCode 3 · sliding window · last index array Table of Contents Problem Statement Approach Algorithm C++ Program Input Sample Output Output Explanation Dry Run Flow of Execution Time Complexity Space Complexity Applications Key Points Interview Questions Frequently Asked Questions Conclusion Problem Statement 3. Longest Substring Without Repeating Characters Given a string s , find the length of the longest substring that contains no repeating characters. A substring is a continuous part of the string, so "pwke" is a subsequence of "pwwkew" but not a substring. Approach We use a sliding window . The window is the part of the string between start and end , and we always keep it free of repeated characters. An array lastIndex of size 128 remembers the most recent position of every ASCII ...

Singly Linked List in C – Full Code and Explanation



Menu Driven Singly Linked List Program in C

✅ Menu Driven Program for Singly Linked List in C

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
struct node {
    int data;
    struct node *next;
};

void inseart_at_begining(struct node**head,int data) {
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    if(newnode==NULL) {
        printf("Memory allocation faild!!");
        return;
    }
    newnode->data=data;
    newnode->next=*head;
    *head=newnode;
}

void inseart_at_end(struct node** head,int data) {
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    if(newnode==NULL) {
        printf("Memory allocation faild!!");
        return;
    }
    newnode->data=data;
    newnode->next=NULL;
    if(*head==NULL) {
        *head=newnode;
        return;
    }
    struct node *temp=*head;
    while(temp->next!=NULL) {
        temp=temp->next;
    }
    temp->next=newnode;
}

void inseart_at_spicific_position(struct node** head,int data,int pos) {
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    if(newnode==NULL) {
        printf("Memory allocation faild!!");
        return;
    }
    newnode->data=data;
    newnode->next=NULL;
    if(*head==NULL&& pos!=1) {
        printf("List is empty:\n");
        free(newnode);
        return;
    }
    if(pos==1) {
        newnode->next=*head;
        *head=newnode;
        return;
    }
    struct node*temp=*head;
    for(int i=0;i<pos-1 && temp!=NULL;i++) {
        temp=temp->next;
    }
    if(temp==NULL) {
        printf("%d IS OUT OF RANGE!\n",pos);
        free(newnode);
        return;
    }
    newnode->next = temp->next;
    temp->next = newnode;
}

void delete_node_at_first(struct node**head) {
    if(*head==NULL) {
        printf("List is Empty:\n");
        return;
    }
    struct node*temp=*head;
    printf("%d is deleted at first:\n",temp->data);
    *head=(*head)->next;
    free(temp);
}

void delete_node_at_end(struct node**head) {
    if(*head==NULL) {
        printf("List is empty:\n");
        return;
    }
    if((*head)->next==NULL) {
        printf("%d is deleted at last:\n",(*head)->data);
        free(*head);
        *head=NULL;
        return;
    }
    struct node*temp=*head;
    while(temp->next->next!=NULL) {
        temp=temp->next;
    }
    printf("%d is deleted at last:",temp->next->data);
    free(temp->next);
    temp->next=NULL;
}

void delete_node_at_position(struct node **head, int pos) {
    if (*head == NULL) {
        printf("List is empty.\n");
        return;
    }
    if (pos == 1) {
        struct node *temp = *head;
        *head = (*head)->next;
        printf("%d is deleted at position %d.\n", temp->data, pos);
        free(temp);
        return;
    }
    struct node *temp = *head;
    for (int i = 1; temp != NULL && i < pos - 1; i++) {
        temp = temp->next;
    }
    if (temp == NULL || temp->next == NULL) {
        printf("Position %d is out of range.\n", pos);
        return;
    }
    struct node *node_to_delete = temp->next;
    temp->next = node_to_delete->next;
    printf("%d is deleted at position %d.\n", node_to_delete->data, pos);
    free(node_to_delete);
}

void display(struct node **head) {
    if(*head==NULL) {
        printf("List is empty:\n");
        return;
    }
    struct node* temp=*head;
    while(temp!=NULL) {
        printf("%d->",temp->data);
        temp=temp->next;
    }
    printf("NULL\n");
}

int main( ) {
    int data,pos,choice;
    struct node *head=NULL;
    do {
        printf("=========MENU=============\n");
        printf("1).ADD NODE AT BEGIN:\n");
        printf("2).ADD NODE AT END:\n");
        printf("3).ADD NODE AT SPECIFIC POSITION:\n");
        printf("4).DELETE NODE AT FIRST:\n");
        printf("5).DELETE NODE AT LAST:\n");
        printf("6).DELETE NODE AT POSITION:\n");
        printf("7).DISPLAY LIST:\n");
        printf("8).EXIT THE PROGRAM:\n");
        printf("===========================\n");
        printf("Enter the choice:\n");
        scanf("%d",&choice);

        switch(choice) {
            case 1:
                printf("Enter the element you want to add first:\n");
                scanf("%d",&data);
                inseart_at_begining(&head,data);
                break;
            case 2:
                printf("Enter the element you want to inseart at end:\n");
                scanf("%d",&data);
                inseart_at_end(&head,data);
                break;
            case 3:
                printf("Enter the position you want to add element:\n");
                scanf("%d",&pos);
                printf("Enter the element you want to add:\n");
                scanf("%d",&data);
                inseart_at_spicific_position(&head,data,pos);
                break;
            case 4:
                delete_node_at_first(&head);
                break;
            case 5:
                delete_node_at_end(&head);
                break;
            case 6:
                printf("Enter the position to delete:\n");
                scanf("%d", &pos);
                delete_node_at_position(&head, pos);
                break;
            case 7:
                display(&head);
                break;
            case 8:
                printf("wait a minute:\n");
                sleep(3);
                printf("Exit successfuly:\n");
                exit(0);
            default:
                printf("Invalid choice try again:\n");
        }
    }while(choice!=9);
}
  

๐Ÿ“˜ Explanation:

This is a menu-driven C program that implements a singly linked list. It allows you to insert nodes at the beginning, end, or at a specific position, and to delete nodes from the beginning, end, or a specific position. The display function prints the current state of the list. This kind of program is useful in understanding dynamic memory allocation and pointer operations in C programming.

๐Ÿ”‘ Keywords:

Singly Linked List in C, C Linked List Program, Add and Delete Nodes in C, Dynamic Memory Allocation in C, Linked List Menu Program, Data Structures in C, Menu Driven Program in C, Linked List Operations, Insert Node at Position, Delete Node from List, Linked List Interview Program, Linked List Beginners Tutorial

๐Ÿ“Œ Hashtags:

#CProgramming #SinglyLinkedList #LinkedListOperations #DataStructuresInC #DynamicMemory #PointerOperations #MenuDrivenProgram #CodingBeginners #CCodeBlogger

๐Ÿ–ฅ️ Sample Output:

=========MENU=============
1).ADD NODE AT BEGIN:
2).ADD NODE AT END:
3).ADD NODE AT SPECIFIC POSITION:
4).DELETE NODE AT FIRST:
5).DELETE NODE AT LAST:
6).DELETE NODE AT POSITION:
7).DISPLAY LIST:
8).EXIT THE PROGRAM:
===========================
Enter the choice:
1
Enter the element you want to add first:
10
1
Enter the element you want to add first:
20
2
Enter the element you want to inseart at end:
5
3
Enter the position you want to add element:
2
Enter the element you want to add:
15
7
20->15->10->5->NULL
4
20 is deleted at first:
7
15->10->5->NULL
5
5 is deleted at last:
7
15->10->NULL
6
Enter the position to delete:
2
10 is deleted at position 2.
7
15->NULL
8
wait a minute:
Exit successfuly:
  

Comments

Popular Posts

๐ŸŒ™