C Program to create and display a singly linked list


May 12, 2022, Learn eTutorial
976

What is a linked list?

In this c program, we have to create a linked list and display the elements in the list. A linked list is simply a set of dynamically allocated nodes arranged so that each node contains one value and one pointer. In other words, linked lists are the best example of a dynamic data structure that uses a pointer for its implementations. Then we also have to know the representation of the linked list in a c program. A pointer represents a linked list to the first node of the linked list. In other words, a linked list is a sequence of data structures that are connected using links. Every link contains a connection with other links. It is the most used data structure after arrays. In a c language, linked lists can be implemented using structures and pointers.

struct LinkedList
{
int data;
struct linkedlist *next;
};

How to create a linked list in C?

The c program's logic is to declare a structure node with members num and a pointer *ptr and set choice = 1. Using a while loop with condition choice!=0, dynamically create the head node by using the malloc function. Read the data item into the variable head->num. Check first not equal zero, then temp->ptr=head, temp=head, else set first=temp=head. Then read the choice from the user and repeat the loop, display the linked list using for loop. Finally, display the count of the list as count.

ALGORITHM

STEP 1: Include the header files to use the built-in header files into the C program.

STEP 2: Declare the structure node with members num,*ptr

STEP 3: Declare the variable NODE as type node,*head,*first,*temp=0.

STEP 4: Set count=0,choice=1,first=0.

STEP 5: By using a while loop with condition choice is true, then do step 6.

STEP 6: Set head=(NODE*) malloc(sizeof(NODE)).

STEP 7: Read the data item into the variable head->num.

STEP 8: Check if first!=0 then temp->ptr=head,temp=head.Else do step 9.

STEP 9: Assign first=temp=head.

STEP 10: Read the user's choice as they want to continue or exit and repeat step 5.

STEP 11: At the end of while loop set temp->ptr = 0,temp=first.

STEP 12: Display the status of the linked list is.

STEP 13: Using a while loop with the conditioning temp!=0, then display temp->num then increment the count by 1.

STEP 14: Assign temp=temp->ptr.

STEP 15: Display the number of nodes in the list as a count.

C Source Code

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

void main() {
  struct node {
    int num;
    struct node * ptr;
  };
  typedef struct node NODE;
  NODE * head, * first, * temp = 0;
  int count = 0;
  int choice = 1;
  first = 0;
  while (choice) {
    head = (NODE * ) malloc(sizeof(NODE));
    printf("Enter the data item\n");
    scanf("%d", & head-> num);
    if (first != 0) {
      temp -> ptr = head;
      temp = head;
    } else {
      first = temp = head;
    }
    fflush(stdin);
    printf("Do you want to continue(Type 0 or 1)?\n");
    scanf("%d", & choice);
  } /* End of while */ 
  temp = first; /* reset temp to the beginning*/
  printf("\nstatus of the linked list is\n");
  while (temp != 0) {
    printf("%d=>", temp -> num);
    count++;
    temp = temp -> ptr;
  }
  printf("NULL\n");
  printf("No. of nodes in the list = %d\n", count);
} /* End of main*/
                                      

OUTPUT

Enter the data item
10

Do you want to continue(Type 0 or 1)?
1

Enter the data item
34

Do you want to continue(Type 0 or 1)?
1

Enter the data item
56

Do you want to continue(Type 0 or 1)?
0

status of the linked list is
10=>34=>56=>NULL
No. of nodes in the list = 3