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.
-
/*
-
* C program to create a linked list and display the elements in the list
-
*/
-
#include <stdio.h>
-
#include <malloc.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);
-
-
}
-
temp->ptr = 0;
-
/* reset temp to the beginning */
-
temp = first;
-
printf("\n status 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);
-
}
$ 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.
/*
-
* C Program to Read a Linked List in Reverse
-
*/
-
#include <stdio.h>
-
#include <stdlib.h>
-
-
struct node
-
{
-
int num;
-
struct node *next;
-
};
-
-
void create(struct node **);
-
void reversedisplay(struct node *);
-
void release(struct node **);
-
void display(struct node *);
-
-
int main()
-
{
-
struct node *p = NULL;
-
struct node_occur *head = NULL;
-
int n;
-
-
printf("Enter data into the list\n");
-
create(&p);
-
printf("Displaying the nodes in the list:\n");
-
display(p);
-
printf("Displaying the list in reverse:\n");
-
reversedisplay(p);
-
release(&p);
-
-
return 0;
-
}
-
-
void reversedisplay(struct node *head)
-
{
-
if (head != NULL)
-
{
-
reversedisplay(head->next);
-
printf("%d\t", head->num);
-
}
-
}
-
-
void create(struct node **head)
-
{
-
int c, ch;
-
struct node *temp, *rear;
-
-
do
-
{
-
printf("Enter number: ");
-
scanf("%d", &c);
-
temp = (struct node *)malloc(sizeof(struct node));
-
temp->num = c;
-
temp->next = NULL;
-
if (*head == NULL)
-
{
-
*head = temp;
-
}
-
else
-
{
-
rear->next = temp;
-
}
-
rear = temp;
-
printf("Do you wish to continue [1/0]: ");
-
scanf("%d", &ch);
-
} while (ch != 0);
-
printf("\n");
-
}
-
-
void display(struct node *p)
-
{
-
while (p != NULL)
-
{
-
printf("%d\t", p->num);
-
p = p->next;
-
}
-
printf("\n");
-
}
-
-
void release(struct node **head)
-
{
-
struct node *temp = *head;
-
*head = (*head)->next;
-
while ((*head) != NULL)
-
{
-
free(temp);
-
temp = *head;
-
(*head) = (*head)->next;
-
}
-
}
$ 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