Untitled

 avatar
unknown
plain_text
4 years ago
1.4 kB
4
Indexable
#include <stdio.h>
#include <stdlib.h>
#define no 5

//delete alternate nodes

struct Node {
        int key;
        struct Node* next;
};

void deleteAlt(struct Node* head) {
        if(head == NULL)
                return;
        struct Node *prev = head;
        struct Node *node = head -> next;
        while(prev != NULL && node != NULL) {
                prev -> next = node -> next;
                free(node);
                prev = prev -> next;
                if(prev != NULL) 
                        node = prev -> next;
        }
}

void insertFront(struct Node** head, int new_key) {
        struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
        new_node -> key = new_key;
        new_node -> next = (*head);
        (*head) = new_node;
}

void display(struct Node* node) {
        while(node != NULL) {
                printf(" -> %d", node -> key);
                node = node -> next;
        }
        printf("\n");
}

void main() {
        struct Node *head = NULL;
        int n[no];
        printf("Enter the values to insert at front of the linked-list:\n");
        for(int i = 0; i < no; i++) {
                scanf("%d", &n[i]);
                insertFront(&head, n[i]);
        }
        printf("List before deleting alternate nodes:\n");
        display(head);
        printf("List after deleting alternate nodes:\n");
        deleteAlt(head);
        display(head);
}
Editor is loading...