Come to get the Impact of Computer Knowledge...

Come to get the Impact of Computer Knowledge...
Mac Computer Education

4.1.13

Sanjeev Sir (Mac Computer Education) Associated with IIT Computers

Linked List Basic Operations
- Creation
- Display
- Insertion ( at First, in Mid, at Last)
- Deletion
- Searching
- Counting


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node{
        int data;
        struct node *next;
} *head;
void create()
{
        char ch;
        do
        {
                struct node *new_node, *current;

                new_node = (struct node *) malloc(sizeof(struct node));
                printf("\nEnter the Data: ");
                scanf("%d",&new_node->data);
                new_node->next=NULL;
                if(head == NULL)
                {
                        head = new_node;
                        current = new_node;
                }
                else
                {
                        current->next=new_node;
                        current=new_node;
                }
                printf("\n\nDo you want to create another [y/n] :");
                ch=getche();
        }while(ch!='n');
}
void insert_beg(int val)
{
        struct node *temp;

        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = val;

        temp->next = head;
        head = temp;
}
void insert_end(int val)
{
        struct node *temp, *ptr;

        temp = (struct node *) malloc(sizeof(struct node));
        temp->data = val;
        temp->next = NULL;
        ptr = head;

        while(ptr->next != NULL)
                ptr=ptr->next;

        ptr->next = temp;
}
void insert_mid(int val, int key)
{
        struct node *temp, *ptr;

        temp =(struct node*) malloc(sizeof(struct node));
        temp->data = val;

        ptr = head;
        if(head->data == key)
        {
                temp->next = head->next;
                head->next = temp;
        }
        else
        {
                while(ptr->next != NULL)
                {
                        if(ptr->next->data == key)
                        {
                                ptr = ptr->next;
                                temp->next = ptr->next;
                                ptr->next = temp;
                        }
                        ptr = ptr->next;
                }
        }
}
void delete_node( int val )
{
        struct node *ptr;
        if(head->data == val)
        {
                head = head->next;
                return;
        }
        ptr=head;
        while(ptr->next != NULL )
        {
                if(ptr->next->data == val)
                {
                        ptr->next = ptr->next->next;
                        return;
                }
                else
                        ptr = ptr->next;
        }
}
void Display_nodes()
{
        struct node *ptr;
        ptr = head;
        while(ptr != NULL)
        {
                printf("| %d |->", ptr->data);
                ptr=ptr->next;
        }
        printf("\b\b    ");
}
int count_nodes()
{
        int i=0;
        struct node *ptr;
        ptr=head;
        while(ptr!=NULL)
        {
                ptr=ptr->next;
                i++;
        }
        printf("\n\nTotal Number of Nodes: %d",i);
}
int search_node(int val)
{
        int pos=0;
        struct node *ptr;
        ptr=head;
        while(ptr!=NULL)
        {
          pos++;
                if(ptr->data == val)
                {
                        return pos;
                }
                ptr=ptr->next;
        }
}

main()
{
        int choice;
        char ch;
        int val,key;
        do
        {
        clrscr();
        printf("\n\n==============| Main Menu |======================\n");
        printf("|\t1. Create List                          |\n");
        printf("|\t2. Display List                         |\n");
        printf("|\t3. Delete Node                          |\n");
        printf("|\t4. Insert Node at First                 |\n");
        printf("|\t5. Insert Node at End                   |\n");
        printf("|\t6. Insert Node in Mid                   |\n");
        printf("|\t7. Search any Node                      |\n");
        printf("|\t8. Count Total Nodes                    |\n");
        printf("|\t9. Exit                                 |\n");
        printf("=================================================\n");
        printf("Enter Your Choice Number: ");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
                printf("\n\n------------------| List Creation |------------------\n");
                printf("\n\nFirst time creation...\n\n");
                create();
                break;
        case 2:
                printf("\n\n-------------------| Display List |-------------------\n");
                printf("\nList : ");
                Display_nodes();
                break;
        case 3:
                printf("\n\n-------------------| Node Deletion |----------------\n");
                printf("\nAvailable List is...\n");
                Display_nodes();
                printf("\n\nEnter the DATA to delete: ");
                scanf("%d",&val);
                delete_node(val);
                printf("\nNow, List is...\n");
                Display_nodes();
                break;
        case 4:
                printf("\n\n-------------------| Insert Node at First |------------\n");
                printf("\n\nEnter the Data: ");
                scanf("%d",&val);
                insert_beg(val);
                Display_nodes();
                break;
        case 5:
                printf("\n\n-------------------| Insert Node at End |------------\n");
                printf("\n\nEnter the Data: ");
                scanf("%d",&val);
                insert_end(val);
                Display_nodes();
                break;
        case 6:
                printf("\n\n-------------------| Insert Node in Mid |------------\n");
                printf("\n\nEnter the Data to insert: ");
                scanf("%d",&val);
                printf("\nEnter the Node Data (key value):");
                scanf("%d",&key);
                insert_mid(val,key);
                Display_nodes();
                break;
        case 7:
                printf("\n\n-------------------| Search any Node |---------------\n");
                printf("\n\nEnter the Data: ");
                scanf("%d",&val);
                key = search_node(val);
                printf("\n\nGiven Data:[ %d ] is at Position: %d",val,key);
                break;
        case 8:
                printf("\n\n-------------------| Count Total Nodes |--------------\n");
                count_nodes();
                break;
        case 9:
                printf("\n\n\nTHANK YOU FOR USING MY PROGRAM....\n\nPress any key to quit...");
                getch();
                exit();
        default:
                printf("\n\n\nINVALID CHOICE NUMBER\n\n\nTRY AGAIN....");
        }
        printf("\n\n\nDo you want to continue [y/n]: ");
        ch=getche();

        }while(ch!='n');

        printf("\nProgram Written By: Sanjeev Kumar\nTopic : Linked-List\n\nPress any key to quit...");

        getch();
}

0 comments: