Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

Project 1 :

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

3. // A structure of linked list node
4. struct node

5. {
6. int data;
7. struct node *next;

8. } *LLOne, *LLTwo, *unionLL, *intersectionLL;

9. void initialize()

10. {
11. LLOne = LLTwo = NULL;
12. }
13. //Given a Inserts a node in front of a singly linked list.

14. void insert(struct node **head, int num)

15. {
16. / Create a new Linked List node
17. struct node* newNode = (struct node*) malloc(sizeof(struct node));
18. newNode->data = num;
19. // Next pointer of new node will point to head node of linked list
20. newNode->next = *head;
21. //make new node as new head of linked list
22. *head = newNode ;
23. }

24. // Searches an element in Linked List by linearly traversing from head to tail
25. int search(struct node *head, int num)

26. {
27. while (head != NULL)

28. {
29. if (head->data == num)

30. {
31. return 1;
32. }
33. head = head->next;
34. }
35. return ;

36. }

37. //Returns the union of two given linked list

38. struct node* findunion(struct node *LLOne, struct node *LLTwo)

39. {
40. unionLL = NULL;
41. // Add all nodes of first Linked List to unionLL
42. struct node *temp = LLOne;
43. while(temp != NULL)

44. {
45. insert(&unionLL, temp->data);
46. temp = temp->next ;
47. }
48. // Insert those nodes of LLTwo which is not present in LLOne
49. while(LLTwo != NULL)

50. {
51. if(!search(LLOne, LLTwo->data))

52. {
53. insert(&unionLL, LLTwo->data);
54. }
55. LLTwo = LLTwo->next ;
56. }
57. return unionLL;
58. }


59. //Returns the Linked List which contains common nodes of two given linked liststruct node*

60. intersection(struct node *LLOne, struct node *LLTwo)

61. {
62. intersectionLL = NULL;
63. //Search every element of LLOne in LLTwo, If found then add it to intersection List
64. while(LLOne != NULL)

65. {
66. if(search (LLTwo, LLOne->data))

67. {
68. insert(&intersectionLL, LLOne->data);
69. }
70. LLOne = LLOne->next;
71. }
72. return intersectionLL;
73. }
74. //Prints a linked list from head node till tail node

75. void printLinkedList(struct node *nodePtr)

76. {
77. while (nodePtr != NULL)

78. {
79. printf("%d", nodePtr->data);
80. nodePtr = nodePtr->next;
81. if(nodePtr != NULL)
82. printf("-->");

83. }
84. }

85. int main()

86. {
87. int i, LLOneCount, LLTwoCount, temp;
88. initialize();
89. //Creating First linked List
90. printf("Enter number of nodes in first Linked List \n");
91. scanf("%d", &LLOneCount);

92. printf("Enter %d integers\n", LLOneCount);
93. for(i=0; i<LLOneCount; i++)

94. {
95. scanf("%d", &temp);
96. insert(&LLOne, temp);
97. }
98. printLinkedList(LLOne);
99. printf("\nEnter number of nodes in second Linked List\n");

100. scanf("%d", &LLTwoCount);
101. printf("Enter %d integers\n", LLTwoCount);
102. for(i=0; i<LLTwoCount; i++)

103. {
104. scanf("%d", &temp);
105. insert(&LLTwo, temp);
106. }
107. printLinkedList(LLTwo);
108. //printing Union of two given linked list
109. findunion(LLOne, LLTwo);
110. intersection(LLOne, LLTwo);
111. printf("\nUnion Linked List\n");
112. printLinkedList(unionLL);
113. printf("\nIntersection Linked List\n");
114. printLinkedList(intersectionLL);

115. return 0;
116. }

out put :

1.* Enter number of nodes in First linked list

* 5

* Enter 5 Integers

* 12

* 2

* 4

* 5

* 6

* 6-->5-->4-->2-->12

* Enter number of nodes in second linked list

* 5

* Enter 5 integers

* 2

* 7

* 12

* 5

* 8

* 8-->5-->12-->2-->4-->5-->6

* Union linked list

* 7-->8-->12-->2-->4-->5-->6

* Interaction Linked List

* 12-->2-->5

* Process returned 0 (0*0) execution time : 17.767 s

2. * Enter number of nodes in First linked list

* 5

* Enter 5 Integers

* 12

* 2

* 4

* 5

* 6

* 6-->5-->4-->3-->2

* Enter number of nodes in second linked list

* 5

* Enter 5 integers

* 2

* 12

* 6

* 5

* 7

* 7-->5-->6-->12-->2

* Union linked list

* 12-->7-->2-->3-->4-->5-->6

* Interaction Linked List

* 2-->5-->6

* Process returned 0 (0*0) execution time : 14. 659 s

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.