Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
21 kB
3
Indexable
Never
#include <stdio.h>(CLL)
#include <stdlib.h>

struct Node{
    int data;
    struct Node* next;
};
typedef struct Node Node;

Node *takeInput();
void printLL(Node *head);
Node *insertElement(Node *head);
Node *insertAtEnd(Node *head, int value);
Node *insertAtBeginning(Node *head, int value);
Node *insertBeforeGivenElement(Node *head, int value, int elem);
Node *insertAfterGivenElement(Node* head, int value, int elem);
Node *deleteElement(Node *head);
Node *deleteFirst(Node *head);
Node *deleteLast(Node *head);
Node *deleteGivenElement(Node *head, int value);
int searchElement(Node *head, int value);
int sizeofLL(Node *head);

int main()
{
    Node *head = takeInput();
    int opt;
    do
    {
        printf("\tMain Menu \n");
        printf("1. Insert an element \n2. Delete an element \n3. Search for an element \n4. Count total number of nodes \n5. Print Linked List \n6. '0' for exit \n");
        printf("Enter your choice: ");
        fflush(stdin);
        scanf("%d", &opt);
        int flag, value;
        int Size = 0;
        switch (opt)
        {
        case 1:
            head = insertElement(head);
            break;
        case 2:
            head = deleteElement(head);
            break;
        case 3:
            printf("Enter the target value: ");
            fflush(stdin);
            scanf("%d", &value);
            flag = searchElement(head, value);
            if (flag == 1)
                printf("Element found. \n");
            else
                printf("Element not found. \n");
            break;
        case 4:
            Size = sizeofLL(head);
            printf("Size of Linked List is: %d \n", Size);
            break;
        case 5:
            printLL(head);
            break;
        }
    } while (opt != 0);
    return 0;
}
Node* takeInput()
{
    Node *head, *tail;
    head = tail = NULL;
    char opt;
    do
    {
        printf("Do you want to add data: \n");
        fflush(stdin);
        scanf("%c", &opt);
        if(opt == 'y')
        {
            int data;
            printf("Enter data: ");
            scanf("%d", &data);
            Node* newNode = (Node*)malloc(sizeof(Node));
            newNode -> data = data;
            newNode -> next = head;
            if(head == NULL)
            {
                head = newNode;
                tail = newNode;
            }
            else
            {
                tail -> next = newNode;
                tail = tail -> next;
            }
        }
    }while(opt != 'n');
    return head;
}
void printLL(Node* head)
{
    if(head == NULL)
    {
        printf("Linked List is empty \n");
    }
    else
    {
        printf("Linked List is: ");
        Node* temp = head;
        do
        {
            printf("%d ", temp -> data);
            temp = temp -> next;
        }while(temp != head);
        printf("\n");
    }
}
Node *insertElement(Node *head)
{
    int opt;
    printf("\n");
    printf("\tInsert Menu: \n");
    printf("1. Insert at beginning \n2. Insert at end \n3. Insert before a given element \n4. Insert after a given element \n");
    printf("Enter your choice: ");
    scanf("%d", &opt);
    switch (opt)
    {
        int value, elem;
    case 1:
        printf("Enter value to insert: ");
        fflush(stdin);
        scanf("%d", &value);
        printf("Before Insertion: \n");
        printLL(head);
        head = insertAtBeginning(head, value);
        printf("After Insertion: \n");
        printLL(head);
        printf("\n");
        break;
    case 2:
        printf("Enter value to insert: ");
        fflush(stdin);
        scanf("%d", &value);
        printf("Before Insertion: \n");
        printLL(head);
        head = insertAtEnd(head, value);
        printf("After Insertion: \n");
        printLL(head);
        printf("\n");

        break;
    case 3:
        printf("Enter value to insert: ");
        fflush(stdin);
        scanf("%d", &value);
        printf("Enter the element before insert: ");
        fflush(stdin);
        scanf("%d", &elem);
        printf("Before Insertion: \n");
        printLL(head);
        head = insertBeforeGivenElement(head, value, elem);
        printf("After Insertion: \n");
        printLL(head);
        printf("\n");
        break;
    case 4:
        printf("Enter value to insert: ");
        fflush(stdin);
        scanf("%d", &value);
        printf("Enter the element after insert: ");
        fflush(stdin);
        scanf("%d", &elem);
        printf("Before Insertion: \n");
        printLL(head);
        head = insertAfterGivenElement(head, value, elem);
        printf("After Insertion: \n");
        printLL(head);
        printf("\n");
        break;
    }
    return head;
}

