Friday 23 September 2016

Circular Singly Linked List: Delete from End

Circular Singly Linked List: Delete from End


#include<stdio.h>
#include<stdlib.h>
struct node
{
                int data;
                struct node *link;
};
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;
}

void display(NODE last)
{
                NODE cur;
                printf("\nContents are:\n");
                if(last == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                cur = last->link;
                                printf("==> ");
                                while(cur != last)
                                {
                                                printf("| %d | %d | => ", cur->data, cur->link);
                                                cur = cur->link;
                                }
                                printf("| %d | %d | => ", cur->data, cur->link);
                }
}

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->link = last->link;
                }
                last->link = temp;
                return temp;
}

NODE delete_at_end(NODE last)
{
                NODE prev, cur;
                if(last == NULL)
                {
                                printf("\nList is empty");
                                return NULL;
                }
                if(last->link == last)
                {
                                printf("\nThe node %d is deleted", last->data);
                                free(last);
                                return NULL;
                }

                prev = last->link;
                while(prev->link!=last)
                {
                                prev = prev->link;
                }
                prev->link = last->link;
                printf("The node %d is deleted \n", last->data);
                free(last);
                return prev;
}

void main()
{
                int ch, item;
                NODE last = NULL;
                while(1)
                {
                                printf("\n\n~~MENU~~");
                                printf("\n1.Insert at end");
                                printf("\n2.Delete at 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 at end
3.Display
4.exit
Enter your choice: 1
Enter the value: 11

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

Contents are:
==> | 11 | 8591216 | =>

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

Enter the value: 12


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

Contents are:
==> | 11 | 8591112 | => | 12 | 8591216 | =>

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


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

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

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

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


Also Check,

No comments:

Post a Comment