P. 1
Data Structure in C (Lab Programs)

Data Structure in C (Lab Programs)

|Views: 85|Likes:
Published by Saket
Programs are complete in best of my knowledge with zero compilation error in IDE Bloodshed Dev-C++. These can be easily portable to any versions of Visual Studio or Qt. If you need any guidance please let me know via comments and Always Enjoy Programming.
Programs are complete in best of my knowledge with zero compilation error in IDE Bloodshed Dev-C++. These can be easily portable to any versions of Visual Studio or Qt. If you need any guidance please let me know via comments and Always Enjoy Programming.

More info:

Categories:Types, Research
Published by: Saket on Apr 01, 2013
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOCX, PDF, TXT or read online from Scribd
See more
See less

06/26/2013

pdf

text

original

2013

Saket Kr. Pathak Software Developer 3D Graphics

Data Structure in C (Lab. Programs)
Programs are complete in best of my knowledge with zero compilation error in IDE Bloodshed Dev-C++. These can be easily portable to any versions of Visual Studio or Qt. If you need any guidance please let me know via comments and Always Enjoy Programming.

Data Structure in C (Lab. Programs)
Content (Programs List) S. No. Subject WAP in C for following Sorting Methods 1. Date Sign Remark

2. 3.

4.

5. 6.

7. 8. 9.

 Bubble Sort  Merge Sort  Insertion Sort  Selection Sort  Quick Sort WAP in C for following Searching Methods  Linear Search  Binary Search WAP in C for array implementation of  Stack  Queue  Circular Queue  Linked List WAP in C using dynamic memory allocation of  Stack  Queue  Circular Queue  Linked List WAP in C for implementation of Binary Tree. WAP in C for Tree Traversal  Pre-Order  In-Order  Post-Order WAP in C for Graph Traversal  Breadth First Search  Depth First Search WAP in C for Minimum Cost Spanning Tree WAP in C for Shortest Path Problem

Saket Kr. Pathak

Page 2

Data Structure in C (Lab. Programs)