Node* insertAtBeginning(Node* head, int value)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = head;
    if(head == NULL)
    {
        newNode -> data = value;
        head = newNode;
        newNode -> next = head;
    }
    else
    {
        Node* temp = head;
        while(temp -> next != head)
        {
            temp = temp -> next;
        }
        head = newNode;
        temp -> next = head;
    }
    return head;
}

Node* insertAtEnd(Node* head, int value)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = head;
    if(head == NULL)
    {
        newNode -> data = value;
        head = newNode;
        newNode -> next = head;
    }
    else
    {
        Node* temp = head;
        while(temp -> next != head)
        {
            temp = temp -> next;
        }
        temp -> next = newNode;
        newNode -> next = head;
    }
    return head;
}

 Node *insertBeforeGivenElement(Node *head, int value, int elem)
{
     if (head == NULL)
     {
         printf("Linked List is empty \n");
         return head;
     }
     if(head -> data == elem)
     {
         head = insertAtBeginning(head, value);
         return head;
     }
     Node *temp, *ptr;
     temp = head -> next;
     ptr = head;
     while(temp -> next != head && temp -> data != elem)
     {
            temp = temp -> next;
            ptr = ptr -> next;
     }
     if(temp -> data != elem)
     {
         printf("Target element not found \n");
         return head;
     }

     Node* newNode = (Node*)malloc(sizeof(Node));
     newNode -> data = value;
     newNode -> next = head;
     ptr -> next = newNode;
     newNode -> next = temp;
     return head;
}

Node* insertAfterGivenElement(Node* head, int value, int elem)
{
    if(head == NULL)
    {
        printf("LinkedList is empty \n");
        return head;
    }
    Node* temp = head;
    while(temp -> next != head && temp -> data != elem)
    {
        temp = temp -> next;
    }
    if(temp -> data == elem)
    {
        Node* newNode = (Node*)malloc(sizeof(Node));
        newNode -> data = value;
        newNode -> next = temp -> next;
        temp -> next = newNode;
    }
    if(temp -> data != elem)
    {
        printf("Target Element not found \n");
        return head;
    }
    return head;
}

Node *deleteElement(Node *head)
{
    printf("\n");
    printf("\tDelete Menu \n");
    printf("1. Delete the first element \n2. Delete the last element \n3. Delete a given element \n");
    int opt;
    printf("Enter choice: ");
    scanf("%d", &opt);
    switch (opt)
    {
    case 1:
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteFirst(head);
        printf("After Deletion: \n");
        printLL(head);
        break;
    case 2:
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteLast(head);
        printf("After Deletion: \n");
        printLL(head);
        break;
    case 3:
        printf("Enter element: ");
        int elem;
        fflush(stdin);
        scanf("%d", &elem);
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteGivenElement(head, elem);
        printf("After Deletion: \n");
        printLL(head);
        break;
    }
    return head;
}

Node* deleteFirst(Node* head)
{
    if(head == NULL)
    {
        printf("Linked List is empty: \n");
        return head;
    }
    Node* temp = head;
    Node* ptr = head;
    if(temp -> next == head)
    {
        free(temp);
        head = NULL;
        return head;
    }
    while(temp -> next != head)
    {
        temp = temp -> next;
    }
    head = ptr -> next;
    temp -> next = head;
    free(ptr);
    return head;
}

Node* deleteLast(Node* head)
{
    if(head == NULL)
    {
        printf("Linked List is empty: \n");
        return head;
    }
    Node* temp = head -> next;
    Node* ptr = head;
    while(temp -> next != head)
    {
        temp = temp -> next;
        ptr = ptr -> next;
    }
    ptr -> next = head;
    free(temp);
    return head;
}

