Showing posts with label Circular Linked List. Show all posts
Showing posts with label Circular Linked List. Show all posts

Saturday, 24 September 2016

Circular Doubly Linked List: Delete at End

Circular Doubly Linked List: Delete at End


#include<stdio.h>
#include<stdlib.h>
struct node
{
                int data;
                struct node *llink;
                struct node *rlink;
};
typedef struct node* NODE;

NODE getnode();
NODE insert_at_end(NODE last, int item);
NODE delete_at_end(NODE last);
void display(NODE last);

NODE getnode()
{
                NODE x;
                x =(NODE) malloc(sizeof(struct node));
                return x;
}
NODE insert_at_end(NODE last, int item)
{
                int val;
                NODE cur, temp;
                temp = getnode();
                temp->data = item;
                if(last == NULL)
                {
                                last = temp;
                }
                else
                {
                                temp->rlink = last->rlink;
                                last->rlink->llink = temp;
                }
                last->rlink = temp;
                temp->llink = last;
                return temp;
}
NODE delete_at_end(NODE last)
{
                NODE prev, cur;
                if(last == NULL)
                {
                                printf("\nList is empty");
                                return NULL;
                }
                if(last->rlink == last)
                {
                                printf("\nThe node %d is deleted",last->data);
                                free(last);
                                return NULL;
                }
                prev = last->rlink;
                while(prev->rlink!=last)
                {
                                prev = prev->rlink;
                }
                prev->rlink=last->rlink;
                last->rlink->llink = prev;
                printf("The node %d is deleted \n",last->data);
                free(last);
                return prev;
}
void display(NODE last)
{
                NODE cur;
                printf("\nContents are:\n");
                if(last == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                 cur = last->rlink;
                                while(cur != last)
                                {
                                                printf("|%d | %d | %d | ==>  ", cur->llink, cur->data, cur->rlink);
                                                cur = cur->rlink;
                                }
                                printf("| %d | %d | %d |  ", cur->llink, cur->data, cur->rlink);
                }
}
void main()
{
                int ch, item;
                NODE last = NULL;
                while(1)
                {
                                printf("\n\n~~MENU~~");
                                printf("\n1.Insert at end");
                                printf("\n2.Delete from end");
                                printf("\n3.Display");
                                printf("\n4.exit");
                                printf("\nEnter your choice: ");
                                scanf("%d",&ch);
                                switch(ch)
                                {
                                              case 1:  printf("\nEnter the value: ");
                                                           scanf("%d", &item);
                                                           last = insert_at_end(last, item);
                                                           break;
                                                case 2:   last = delete_at_end(last);
                                                               break;
                                                case 3:   display(last);
                                                              break;
                                                case 4:   exit(0);
                                }
                }
}




Output:
~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 11

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 1
Enter the value: 13

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 3
Contents are:
|5321840 | 11 | 5321816 | ==>  |5321792 | 12 | 5321840 | ==>  | 5321816 | 13 | 5321792 |

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 2
The node 13 is deleted

~~MENU~~
1.Insert at end
2.Delete from end
3.Display
4.exit
Enter your choice: 4


Circular Doubly Linked List: Delete from front

Circular Doubly Linked List: Delete from Front


#include<stdio.h>
#include<stdlib.h>
struct node
{
        int data;
        struct node *llink;
        struct node *rlink;
};
typedef struct node* NODE;

NODE getnode();
NODE insert_at_front(NODE last, int item);
NODE delete_at_front(NODE last);
void display(NODE last);