1. WAP in C for following Sorting Methods  Bubble Sort  Merge Sort  Insertion Sort  Selection Sort  Quick Sort Program: Bubble Sort: Algorithm – [1] Compare each pair of adjacent elements from the beginning of an array and, if they are in reversed order, swap them. [2] If at least one swap has been done, repeat step 1. Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include <stdio.h> int* bubble_sort(int i_store[], int i_size); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("\n\n\n"); printf("\t\t\tWAP of Bubble sort."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = bubble_sort(ip_store, i_store_size); O (n) time O (n2) time O (n2) time

Saket Kr. Pathak

Page 3

int i_count_0.1). } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size). (ip_store+i_count)). i_store_size). i_count_1 <= i_count_0. printf("\n\n\n"). for (i_count_0 = (i_size . disp_elem(i_store. i_count++. int i_count = 0. printf("\n\nSwapping Steps: \n").1] = i_store[i_count_1]. return 0.Data Structure in C (Lab. Programs) printf("\n\nResult: \n"). i_store[i_count_1 . int i_size) { int i_temp. i_count_0 > 0. --i_count_0) { for (i_count_1 = 1. ++i_count_1) { if (i_store[i_count_1 . disp_elem(ip_store. scanf("%d".1] > i_store[i_count_1]) { i_temp = i_store[i_count_1 . while (i_temp_size) { printf("\nEnter item for index . } int* bubble_sort(int i_store[]. int i_temp_size = i_size. } disp_elem(ip_store. i_size). } } Saket Kr. system("pause").1]. return ip_store. i_count). Pathak Page 4 . i_size). i_temp_size--. i_count_1.%d : ". i_store[i_count_1] = i_temp.

} void disp_elem(int i_store[]. Pathak Page 5 O (n * log (n)) time O (n * log (n)) time O (n * log (n)) time .h> #include<stdbool.i_store[i_ndx]). printf("Elements stored in order: "). int i_size) { printf("\n------------------------------------------------------\n").h> #define ARRAY_SIZE 1024 Saket Kr. while (i_size) { printf("| %d |". Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include<stdio. [4] Merge the two sorted subsequences to form the output sequence.Data Structure in C (Lab. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Merge Sort: Algorithm – [1] If the input sequence has fewer than two elements. return. i_size--. i_ndx++. } printf("\n------------------------------------------------------"). Programs) } return i_store. [3] Sort the two subsequences using the same algorithm. [2] Partition the input sequence into two halves. int i_ndx = 0.

void divide_(int* i_store. } int i_mid = (i_low + i_high) / 2. printf("\n\n\n"). conquer_(i_store. i_low. int main() { printf("\n\n\n"). i_mid. int i_low. i_high. int i_height). int i_count. int i_low. } void divide_(int* i_store. disp_elements(i_store). divide_(i_store. 0). int i_low. for (i_count = i_low. int i_high). void combine_(int* storage. i_low. int* get_elements(void). i_store = get_elements(). } } Saket Kr. Programs) int i_size. int i_low. i_mid. i_low. int* i_store. bool b_flag). 0). i_mid + 1.").Data Structure in C (Lab. 1). int i_high. i_high). void disp_elements(int*). void conquer_(int* ip_storage. bool b_flag) { printf("\nDivide low(%d) | high(%d) | flag(%d): ". i_size-1. ++i_count) { printf("%d". if (i_low < i_high) { divide_(i_store. printf("\t\t\tWAP of Merge sort. int i_mid. b_flag). return 0. int i_high. int* temp_arr_2. Pathak Page 6 . i_high. divide_(i_store. getch(). 0. *(i_store + i_count)). i_count <= i_high. int* temp_arr_1.

for(j=0. n2 = i_high . ip_temp_store_2[j] = 10000000. ip_temp_store_1. j++) { ip_temp_store_2[j] = ip_storage[i_mid+j+1]. i_high). int k. Programs) void conquer_(int* ip_storage. i++) { ip_temp_store_1[i] = ip_storage[i_low+i]. else Saket Kr. k++) { if (temp_arr_1[i] <= temp_arr_2[j]) storage[k] = temp_arr_1[i++]. } void combine_(int* storage. printf("%d". for(i=0. int i_low.i. int n1. ip_temp_store_2. int* temp_arr_2. printf("\nResult: ").n2. i_low. Pathak Page 7 . int j=0. } printf("\n\tip_temp_store_2: "). combine_(ip_storage. i_mid.i_mid. j. int* temp_arr_1.j. } // To mark the end of each temporary array ip_temp_store_1[i] = 10000000. int* ip_temp_store_2 = malloc(sizeof(int) * ARRAY_SIZE). i_high). int i_mid. i_low. for (k=i_low. ip_temp_store_1[i]).i_low + 1. int i_low. j<n2.Data Structure in C (Lab. i<n1. int i_high) { int* ip_temp_store_1 = malloc(sizeof(int) * ARRAY_SIZE). int i_height) { int i=0. i. printf("\n\tMerge low(%d) | mid(%d) | high(%d): ". n1 = i_mid . k<=i_height. printf("\n\tip_temp_store_1: "). ip_temp_store_2[j]).k. printf("%d".

} int* get_elements(void) { int i_count. ++i_count) { printf("%d".*(i_storage + i_count)). scanf("%d". int i_count. i_count). i_count++) { printf("Enter the element at pos[%d]: ". } printf("\n"). Programs) storage[k]=temp_arr_2[j++]. int* i_storage = (int*) malloc(sizeof(int) * i_size). printf("Elements: ").Data Structure in C (Lab. } return i_storage. printf("Enter the size of array: "). i_count < i_size. i_count < i_size. scanf("%d". Pathak Page 8 . for (i_count = 0.&i_size). storage[k]). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Insertion Sort: Algorithm – Saket Kr. for(i_count = 0. } printf("\n\n\n").(i_storage + i_count)). } void disp_elements(int* i_storage) { printf("\n\n\n"). printf("%d".

scanf("%d". starting from the highest (rightmost) element. printf("\n\n\n"). [3] Compare the removed element against the current result. int i_size). } int* get_elem(int i_size) Saket Kr. and start again from (2) with the next input element. int* ip_store = get_elem(i_store_size). int i_size). [2] Loop over the input until it is empty."). ip_store = insertion_sort(ip_store. and repeat with the next lowest result element. &i_store_size). int main() { printf("\n\n\n").Data Structure in C (Lab. Programs) [1] Start with the result as the first element of the input. i_store_size). disp_elem(ip_store. void disp_elem(int i_store[]. Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include <stdio. [5] Otherwise. printf("\n\n\n"). int* get_elem(int i_size). int i_store_size. printf("Enter the total number of items to store: "). return 0. [4] If the removed input element is lower than the current result element. "removing" the first remaining (leftmost) element. save it in the cell left by copying the last examined result up. the new element is in the correct location. printf("\t\t\tWAP of Insertion sort. Pathak Page 9 O (n) time O (n2) time O (n2) time .h> int* insertion_sort(int i_store[]. copy that value into the following element to make room for the new element below. system("pause"). i_store_size). and working left towards the lowest element.

++i_count_1) { if (i_store[i_count_1] > i_store[i_count_0]) { i_temp = i_store[i_count_1] . i_count_0 <= (i_size-1). int i_size) { int i_temp. i_store[i_count_2 + 1] = i_temp . i_count++. i_count_2--) i_store[i_count_2] = i_store[i_count_2 . i_count_2 > i_count_1. for (i_count_0 = 1. int i_count_0.%d : ".Data Structure in C (Lab. } return ip_store. i_size--. Saket Kr. scanf("%d". int i_size) { printf("\nDisplaying Elements of store: ").1] . Programs) { int* ip_store = malloc(sizeof(int) * i_size). for (i_count_2 = i_count_0. i_count). i_count_1 < i_count_0. i_count_2. i_count_1. int i_ndx = 0. i_store[i_count_1] = i_store[i_count_0] . } void disp_elem(int i_store[]. Pathak Page 10 . (ip_store+i_count)). } } } return i_store. } int* insertion_sort(int i_store[]. while (i_size) { printf("\nEnter item for index . i_store[i_ndx]). while (i_size) { printf("%d". int i_count = 0. ++i_count_0) { for (i_count_1 = 0.

printf("\t\t\tWAP of Selection sort. Saket Kr. [3] Repeat steps 4 through 6 until the unsorted section is empty. printf("Enter the total number of items to store: "). int* ip_store = get_elem(i_store_size). scanf("%d". int* get_elem(int i_size). [4] Select the first unsorted card. } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Selection Sort: Algorithm – [1] Get a hand of unsorted cards/numbers. [5] Swap this card to the left until it arrives at the correct sorted position. i_size--.h> int* selection_sort(int i_store[]. i_store_size). Programs) i_ndx++. int i_size). int i_size)."). printf("\n\n\n"). int main() { printf("\n\n\n"). int i_store_size. ip_store = selection_sort(ip_store. [6] Advance the marker to the right one card.Data Structure in C (Lab. [2] Set a marker for the sorted section after the first card of the hand. [7] Stop Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include <stdio. void disp_elem(int i_store[]. Pathak Page 11 O (n2) time O (n2) time O (n2) time . &i_store_size).

Data Structure in C (Lab. Programs)
disp_elem(ip_store, i_store_size); printf("\n\n\n"); system("pause"); return 0; } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("\nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store; } int* selection_sort(int i_store[], int i_size) { int i_temp; int i_count_0, i_count_1; for (i_count_0 = 0; i_count_0 < (i_size-1); ++i_count_0) { for (i_count_1 = (i_count_0+1); i_count_1 < i_size; ++i_count_1) { if (i_store[i_count_0] > i_store[i_count_1]) { i_temp = i_store[i_count_0]; i_store[i_count_0] = i_store[i_count_1]; i_store[i_count_1] = i_temp; } } } return i_store; } void disp_elem(int i_store[], int i_size) { printf("\nDisplaying Elements of store: "); int i_ndx = 0; Saket Kr. Pathak Page 12

Data Structure in C (Lab. Programs)
while (i_size) { printf("%d", i_store[i_ndx]); i_ndx++; i_size--; } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Quick Sort: Algorithm – [1] Choosing the pivot. [2] Partitioning. [3] Recursively quick-sort the left and the right parts. Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include <stdio.h> int* quick_sort(int i_store[], int i_start, int i_end); int* get_elem(int i_size); void disp_elem(int i_store[], int i_size); int main() { printf("\n\n\n"); printf("\t\t\tWAP of Quick sort."); printf("\n\n\n"); int i_store_size; printf("Enter the total number of items to store: "); scanf("%d", &i_store_size); int* ip_store = get_elem(i_store_size); ip_store = quick_sort(ip_store, 0, i_store_size); disp_elem(ip_store, i_store_size); Saket Kr. Pathak Page 13 O (n * log (n)) time O (n * log (n)) time O (n2) time

Data Structure in C (Lab. Programs)
printf("\n\n\n"); system("pause"); return 0; } int* get_elem(int i_size) { int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; while (i_size) { printf("\nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_size--; } return ip_store; } int* quick_sort(int i_store[], int i_start, int i_end) { int i_begin, i_finish, i_pivot, i_temp; if (i_finish > i_begin) { i_begin = i_start; i_finish = i_end; i_pivot = i_begin; while (i_begin < i_finish) { while ((i_store[i_begin] <= i_store[i_pivot]) && (i_begin < i_end)) { ++i_begin; } while (i_store[i_finish] > i_store[i_pivot]) { --i_finish; } if (i_begin < i_finish) { i_temp = i_store[i_begin]; Saket Kr. Pathak Page 14

i_ndx++.1). quick_sort(i_store.Data Structure in C (Lab. i_store[i_finish] = i_temp. i_size--. while (i_size) { printf("%d". } } i_temp = i_store[i_pivot]. quick_sort(i_store. i_start. i_store[i_pivot] = i_store[i_finish]. i_finish . } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Saket Kr. int i_size) { printf("\nDisplaying Elements of store: "). } void disp_elem(int i_store[]. Pathak Page 15 . i_end). int i_ndx = 0. i_store[i_finish] = i_temp. i_store[i_ndx]). i_finish + 1. Programs) i_store[i_begin] = i_store[i_finish]. } return i_store.

i_count). i_count < i_num_item. i_srch_item. for (i_count = 0.h> <stdbool. [2] Output: first index where key’s phone number matches D.Data Structure in C (Lab. or -1 if not found Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include #include #include #include #include <stdio. int i_srch_item.h> <string. Programs) 2. bool b_flag = false.h> <math. printf("Item (%d) is found at position: %d". } } if (!b_flag) Saket Kr. ++i_count) { if (i_storage[i_count] == i_srch_item) { b_flag = true. break.h> O (1) time O (n) time O (n) time void linear_search(int* i_storage. WAP in C for following Searching Methods  Linear Search  Binary Search Program: Linear Search: Algorithm – [1] Input: Array D of Business objects.h> <stdlib. int i_num_item) { int i_count. phone number key. Pathak Page 16 .

". linear_search(i_storage. get_input(i_storage. printf("\nLimitation: \n\t-> Items are restrickted with integer number. } printf("\n\n\n"). int i_loop_count. 9.Data Structure in C (Lab.\n\t-> Starting index of storage is 0. i_num_item). scanf("%d". (sizeof(i_storage)/sizeof(int))). int i_num_item) { printf("Your Storage Items are: \n\t"). } void set_argument(void) { printf("WAP for Linear . } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Saket Kr."). i_loop_count < i_num_item. printf("\n\n\n"). int i_storage[] = {1. 7. } void get_input(int* i_storage. 6. 5. Pathak Page 17 . int i_srch_item. printf("Please Enter the item (number) to search: ").").Search. 2. getch(). printf("\n\n\n"). return 0. &i_srch_item). 8."). printf("\n\n\n"). 10}. 4. i_srch_item. ++i_loop_count) { printf("%d. Programs) printf("Item not found. } int main() { set_argument(). for(i_loop_count = 0. i_storage[i_loop_count]). 3.

go to the step 1 for the part of the array.h> <string. than the middle element. int i_srch_item. [2] If the middle element equals to the searched value. i_mid = (i_low + i_hi)/2. return true. the algorithm stops.List is in Sorted order.h> O (1) time O (log (n)) time O (n) time bool check_item_order(int* i_storage) { //Function to check the Item . [3] Otherwise.h> <stdbool.h> <stdlib. before middle element. after middle element.  Searched value is greater. Saket Kr. two cases are possible:  Searched value is less. int i_low. In this case. } else if(i_storage[i_mid] > i_srch_item) { i_hi = i_mid. Time Complexity – Best case: Average case: Worst case: Code – Snippet: #include #include #include #include #include <stdio. than the middle element. Programs) Binary Search: Algorithm – [1] Get the middle element. go to the step 1 for the part of the array. int i_hi) { if(i_low < i_hi) { if(i_storage[i_mid] == i_srch_item) { printf("Item (%d) is found at position: %d". } void recursive_bin_search(int* i_storage. i_srch_item. In this case. int i_mid.Data Structure in C (Lab.h> <math. Pathak Page 18 . i_mid).

i_mid.Data Structure in C (Lab. i_low. } printf("\n\n\n"). printf("Your Storage Items are: \n\t"). Programs) recursive_bin_search(i_storage.\n"). i_mid = (i_low + i_hi)/2. } void binary_search(void) Saket Kr. &i_srch_item). if(b_check) recursive_bin_search(i_storage. int i_loop_count. scanf("%d". int i_mid = (i_low + i_hi)/2. i_hi). int i_num_item) { bool b_check = check_item_order(i_storage). i_mid. i_hi). ++i_loop_count) { printf("%d. Pathak Page 19 . i_low. } } } void recursive_binary_search(int* i_storage. printf("\n\n\n"). int i_hi = i_num_item. i_srch_item. else printf("\nItems are not in Order. recursive_bin_search(i_storage. i_srch_item. printf("Please Enter the item (number) to search: "). ". for(i_loop_count = 0. i_srch_item. i_storage[i_loop_count]). } else if(i_storage[i_mid] < i_srch_item) { i_low = i_mid. i_mid. i_loop_count < i_num_item. i_low. i_hi). int i_low = 0. int i_srch_item.

10}. 9. 2. printf("\n\n\n"). 6. 5."). 3. int i_storage[] = {1.Data Structure in C (Lab. Pathak Page 20 . 4. printf("\n\n\n"). printf("\nLimitation: \n\t-> Items are restrickted with integer number. (sizeof(i_storage)/sizeof(int))). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Saket Kr. Programs) { printf("WAP for Binary .Search. getch(). } int main() { binary_search(). return 0. recursive_binary_search(i_storage. 8.").\n\t-> Starting index of storage is 0. 7.

h> #include <stdbool. Programs) 3.Data Structure in C (Lab."). Pop Item: \t\t(Press) 2"). printf("\n\n\tPlease Enter Your Choice: "). WAP in C for array implementation of  Stack  Queue  Circular Queue  Linked List Program: Stack: Code – Snippet: #include <stdio. int stack[STACK_SIZE]. else return 0. } else Saket Kr. return 0. Exit: \t\t(Press) 4"). int i_choice. if((i_choice > 0) && (i_choice < 5)) return i_choice. Display Item: \t(Press) 3"). &i_choice). } int push_item(int i_item) { if(i_top == (STACK_SIZE .1)) { printf("\n\tStack Overflow. scanf("%d". Pathak Page 21 . int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1").h> #define STACK_SIZE 1024 int i_top = -1.

} } int disp_item(void) { if(i_top == -1) { printf("\n\tStack is Empty. Pathak Page 22 . b_flag = true. } else { Saket Kr. int i_count."). i_item). Programs) { stack[++i_top] = i_item.".2). } else if(b_flag) { stack[i_count] = stack[i_count+1]. has successfully pushed into Stack. } } int pop_item(int i_item) { if(i_top == -1) { printf("\n\tStack is Underflow. return 0. //Substracting: 2 = (additional loop increment + 1 deleted item) return 1. for(i_count = 0. } else { bool b_flag = false. return 0.Data Structure in C (Lab. } } i_top = (i_count . i_count <= i_top."). printf("\n\tItem . ++i_count) { if((stack[i_count] == i_item)&&(!b_flag)) { stack[i_count] = stack[i_count+1]. return 1.%d.

Data Structure in C (Lab."). Programs) int i_count. i_count. } } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Stack. } return 1. int i_check = push_item(i_item). for(i_count = 0. stack[i_count]). if(i_check == 1) return 1. scanf("%d". printf("\n\tElements of Stack are:"). } case 2: { printf("\n\tTo Pop Item from Stack. &i_item). i_count <= i_top. int i_check = pop_item(i_item). if(i_check == 1) return 1. Pathak Page 23 . break."). printf("\n\tPlease Enter the item: "). Saket Kr. break. scanf("%d". ++i_count) { printf("\n\tIndex: %d | Item: %d". else return 0. } case 3: { printf("\n\tTo Display Item of Stack."). int i_item = 0. if(i_check == 1) return 1. int i_check = disp_item(). int i_item = 0. else return 0. &i_item). printf("\n\tPlease Enter the item: ").

if(i_check == 0) printf("\n\n\n\tInvalid input. printf("\n\t\t\t Array . Saket Kr. i_check = set_Argument(). break. } } } int set_Argument(void) { printf("\n\t-------------------------------------------------\n"). if(i_check == 0) printf("\n\n\n\tInvalid input."). } default: { return 0.").Data Structure in C (Lab. printf("\n\t-------------------------------------------------\n\n"). return 0. break. Pathak Page 24 . else { int i_state. if(i_check == 4) //Check for Exit. } int main() { int i_check = set_Argument().\n"). return i_check. Programs) else return 0. int i_check = select_choice(). } case 4: { printf("\n\tTo Exit.Container. break. do { i_state = process_stack(i_check). else .").

do { i_state = process_stack(i_check). Saket Kr. } printf("\n\n\n"). int i_rear = -1. i_state = 0. getch(). if(i_check == 0) printf("\n\n\n\tInvalid input. else { int i_state.h> #define QUEUE_SIZE 1024 int i_front = -1. if(i_check == 4) for Exit. int queue[QUEUE_SIZE]. int main() { int i_check = set_Argument(). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Queue: Code – Snippet: #include <stdio."). return 0. } printf("\n\n\n"). Programs) i_state = 0. Pathak Page 25 //Check .Data Structure in C (Lab. } while(i_state == 1). i_check = set_Argument(). } while(i_state == 1).h> #include <stdbool.

} int push_item(int i_item) { if(i_rear == (QUEUE_SIZE . return i_check. printf("\n\t-------------------------------------------------\n\n"). int i_check = select_choice(). Exit: \t\t(Press) 4"). if((i_choice > 0) && (i_choice < 5)) return i_choice.\n")."). Display Item: \t(Press) 3"). else . return 0. printf("\n\n\tPlease Enter Your Choice: ").Container. Pop Item: \t\t(Press) 2").Data Structure in C (Lab. int i_choice. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). } else Saket Kr. printf("\n\t\t\t Array . else return 0. scanf("%d". } int set_Argument(void) { printf("\n\t-------------------------------------------------\n"). return 0.1)) { printf("\n\tQueue Overflow. Programs) getch()."). if(i_check == 0) printf("\n\n\n\tInvalid input. &i_choice). Pathak Page 26 .

i_count <= i_rear. printf("\n\tItem . return 0. } } int disp_item(void) { if(i_front == -1) { printf("\n\tQueue is Empty. //Substracting: 2 = (additional loop increment + 1 deleted item) return 1. for(i_count = i_front. return 0. i_item). ++i_count) { if((queue[i_count] == i_item)&&(!b_flag)) { queue[i_count] = queue[i_count+1]. queue[++i_rear] = i_item.". } else Saket Kr. Programs) { i_front = 0. b_flag = true.Data Structure in C (Lab."). } else if(b_flag) { queue[i_count] = queue[i_count+1]. } else { bool b_flag = false.%d.2). } } int pop_item(int i_item) { if((i_front == -1)||(i_front > i_rear)) { printf("\n\tQueue is Underflow. return 1."). } } i_rear = (i_count . Pathak Page 27 . int i_count. has successfully pushed into Stack.

Data Structure in C (Lab. Programs)
{ printf("\n\tElements of Queue are:"); int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { printf("\n\tIndex: %d | Item: %d", i_count, queue[i_count]); } return 1; } } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Queue."); int i_item = 0; printf("\n\tPlease Enter the item: "); scanf("%d", &i_item); int i_check = push_item(i_item); if(i_check == 1) return 1; else return 0; break; } case 2: { printf("\n\tTo Pop Item from Queue."); int i_item = 0; printf("\n\tPlease Enter the item: "); scanf("%d", &i_item); int i_check = pop_item(i_item); if(i_check == 1) return 1; else return 0; break; } case 3: { printf("\n\tTo Display Item of Queue."); int i_check = disp_item(); if(i_check == 1) Saket Kr. Pathak Page 28

Data Structure in C (Lab. Programs)
return 1; else return 0; break; } case 4: { printf("\n\tTo Exit."); return 0; break; } default: { return 0; break; } } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Circular Queue: Code – Snippet: #include <stdio.h> #include <stdbool.h> #define QUEUE_SIZE 1024 int i_front = -1; int i_rear = -1; int circular_queue[QUEUE_SIZE]; int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo }

Push Item: \t\t(Press) 1"); Pop Item: \t\t(Press) 2"); Display Item: \t(Press) 3"); Exit: \t\t(Press) 4");

