Showing posts with label Module 3. Show all posts
Showing posts with label Module 3. Show all posts

Saturday 24 September 2016

Circular Linked List with Header Node: Delete from End

Circular Linked List with Header Node: Delete from end


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

struct node
{
            int data;
            struct node *link;
};
typedef struct node* NODE;
int count = 0;              /* To count no of nodes in the list and put it into header node */

NODE getnode()
{
            NODE x;
            x = (NODE)malloc(sizeof(struct node));
            return x;
}
NODE insert_at_end(NODE head, int item)
{
            NODE temp, cur;
            temp = getnode();
            temp->data = item;
            cur = head->link;
            while(cur->link!=head)
            {
                        cur = cur->link;
            }
            cur->link = temp;
            temp->link = head;
            head->data = ++count;
            return head;
}
NODE delete_at_end(NODE head)
{
            NODE prev, cur;
            if(head->link == head)
            {
                        printf("\nCLL is empty");
                        return head;
            }
            prev = head;
            cur = head->link;
            while(cur->link!=head)
            {
                        prev = cur;
                        cur = cur->link;
            }
            prev->link = cur->link;
            printf("\nNode with item %d is deleted", cur->data);
            free(cur);
            head->data = --count;
            return head;
}

void display(NODE head)
{
            NODE temp;
            if(head->link == head)
            {
                        printf("\nList is empty.");
                        return;
            }
            printf("\nContents of the CLL:\n");
            printf("\nNo of nodes in the CLL is %d\n", head->data);
            temp = head->link;
            while(temp!=head)
            {
                        printf("|%d|%d| --> ",temp->data, temp->link);
                        temp = temp->link;
            }
}

void main()
{
            int ch, item;
            NODE head;
            head = getnode();
            head->data = 0;
            head->link = head;                  /*Empty header node*/
            while(1)
            {
                        printf("\n~~MENU~~");
                        printf("\n1:Insert at End \n2.Delete at End \n3:Display \n4:Exit \n");
                        printf("Enter the choice: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1: printf("\nEnter the item to be inserted : ");
                                                scanf("%d", &item);
                                                head = insert_at_end(head, item);
                                                break;
                                    case 2: head = delete_at_end(head);
                                                break;
                                    case 3: display(head);
                                                break;
                                    case 4: exit(0);
                        }
            }
}

Output:
~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 1
Enter the item to be inserted : 11

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 1
Enter the item to be inserted : 12

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 1
Enter the item to be inserted : 14

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 3
Contents of the CLL:
No of nodes in the CLL is 3
|11|5256304| --> |12|5256320| --> |14|5248880| -->

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 2
Node with item 14 is deleted

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 3
Contents of the CLL:
No of nodes in the CLL is 2
|11|5256304| --> |12|5248880| -->

~~MENU~~
1:Insert at End
2.Delete at End
3:Display
4:Exit
Enter the choice: 4


Circular Linked List with Header Node: Insert at End

Circular Linked List with Header Node: Insert at end


#include<stdio.h>
#include<stdlib.h>
struct node
{
            int data;
            struct node *link;
};
typedef struct node* NODE;
int count = 0;         /* To count no of nodes in the list and put it into header node */

NODE getnode()
{
            NODE x;
            x = (NODE)malloc(sizeof(struct node));
            return x;
}
NODE insert_at_end(NODE head, int item)
{
            NODE temp, cur;
            temp = getnode();
            temp->data = item;
            cur = head->link;
            while(cur->link!=head)
            {
                        cur = cur->link;
            }
            cur->link = temp;
            temp->link = head;
            head->data = ++count;
            return head;
}
void display(NODE head)
{
            NODE temp;
            if(head->link == head)
            {
                        printf("\nList is empty.");
                        return;
            }
            printf("\nContents of the CLL:\n");
            printf("\nNo of nodes in the CLL is %d\n", head->data);
            temp = head->link;
            while(temp!=head)
            {
                        printf("|%d|%d| --> ",temp->data,temp->link);
                        temp = temp->link;
            }
}
void main()
{
            int ch, item;
            NODE head;
            head = getnode();
            head->data = 0;
            head->link = head;      /*Empty header node*/
            while(1)
            {
                        printf("\n~~MENU~~");
                        printf("\n1:Insert At End \n2:Display \n3:Exit \n");
                        printf("Enter the choice: ");
                        scanf("%d", &ch);
                        switch(ch)
                        {
                                    case 1: printf("\nEnter the item to be inserted : ");
                                                scanf("%d", &item);
                                                head = insert_at_end(head, item);
                                                break;
                                    case 2: display(head);
                                                break;
                                    case 3: exit(0);
                        }
            }
}
Output:
~~MENU~~
1:Insert At End
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 11

~~MENU~~
1:Insert At End
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 12

~~MENU~~
1:Insert At End
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 14

~~MENU~~
1:Insert At End
2:Display
3:Exit
Enter the choice: 2
Contents of the CLL:
No of nodes in the CLL is 3
|11|4863088| --> |12|4863104| --> |14|4855664| -->


Circular Linked List with Header Node: Insert at Front

Circular Linked List with Header Node: Insert at Front


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

struct node
{
            int data;
            struct node *link;
};
typedef  struct node* NODE;
int count = 0;              /* To count no of nodes in the list and put it into header node */

NODE getnode()
{
            NODE x;
            x = (NODE)malloc(sizeof(struct node));
            return x;
}
NODE insert_at_front(NODE head, int item)
{
            NODE temp;
            temp = getnode();
            temp->data = item;
            temp->link = head->link;
            head->link = temp;
            head->data = ++count;
            return head;
}
void display(NODE head)
{
            NODE temp;
            if(head->link == head)
            {
                        printf("\nList is empty.");
                        return;
            }
            printf("\nContents of the CLL:\n");
            printf("\nNo of nodes in the CLL is %d\n", head->data);
            temp = head->link;
            while(temp!=head)
            {
                        printf("|%d|%d| --> ",temp->data, temp->link);
                        temp = temp->link;
            }
}
void main()
{
            int ch, item;
            NODE head;
            head = getnode();
            head->data = 0;
            head->link = head;               /*Empty header node*/
            while(1)
            {
                        printf("\n~~MENU~~");
                        printf("\n1:Insert At Front \n2:Display \n3:Exit \n");
                        printf("Enter the choice: ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1: printf("\nEnter the item to be inserted : ");
                                                scanf("%d", &item);
                                                head = insert_at_front(head, item);
                                                break;
                                    case 2: display(head);
                                                break;
                                    case 3: exit(0);
                        }
            }
}
Output:
~~MENU~~
1:Insert At Front
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 11

~~MENU~~
1:Insert At Front
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 12

~~MENU~~
1:Insert At Front
2:Display
3:Exit
Enter the choice: 2
Contents of the CLL:
No of nodes in the CLL is 2
|12|8328968| --> |11|8329072| -->

~~MENU~~
1:Insert At Front
2:Display
3:Exit
Enter the choice: 1
Enter the item to be inserted : 14

~~MENU~~
1:Insert At Front
2:Display
3:Exit
Enter the choice: 2
Contents of the CLL:
No of nodes in the CLL is 3
|14|8336504| --> |12|8328968| --> |11|8329072| -->