NODE getnode()
{
        NODE x;
        x =(NODE) malloc(sizeof(struct node));
        return x;
}
NODE insert_at_front(NODE last, int item)
{
        int val;
        NODE temp;
        temp = getnode();
        temp->data = item;
        if(last == NULL)                                          /*Case 1: List is empty*/
        {
                last = temp;
        }
        else                                                                /*Case 2: If not empty*/
        {
                temp->rlink = last->rlink;
                last->rlink->llink = temp;
        }
        temp->llink = last;
        last->rlink = temp;
        return last;
}
NODE delete_at_front(NODE last)
{
        NODE temp;
        if(last == NULL)
        {
                printf("\n List is empty");
                return NULL;
        }
        if(last->rlink == last)
        {
                printf("\nThe node %d is deleted",last->data);
                free(last);
                return NULL;
        }
        temp = last->rlink;
        last->rlink = temp->rlink;
        last->rlink->llink = last;
        printf("\nThe node  %d is deleted",temp->data);
        free(temp);
        return last;
}
void display(NODE last)
{
        NODE cur;
        printf("\nContents are:\n");
        if(last == NULL)
                printf("\nList is empty. Nothing to display.");
        else
        {
                cur = last->rlink;
                while(cur != last)
                {
                        printf("|%d | %d | %d | ==>  ", cur->llink, cur->data, cur->rlink);
                        cur = cur->rlink;
                }
                printf("| %d | %d | %d |  ", cur->llink, cur->data, cur->rlink);
        }
}
void main()
{
        int ch, item;
        NODE last = NULL;
        while(1)
        {
            printf("\n\n~~MENU~~");
            printf("\n1.Insert at front");
            printf("\n2.Delete at front");
            printf("\n3.Display");
            printf("\n4.exit");
            printf("\nEnter your choice: ");
            scanf("%d",&ch);
            switch(ch)
            {
                    case 1:     printf("\nEnter the value: ");
                                scanf("%d",&item);
                                last = insert_at_front(last, item);
                                break;
                    case 2:     last = delete_at_front(last);
                                break;
                    case 3:     display(last);
                                break;
                    case 4: exit(0);
            }
        }
}
Output:
~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 1
Enter the value: 11
~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 3
Contents are:
|5125192 | 12 | 5125192 | ==>  | 5125216 | 11 | 5125216 |

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 1
Enter the value: 14

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 3
Contents are:
|5125192 | 14 | 5125216 | ==>  |5125240 | 12 | 5125192 | ==>  | 5125216 | 11 | 5125240 |

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 2
The node  14 is deleted

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 2
The node  12 is deleted

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 2
The node 11 is deleted
~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 2
List is empty

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 3
Contents are:
List is empty. Nothing to display.

~~MENU~~
1.Insert at front
2.Delete at front
3.Display
4.exit
Enter your choice: 4


Circular Doubly Linked List: Insert at End

Circular Doubly Linked List: Insert at End


#include<stdio.h>
#include<stdlib.h>
struct node
{
                int data;
                struct node *llink;
                struct node *rlink;
};
typedef struct node* NODE;

NODE getnode();
NODE insert_at_end(NODE last, int item);
void display(NODE last);

NODE getnode()
{
                NODE x;
                x =(NODE) malloc(sizeof(struct node));
                return x;
}
NODE insert_at_end(NODE last, int item)
{
                int val;
                NODE cur, temp;
                temp = getnode();
                temp->data = item;
                if(last == NULL)
                {
                                last = temp;
                }
                else
                {
                                temp->rlink = last->rlink;
                                last->rlink->llink = temp;
                }
                last->rlink = temp;
                temp->llink = last;
                return temp;
}
void display(NODE last)
{
                NODE cur;
                printf("\nContents are:\n");
                if(last == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                cur = last->rlink;
                                while(cur != last)
                                {
                                                printf("|%d | %d | %d | ==>  ", cur->llink, cur->data, cur->rlink);
                                                cur = cur->rlink;
                                }
                                printf("| %d | %d | %d |  ", cur->llink, cur->data, cur->rlink);
                 }
}
void main()
{              int ch, item;
                NODE last = NULL;
                while(1)
                {
                                printf("\n\n~~MENU~~");                           printf("\n1.Insert at End");
                                printf("\n2.Display");                                     printf("\n3.exit");
                                printf("\nEnter your choice: ");                                                            scanf("%d",&ch);
                                switch(ch)
                                {
                                                case 1:                  printf("\nEnter the value: ");
                                                                                scanf("%d",&item);
                                                                                last = insert_at_end(last, item);
                                                                                break;
                                                case 2:                  display(last);
                                                                                break;
                                                case 3:                  exit(0);
                                }
                }
}
Output:
~~MENU~~
1.Insert at End
2.Display
3.exit
Enter your choice: 1
Enter the value: 11

~~MENU~~
1.Insert at End
2.Display
3.exit
Enter your choice: 2
Contents are:
| 4994112 | 11 | 4994112 |

~~MENU~~
1.Insert at End
2.Display
3.exit
Enter your choice: 1
Enter the value: 12

~~MENU~~
1.Insert at End
2.Display
3.exit
Enter your choice: 1
Enter the value: 13

~~MENU~~
1.Insert at End
2.Display
3.exit
Enter your choice: 2
Contents are:

|4994160 | 11 | 4994136 | ==>  |4994112 | 12 | 4994160 | ==>  | 4994136 | 13 | 4994112 |