int i_choice; printf("\n\n\tPlease Enter Your Choice: "); Saket Kr. Pathak Page 29

Data Structure in C (Lab. Programs)
scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0; } int push_item(int i_item) { if(((i_front == 0) && (i_rear == (QUEUE_SIZE - 1))) || (i_front == i_rear + 1)) { printf("\n\tQueue Overflow."); return 0; } else { if(i_rear == -1) { i_rear = 0; i_front = 0; } else if(i_rear == QUEUE_SIZE-1) i_rear = 0; else i_rear++; circular_queue[i_rear] = i_item; printf("\n\tItem - %d, has successfully pushed into Stack.", i_item); return 1; } } int pop_item(int i_item) { if(i_front == -1) { printf("\n\tQueue is Underflow."); return 0; } else { bool b_flag = false; int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { Saket Kr. Pathak Page 30

return 0. Programs) if((circular_queue[i_count] == i_item)&&(!b_flag)) { circular_queue[i_count] = circular_queue[i_count+1]. //Substracting: 2 = (additional loop increment + 1 deleted item) return 1."). Pathak Page 31 . i_count <= i_rear. } else if(b_flag) { circular_queue[i_count] = circular_queue[i_count+1]. b_flag = true.Data Structure in C (Lab. Saket Kr. } } int disp_item(void) { if((i_front == -1) || (i_front == i_rear + 1)) { printf("\n\tQueue is Empty. i_count. } return 1. for(i_count = i_front."). circular_queue[i_count]).2). int i_count. } } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Queue. } else { printf("\n\tElements of Queue are:"). ++i_count) { printf("\n\tIndex: %d | Item: %d". } } i_rear = (i_count .

if(i_check == 1) return 1. printf("\n\tPlease Enter the item: "). } case 2: { printf("\n\tTo Pop Item from Queue. break. break. } default: { return 0. break. scanf("%d". &i_item). printf("\n\tPlease Enter the item: "). Programs) int i_item = 0. } } } Saket Kr."). return 0. break. } case 3: { printf("\n\tTo Display Item of Queue. int i_check = push_item(i_item)."). else return 0. if(i_check == 1) return 1. int i_check = pop_item(i_item). int i_item = 0. Pathak Page 32 . scanf("%d"."). &i_item). } case 4: { printf("\n\tTo Exit. break. else return 0. if(i_check == 1) return 1. else return 0.Data Structure in C (Lab. int i_check = disp_item().

} int main() { int i_check = set_Argument(). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Linked List: Code – Snippet: (Singly Linked List) Saket Kr. Pathak Page 33 . getch(). printf("\n\t-------------------------------------------------\n\n"). if(i_check == 4) //Check for Exit. else { int i_state.\n"). i_state = 0. return 0. printf("\n\t\t\t Array . if(i_check == 0) printf("\n\n\n\tInvalid input. else . } printf("\n\n\n"). i_check = set_Argument().Container."). Programs) int set_Argument(void) { printf("\n\t-------------------------------------------------\n"). } while(i_state == 1). if(i_check == 0) printf("\n\n\n\tInvalid input."). do { i_state = process_stack(i_check). return i_check. int i_check = select_choice().Data Structure in C (Lab.

if(i_check == 0) printf("\n\n\n\tInvalid input. } while(i_state == 1). getch(). process_stack(int i_choice). i_state = 0. int main() { int i_check = set_argument().Data Structure in C (Lab. select_choice(void). else { int i_state. push_node(int i_item.h> #include <stdbool. do { i_state = process_stack(i_check). } int set_argument(void) { printf("\n\t-------------------------------------------------\n"). if(i_check == 4) //Check for Exit. }ll_node[LINKED_LIST_SIZE]. i_check = set_argument(). Pathak Page 34 . int i_ll_size = 0. int i_indx). int i_next_idx.h> #define LINKED_LIST_SIZE 1024 int int int int int int set_argument(void). disp_node(void). Programs) #include <stdio. Saket Kr. struct node { int i_val. } printf("\n\n\n")."). pop_node(int i_option). return 0.

scanf("%d". int i_choice.\n"). scanf("%d". Programs) printf("\n\t\t\t Array . &i_item). int i_indx = 0. Pathak Page 35 . printf("\n\tPlease Enter the index: ")."). if((i_choice > 0) && (i_choice < 5)) return i_choice. int i_check = push_node(i_item.Singly Linked-List. printf("\n\t-------------------------------------------------\n\n"). scanf("%d". Saket Kr. &i_choice). int i_item = 0. } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). else return 0. Exit: \t\t(Press) 4"). Pop Item: \t\t(Press) 2"). Display Item: \t(Press) 3").Data Structure in C (Lab. return i_check.\n"). i_indx). printf("\n\n\tPlease Enter Your Choice: "). if(i_check == 0) printf("\n\n\n\tInvalid input. if(i_check == 1) return 1. &i_indx). int i_check = select_choice(). else . printf("\n\tPlease Enter the item: ").

{ if (i_check == 0) printf("\n\n\n\tInvalid input. else return 0. break. } default: { return 0. } i_check = pop_node(i_check). if(i_check == 1) return 1."). Pathak Page 36 . break. int i_check = del_option()."). if(i_check == 1) return 1.Data Structure in C (Lab. } case 2: { printf("\n\tTo Pop Item from Queue. Programs) else return 0. return 0."). break."). int i_check = disp_node(). } } } int del_option(void) { Saket Kr. else . break. else return 0. } case 3: { printf("\n\tTo Display Item of Queue. break. } case 4: { printf("\n\tTo Exit.

i_count = i_nxt_idx. i_size++. Programs) printf("\n\t\t---------------------------"). i_size < i_ll_size. Pathak Page 37 .Data Structure in C (Lab. if((i_choice > 0) && (i_choice < 5)) return i_choice. printf("\n\n\t\tPlease Enter Your Choice: ").i_val. } int pop_node(int i_option) { if (i_option == 1) { int i_del_item. &i_choice). } else if (b_flag) { Saket Kr. { printf("\n\t\tBy Item: \t\t(Press) 1").i_next_idx.i_next_idx = ll_node[i_nxt_idx]. printf("\n\t\t---------------------------"). ) { if ((ll_node[i_count]. i_count < (LINKED_LIST_SIZE-1).i_val = ll_node[i_nxt_idx]. printf("\n\tItem to delete: "). bool b_flag = false. b_flag = true. int i_size = 0. for (i_count = 0. scanf("%d". ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count]. scanf("%d". } int i_choice.i_val == i_del_item) { int i_nxt_idx = ll_node[i_count]. &i_del_item). int i_count = 0.i_next_idx. ll_node[i_count]. else return 0. printf("\n\t\tBy Reference: \t\t(Press) 2").

i_val = ll_node[i_nxt_idx]. b_flag = true.i_next_idx. ll_node[i_count].i_next_idx.i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count]. &i_del_idx).i_next_idx == i_del_idx) { int i_nxt_idx = ll_node[i_count].i_next_idx = ll_node[i_nxt_idx]. i_count = i_nxt_idx. scanf("%d".i_val. } else { i_count = ll_node[i_count]. i_size++. int i_size = 0. } } else if (i_option == 2) { int i_del_idx. ll_node[i_count]. printf("\n\tIndex to delete: "). for (i_count = 0. bool b_flag = false.Data Structure in C (Lab. i_count < (LINKED_LIST_SIZE-1). i_size++.i_val = ll_node[i_nxt_idx]. } } else break. int i_count = 0. Programs) int i_nxt_idx = ll_node[i_count]. Saket Kr. ll_node[i_count]. Pathak Page 38 . i_size < i_ll_size.i_next_idx. ) { if ((ll_node[i_count]. } else if (b_flag) { int i_nxt_idx = ll_node[i_count]. i_count = i_nxt_idx. i_size++.i_next_idx = ll_node[i_nxt_idx].i_next_idx. ll_node[i_count].i_next_idx.i_next_idx.i_val.

i_size++. int i_size = 0. } int push_node(int i_item.i_next_idx = ll_node[i_nxt_idx].i_next_idx = i_indx. i_count < (LINKED_LIST_SIZE-1). int i_indx) { if (i_indx < (LINKED_LIST_SIZE-1)) { ll_node[i_ll_size]. i_count = i_nxt_idx. ll_node[i_count].i_next_idx = 0.i_val. ll_node[i_ll_size]. i_ll_size = i_indx. } int disp_node(void) { int i_count = 0. } } else break. for (i_count = 0. //------------return 1.i_val = ll_node[i_nxt_idx].Data Structure in C (Lab. i_size++.i_next_idx. } } return 1. Programs) ll_node[i_count]. i_size < i_ll_size.i_next_idx.i_val = i_item. } else { i_count = ll_node[i_count]. } else return 0. ) { Saket Kr. Pathak Page 39 . //For Last Node ll_node[i_ll_size]. ll_node[i_ll_size].i_val = 100001.

int main() { int i_check = set_argument(). pop_node(int i_option). int i_indx). ll_node[i_count].i_next_idx). } return 1. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Code – Snippet: (Doubly Linked List) #include <stdio. printf("\n\t************************\n").i_val). Saket Kr. Pathak Page 40 . select_choice(void). Programs) if (ll_node[i_count]. int i_val.Data Structure in C (Lab.i_next_idx != 0) { printf("\n\tItem: %d". i_size++. } else break. ll_node[i_count]. disp_node(void). printf("\n\tNext Index: %d".h> #define LINKED_LIST_SIZE 1024 int int int int int int set_argument(void).h> #include <stdbool. int i_ll_size = 0. struct node { int i_prev_idx. process_stack(int i_choice).i_next_idx. i_count = ll_node[i_count]. int i_next_idx. }ll_node[LINKED_LIST_SIZE]. push_node(int i_item.

return 0. Exit: \t\t(Press) 4"). i_state = 0.\n"). return i_check. Pop Item: \t\t(Press) 2"). } while(i_state == 1). printf("\n\n\tPlease Enter Your Choice: "). do { i_state = process_stack(i_check). Saket Kr. getch(). if(i_check == 4) //Check for Exit. Display Item: \t(Press) 3"). Pathak Page 41 . printf("\n\t\t\t Array . else { int i_state. Programs) if(i_check == 0) printf("\n\n\n\tInvalid input."). int i_choice.Data Structure in C (Lab. if(i_check == 0) printf("\n\n\n\tInvalid input. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). } int set_argument(void) { printf("\n\t-------------------------------------------------\n")."). i_check = set_argument(). else . } printf("\n\n\n"). int i_check = select_choice(). printf("\n\t-------------------------------------------------\n\n").Singly Linked-List.

scanf("%d". int i_check = push_node(i_item. break. printf("\n\tPlease Enter the item: "). &i_choice). else return 0. Programs) scanf("%d". } i_check = pop_node(i_check). Pathak Page 42 .Data Structure in C (Lab. { if (i_check == 0) printf("\n\n\n\tInvalid input. &i_indx). if((i_choice > 0) && (i_choice < 5)) return i_choice. &i_item). i_indx). if(i_check == 1) return 1. else . } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List. int i_item = 0."). scanf("%d". else return 0.").\n"). int i_indx = 0. printf("\n\tPlease Enter the index: "). } case 2: { printf("\n\tTo Pop Item from Queue. if(i_check == 1) return 1. else return 0. break. } case 3: { Saket Kr. int i_check = del_option().

else return 0. scanf("%d". if(i_check == 1) return 1. else return 0. Pathak Page 43 .Data Structure in C (Lab. } int i_choice. int i_check = disp_node(). Programs) printf("\n\tTo Display Item of Queue."). break. printf("\n\t\t---------------------------"). } } } int del_option(void) { printf("\n\t\t---------------------------"). } case 4: { printf("\n\tTo Exit. return 0. } default: { return 0. } int pop_node(int i_option) { if (i_option == 1) { int i_del_item."). Saket Kr. printf("\n\n\t\tPlease Enter Your Choice: "). break. printf("\n\t\tBy Reference: \t\t(Press) 2"). if((i_choice > 0) && (i_choice < 5)) return i_choice. { printf("\n\t\tBy Item: \t\t(Press) 1"). &i_choice). break.

i_size++. i_count = i_nxt_idx.i_val.i_val = ll_node[i_nxt_idx].i_next_idx.i_prev_idx = ll_node[i_nxt_idx].i_val = ll_node[i_nxt_idx]. } } else if (i_option == 2) Saket Kr. Pathak Page 44 .i_prev_idx. scanf("%d".i_next_idx. i_count < (LINKED_LIST_SIZE-1). ll_node[i_count]. ll_node[i_count].i_next_idx = ll_node[i_nxt_idx].i_next_idx.i_val. i_size < i_ll_size. bool b_flag = false.i_val == i_del_item) { int i_nxt_idx = ll_node[i_count]. int i_size = 0. i_count = i_nxt_idx. ll_node[i_count].i_next_idx.Data Structure in C (Lab. &i_del_item). ll_node[i_count]. for (i_count = 0.i_next_idx. i_size++. } else if (b_flag) { int i_nxt_idx = ll_node[i_count]. Programs) printf("\n\tItem to delete: "). ) { if ((ll_node[i_count]. int i_count = 0. b_flag = true. i_size++. ll_node[i_count]. } else { i_count = ll_node[i_count].i_prev_idx.i_next_idx = ll_node[i_nxt_idx]. } } else break.i_prev_idx = ll_node[i_nxt_idx]. ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count].

