Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Linked List program using C

LinkedList.txt
File size: 3.97 KB

File content type: text/plain

Category: Assignment

Course: B.Tech.

Semester: 3

#include<stdio.h>
#include<stdlib.h>
struct node {
	
    int data;
    struct node *next;
    
} *head = NULL;

int isEmpty() {
	
    return (head==NULL);
}

void insert_At_Beginning(int value) {
	
    struct node *temp = (struct node*)malloc(sizeof(struct node));
    temp -> data = value;
    temp -> next = head;
    head = temp;
}

void insert_At_End(int value) {
	
    struct node *temp = (struct node*)malloc(sizeof(struct node));
	struct node *current = head;
    temp -> data = value;
    temp -> next = NULL;

    if(isEmpty()) {
        head = temp;
        return;
    }

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

    current -> next = temp;
}

void insert_After_Value(int desired, int value) {
	
    struct node *current = head;

    while(current != NULL && current -> data != desired)
        current = current -> next;

    if(current == NULL)
        printf("Element %d is not in the list.\n",desired);
    else {
    	
        struct node *temp = (struct node*)malloc(sizeof(struct node));
        temp -> data = value;
        temp -> next = current -> next;
        current -> next = temp;
        
    }
}

void delete_At_Beginning() {
	
    if(isEmpty())
        printf("List is empty.\n");
    else {
    	
        struct node *temp = head;
        head = head -> next;
        temp -> next = NULL;
        free(temp);
        
    }
}

void delete_At_End() {
	
    if(isEmpty()) {
        printf("List is empty.\n");
        return ;
    }

    struct node *temp = head, *prev;

    if(head -> next == NULL) {
        head = NULL;
        free(temp);
        return ;
    }

    while(temp -> next != NULL) {
        prev = temp;
        temp = temp -> next;
    }

    prev -> next = temp -> next;
    temp -> next = NULL;
    free(temp);
}

void delete_With_Value(int desired) {
	
    if(isEmpty()) {
        printf("List is empty.\n");
        return ;
    }

    struct node *temp = head, *prev;

    if(head -> data == desired) {
    	
        head = head -> next;
        temp -> next = NULL;
        free(temp);
        return ;
    }

    while(temp != NULL && temp -> data != desired) {
    	
        prev = temp;
        temp = temp -> next;
    }

    if(temp == NULL)
        printf("Element %d not in the list\n",desired);
    else {
    	
        prev -> next = temp -> next;
        temp -> next = NULL;
        free(temp);
    }
}

void search(int desired) {
	
    struct node *temp = head;

    while(temp != NULL && temp -> data != desired)
        temp = temp -> next;

    if(temp == NULL)
        printf("Element %d not found in the list.\n",desired);
    else
        printf("Element %d is present in the list.\n",desired);
}

void printList() {

    if(isEmpty()) {
        printf("List is Empty\n");
        return ;
    }

    struct node *current = head;

    while(current -> next != NULL) {
    	printf("%d -> ",current->data);
        current = current -> next;
    }
    
	printf("%d\n",current->data);
}


void iterative_Reverse() {
	
    struct node* prev   = NULL;
    struct node* current = head;
    struct node* next;
    
    while (current != NULL) {
        next  = current->next;  
        current->next = prev;   
        prev = current;
        current = next;
    }
    
    head = prev;
}

int main() {
	
    int i;

    for(i = 0; i < 5; i++)
        insert_At_Beginning(i);

    printList();

    for(i = 5; i < 10; i++)
        insert_At_End(i);

    printList();

    insert_After_Value(5, 9);
    insert_After_Value(10, 9);

    printList();

    for(i = 0; i < 3; i++)
        delete_At_End();

    printList();

    for(i = 0; i < 3; i++)
        delete_At_Beginning();

    printList();

    delete_With_Value(1);
    delete_With_Value(5);
    printList();
    
    iterative_Reverse(head);
    printList();
	
    search(6);
    search(8);

}

If you find any error comment or send mail at [email protected]

Added by inventionsbyhamid

Comments