Experiments of Data Structure
Experiments of Data Structure
Experiments of Data Structure
/*
2. * C Program Count the Number of Occurrences of an Element in the Linked
List
3. * without using Recursion
4. */
5. #include <stdio.h>
6.
7. int occur(int [], int, int);
8.
9. int main()
10. {
11. int size, key, count;
12. int list[20];
13. int i;
14.
15. printf("Enter the size of the list: ");
16. scanf("%d", &size);
17. printf("Printing the list:\n");
18. for (i = 0; i < size; i++)
19. {
20. list[i] = rand() % size;
21. printf("%d ", list[i]);
22. }
23. printf("\nEnter the key to find it's occurence: ");
24. scanf("%d", &key);
25. count = occur(list, size, key);
26. printf("%d occurs for %d times.\n", key, count);
27. return 0;
28. }
29.
30. int occur(int list[], int size, int key)
31. {
32. int i, count = 0;
33.
34. for (i = 0; i < size; i++)
35. {
36. if (list[i] == key)
37. {
38. count += 1;
39. }
40. }
41. return count;
42. }
$ gcc occurnumber.c -o occurnumber
$ a.out
Enter the size of the list: 10
Printing the list:
3 6 7 5 3 5 6 2 9 1
Enter the key to find it's occurence: 3
h = h->next;
}
return false;
}
push(&head, 20);
push(&head, 4);
push(&head, 15);
push(&head, 10);
if (detectLoop(head))
cout << "Loop found";
else
cout << "No Loop";
return 0;
}
// This code is contributed by Geetanjali
Copy CodeRun on IDE
Output :
Loop Found
/* UTILITY FUNCTIONS */
/* Function to insert a node at the beginging of the linked
list */
void push(struct Node** head_ref, int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
return 0;
}
Copy CodeRun on IDE
Output:
Linked list before duplicate removal 11 11 11 13 13 20
if(head == NULL)
return;
/* UTILITY FUNCTIONS */
/* Function to insert a node at the begining of a Circular
linked lsit */
void push(struct Node **head_ref, int data)
{
struct Node *ptr1 = (struct Node *)malloc(sizeof(struct
Node));
struct Node *temp = *head_ref;
ptr1->data = data;
ptr1->next = *head_ref;
*head_ref = ptr1;
}
getchar();
return 0;
}
Copy CodeRun on IDE
Output:
11 2 56 12
11 2
56 12
// Update next of top and then top for stack number 'sn'
next[i] = top[sn];
top[sn] = i;
cout << "Popped element from stack 2 is " << ks.pop(2) << endl;
cout << "Popped element from stack 1 is " << ks.pop(1) << endl;
cout << "Popped element from stack 0 is " << ks.pop(0) << endl;
return 0;
}
Copy CodeRun on IDE
Output:
Popped element from stack 2 is 45
Consider two stacks s1 & s2 ( we will be using STL stacks for implementation ).
Dequeue Operation :: Transfer all elements from s1 onto s2. Pop the top element from s2. Transfer
// main
int main() {
queue q;
q.enqueue(5);
q.enqueue(11);
q.enqueue(1);
q.enqueue(7);
q.enqueue(13);
q.enqueue(11);
q.displayQueue();
int dq_element = q.dequeue();
cout<<"\nAfter dequeing :-";
q.displayQueue();
cout<<endl;
return 0;
}
#include <stdio.h>
#include <stdlib.h>
return(node);
}
root2->left = newNode(2);
root2->right = newNode(3);
root2->left->left = newNode(4);
root2->left->right = newNode(5);
if(identicalTrees(root1, root2))
printf("Both tree are identical.");
else
printf("Trees are not identical.");
getchar();
return 0;
}