ll_node[i_count]. i_count = i_nxt_idx. i_size++.i_val.i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count]. } } else break. i_size < i_ll_size.i_next_idx. i_count = i_nxt_idx. &i_del_idx).i_next_idx = ll_node[i_nxt_idx].i_val = ll_node[i_nxt_idx]. } } return 1. } else { i_count = ll_node[i_count].i_val = ll_node[i_nxt_idx]. int i_count = 0. Programs) { int i_del_idx.i_next_idx. ) { if ((ll_node[i_count]. i_size++.i_next_idx. printf("\n\tIndex to delete: ").i_next_idx. } else if (b_flag) { int i_nxt_idx = ll_node[i_count]. bool b_flag = false. Saket Kr.i_next_idx = ll_node[i_nxt_idx].i_next_idx == i_del_idx) { int i_nxt_idx = ll_node[i_count]. ll_node[i_count]. i_size++. i_count < (LINKED_LIST_SIZE-1).i_val.Data Structure in C (Lab. int i_size = 0. scanf("%d". for (i_count = 0.i_next_idx. ll_node[i_count]. Pathak Page 45 . ll_node[i_count]. b_flag = true.

i_prev_idx = i_indx. Pathak Page 46 . } else return 0. ll_node[i_ll_size]. i_size < i_ll_size. ) { if (ll_node[i_count]. ll_node[i_ll_size]. ll_node[i_count]. } else { ll_node[i_ll_size].i_next_idx = 0.Data Structure in C (Lab. i_ll_size = i_indx.i_val = 100001. int i_size = 0.i_prev_idx = ll_node[i_ll_size]. ll_node[i_ll_size]. printf("\n\tItem: %d". //------------} return 1.i_next_idx = i_indx. i_count < (LINKED_LIST_SIZE-1).i_val = i_item. int i_indx) { if (i_indx < (LINKED_LIST_SIZE-1)) { if (i_ll_size == 0) { ll_node[i_ll_size].i_next_idx != 0) { printf("\n\tPrev Index: %d". ll_node[i_ll_size]. Saket Kr. ll_node[i_count].i_prev_idx.i_val). for (i_count = 0.i_prev_idx).i_prev_idx = 0.i_next_idx = i_indx.i_val = i_item. ll_node[i_ll_size]. //For Last Node ll_node[i_ll_size]. i_ll_size = i_indx. ll_node[i_ll_size]. Programs) } int push_node(int i_item. } int disp_node(void) { int i_count = 0.

} else break. i_size++.Data Structure in C (Lab.i_next_idx). ll_node[i_count]. i_count = ll_node[i_count].i_next_idx. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. Programs) printf("\n\tNext Index: %d". printf("\n\t************************\n"). Pathak Page 47 . } return 1.

"). if(i_check == 0) printf("\n\n\n\tInvalid input. i_state = 0. WAP in C using dynamic memory allocation of  Stack  Queue  Circular Queue  Linked List Program: Stack: Code – Snippet: #include <stdio. return 0. } Saket Kr. getch(). int main() { int i_check = set_Argument(). stack = (int*)malloc(sizeof(int) * STACK_SIZE).h> #define STACK_SIZE 1024 int i_top = -1. if(i_check == 4) for Exit. i_check = set_Argument().Data Structure in C (Lab. do { i_state = process_stack(i_check). } while(i_state == 1). int *stack. } printf("\n\n\n"). Programs) 4.h> #include <stdbool. Pathak Page 48 //Check . else { int i_state.

"). printf("\n\n\tPlease Enter Your Choice: "). int i_check = select_choice(). else return 0. &i_item)."). else . if(i_check == 0) printf("\n\n\n\tInvalid input. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). printf("\n\t\t\t Array . return i_check. if(i_check == 1) Saket Kr. printf("\n\tPlease Enter the item: "). } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Stack. Pop Item: \t\t(Press) 2").Container. scanf("%d". scanf("%d". int i_item = 0.\n"). int i_check = push_item(i_item). if((i_choice > 0) && (i_choice < 5)) return i_choice. Exit: \t\t(Press) 4"). int i_choice.Data Structure in C (Lab. Pathak Page 49 . Display Item: \t(Press) 3"). Programs) int set_Argument(void) { printf("\n\t-------------------------------------------------\n"). printf("\n\t-------------------------------------------------\n\n"). &i_choice).

"). break. &i_item). break. if(i_check == 1) return 1.1)) { printf("\n\tStack Overflow.Data Structure in C (Lab. Programs) return 1. printf("\n\tPlease Enter the item: "). else return 0.").")."). else return 0. break. Saket Kr. } case 4: { printf("\n\tTo Exit. if(i_check == 1) return 1. } case 3: { printf("\n\tTo Display Item of Stack. Pathak Page 50 . break. break. scanf("%d". int i_check = pop_item(i_item). int i_check = disp_item(). } case 2: { printf("\n\tTo Pop Item from Stack. return 0. int i_item = 0. } } } int push_item(int i_item) { if(i_top == (STACK_SIZE . else return 0. } default: { return 0.

2). return 0. return 0. Pathak Page 51 . } } int pop_item(int i_item) { if(i_top == -1) { printf("\n\tStack is Underflow. printf("\n\tItem . } } i_top = (i_count . } } int disp_item(void) { if(i_top == -1) { printf("\n\tStack is Empty. b_flag = true. Programs) return 0. for(i_count = 0. return 1.%d. //Substracting: 2 = (additional loop increment + 1 deleted item) return 1. ++i_count) { if((*(stack + i_count) == i_item)&&(!b_flag)) { *(stack + i_count) = *(stack + (i_count+1)).". int i_count. } else if(b_flag) { *(stack + i_count) = *(stack + (i_count+1)). } else { bool b_flag = false. } else { *(stack + (++i_top)) = i_item. i_count <= i_top. Saket Kr. has successfully pushed into Stack.Data Structure in C (Lab."). i_item).").

} } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Queue: Code – Snippet: #include <stdio. if(i_check == 4) //Check for Exit. int i_rear = -1. int main() { int i_check = set_Argument(). if(i_check == 0) printf("\n\n\n\tInvalid input. int *queue.Data Structure in C (Lab. for(i_count = 0. *(stack + i_count)). Pathak Page 52 . ++i_count) { printf("\n\tIndex: %d | Item: %d". Programs) } else { int i_count. queue = (int*)malloc(sizeof(int) * QUEUE_SIZE). else { int i_state.h> #define QUEUE_SIZE 1024 int i_front = -1. Saket Kr.h> #include <stdbool. i_count. i_check = set_Argument(). printf("\n\tElements of Stack are:"). i_count <= i_top. } return 1."). do { i_state = process_stack(i_check).

Programs) i_state = 0. } int set_Argument(void) { printf("\n\t-------------------------------------------------\n").Container. Pop Item: \t\t(Press) 2"). printf("\n\t-------------------------------------------------\n\n"). scanf("%d". Exit: \t\t(Press) 4"). } while(i_state == 1). printf("\n\n\tPlease Enter Your Choice: ").\n"). &i_choice). int i_check = select_choice(). else . return i_check."). int i_choice. return 0. if((i_choice > 0) && (i_choice < 5)) return i_choice. if(i_check == 0) printf("\n\n\n\tInvalid input. printf("\n\t\t\t Array . getch(). } printf("\n\n\n"). Display Item: \t(Press) 3"). } int push_item(int i_item) { Saket Kr. else return 0. Pathak Page 53 .Data Structure in C (Lab. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1").

