Create a singly linked list of integers

Estudies4you

S.NO PROGRAM CODE

1 #include <stdio.h>

2 #include <alloc.h>

3 #include <stdlib.h>

4 void main()

5 {

6  struct node

7  {

8 int num;

9 struct node *ptr;

10 };

11  typedef struct node NODE;

12 NODE *head, *first, *temp=0;

13  int count = 0;

14 int choice = 1;

15 first = 0;

16 while(choice)

17 {

18 head =(NODE*) malloc(sizeof(NODE));

19 printf("Enter the data item\n");

20 scanf("%d", &head-> num);

21 if(first != 0)

22 {

23 temp->ptr = head;

24 temp = head;

25 }

26 else

27 {

28 first = temp = head;

29 }

30 fflush(stdin);

31 printf("Do you want to continue(Type 0 or 1)?\n");

32 scanf("%d", &choice);

33  }   /* End of while */

34 temp->ptr = 0;

35 temp = first;  /* reset temp to the beginning*/

36 printf("\nstatus of the linked list is\n");

37 while(temp!=0)

38 {

39 printf("%d=>", temp->num);

40 count++;

41 temp = temp -> ptr;

42 }

43 printf("NULL\n");

44 printf("No. of nodes in the list = %d\n", count);

45 }  /* End of main*/


 

Click Here to download R13  Data Structures Lab Manual

To Top