CLL by smd

#include <stdio.h>
#include <stdlib.h>
int count = 0;
struct node {
 int data;
struct node *next;
};
struct node *head = NULL, *newnode, *temp;
void create_circle_linked_list();
void insert_at_begin();
void insert_at_end();
void insert_at_specific_point();
void delete_at_begin();
void delete_at_end();
void delete_at_specific_point();
void display();
void create_circle_linked_list() 
{
 int value;
printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
if (head == NULL) 
{
 head = newnode;
 newnode->next = head;
 } else 
{
 temp = head;
 while (temp->next != head)
temp = temp->next;
temp->next = newnode;
newnode->next = head;
    }
    count++;
}
void insert_at_begin() 
{
int value;
 printf("Enter the value to be inserted: ");
scanf("%d", &value);
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = value;
 if (head == NULL) 
    {
        head = newnode;
        newnode->next = head;
    } else 
    {
        temp = head;
        while (temp->next != head)
            temp = temp->next;
        newnode->next = head;
        temp->next = newnode;
        head = newnode;
    }
    count++;
}
void insert_at_end() 
{
    int value;
    printf("Enter the value to be inserted: ");
    scanf("%d", &value);
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = value;
    if (head == NULL)
     {
        head = newnode;
        newnode->next = head;
    } else 
    {
        temp = head;
        while (temp->next != head)
            temp = temp->next;
        temp->next = newnode;
        newnode->next = head;
    }
    count++;
}
void insert_at_specific_point()
 {
    if (count == 0) {
        printf("List is empty. Use create/insert at beginning.\n");
        return;
    }
    int loc, value, i = 1;
    printf("Enter the position (1 to %d): ", count + 1);
    scanf("%d", &loc);
    if (loc < 1 || loc > count + 1) {
        printf("Invalid position\n");
        return;
    }
    printf("Enter the value to be inserted: ");
    scanf("%d", &value);
    newnode = (struct node*)malloc(sizeof(struct node));
    newnode->data = value;
    if (loc == 1) 
    {
        insert_at_begin();
        free(newnode);
        return;
    } else 
    {
        temp = head;
        while (i < loc - 1) 
        {
            temp = temp->next;
            i++;
        }
        newnode->next = temp->next;
        temp->next = newnode;
    }
    count++;
}

void display()
 {
    if (head == NULL) 
    {
        printf("List is empty\n");
        return;
    }
    temp = head;
    printf("Elements in the list: ");
    do {
        printf("%d -> ", temp->data);
        temp = temp->next;
    } while (temp != head);
    printf("(back to head)\n");
}

void delete_at_begin() 
{
    if (head == NULL)
     {
        printf("List is empty, nothing to delete\n");
        return;
    }
    if (head->next == head) 
    {
        printf("%d is deleted\n", head->data);
        free(head);
        head = NULL;
    } else
     {
        temp = head;
        while (temp->next != head)
            temp = temp->next;
        struct node *del = head;
        head = head->next;
        temp->next = head;
        printf("%d is deleted\n", del->data);
        free(del);
    }
    count--;
}
void delete_at_end() 
{
    if (head == NULL) 
    {
        printf("List is empty, nothing to delete\n");
        return;
    }
    if (head->next == head) 
    {
        printf("%d is deleted\n", head->data);
        free(head);
        head = NULL;
    } else {
        struct node *prev = NULL;
        temp = head;
        while (temp->next != head) {
            prev = temp;
            temp = temp->next;
        }
        prev->next = head;
        printf("%d is deleted\n", temp->data);
        free(temp);
    }
    count--;
}
void delete_at_specific_point() 
{
    if (head == NULL) 
    {
        printf("List is empty, nothing to delete\n");
        return;
    }
    int pos, i = 1;
    printf("Enter the position to delete (1 to %d): ", count);
    scanf("%d", &pos);
    if (pos < 1 || pos > count) 
    {
        printf("Invalid position\n");
        return;
    }
    if (pos == 1) 
    {
        delete_at_begin();
        return;
    }
    temp = head;
    struct node *prev = NULL;
    while (i < pos)
     {
        prev = temp;
        temp = temp->next;
        i++;
    }
    prev->next = temp->next;
    printf("%d is deleted\n", temp->data);
    free(temp);
    count--;
}
int main() 
{
    int choice = 0;
    while (choice != 9)
     {
printf("\nChoose the options below\n");
printf("1. Create circle linked list\n");
printf("2. Insert at beginning\n");
printf("3. Insert at end\n");
printf("4. Insert at specific point\n");
printf("5. Delete at beginning\n");
 printf("6. Delete at end\n");
printf("7. Delete at specific point\n");
printf("8. Display\n");
printf("9. Exit\n");
scanf("%d", &choice);
 switch (choice)
  {
 case 1:
                create_circle_linked_list();
                break;
 case 2:
                insert_at_begin();
                break;
case 3:
                insert_at_end();
                break;
 case 4:
                insert_at_specific_point();
                break;
 case 5:
                delete_at_begin();
                break;
 case 6:
                delete_at_end();
                break;
case 7:
                delete_at_specific_point();
                break;
 case 8:
                display();
                break;
case 9:
                printf("Exiting...GoodBye.\n");
                break;
 default:
                printf("Invalid entry\n");
                break;
        }
    }
    return 0;
}







Output:


Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
1
Enter the value to be inserted: 20

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
1
Enter the value to be inserted: 30

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
2
Enter the value to be inserted: 10

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
3
Enter the value to be inserted: 50

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
4
Enter the position (1 to 5): 4
Enter the value to be inserted: 40

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
8
Elements in the list: 10 -> 20 -> 30 -> 40 -> 50 -> (back to head)

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
5
10 is deleted

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
6
50 is deleted

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
7
Enter the position to delete (1 to 3): 3
40 is deleted

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
8
Elements in the list: 20 -> 30 -> (back to head)

Choose the options below
1. Create circle linked list
2. Insert at beginning
3. Insert at end
4. Insert at specific point
5. Delete at beginning
6. Delete at end
7. Delete at specific point
8. Display
9. Exit
9
Exiting...GoodBye.

Comments

Popular posts from this blog

SINGLE LINKED LIST by smd

QUEUE USING ARRAYS by smd