Programs) if(i_rear == (QUEUE_SIZE . } } i_rear = (i_count . *(queue + (++i_rear)) = i_item. } else { i_front = 0. ++i_count) { if((*(queue + i_count) == i_item)&&(!b_flag)) { *(queue + i_count) = *(queue + (i_count+1)). return 0. return 1.Data Structure in C (Lab. b_flag = true. i_item). } } int disp_item(void) { Saket Kr. } else if(b_flag) { *(queue + i_count) = *(queue + (i_count+1)). return 0. } } int pop_item(int i_item) { if((i_front == -1)||(i_front > i_rear)) { printf("\n\tQueue is Underflow.".1)) { printf("\n\tQueue Overflow. //Substracting: 2 = (additional loop increment + 1 deleted item) return 1. i_count <= i_rear. has successfully pushed into Stack. for(i_count = i_front. Pathak Page 54 .").%d. printf("\n\tItem . } else { bool b_flag = false. int i_count.2).").

int i_item = 0. return 0. scanf("%d". ++i_count) { printf("\n\tIndex: %d | Item: %d". } else { printf("\n\tElements of Queue are:"). int i_check = push_item(i_item). printf("\n\tPlease Enter the item: "). &i_item).Data Structure in C (Lab. } } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Queue. i_count. printf("\n\tPlease Enter the item: "). i_count <= i_rear. else return 0. break. else return 0. Pathak Page 55 . if(i_check == 1) return 1. for(i_count = i_front. int i_item = 0. int i_check = pop_item(i_item). Programs) if(i_front == -1) { printf("\n\tQueue is Empty."). scanf("%d". Saket Kr. int i_count. break. } case 2: { printf("\n\tTo Pop Item from Queue."). } return 1."). *(queue + i_count)). if(i_check == 1) return 1. &i_item).

Programs) } case 3: { printf("\n\tTo Display Item of Queue. int i_rear = -1.Data Structure in C (Lab. } default: { return 0. int i_check = disp_item(). int main() { int i_check = set_Argument().h> #define QUEUE_SIZE 1024 int i_front = -1. Saket Kr. } case 4: { printf("\n\tTo Exit. break. return 0."). } } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Circular Queue: Code – Snippet: #include <stdio. if(i_check == 0) printf("\n\n\n\tInvalid input."). break. if(i_check == 1) return 1. break. Pathak Page 56 . else return 0.h> #include <stdbool. int *circular_queue.").

i_check = set_Argument().Container.\n"). int i_choice. Pathak Page 57 . } printf("\n\n\n"). } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). printf("\n\t\t\t Array . else . } while(i_state == 1).Data Structure in C (Lab. } int set_Argument(void) { printf("\n\t-------------------------------------------------\n"). int i_check = select_choice(). Exit: \t\t(Press) 4"). do { i_state = process_stack(i_check)."). if(i_check == 4) //Check for Exit. if(i_check == 0) printf("\n\n\n\tInvalid input. Display Item: \t(Press) 3"). Programs) else { int i_state. i_state = 0. getch(). Pop Item: \t\t(Press) 2"). Saket Kr. printf("\n\t-------------------------------------------------\n\n"). return 0. circular_queue = (int*)malloc(sizeof(int) * QUEUE_SIZE). return i_check. printf("\n\n\tPlease Enter Your Choice: ").

")."). return 0. } else { if(i_rear == -1) { i_rear = 0. } else { bool b_flag = false. } else if(i_rear == QUEUE_SIZE-1) i_rear = 0.". else i_rear++. return 1. ++i_count) { Saket Kr. i_item). for(i_count = i_front. i_front = 0. &i_choice). has successfully pushed into Stack. printf("\n\tItem . } } int pop_item(int i_item) { if(i_front == -1) { printf("\n\tQueue is Underflow. return 0. int i_count. i_count <= i_rear. } int push_item(int i_item) { if(((i_front == 0) && (i_rear == (QUEUE_SIZE . else return 0.1))) || (i_front == i_rear + 1)) { printf("\n\tQueue Overflow.Data Structure in C (Lab. if((i_choice > 0) && (i_choice < 5)) return i_choice.%d. *(circular_queue + i_rear) = i_item. Programs) scanf("%d". Pathak Page 58 .

Saket Kr."). Programs) if((*(circular_queue + i_count) == i_item)&&(!b_flag)) { *(circular_queue + i_count) = *(circular_queue + (i_count+1)). b_flag = true. ++i_count) { printf("\n\tIndex: %d | Item: %d".2). //Substracting: 2 = (additional loop increment + 1 deleted item) return 1. } return 1. } } int disp_item(void) { if((i_front == -1) || (i_front == i_rear + 1)) { printf("\n\tQueue is Empty. i_count. } } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Queue. } else if(b_flag) { *(circular_queue + i_count) = *(circular_queue + (i_count+1)). int i_count. *(circular_queue + i_count)). } else { printf("\n\tElements of Queue are:"). for(i_count = i_front. i_count <= i_rear. } } i_rear = (i_count ."). Pathak Page 59 . return 0.Data Structure in C (Lab.

break. } } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. else return 0. if(i_check == 1) return 1. else return 0. } case 3: { printf("\n\tTo Display Item of Queue. int i_check = pop_item(i_item). break. &i_item). break. int i_item = 0."). printf("\n\tPlease Enter the item: ").")."). break. printf("\n\tPlease Enter the item: "). break. int i_check = push_item(i_item). int i_check = disp_item(). &i_item). } default: { return 0.Data Structure in C (Lab. scanf("%d". } case 4: { printf("\n\tTo Exit. scanf("%d". return 0. else return 0. Programs) int i_item = 0. if(i_check == 1) return 1. Pathak Page 60 . } case 2: { printf("\n\tTo Pop Item from Queue. if(i_check == 1) return 1.

disp_node(void). pop_node(int i_option). int i_ll_size = 0. else { int i_state. int i_next_idx. ll_node = (struct node*)malloc(sizeof(int) * LINKED_LIST_SIZE). i_check = set_argument(). Programs) Linked List: Code – Snippet: (Singly Linked List) #include <stdio. getch(). do { i_state = process_stack(i_check). process_stack(int i_choice). i_state = 0. Saket Kr. }*ll_node.h> #include <stdbool. push_node(int i_item.Data Structure in C (Lab. return 0. struct node { int i_val. int i_indx). if(i_check == 4) //Check for Exit. } printf("\n\n\n"). int main() { int i_check = set_argument(). } while(i_state == 1). select_choice(void)."). if(i_check == 0) printf("\n\n\n\tInvalid input. Pathak Page 61 .h> #define LINKED_LIST_SIZE 1024 int int int int int int set_argument(void).

} int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List. return i_check. if(i_check == 0) printf("\n\n\n\tInvalid input.Data Structure in C (Lab. if((i_choice > 0) && (i_choice < 5)) return i_choice. printf("\n\t-------------------------------------------------\n\n"). Pop Item: \t\t(Press) 2").\n"). } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). int i_check = select_choice(). Pathak Page 62 . printf("\n\n\tPlease Enter Your Choice: ").\n"). int i_choice. int i_item = 0. printf("\n\tPlease Enter the item: "). scanf("%d". printf("\n\t\t\t Array . Exit: \t\t(Press) 4"). else return 0."). Saket Kr. int i_indx = 0. Display Item: \t(Press) 3").Singly Linked-List. else . &i_choice). Programs) } int set_argument(void) { printf("\n\t-------------------------------------------------\n").

{ if (i_check == 0) printf("\n\n\n\tInvalid input. break. Programs) scanf("%d". &i_item). Pathak Page 63 ."). } default: { return 0. else return 0. i_indx). if(i_check == 1) return 1."). else . } case 3: { printf("\n\tTo Display Item of Queue. if(i_check == 1) return 1. break. if(i_check == 1) return 1. } i_check = pop_node(i_check). int i_check = push_node(i_item. return 0. Saket Kr. printf("\n\tPlease Enter the index: "). int i_check = del_option(). break. break. scanf("%d". else return 0. } case 2: { printf("\n\tTo Pop Item from Queue."). else return 0. &i_indx). } case 4: { printf("\n\tTo Exit.").Data Structure in C (Lab. break. int i_check = disp_node().

&i_del_item). printf("\n\tItem to delete: "). { printf("\n\t\tBy Item: \t\t(Press) 1"). int i_count = 0. else return 0. bool b_flag = false.i_val = (*(ll_node + i_nxt_idx)). scanf("%d".i_val == i_del_item) { int i_nxt_idx = (*(ll_node + i_count)). (*(ll_node + i_count)). } int pop_node(int i_option) { if (i_option == 1) { int i_del_item. printf("\n\t\tBy Reference: \t\t(Press) 2"). Pathak Page 64 . i_size < i_ll_size. Saket Kr. if((i_choice > 0) && (i_choice < 5)) return i_choice.Data Structure in C (Lab. scanf("%d". i_count < (LINKED_LIST_SIZE-1). for (i_count = 0. printf("\n\t\t---------------------------"). printf("\n\n\t\tPlease Enter Your Choice: "). } int i_choice.i_val.i_next_idx. Programs) } } } int del_option(void) { printf("\n\t\t---------------------------"). &i_choice). int i_size = 0. ) { if (((*(ll_node + i_count)).i_next_idx != 0)&&(!b_flag)) { if ((*(ll_node + i_count)).

bool b_flag = false.i_next_idx.i_next_idx = (*(ll_node + i_nxt_idx)). i_count = i_nxt_idx. i_size++. scanf("%d". int i_count = 0. int i_size = 0. } } else if (i_option == 2) { int i_del_idx. Programs) (*(ll_node + i_count)). Pathak Page 65 .i_next_idx == i_del_idx) { int i_nxt_idx = (*(ll_node + i_count)). printf("\n\tIndex to delete: ").i_val. ) { if (((*(ll_node + i_count)).i_next_idx.i_next_idx. (*(ll_node + i_count)). b_flag = true. } } else break. Saket Kr. i_size < i_ll_size.i_next_idx. } else if (b_flag) { int i_nxt_idx = (*(ll_node + i_count)).i_next_idx. (*(ll_node + i_count)).i_val = (*(ll_node + i_nxt_idx)).i_next_idx != 0)&&(!b_flag)) { if ((*(ll_node + i_count)). } else { i_count = (*(ll_node + i_count)). i_count = i_nxt_idx. i_size++.i_next_idx = (*(ll_node + i_nxt_idx)). i_size++.Data Structure in C (Lab. &i_del_idx). for (i_count = 0. i_count < (LINKED_LIST_SIZE-1).

i_size++.i_next_idx = (*(ll_node + i_nxt_idx)).i_next_idx = i_indx.i_val. i_size++.i_next_idx. (*(ll_node + i_ll_size)). } } else break. i_count = i_nxt_idx.i_next_idx. Saket Kr. i_count = i_nxt_idx. (*(ll_node + i_count)).Data Structure in C (Lab. Programs) (*(ll_node + i_count)). i_ll_size = i_indx.i_val. (*(ll_node + i_count)).i_val = (*(ll_node + i_nxt_idx)). b_flag = true.i_next_idx = (*(ll_node + i_nxt_idx)). //For Last Node (*(ll_node + i_ll_size)). } else { i_count = (*(ll_node + i_count)).i_val = i_item.i_next_idx.i_val = 100001. } int push_node(int i_item. (*(ll_node + i_ll_size)). //------------return 1.i_next_idx = 0.i_next_idx. i_size++.i_val = (*(ll_node + i_nxt_idx)). (*(ll_node + i_count)). int i_indx) { if (i_indx < (LINKED_LIST_SIZE-1)) { (*(ll_node + i_ll_size)). } } return 1. } else if (b_flag) { int i_nxt_idx = (*(ll_node + i_count)). Pathak Page 66 .

} int disp_node(void) { int i_count = 0. Pathak . i_count < (LINKED_LIST_SIZE-1). process_stack(int i_choice). disp_node(void). push_node(int i_item. ) { if ((*(ll_node + i_count)). } else break. Page 67 Saket Kr. select_choice(void).i_val).i_next_idx. Programs) } else return 0. printf("\n\tNext Index: %d". i_count = (*(ll_node + i_count)).Data Structure in C (Lab.i_next_idx).h> #include <stdbool. int i_indx). (*(ll_node + i_count)). pop_node(int i_option). (*(ll_node + i_count)). i_size < i_ll_size. } return 1. for (i_count = 0. i_size++.i_next_idx != 0) { printf("\n\tItem: %d". printf("\n\t************************\n"). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Code – Snippet: (Doubly Linked List) #include <stdio.h> #define LINKED_LIST_SIZE 1024 int int int int int int set_argument(void). int i_size = 0.

}*ll_node. i_state = 0. int i_check = select_choice(). if(i_check == 0) printf("\n\n\n\tInvalid input. } int set_argument(void) { printf("\n\t-------------------------------------------------\n"). do { i_state = process_stack(i_check). if(i_check == 0) printf("\n\n\n\tInvalid input. getch(). else . int i_next_idx. } printf("\n\n\n"). printf("\n\t-------------------------------------------------\n\n"). } while(i_state == 1). if(i_check == 4) //Check for Exit. int main() { int i_check = set_argument().\n").Data Structure in C (Lab. int i_ll_size = 0. Pathak Page 68 . int i_val. Programs) struct node { int i_prev_idx.Singly Linked-List. ll_node = (struct node*)malloc(sizeof(int) * LINKED_LIST_SIZE)."). i_check = set_argument(). else { int i_state."). Saket Kr. printf("\n\t\t\t Array . return 0.