Node* deleteGivenElement(Node* head, int elem)
{
    if(head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    Node* temp = head;
    if(temp -> data == elem)
    {
        head = deleteFirst(temp);
        return head;
    }
    temp = head -> next;
    Node* ptr = head;
    while(temp -> next != head && temp -> data != elem)
    {
        temp = temp -> next;
        ptr = ptr -> next;
    }

    if(temp -> data == elem)
    {
        ptr -> next = temp -> next;
        free(temp);
    }
    if(temp -> data != elem)
    {
        printf("Target element not found \n");
    }
    return head;
}

int sizeofLL(Node* head)
{
    int cnt = 0;
    Node* temp = head;
    do
    {
        cnt++;
        temp = temp -> next;
    }while(temp != head);
    return cnt;
}

int searchElement(Node* head, int value)
{
    Node* temp = head;
    int flag = 0;
    do
    {
        if(temp -> data == value)
        {
            flag = 1;
            break;
        }
        temp = temp -> next;
    }while(temp != head);
    return flag;
}





DLL(Doubly)

# include<stdio.h>
#include<stdlib.h>

struct Node{
    int data;
    struct Node* next;
    struct Node* prev;
};
typedef struct Node Node;

Node *takeInput();
void printLL(Node *head);
Node *insertElement(Node *head);
Node *insertAtEnd(Node *head, int value);
Node *insertAtBeginning(Node *head, int value);
Node *insertBeforeGivenElement(Node *head, int value, int elem);
Node *insertAfterGivenElement(Node *head, int value, int elem);
Node *deleteElement(Node *head);
Node *deleteFirst(Node *head);
Node *deleteLast(Node *head);
Node *deleteGivenElement(Node *head, int value);
int searchElement(Node *head, int value);
int sizeofLL(Node *head);

int main()
{
    Node *head = takeInput();
    int opt;
    do
    {
        printf(" \n\tMain Menu \n");
        printf("1. Insert an element \n2. Delete an element \n3. Search for an element \n4. Count total number of nodes \n5. Print Linked List \n6. '0' for exit \n");
        printf("Enter your choice: ");
        fflush(stdin);
        scanf("%d", &opt);
        int flag, value;
        int Size = 0;
        switch (opt)
        {
        case 1:
            head = insertElement(head);
            break;
        case 2:
            head = deleteElement(head);
            break;
        case 3:
            printf("Enter the target value: ");
            fflush(stdin);
            scanf("%d", &value);
            flag = searchElement(head, value);
            if (flag == 1)
                printf("Element found. \n");
            else
                printf("Element not found. \n");
            break;
        case 4:
            Size = sizeofLL(head);
            printf("Size of Linked List is: %d \n", Size);
            break;
        case 5:
            printLL(head);
            break;
        }
    } while (opt != 0);
    return 0;
}
Node* takeInput()
{
    Node *head, *tail, *tailPrev;
    head = tail = NULL;
    char opt;
    do
    {
        printf("Do you want to insert element: \n");
        fflush(stdin);
        scanf("%c", &opt);
        if(opt == 'y')
        {
            int data;
            printf("Enter element: ");
            scanf("%d", &data);
            Node* newNode = (Node*)malloc(sizeof(Node));
            newNode -> data = data;
            newNode -> next = NULL;
            newNode -> prev = NULL;
            if(head == NULL)
            {
                head = newNode;
                tail = newNode;
                tailPrev = head;
            }
            else
            {
                tail -> next = newNode;
                tail -> prev = tailPrev;
                tailPrev = tailPrev -> prev;
                tail = tail -> next;
            }
        }
    }while(opt != 'n');
    return head;
}

Node *insertElement(Node *head)
{
    int opt;
    printf("\tInsert Menu: \n");
    printf("1. Insert at beginning \n2. Insert at end \n3. Insert before a given element \n4. Insert after a given element \n");
    printf("Enter your choice: ");
    scanf("%d", &opt);
    switch (opt)
    {
        int value, elem;
        case 1:
            printf("Enter value to insert: ");
            fflush(stdin);
            scanf("%d", &value);
            printf("Before Insertion: \n");
            printLL(head);
            head = insertAtBeginning(head, value);
            printf("After Insertion: \n");
            printLL(head);
            break;
        case 2:
            printf("Enter value to insert: ");
            fflush(stdin);
            scanf("%d", &value);
            printf("Before Insertion: \n");
            printLL(head);
            head = insertAtEnd(head, value);
            printf("After Insertion: \n");
            printLL(head);
            break;
        case 3:
            printf("Enter value to insert: ");
            fflush(stdin);
            scanf("%d", &value);
            printf("Enter the element before insert: ");
            fflush(stdin);
            scanf("%d", &elem);
            printf("Before Insertion: \n");
            printLL(head);
            head = insertBeforeGivenElement(head, value, elem);
            printf("After Insertion: \n");
            printLL(head);
            break;
        case 4:
            printf("Enter value to insert: ");
            fflush(stdin);
            scanf("%d", &value);
            printf("Enter the element after insert: ");
            fflush(stdin);
            scanf("%d", &elem);
            printf("Before Insertion: \n");
            printLL(head);
            head = insertAfterGivenElement(head, value, elem);
            printf("After Insertion: \n");
            printLL(head);
            break;
    }
    return head;
}

Node* insertAtBeginning(Node* head, int value)
{
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = NULL;
    newNode -> prev = NULL;
    newNode -> next = head;
    head = newNode;
    return head;
}
Node* insertAtEnd(Node* head, int value)
{
    if(head == NULL)
    {
        head = insertAtBeginning(head, value);
        return head;
    }
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = NULL;
    newNode -> prev = NULL;
    Node* temp = head;
    while(temp -> next != NULL)
    {
        temp = temp -> next;
    }
    temp -> next = newNode;
    newNode -> prev = temp;
    return head;
}
Node *insertBeforeGivenElement(Node *head, int value, int elem)
{
    if (head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    if(head -> data == elem)
    {
        head = insertAtBeginning(head, value);
        return head;
    }
    Node *temp, *ptr;
    temp = head -> next;
    ptr = head;
    while(temp != NULL && temp -> data != elem)
    {
        temp = temp -> next;
        ptr = ptr -> next;
    }
    if(temp == NULL)
    {
        printf("Target element not found \n");
        return head;
    }
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = NULL;
    newNode -> prev = NULL;
    ptr -> next = newNode;
    newNode -> next = temp;
    newNode -> prev = ptr;
    return head;
}
Node* insertAfterGivenElement(Node* head, int value, int elem)
{
    if(head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    Node* temp = head;
    while(temp !=NULL && temp -> data != elem)
    {
        temp = temp -> next;
    }
    if(temp == NULL)
    {
        printf("Target Element Not Found \n");
        return head;
    }
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode -> data = value;
    newNode -> next = NULL;
    newNode -> prev = NULL;
    newNode -> next = temp -> next;
    newNode -> prev = temp;
    temp -> next = newNode;
    return head;
}

Node *deleteElement(Node *head)
{
    printf("\tDelete Menu \n");
    printf("1. Delete the first element \n2. Delete the last element \n3. Delete a given element \n");
    int opt;
    printf("Enter choice: ");
    fflush(stdin);
    scanf("%d", &opt);
    switch (opt)
    {
    case 1:
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteFirst(head);
        printf("After Deletion: \n");
        printLL(head);
        break;
    case 2:
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteLast(head);
        printf("After Deletion: \n");
        printLL(head);
        break;
    case 3:
        printf("Enter element: ");
        int elem;
        fflush(stdin);
        scanf("%d", &elem);
        printf("Before Deletion: \n");
        printLL(head);
        head = deleteGivenElement(head, elem);
        printf("After Deletion: \n");
        printLL(head);
        break;
    }
    return head;
}

Node *deleteFirst(Node *head)
{
    if (head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    Node *temp = head;
    head = temp->next;
    head -> prev = NULL;
    free(temp);
    return head;
}

Node *deleteLast(Node *head)
{
    if (head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    if(head -> next == NULL)
    {
        Node* temp = head;
        free(temp);
        head = NULL;
        return head;
    }
    Node *temp = head->next;
    Node *ptr = head;
    while (temp->next != NULL)
    {
        temp = temp->next;
        ptr = ptr->next;
    }
    ptr->next = NULL;
    free(temp);
    return head;
}
Node *deleteGivenElement(Node *head, int value)
{
    Node* temp;
    if (head == NULL)
    {
        printf("Linked List is empty \n");
        return head;
    }
    if(head -> data == value)
    {
        temp = head;
        head = temp -> next;
        head -> prev = NULL;
        free(temp);
        return head;
    }
    temp = head -> next;
    Node *ptr = head;
    while (temp -> next != NULL && temp->data != value)
    {
        temp = temp->next;
        ptr = ptr->next;
    }
    if(temp -> data == value)
    {
        if(temp -> next == NULL)
        {
            ptr -> next = NULL;
            free(temp);
        }
        else
        {
            ptr -> next = temp -> next;
            temp -> next -> prev = ptr;
            free(temp);
        }
    }
    if(temp -> next == NULL)
    {
        printf("Target Element Not Found \n");
        return head;
    }
    return head;
}

void printLL(Node* head)
{

    printf("Linked List is: ");
    Node* temp = head;
    while(temp != NULL)
    {
        printf("%d ", temp -> data);
        temp = temp -> next;
    }
    printf("\n");
}

int sizeofLL(Node* head)
{
    int cnt = 0;
    Node* temp = head;
    while(temp != NULL)
    {
        cnt++;
        temp = temp -> next;
    }
    return cnt;
}
int searchElement(Node* head, int value)
{
    int flag = 0;
    if(head == NULL)
    {
        printf("Linked List is empty \n");
    }
    else
    {
        Node* temp = head;
        while(temp -> next != NULL && temp -> data != value)
        {
            temp = temp -> next;
        }
        if(temp -> data == value)
        {
            flag = 1;
        }
    }
    return flag;
}