Users Online

· Guests Online: 31

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C Examples on Creating and Displaying the Elements of a Linked List

C Examples on Creating and Displaying the Elements of a Linked List

 

 

A Linked List is a dynamic data structure which contains memory blocks occupying random memory locations. The elements in the linked list are called nodes. The C programs in this section focuses on the creation of a linked list and displays the elements(nodes) of a Linked List. The other program in the sections reads the linked list in reverse.

 

 

C Program to Create a Linked List & Display the Elements in the List 
C Program to Read a Linked List in Reverse

 

 


 

C Program to Create a Linked List & Display the Elements in the List

This C Program create a linked list & display the elements in the list. Linked list is an ordered set of data elements, each containing a link to its successor. This program is to create a linked list and display all the elements present in the created list.

 

Here is source code of the C program to create a linked list & display the elements in the list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to create a linked list and display the elements in the list
  3.  */
  4. #include <stdio.h>
  5. #include <malloc.h>
  6. #include <stdlib.h>
  7.  
  8. void main()
  9. {
  10.     struct node
  11.     {
  12.         int num;
  13.         struct node *ptr;
  14.     };
  15.     typedef struct node NODE;
  16.  
  17.     NODE *head, *first, *temp = 0;
  18.     int count = 0;
  19.     int choice = 1;
  20.     first = 0;
  21.  
  22.     while (choice)
  23.     {
  24.         head  = (NODE *)malloc(sizeof(NODE));
  25.         printf("Enter the data item\n");
  26.         scanf("%d", &head-> num);
  27.         if (first != 0)
  28.         {
  29.             temp->ptr = head;
  30.             temp = head;
  31.         }
  32.         else
  33.         {
  34.             first = temp = head;
  35.         }
  36.         fflush(stdin);
  37.         printf("Do you want to continue(Type 0 or 1)?\n");
  38.         scanf("%d", &choice);
  39.  
  40.     }
  41.     temp->ptr = 0;
  42.     /*  reset temp to the beginning */
  43.     temp = first;
  44.     printf("\n status of the linked list is\n");
  45.     while (temp != 0)
  46.     {
  47.         printf("%d=>", temp->num);
  48.         count++;
  49.         temp = temp -> ptr;
  50.     }
  51.     printf("NULL\n");
  52.     printf("No. of nodes in the list = %d\n", count);
  53. }

 

$ cc pgm98.c
$ a.out
Enter the data item
5
Do you want to continue(Type 0 or 1)?
0
 
status of the linked list is
5=>NULL
No. of nodes in the list = 1
 
$ a.out
Enter the data item
5
Do you want to continue(Type 0 or 1)?
1
Enter the data item
9
Do you want to continue(Type 0 or 1)?
1
Enter the data item
3
Do you want to continue(Type 0 or 1)?
0
 
status of the linked list is
5=>9=>3=>NULL
No. of nodes in the list = 3



Here is a source code of the C Program to read a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

 

  1. /*

  2.  * C Program to Read a Linked List in Reverse

  3.  */

  4. #include <stdio.h>

  5. #include <stdlib.h>

  6.  

  7. struct node

  8. {

  9.     int num;

  10.     struct node *next;

  11. };

  12.  

  13. void create(struct node **);

  14. void reversedisplay(struct node *);

  15. void release(struct node **);

  16. void display(struct node *);

  17.  

  18. int main()

  19. {

  20.     struct node *p = NULL;

  21.     struct node_occur *head = NULL;

  22.     int n;

  23.  

  24.     printf("Enter data into the list\n");

  25.     create(&p);

  26.     printf("Displaying the nodes in the list:\n");

  27.     display(p);

  28.     printf("Displaying the list in reverse:\n");

  29.     reversedisplay(p);

  30.     release(&p);

  31.  

  32.     return 0;

  33. }

  34.  

  35. void reversedisplay(struct node *head)

  36. {

  37.     if (head != NULL)

  38.     {

  39.         reversedisplay(head->next);

  40.         printf("%d\t", head->num);

  41.     }

  42. }

  43.  

  44. void create(struct node **head)

  45. {

  46.     int c, ch;

  47.     struct node *temp, *rear;

  48.  

  49.     do

  50.     {

  51.         printf("Enter number: ");

  52.         scanf("%d", &c);

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

  54.         temp->num = c;

  55.         temp->next = NULL;

  56.         if (*head == NULL)

  57.         {

  58.             *head = temp;

  59.         }

  60.         else

  61.         {

  62.             rear->next = temp;

  63.         }

  64.         rear = temp;

  65.         printf("Do you wish to continue [1/0]: ");

  66.         scanf("%d", &ch);

  67.     } while (ch != 0);

  68.     printf("\n");

  69. }

  70.  

  71. void display(struct node *p)

  72. {

  73.     while (p != NULL)

  74.     {

  75.         printf("%d\t", p->num);

  76.         p = p->next;

  77.     }

  78.     printf("\n");

  79. }

  80.  

  81. void release(struct node **head)

  82. {

  83.     struct node *temp = *head;

  84.     *head = (*head)->next;

  85.     while ((*head) != NULL)

  86.     {

  87.         free(temp);

  88.         temp = *head;

  89.         (*head) = (*head)->next;

  90.     }

  91. }

$ cc readreverse.c

$ ./a.out

Enter data into the list

Enter number: 1

Do you wish to continue [1/0]: 1

Enter number: 2

Do you wish to continue [1/0]: 1

Enter number: 3

Do you wish to continue [1/0]: 1

Enter number: 4

Do you wish to continue [1/0]: 1

Enter number: 5

Do you wish to continue [1/0]: 0

 

Displaying the nodes in the list:

1      2      3      4      5     

Displaying the list in reverse:

5      4      3      2      1

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.83 seconds
10,799,858 unique visits