else return 0. printf("\n\tPlease Enter the item: "). if((i_choice > 0) && (i_choice < 5)) return i_choice. scanf("%d". break. printf("\n\n\tPlease Enter Your Choice: "). int i_item = 0. if(i_check == 1) return 1. } int select_choice(void) { { printf("\n\tTo printf("\n\tTo printf("\n\tTo printf("\n\tTo } Push Item: \t\t(Press) 1"). printf("\n\tPlease Enter the index: "). } int process_stack(int i_choice) { switch(i_choice) { case 1: { printf("\n\tTo Push Item into Linked-List. &i_choice). scanf("%d". } case 2: { printf("\n\tTo Pop Item from Queue. int i_indx = 0. Pathak Page 69 ."). &i_item). scanf("%d". int i_choice. Programs) return i_check.Data Structure in C (Lab. Saket Kr. Pop Item: \t\t(Press) 2"). &i_indx). Display Item: \t(Press) 3").\n"). i_indx). int i_check = push_node(i_item. else return 0. int i_check = del_option(). Exit: \t\t(Press) 4").

Saket Kr. Pathak Page 70 . int i_check = disp_node(). } case 3: { printf("\n\tTo Display Item of Queue. else return 0.Data Structure in C (Lab. break. } default: { return 0. else . break. Programs) { if (i_check == 0) printf("\n\n\n\tInvalid input. printf("\n\n\t\tPlease Enter Your Choice: ")."). if(i_check == 1) return 1. break. } case 4: { printf("\n\tTo Exit. } } } int del_option(void) { printf("\n\t\t---------------------------")."). { printf("\n\t\tBy Item: \t\t(Press) 1"). return 0. else return 0. break. if(i_check == 1) return 1."). printf("\n\t\tBy Reference: \t\t(Press) 2"). } i_check = pop_node(i_check). } int i_choice.

&i_choice). i_count < (LINKED_LIST_SIZE-1). bool b_flag = false.i_next_idx != 0)&&(!b_flag)) { if ((*(ll_node + i_count)).Data Structure in C (Lab. printf("\n\tItem to delete: "). i_count = i_nxt_idx. i_size++. } else if (b_flag) { int i_nxt_idx = (*(ll_node + i_count)). (*(ll_node + i_count)).i_val = (*(ll_node + i_nxt_idx)).i_prev_idx = (*(ll_node + i_nxt_idx)). i_size < i_ll_size. else return 0.i_prev_idx. int i_count = 0.i_next_idx.i_next_idx. Pathak Page 71 . &i_del_item). if((i_choice > 0) && (i_choice < 5)) return i_choice. (*(ll_node + i_count)).i_val. Saket Kr.i_next_idx = (*(ll_node + i_nxt_idx)). (*(ll_node + i_count)). for (i_count = 0.i_prev_idx = (*(ll_node + i_nxt_idx)). ) { if (((*(ll_node + i_count)). b_flag = true. Programs) scanf("%d". int i_size = 0. scanf("%d". printf("\n\t\t---------------------------"). } int pop_node(int i_option) { if (i_option == 1) { int i_del_item. (*(ll_node + i_count)).i_val == i_del_item) { int i_nxt_idx = (*(ll_node + i_count)).i_prev_idx.i_next_idx.

bool b_flag = false. } } else if (i_option == 2) { int i_del_idx.Data Structure in C (Lab.i_next_idx. printf("\n\tIndex to delete: "). i_size++.i_next_idx = (*(ll_node + i_nxt_idx)). (*(ll_node + i_count)). i_size++.i_next_idx == i_del_idx) { int i_nxt_idx = (*(ll_node + i_count)).i_val = (*(ll_node + i_nxt_idx)).i_val.i_next_idx. } else if (b_flag) { Saket Kr. } else { i_count = (*(ll_node + i_count)). i_size++.i_next_idx = (*(ll_node + i_nxt_idx)).i_next_idx. } } else break. i_count < (LINKED_LIST_SIZE-1). i_size < i_ll_size.i_val. b_flag = true. &i_del_idx). ) { if (((*(ll_node + i_count)). Programs) (*(ll_node + i_count)). int i_count = 0. i_count = i_nxt_idx. for (i_count = 0.i_next_idx. (*(ll_node + i_count)). int i_size = 0.i_val = (*(ll_node + i_nxt_idx)). Pathak Page 72 . (*(ll_node + i_count)).i_next_idx != 0)&&(!b_flag)) { if ((*(ll_node + i_count)). i_count = i_nxt_idx. scanf("%d".

} else { (*(ll_node + i_ll_size)). } else { i_count = (*(ll_node + i_count)).i_next_idx.i_next_idx. } int push_node(int i_item.i_next_idx = i_indx. Pathak Page 73 .i_next_idx. //------------Saket Kr.i_val = 100001.i_prev_idx = (*(ll_node + i_ll_size)).i_next_idx = i_indx. (*(ll_node + i_count)).i_prev_idx = i_indx. (*(ll_node + i_ll_size)).i_val = (*(ll_node + i_nxt_idx)). (*(ll_node + i_ll_size)). } } return 1. i_ll_size = i_indx.i_val = i_item. //For Last Node (*(ll_node + i_ll_size)). (*(ll_node + i_ll_size)).i_prev_idx = 0. i_count = i_nxt_idx. i_size++. (*(ll_node + i_count)).Data Structure in C (Lab. int i_indx) { if (i_indx < (LINKED_LIST_SIZE-1)) { if (i_ll_size == 0) { (*(ll_node + i_ll_size)). (*(ll_node + i_ll_size)).i_val.i_prev_idx. Programs) int i_nxt_idx = (*(ll_node + i_count)). i_size++.i_next_idx = 0. (*(ll_node + i_ll_size)).i_val = i_item. } } else break. (*(ll_node + i_ll_size)). i_ll_size = i_indx.i_next_idx = (*(ll_node + i_nxt_idx)).

(*(ll_node + i_count)).i_next_idx != 0) { printf("\n\tPrev Index: %d". printf("\n\tItem: %d". } int disp_node(void) { int i_count = 0. (*(ll_node + i_count)). int i_size = 0.i_prev_idx). i_count < (LINKED_LIST_SIZE-1). (*(ll_node + i_count)). } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. printf("\n\t************************\n"). Programs) } return 1. } else break.i_val).Data Structure in C (Lab. printf("\n\tNext Index: %d". i_size < i_ll_size. i_count = (*(ll_node + i_count)). ) { if (ll_node[i_count]. for (i_count = 0.i_next_idx. Pathak Page 74 . } return 1. i_size++. } else return 0.i_next_idx).

h> #include <stdlib. Program: Code – Snippet: #include <stdio. void disp_tree(struct node *root). struct node* delete_node(struct node *current_node. printf("\t\t\tWAP for implementation of Binary Tree. struct node *nw_node).h> struct node { int i_data. struct node *left_node. } struct node* get_tree_element(int i_no_elem) Saket Kr. system("pause"). bool insert_node(struct node **root. printf("\n\n\n"). int i_tree_elem. struct node* crea_tree(struct node *root. Pathak Page 75 . printf("\n\n\n").. root = get_tree_element(i_tree_elem). int i_data). struct node* get_tree_element(int i_no_elem). Programs) 5. &i_tree_elem). printf("\tWAP of Selection sort.h> #include <stdbool. struct node *root = (struct node*)malloc(sizeof(struct node)). int main() { printf("\n\n\n").\n"). printf("Enter the total number of elements: "). return 0. struct node *right_node. }."). disp_tree(root). printf("\n\n\n").Data Structure in C (Lab. int i_data). scanf("%d". WAP in C for implementation of Binary Tree.

} else { //If ITEM is less that the ITEM in Root Node //Traverse the node in LEFT . scanf("%d". } bool insert_node(struct node **root. ++i_count) { int i_temp.SIDE. nw_node). current_node->i_data = i_data. } return root. //If ITEM is greater that the ITEM in Root Node //Traverse the node in RIGHT . int i_count. i_temp). Saket Kr. Pathak Page 76 . Programs) { struct node *root = (struct node*)malloc(sizeof(struct node)). current_node->left_node = current_node->right_node = NULL. for (i_count = 0. } struct node* crea_tree(struct node *root. i_count < i_no_elem. current_node). root = crea_tree(root. i_count). return true.SIDE. struct node *nw_node) { //Check for Null Root-Node if(!(*root)) { *root = nw_node. int i_data) { struct node *current_node = (struct node*) malloc(sizeof(struct node)). root = NULL. insert_node(&root. printf("\n\tEnter Element:%d: ". return root. &i_temp).Data Structure in C (Lab. if(nw_node->i_data < (*root)->i_data) insert_node(&(*root)->left_node.

temp_node = NULL. child_node = NULL. struct node *del_node = (struct node*)malloc(sizeof(struct node*)).Node if(root->right_node) disp_tree(root->right_node). root->i_data).Node if(root->left_node) disp_tree(root->left_node). Saket Kr. struct node *temp_node = (struct node*)malloc(sizeof(struct node*)). current_node = current_node->left_node. del_node = NULL. //Check for NULL in RIGHT . root>left_node. } } void disp_tree(struct node *root) { //Check for NULL in LEFT . int i_data) { struct node *parent_node = (struct node*)malloc(sizeof(struct node*)). while (current_node != NULL) { if (current_node->i_data > i_data) { parent_node = current_node. current_node = current_node->right_node. printf("\nNode. } struct node* delete_node(struct node *current_node. nw_node). Pathak Page 77 . root->right_node. } else if (current_node->i_data < i_data) { parent_node = current_node. parent_node = NULL. Programs) else if(nw_node->i_data > (*root)->i_data) insert_node(&(*root)->right_node.Data Structure in C (Lab.Left: %d \t Right: %d \t Item: %d\n". return false. struct node *child_node = (struct node*)malloc(sizeof(struct node*)).

del_node->i_data). left of the parent. left of the parent. free(del_node). } } //Node to delete has one child else if ((del_node->left_node == NULL) || (del_node->right_node == NULL)) { //If the node to delete is on the left-side if (parent_node->left_node == del_node) { if (del_node->left_node != NULL) { printf("\n\tNode deleting with value (%d). } //If the node to delete is on the right-side else if (parent_node->right_node == del_node) { printf("\n\tNode deleting with value (%d). Pathak Page 78 . Programs) } else if (i_data == current_node->i_data) { del_node = current_node. free(del_node). parent_node->right_node = NULL. parent_node->left_node = del_node>left_node.".". } else if (del_node->right_node != NULL) { Saket Kr. break. right of the parent. break. del_node->i_data). free(del_node). //Node to delete is Leaf-Node if ((del_node->left_node == NULL) && (del_node>right_node == NULL)) { //If the node to delete is on the left-side if (parent_node->left_node == del_node) { printf("\n\tNode deleting with value (%d). del_node->i_data).". parent_node->left_node = NULL. break.Data Structure in C (Lab.

