Add

C program to display and create a Linked List in Reverse

 


Create Linked List in Reverse

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

    int data;

    struct node* next;

} node;

void insert_node(node** head, int val, int position);

void insert_node(node** head, int val, int position)

{

    struct node *curr = *head, *tmp_node = NULL;

    int count = 1;

    tmp_node = (node*)malloc(sizeof(node));

    if (tmp_node == NULL) {

        printf("Memory allocation is failed:");

        return;

    }

    tmp_node->data = val;

    tmp_node->next = NULL;

    if (*head == NULL) {

        // List is empty, assigning head pointer to tmp_node

        *head = tmp_node;

        return;

    }

    if (position == 1) {

        // Inserting node at the beginning of the list

        tmp_node->next = *head;

        *head = tmp_node;

        return;

    }

    while (curr && count < position - 1) {

        curr = curr->next;

        count++;

    }

    if (position > (count + 1)) {

        printf("\n position doesn't exists in the list ");

        return;

    }

    if (count + 1 == position && curr->next == NULL) {

        // Inseting node at the end of the list

        curr->next = tmp_node;

        return;

    }

    // Inserting node in the list at given position

    tmp_node->next = curr->next;

    curr->next = tmp_node;

}

void print_list(node* head)

{

    printf("\nList elements:\n");

    while (head) {

        printf("%d ", head->data);

        head = head->next;

    }

    printf("\n");

    return;

}

int main()

{

    int num_nodes, value, index, position;

    node* head = NULL;

    printf("Enter the no. of nodes to create list: ");

    scanf("%d", &num_nodes);

    for (index = 1; index <= num_nodes; index++) {

        printf("Enter node data for position %d in the list:  ", index);

        scanf("%d", &value);

        insert_node(&head, value, index);

    }

    print_list(head);

    printf("\nInsert the element at 1st position:  ");

    scanf("%d", &value);

    insert_node(&head, value, 1);

    // We have inserted one more element, hence num_nodes get increased by 1

    num_nodes += 1;

    print_list(head);

    printf("\nInsert the element at last position:  ");

    scanf("%d", &value);

    insert_node(&head, value, num_nodes + 1);

    // We have inserted one more element, hence num_nodes will get increased by 1

    num_nodes += 1;

    print_list(head);

    printf("\nInsert the element at any position in the list\n");

    printf("Enter the position: ");

    scanf("%d", &position);

    printf("Enter the element value: ");

    scanf("%d", &value);

    insert_node(&head, value, position);

    // We have inserted one more element, hence num_nodes will get increased by 1

    num_nodes += 1;

    print_list(head);

    return 0;

}

Output

Enter the no. of nodes to create list: 5

Enter node data for position 1 in the list:  11 

Enter node data for position 2 in the list:  22 

Enter node data for position 3 in the list:  33 

Enter node data for position 4 in the list:  44 

Enter node data for position 5 in the list:  55   

List elements:  

11 22 33 44 55    

Insert the element at 1st position:  10 

List elements:  

10 11 22 33 44 55

Insert the element at last position:  20

List elements:  

10 11 22 33 44 55 20

Insert the element at any position in the list  

Enter the position: 4

Enter the element value: 40 

List elements:  

10 11 22 40 33 44 55 20

 

Display Linked List in Reverse

 

#include <stdio.h>

#include <stdlib.h>

struct node{

            int data; // data field

            struct node *next;

};

void display(struct node* head){

            struct node* current=head; // current node set to head

            printf("traversing the list...\n");

            while(current!=NULL){ //traverse until current node isn't NULL

                        printf("%d ",current->data);

                        current=current->next; // go to next node

            }

}

void reverse_display(struct node* head){

            if(head){

                        //recursive call to display in reverse order

                        reverse_display(head->next);

                        printf("%d ",head->data);

            }

}

struct node* creatnode(int d){

            struct node* temp=malloc(sizeof(struct node));

            temp->data=d;

            temp->next=NULL;

            return temp;

}

int main(){

            printf("creating the linked list by inserting new nodes at the end\n");

            printf("enter 0 to stop building the list, else enter any integer\n");

            int k,count=1,x;

            struct node* curr,*temp;

            scanf("%d",&k);

            struct node* head=creatnode(k); //buliding list, first node

            scanf("%d",&k);

            temp=head;

            ///////////////////inserting at the end//////////////////////

            while(k){

                        curr=creatnode(k);

                        temp->next=curr;//appending each node

                        temp=temp->next;

                        scanf("%d",&k);

            }

            display(head); // displaying the list

            printf("\ndisplaying in reverse order...\n");

            reverse_display(head);//display in reverse order

            return 0;

}

Output

First run:

creating the linked list by inserting new nodes at the end

enter 0 to stop building the list, else enter any integer

1 2 3 4 0

traversing the list...

1 2 3 4

displaying in reverse order...

4 3 2 1

Second run:

creating the linked list by inserting new nodes at the end

enter 0 to stop building the list, else enter any integer

34 55 2 4 76 -8 6 0

traversing the list...

34 55 2 4 76 -8 6

displaying in reverse order...

6 -8 76 4 2 55 34

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.