Pages

Saturday, 27 May 2017

Linked List in C

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

/*
**
**
*/

struct node{
    int val;
    struct node *next;
};
struct node *head = NULL, *last = NULL;

/***********************Insert in Last*******************************/

void insertLast(int value){
    struct node *temp;
    temp = (struct node*)malloc(sizeof(struct node));

    temp->val = value;
    temp->next = NULL;

    if (head == NULL){
        head = temp;
        last = temp;
    }
    else{
        last->next = temp;
        last = temp;
    }
    //free(temp);
}

/**********************Insert in first*******************************/

void insertFirst(int value)
{
    struct node *temp, *last;
    temp = (struct node*)malloc(sizeof(struct node));

    temp->val = value;
    temp->next = NULL;

    if (head == NULL){
        head = temp;
        last = temp;
    }
    else{
        temp->next = head;
        head = temp;
    }
}
/*******Insert in Any place between first to last node*************/

void insertChoisePosition(int value){
    int num;
    printf("Enter the value position value:\n");
    scanf("%d", &num);
    struct node *temp = head,*current = NULL ;
    if (head == NULL){
        printf("List is Empty!!!!\a\n\n");
        return;
    }
    while (temp != NULL){

        if (temp->val == num){
            break;
        }
        temp = temp->next;
    }
        current = (struct node*)malloc(sizeof(struct node));
        current->val = value;
        current->next = temp->next;
        temp->next = current;



        if (temp == NULL){
          current->next = NULL;
        }

}


/**************************Print The Data****************************/

void printList(){
    struct node *temp = head;
    printf("\nThe list is : \n");
    while (temp != NULL){
        printf("%d\n", temp->val);
        temp = temp->next;
    }
}

/***********************Search The Value****************************/

int search(int value){
    struct node *temp = head;
    while (temp != NULL){
        if (temp->val == value){
            return 1;
        }
        temp = temp->next;
    }
    return 0;
}
/*********************Delete The value*****************************/

void deleteNode(int value){
    struct node *temp = head, *prev = NULL;

    while (temp != NULL){
            if (temp->val == value){
                if (prev == NULL){
                    head = temp->next;
                }
                else{
                    prev->next = temp->next;
                }
                break;
            }
            prev = temp;
            temp = temp->next;
    }
}
/*********************Main function*****************************/
int main()
{
    int num,choice;

    while (1){
        printf("1.Insert value Last\n");
        printf("2.Insert value first\n");
        printf("3.Insert value Your choice position\n");
        printf("4.Print the list \n");
        printf("5.Search the value \n");
        printf("6.Delete the value \n");
        printf("7.Quit \n");

        printf("Enter your choice : ");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            while (1){
                printf("Enter value (-1 to exit):");
                scanf("%d", &num);
            if (num==-1){
                break;
                }
                insertLast(num);
            }
            break;



        case 4:
            printList();
            break;

        case 5:
            printf("Enter the search Value: \n");
            scanf("%d", &num);

            if (search(num) == 1){
                printf("The number is here!\n");
            }
            else{
                printf("The number is not here!\n\a");
            }
            break;
            }
        }
    }

No comments:

Post a Comment