break. free(del_node). Saket Kr. left of the parent. temp_node = del_node. parent_node->right_node = del_node->right_node. } } else if (parent_node->right_node == del_node) { if (del_node->left_node != NULL) { printf("\n\tNode deleting with value (%d). if (child_node->left_node == NULL) { child_node->left_node = del_node->left_node. right of the parent.Data Structure in C (Lab. right of the parent.". } } } //Node to delete has two child else if ((del_node->left_node != NULL) && (del_node->right_node != NULL)) { printf("\n\tNode to delete has two children\n"). parent_node->right_node = del_node>left_node. free(del_node). Pathak Page 79 .". //If the node to delete is Root if (parent_node == NULL) { child_node = del_node>right_node. free(del_node). parent_node->left_node = del_node>right_node. del_node->i_data). } else if (del_node->right_node != NULL) { printf("\n\tNode deleting with value (%d).". free(del_node). del_node->i_data). Programs) printf("\n\tNode deleting with value (%d). del_node->i_data).

del_node = child_node. Pathak Page 80 . } else { while (child_node>left_node != NULL) { parent_node = child_node. } temp_node->i_data = child_node->i_data. break. free(del_node). child_node = parent_node->left_node. if (child_node->left_node == NULL) { parent_node->left_node = child_node. parent_node->left_node = child_node->right_node. free(del_node). break. Programs) break. child_node->left_node = del_node->left_node. Saket Kr. child_node = parent_node->left_node. } } //If the node to delete is on the Left else if (parent_node->left_node == del_node) { child_node = del_node>right_node.Data Structure in C (Lab. } else { while (child_node>left_node != NULL) { parent_node = child_node.

free(del_node). } temp_node->i_data = child_node->i_data. } } //If the node to delete is on the Right else if (parent_node->right_node == del_node) { child_node = del_node>right_node. free(del_node).Data Structure in C (Lab. } else { while (child_node->left_node != NULL) { parent_node = child_node. child_node->left_node = del_node->left_node. parent_node->left_node = child_node->right_node. break. Programs) } temp_node->i_data = child_node->i_data. Pathak Page 81 . if (child_node->left_node == NULL) { parent_node->right_node = child_node. child_node = parent_node>left_node. break. parent_node->left_node = child_node>right_node. } } } } } Saket Kr. del_node = child_node. break. del_node = child_node. free(del_node).

Data Structure in C (Lab. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. Pathak Page 82 . Programs) return current_node.

scanf("%d". int i_data).Data Structure in C (Lab. &i_tree_elem). struct node *nw_node). int disp_menu(void). Saket Kr. WAP in C for Tree Traversal  Pre-Order  In-Order  Post-Order Program: Code – Snippet: #include <stdio. bool insert_node(struct node **root. void traverse_tree(int i_choice. void postorder(struct node *tree_node).. printf("\tWAP of Selection sort. struct node* crea_tree(struct node *root.h> #include <stdlib.h> #include <stdbool. printf("\n\n\n"). struct node* get_tree_element(int i_no_elem)."). struct node* root). printf("\t\t\tWAP for implementation of Binary Tree. }. int i_tree_elem. int main() { printf("\n\n\n").\n"). struct node *right_node. root = get_tree_element(i_tree_elem). printf("Enter the total number of elements: "). void inorder(struct node *tree_node). Programs) 6. struct node *left_node. void disp_tree(struct node *root). struct node *root = (struct node*)malloc(sizeof(struct node)).h> struct node { int i_data. printf("\n\n\n"). Pathak Page 83 . void preorder(struct node *tree_node).

