Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List
- A linked list is a linear data structure containing a collection of daa elements called nodes.
- each data element contains two parts,that is ,
- payload or data,or real world information.
- The linked field contains the address of the adjacent node.

- To terminate the linked list "NULL" keyword is used.Any value which needs to be inserted further can be only inserted from NULL poistion and not from the header.
- Initialization of the LinkedList should be implemented with the concept called node. This is only a variable name which is very often used.
typedef struct node{ int data; //data is used to store values struct node *next; //node *next is a memory address of next node }Node; //redefine the structure named node as Node for convenience - The reason of defining pointer *next as struct node*next and not int *next is that we need to invoke the compiler to call the struct node recursively because doing so will link the nodes with each other.
- In this project set theory is implemented with the help of concept of single linkedList. In this linked list 5 seperate functions are defined and declared individually and then called inside the main function.
- The operations needed to be done are :
- Insert
- Display
- Compare
- Union
- Intersection
The expected output of the program is we get the union and intersection of the two lists and the data type is user defined.
Insert Function:
-
Node *insert_node(Node *head_ref,int n)//declaration of insert function { printf("\nInserting Elements at the beginning...\n");//print the statement newnode =(Node*) malloc(sizeof(Node)); //assign the block of memory for newnode using malloc, size of the block is equal to size of Node printf("Address of created newnode :%p\n",newnode);//%p is used to print the address of the memory location without any error newnode->data = n;// printf("Inserted value is :%d\n",newnode->data); newnode->next = head_ref; head_ref = newnode; return head_ref; } - Display list Function
-
void display_list(Node *head_ref)//declaration of display list function { if(head_ref == 0) //always check for null or zero value when performing operations on pointers { printf(" List is empty."); //print the statement if the condition is true } else { tail = head_ref; //tail is equal to head_ref while(tail != 0)//when tail is not equal to zero execute the action inside the loop which is { printf("%d ", tail->data);// prints the data of current node tail = tail->next;// advances the position of current node } tail = newnode;//after coming out of the loop tail should point to null position called newnode } } - get_union function:
-
Node *get_union(Node *head_pointer1,Node *head_pointer2) { while(head_pointer1 != 0) { head_union = insert_node(head_union,head_pointer1->data); head_pointer1 = head_pointer1->next; } while(head_pointer2 != 0) { if(!compare(head_union,head_pointer2->data)) head_union = insert_node(head_union,head_pointer2->data); head_pointer2 = head_pointer2->next; } return head_union; } - get_intersection:
-
Node *get_intersection(Node *head_pointer1,Node *head_pointer2) { while (head_pointer1 != 0) { if (compare(head_pointer2, head_pointer1->data)) head_intersection = insert_node(head_intersection,head_pointer1->data); head_pointer1 = head_pointer1->next; } return head_intersection; } - Compare function:
The output obtained is as shown below:

Code flow:

Hence the union and intersection of two single linkedlist is performed successfully which is similar to set theory in mathematics.