Write a program to read integers, store them in a linked list, display the content of list, reverse the list, display the reversed list, deletes the list and exits

Source Code

Brief explanation is provided after the source code.

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

typedef struct node *LINK;
struct node {
    int item;
    LINK next;
};

void list_add(LINK *head, int item);
void list_prn(LINK head);
void list_delete(LINK head);
LINK reverse(LINK head);

int main(int argc, char **argv) {
    int i;
    LINK head, r;
    head = NULL; /* empty list */
    // Generate 10 random numbers and store them in a linked list
    for (i = 1; i <= 10; i++) {
        list_add(&head, rand() % 1000);
    }
    // Display content of list
    list_prn(head);

    // Reverse list
    r = reverse(head);

    // Display content of reverse list
    printf("\nReverse List\n");
    list_prn(r);

    // Delete list
    list_delete(head);
    printf("\nList deleted\n");

    return 0;
}

// Add items to the front or beginning of the list
void list_add(LINK *head, int item) {
    LINK tmp;
    /*allocate space for new node*/
    if ((tmp = malloc(sizeof(*tmp))) == NULL) {
        printf("\nNot enough memory");
        exit(1);
    }
    tmp -> item = item;
    tmp -> next = *head;
    *head = tmp;
}

// Print list beginning with the head
void list_prn(LINK head) {
    LINK tmp = head;
    printf("List items:\n");
    while(tmp != NULL) {
        printf("%d ", tmp->item);
        tmp = tmp->next;
    }
    printf("\n");
}

// Loop through each node and free up the space used by that node
void list_delete(LINK head) {
    LINK tmp;
    while (head != NULL) {
       tmp = head -> next;
       free(head);
       head = tmp;
    }
}

// Optional code to reverse list
LINK reverse(LINK head) {
    LINK t, y = head, r = NULL;
    while(y != NULL) {
        t = y -> next;
        y -> next = r;
        r = y;
        y = t;
    }
    return r; /* r is the new head of the reverse list */
}

When you compile and execute the above program it produces the following result on Linux:

List items:
421 649 492 386 335 793 915 777 886 383 

Reverse List
List items:
383 886 777 915 793 335 386 492 649 421 

List deleted

Brief Explanation

  • A linked list is a collection of linearly connected structures.
  • A variable of type LINK is a pointer to a node on the linked list
  • The program starts by creating an empty list (list head is set to NULL).
  • A list with ten nodes is created by iteratively calling function list_add to create a node and adds the item to the list.
  • Function list_prn, display the content of the list beginning with the head.
  • Function reverse, takes a list (pointer to the head of a list) as parameter, and returns the list with the links reversed such that what used to be the tail of the list becomes the new head.
  • Function list_delete, takes a list (pointer to the head of a list) as parameter, and free up the space allocated for each node until it reaches the end of the list.

Add comment


Security code
Refresh