int i_choice = disp_menu(). return root. printf("\n\tEnter Element:%d: ".Data Structure in C (Lab. } struct node* get_tree_element(int i_no_elem) { struct node *root = (struct node*)malloc(sizeof(struct node)). current_node->left_node = current_node->right_node = NULL. return 0. struct node *nw_node) { //Check for Null Root-Node if(!(*root)) { Saket Kr. i_temp). scanf("%d". root). Pathak Page 84 . int i_data) { struct node *current_node = (struct node*) malloc(sizeof(struct node)). current_node->i_data = i_data. i_count < i_no_elem. } return root. Programs) disp_tree(root). &i_temp). traverse_tree(i_choice. int i_count. i_count). insert_node(&root. root = crea_tree(root. } bool insert_node(struct node **root. root = NULL. system("pause"). for (i_count = 0. current_node). ++i_count) { int i_temp. printf("\n\n\n"). } struct node* crea_tree(struct node *root.

return true. } } void disp_tree(struct node *root) { //Check for NULL in LEFT . } int disp_menu(void) { printf("\n\n\n"). root->right_node. return false. printf("\n\tPlease Enter your choice: ").SIDE. printf("\tTree Traversal: Pre-Order . root>left_node.Data Structure in C (Lab. nw_node). &i_choice). int i_choice. if(nw_node->i_data < (*root)->i_data) insert_node(&(*root)->left_node.Left: %d \t Right: %d \t Item: %d\n". return i_choice. //If ITEM is greater that the ITEM in Root Node //Traverse the node in RIGHT . scanf("%d". Pathak Page 85 . //Check for NULL in RIGHT . } Saket Kr. else if(nw_node->i_data > (*root)->i_data) insert_node(&(*root)->right_node. printf("\tTree Traversal: In-Order . Programs) *root = nw_node. root->i_data).Node if(root->left_node) disp_tree(root->left_node).SIDE.Node if(root->right_node) disp_tree(root->right_node).2\n").3\n"). nw_node). } else { //If ITEM is less that the ITEM in Root Node //Traverse the node in LEFT . printf("\tTree Traversal: Post-Order .1\n"). printf("\nNode.

tree_node->i_data). printf("\nData :%d". case 2: postorder(root). } } void preorder(struct node *tree_node) { if (tree_node!=NULL) { printf("\nData :%d". break. break.tree_node->i_data). struct node* root) { switch(i_choice) { case 1: inorder(root). case 3: preorder(root). Programs) void traverse_tree(int i_choice. Pathak Page 86 . preorder(tree_node->right_node).Data Structure in C (Lab. } } void postorder(struct node *tree_node) { if (tree_node!=NULL) { postorder(tree_node->left_node). } } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. postorder(tree_node->right_node). preorder(tree_node->left_node). inorder(tree_node->right_node). break.tree_node->i_data). } } void inorder(struct node *tree_node) { if (tree_node!=NULL) { inorder(tree_node->left_node). printf("\nData :%d".

Otherwise enqueue any successors (the direct child nodes) that have not yet been discovered.h> int int int int int **i_adjacency_matrix. *i_bfs_path.h> #include <stdlib. n_flag = -1. initialize_nodes(void). *i_nodes_visit. [4] If the queue is not empty.h> #include <stdbool. i_no_nodes. This gives a time complexity of O (n + e). flag. once from each direction. WAP in C for Graph Traversal  Breadth First Search  Depth First Search Program: Breadth First Search: Algorithm – [1] Enqueue the root node. repeat from Step 2 Time Complexity – The total time for initializing is O (n) and the total time for the queuing operations is O (n) because each node is put on the queue exactly once.Data Structure in C (Lab. b. If the element sought is found in this node. quit the search and return a result. get_graph_input(void). Programs) 7. The total time in the main loop is O (e) because we look at each edge at most twice. every node on the graph has been examined – quit the search and return "not found". Code – Snippet: #include <stdio. [3] If the queue is empty. Pathak . Page 87 bool bool void void Saket Kr. [2] Dequeue a node and examine it a. breadth_first_search(void). check_node(int).

if ((i_temp == 1)||(i_temp == 0)) i_adjacency_matrix[i_count][j_count] = i_temp.Data Structure in C (Lab."). j_count < i_no_nodes. &i_no_nodes). Saket Kr. return 0. b_check = get_graph_input(). scanf("%d". ++j_count) { printf("\nConection of node %d to node %d is: ". printf("\n\n\n"). Programs) int main() { printf("\n\n\n"). breadth_first_search(). else { printf("Input is invalid. for (i_count = 0. system("pause"). scanf("%d". i_count. bool b_check. if (b_check) b_check = initialize_nodes()."). i_nodes_visit = malloc(sizeof(int) * i_no_nodes). } bool get_graph_input(void) { printf("Enter Number of Nodes: "). Pathak Page 88 . printf("\n\n\n"). ++i_count) { *(i_adjacency_matrix + i_count) = (int*)malloc(sizeof(int) * i_no_nodes). j_count. i_bfs_path = malloc(sizeof(int) * i_no_nodes). j_count). i_adjacency_matrix = malloc(sizeof(int) * i_no_nodes). printf("Depth First Path within the given graph:\n"). printf("\t\t\tWAP for Graph Traversal (Depth First Search). &i_temp). int i_temp = 0. printf("\n\n\n"). for (j_count = 0. int i_count. i_count < i_no_nodes.

i_count). check_node(i_start_node). Saket Kr. Pathak Page 89 . i_adjacency_matrix[i_count][j_count]). return true. for (j_count = 0. int i_start_node. &i_start_node). j_count < i_no_nodes. scanf("%d". i_count <= i_no_nodes. printf("\n\tPlease Enter Starting Node: "). so formed:"). ++j_count) printf("\t%d". ++i_count) i_nodes_visit[i_count] = 0. } } } printf("\n\n\nAdjacency Matrix. ++i_count) { printf("\n"). } void breadth_first_search(void) { int i_count. } return true.Data Structure in C (Lab. for (i_count = 0. } void check_node(int i_start_node) { int i_count. else printf("\n Bfs is not possible"). for (i_count = 0. i_count < i_no_nodes. ++i_count) if (i_nodes_visit[i_count]) printf("%d\t". for (i_count = 1. } bool initialize_nodes(void) { int i_count. Programs) return false. i_count < i_no_nodes.

 If success is returned. signal success. say E.Data Structure in C (Lab. Time Complexity – The time complexity is O (E + V). [2] Otherwise. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Depth First Search: Algorithm – [1] If the initial state is a goal state.  Generate a state. } return. int *i_nodes_visit. Pathak Page 90 . If there is no successor. quit and return success. Programs) for (i_count = 0.h> #include <stdlib.h> #define MAX_NUM_NODES 1024 int **i_adjacency_matrix. loop until success or failure is signaled. bool get_graph_input(void).h> #include <stdbool. Code – Snippet: #include <stdio. and let it be the successor of the initial state. i_count <= i_no_nodes. Otherwise continue in this loop. if (flag <= n_flag) { i_nodes_visit[i_bfs_path[flag]] = 1. int i_no_nodes. check_node(i_bfs_path[flag++]). Saket Kr.  Call Depth-First Search with E as the initial state. ++i_count) if ((i_adjacency_matrix[i_start_node][i_count]) && (!i_nodes_visit[i_count])) i_bfs_path[++n_flag] = i_count. signal failure.

printf("\n\n\n"). void check_node(int).Data Structure in C (Lab. int main() { printf("\n\n\n"). if ((i_temp == 1)||(i_temp == 0)) i_adjacency_matrix[i_count][j_count] = i_temp. depth_first_search(). j_count < i_no_nodes. scanf("%d". &i_temp). int i_count. j_count. void depth_first_search(void). Saket Kr. bool b_check. i_adjacency_matrix = malloc(sizeof(int) * i_no_nodes). printf("Depth First Path within the given graph:\n"). Pathak Page 91 . i_nodes_visit = malloc(sizeof(int) * i_no_nodes). } bool get_graph_input(void) { printf("Enter Number of Nodes: "). i_count. for (i_count = 0. printf("\n\n\n"). int i_temp = 0. i_count < i_no_nodes. for (j_count = 0. ++j_count) { printf("\nConection of node %d to node %d is: ". ++i_count) { *(i_adjacency_matrix + i_count) = (int*)malloc(sizeof(int) * i_no_nodes). printf("\t\t\tWAP for Graph Traversal (Depth First Search). if (b_check) b_check = initialize_nodes(). return 0."). j_count). Programs) bool initialize_nodes(void). scanf("%d". b_check = get_graph_input(). &i_no_nodes). printf("\n\n\n"). system("pause").

j_count < i_no_nodes. ++j_count) printf("\t%d". return true. i_adjacency_matrix[i_count][j_count]). } bool initialize_nodes(void) { int i_count. for (i_count = 0. Pathak Page 92 . } void check_node(int i_node) { int i_count. for (j_count = 0. so formed:"). printf("Node(%d)\t". i_node). ++i_count) if(i_nodes_visit[i_count] == 0) check_node(i_count). for (i_count = 0. i_nodes_visit[i_node] = 1. Programs) else { printf("Input is invalid. i_count < i_no_nodes. ++i_count) Saket Kr.Data Structure in C (Lab. } return true. printf("\n\t")."). for (i_count = 0. } } } printf("\n\n\nAdjacency Matrix. i_count < i_no_nodes. ++i_count) { printf("\n"). for (i_count = 0. i_count < i_no_nodes. return false. i_count < i_no_nodes. } void depth_first_search(void) { int i_count. ++i_count) i_nodes_visit[i_count] = 0.

Programs) if (i_nodes_visit[i_count] == 0) check_node(i_count). return.Data Structure in C (Lab. Pathak Page 93 . } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr.

b_check = get_tree_input(). Code – Snippet: #include <stdio."). printf("\n Minimun cost = %d". printf("\n\n\n"). Programs) 8. i_min_cost_spanning_tree). Saket Kr.Data Structure in C (Lab.h> #include <stdbool. int calc_cost_prims_algo(void). Pathak Page 94 . bool b_check. printf("\t\t\tWAP for Minimum Cost Spanning Tree (Prim's algorithm). bool get_tree_input(void). WAP in C for Minimum Cost Spanning Tree Program: Algorithm (Prim’s) – [1] create a tree containing a single vertex. chosen arbitrarily from the graph [2] create a set containing all the edges in the graph [3] loop until every edge in the set connects two vertices in the tree  remove from the set an edge with minimum weight that connects a vertex in the tree with a vertex not in the tree  add that edge to the tree Time Complexity – O (E * log (V)) where E is the number of edges and V is the number of vertices.h> #include <stdlib. printf("\n\n\n"). int *i_nodes_visit. int main() { printf("\n\n\n"). int i_min_cost_spanning_tree = calc_cost_prims_algo().h> #define MIN_COST 3000 int **i_adjacency_matrix. int i_no_nodes.

else i_adjacency_matrix[i_count][j_count] = MIN_COST. j_count. int i_temp = 0. } return true. ++i_count) { *(i_adjacency_matrix + i_count) = (int*)malloc(sizeof(int) * i_no_nodes). } } printf("\n\n\nAdjacency Matrix. ++j_count) printf("\t%d". return 0. ++i_count) { printf("\n"). for (j_count = 0. int i_count. for (j_count = 0. } bool get_tree_input(void) { printf("Enter Number of Nodes: "). so formed:"). i_count < i_no_nodes. Programs) system("pause"). Saket Kr. scanf("%d". i_count. int i_count. i_nodes_visit = malloc(sizeof(int) * i_no_nodes). if ((i_temp != 0)) i_adjacency_matrix[i_count][j_count] = i_temp. i_total_cost. j_count < i_no_nodes. i_adjacency_matrix[i_count][j_count]). for (i_count = 0. &i_no_nodes). ++j_count) { printf("\nCost of node %d to node %d is: ". j_count). } int calc_cost_prims_algo(void) { int i_min_cost. Pathak Page 95 . i_adjacency_matrix = malloc(sizeof(int) * i_no_nodes). j_count < i_no_nodes. scanf("%d". j_count. for (i_count = 0. &i_temp).Data Structure in C (Lab. i_count < i_no_nodes.

Pathak Page 96 . ++j_count) if ((i_adjacency_matrix[i_count][j_count] < i_min_cost) && (i_nodes_visit[i_count] != 0)) { i_min_cost = i_adjacency_matrix[i_count][j_count]. i_total_cost += i_min_cost. i_node_1 = i_count. int i_new_node = 0. } return i_total_cost. i_node_2. i_count <= i_no_nodes. j_count <= i_no_nodes. while (i_new_node < i_no_nodes) { for (i_count = 0. i_node_2. i_nodes_visit[i_node_2] = 1. ++i_count) for (j_count = 0. i_min_cost). } if ((i_nodes_visit[i_node_1] == 0) || (i_nodes_visit[i_node_2] == 0)) { printf("\n Edge %d:(%d %d) cost:%d". i_node_1. } i_adjacency_matrix[i_node_1][i_node_2] = i_adjacency_matrix[i_node_2][i_node_1] = MIN_COST.Data Structure in C (Lab. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr. i_nodes_visit[0] = 1. Programs) int i_node_1. i_min_cost = MIN_COST.i_new_node++. i_node_2 = j_count.

consider all of its unvisited neighbors and calculate their tentative distances. The algorithm has finished. int i_no_nodes. A visited node will never be checked again.h> #define UNDEFINED_COST 3000 int **i_adjacency_matrix.h> #include <stdbool. Programs) 9. Time Complexity – Time complexity of the following algorithm is O (M * log (N)). WAP in C for Shortest Path Problem Program: Algorithm (Dijkstra’s) – [1] Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes. int *i_nodes_visit. Pathak Page 97 . and set it as the new "current node" then go back to step 3. [4] When we are done considering all of the neighbors of the current node. [5] If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal). [6] Select the unvisited node that is marked with the smallest tentative distance. int main() { Saket Kr. then stop. where M is number of edges and N is number of vertices. mark the current node as visited and remove it from the unvisited set. [2] Mark all nodes unvisited.h> #include <stdlib.Data Structure in C (Lab. Set the initial node as current. void calc_cost_Dijkstra_algo(int). [3] For the current node. Create a set of the unvisited nodes called the unvisited set consisting of all the nodes except the initial node. Code – Snippet: #include <stdio. int get_tree_input(void).

i_source = get_tree_input(). Programs) printf("\n\n\n"). } } printf("\n\n\nAdjacency Matrix. int i_temp = 0. i_nodes_visit = malloc(sizeof(int) * i_no_nodes). i_count < i_no_nodes. so formed:"). printf("\n\n\n"). ++j_count) Saket Kr. j_count). scanf("%d". ++j_count) { printf("\nCost of node %d to node %d is: ". printf("\n\n\n"). j_count. j_count < i_no_nodes. return 0. else i_adjacency_matrix[i_count][j_count] = UNDEFINED_COST. int i_source. int i_count. } int get_tree_input(void) { printf("Enter Number of Nodes: "). for (j_count = 0. printf("\t\t\tWAP for Shortest Path Problem (Dijkstra's algorithm). for (i_count = 0. i_adjacency_matrix = malloc(sizeof(int) * i_no_nodes). i_count < i_no_nodes. &i_no_nodes). if ((i_temp != 0)) i_adjacency_matrix[i_count][j_count] = i_temp. scanf("%d". ++i_count) { *(i_adjacency_matrix + i_count) = (int*)malloc(sizeof(int) * i_no_nodes). calc_cost_Dijkstra_algo(i_source). i_count."). system("pause"). &i_temp). for (i_count = 0.Data Structure in C (Lab. ++i_count) { printf("\n"). Pathak Page 98 . j_count < i_no_nodes. for (j_count = 0.

&i_source_node). return i_source_node. int flag[i_no_nodes]. for (i_count = 0. } i_flag = 1. i_nodes_visit[0] = 1. i_count < i_no_nodes. } flag[i_counter] = 1. } int i_source_node. i_count < i_no_nodes. for (i_count = 0. for (i_count = 0. j_count. i_counter = i_count. Programs) printf("\t%d". while (i_init < i_no_nodes) { int i_undef_cost = UNDEFINED_COST. int i_init = 1. ++i_count) { if ((i_nodes_visit[i_count] < i_undef_cost) && (!flag[i_count])) { i_undef_cost = i_nodes_visit[i_count]. } void calc_cost_Dijkstra_algo(int i_source_node) { int i_count. i_flag++. ++i_count) { flag[i_count] = 0. i_adjacency_matrix[i_count][j_count]). Pathak Page 99 . i_count < i_no_nodes. int i_counter. ++i_count) if ((i_nodes_visit[i_counter] + i_adjacency_matrix[i_counter][i_count] < i_nodes_visit[i_count]) && (!flag[i_count])) Saket Kr.Data Structure in C (Lab. printf("\nEnter the source node: "). scanf("%d". i_flag. i_nodes_visit[i_count] = i_adjacency_matrix[i_source_node][j_count].

cost=%d\n". Pathak Page 100 .Data Structure in C (Lab.i_source_node. ++i_count) if (i_count != i_source_node) printf("Source: %d to %d. i_count. } } printf("\n Shortest path so obtained :\n"). Programs) i_nodes_visit[i_count] = i_nodes_visit[i_counter] + i_adjacency_matrix[i_counter][i_count]. i_count < i_no_nodes. i_nodes_visit[i_count]). for (i_count = 0. } ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Saket Kr.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->