Your SlideShare is downloading. ×
2013Saket Kr. PathakSoftware Developer3D GraphicsData Structure in C (Lab. Programs)Programs are complete in best of my kn...
Data Structure in C (Lab. Programs)                                  Content (Programs List)S. No.                       S...
Data Structure in C (Lab. Programs)   1. WAP in C for following Sorting Methods        Bubble Sort        Merge Sort    ...
Data Structure in C (Lab. Programs)     printf("nnResult: n");     disp_elem(ip_store, i_store_size);     printf("nnn");  ...
Data Structure in C (Lab. Programs)       }       return i_store;}void disp_elem(int i_store[], int i_size){     printf("n...
Data Structure in C (Lab. Programs)int i_size;int* get_elements(void);void disp_elements(int*);void divide_(int* i_store, ...
Data Structure in C (Lab. Programs)void conquer_(int* ip_storage, int i_low, int i_mid, int i_high){    int* ip_temp_store...
Data Structure in C (Lab. Programs)                   storage[k]=temp_arr_2[j++];            printf("%d", storage[k]);    ...
Data Structure in C (Lab. Programs)          [1] Start with the result as the first element of the input.          [2] Loo...
Data Structure in C (Lab. Programs){       int* ip_store = malloc(sizeof(int) * i_size);       int i_count = 0;       whil...
Data Structure in C (Lab. Programs)                i_ndx++;                i_size--;       }}      ~~~~~~~~~~~~~~~~~~~~~**...
Data Structure in C (Lab. Programs)     disp_elem(ip_store, i_store_size);     printf("nnn");     system("pause");     ret...
Data Structure in C (Lab. Programs)       while (i_size)       {             printf("%d", i_store[i_ndx]);             i_n...
Data Structure in C (Lab. Programs)     printf("nnn");     system("pause");     return 0;}int* get_elem(int i_size){     i...
Data Structure in C (Lab. Programs)                        i_store[i_begin] = i_store[i_finish];                        i_...
Data Structure in C (Lab. Programs)   2. WAP in C for following Searching Methods        Linear Search        Binary Sea...
Data Structure in C (Lab. Programs)           printf("Item not found.");}void get_input(int* i_storage, int i_num_item){  ...
Data Structure in C (Lab. Programs)       Binary Search:       Algorithm –          [1] Get the middle element;          [...
Data Structure in C (Lab. Programs)recursive_bin_search(i_storage, i_srch_item, i_low, i_mid,i_hi);               }       ...
Data Structure in C (Lab. Programs){     printf("WAP for Binary - Search.");printf("nLimitation: nt-> Items are restrickte...
Data Structure in C (Lab. Programs)    3. WAP in C for array implementation of            Stack            Queue        ...
Data Structure in C (Lab. Programs)       {          stack[++i_top] = i_item;          printf("ntItem - %d, has successful...
Data Structure in C (Lab. Programs)        int i_count;          printf("ntElements of Stack are:");          for(i_count ...
Data Structure in C (Lab. Programs)                   else                        return 0;                   break;      ...
Data Structure in C (Lab. Programs)                        i_state = 0;              }              while(i_state == 1);  ...
Data Structure in C (Lab. Programs)       getch();       return 0;}int set_Argument(void){     printf("nt-----------------...
Data Structure in C (Lab. Programs)       {          i_front = 0;          queue[++i_rear] = i_item;          printf("ntIt...
Data Structure in C (Lab. Programs)       {          printf("ntElements of Queue are:");          int i_count;          fo...
Data Structure in C (Lab. Programs)                          return 1;                   else                        retur...
Data Structure in C (Lab. Programs)       scanf("%d", &i_choice);       if((i_choice > 0) && (i_choice < 5))            re...
Data Structure in C (Lab. Programs)               if((circular_queue[i_count] ==i_item)&&(!b_flag))               {       ...
Data Structure in C (Lab. Programs)                   int i_item = 0;                   printf("ntPlease Enter the item: "...
Data Structure in C (Lab. Programs)int set_Argument(void){     printf("nt-------------------------------------------------...
Data Structure in C (Lab. Programs)#include <stdio.h>#include <stdbool.h>#define LINKED_LIST_SIZE 1024int   set_argument(v...
Data Structure in C (Lab. Programs)     printf("nttt Array - Singly Linked-List.n");     printf("nt-----------------------...
Data Structure in C (Lab. Programs)                   else                        return 0;                   break;      ...
Data Structure in C (Lab. Programs)     printf("ntt---------------------------");     {           printf("nttBy Item: tt(P...
Data Structure in C (Lab. Programs)                    int i_nxt_idx = ll_node[i_count].i_next_idx;                    ll_...
Data Structure in C (Lab. Programs)                      ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val;                ...
Data Structure in C (Lab. Programs)         if (ll_node[i_count].i_next_idx != 0)         {            printf("ntItem: %d"...
Data Structure in C (Lab. Programs)       if(i_check == 0)            printf("nnntInvalid input.");       else       {    ...
Data Structure in C (Lab. Programs)       scanf("%d", &i_choice);       if((i_choice > 0) && (i_choice < 5))            re...
Data Structure in C (Lab. Programs)                   printf("ntTo Display Item of Queue.");                   int i_check...
Data Structure in C (Lab. Programs)         printf("ntItem to delete: ");         scanf("%d", &i_del_item);       int i_co...
Data Structure in C (Lab. Programs)     {            int i_del_idx;            printf("ntIndex to delete: ");            s...
Data Structure in C (Lab. Programs)}int push_node(int i_item, int i_indx){    if (i_indx < (LINKED_LIST_SIZE-1))    {     ...
Data Structure in C (Lab. Programs)            printf("ntNext Index: %d",ll_node[i_count].i_next_idx);            i_count ...
Data Structure in C (Lab. Programs)    4. WAP in C using dynamic memory allocation of         Stack         Queue       ...
Data Structure in C (Lab. Programs)int set_Argument(void){     printf("nt-------------------------------------------------...
Data Structure in C (Lab. Programs)                          return 1;                   else                        retur...
Data Structure in C (Lab. Programs)              return 0;       }       else       {          *(stack + (++i_top)) = i_it...
Data Structure in C (Lab. Programs)     }     else     {        int i_count;          printf("ntElements of Stack are:"); ...
Data Structure in C (Lab. Programs)                        i_state = 0;              }              while(i_state == 1);  ...
Data Structure in C (Lab. Programs)     if(i_rear == (QUEUE_SIZE - 1))     {          printf("ntQueue Overflow.");        ...
Data Structure in C (Lab. Programs)     if(i_front == -1)     {          printf("ntQueue is Empty.");          return 0;  ...
Data Structure in C (Lab. Programs)              }              case 3:              {                   printf("ntTo Disp...
Data Structure in C (Lab. Programs)       else       {          int i_state;          circular_queue = (int*)malloc(sizeof...
Data Structure in C (Lab. Programs)       scanf("%d", &i_choice);       if((i_choice > 0) && (i_choice < 5))            re...
Data Structure in C (Lab. Programs)               if((*(circular_queue + i_count) ==i_item)&&(!b_flag))               {   ...
Data Structure in C (Lab. Programs)                   int i_item = 0;                   printf("ntPlease Enter the item: "...
Data Structure in C (Lab. Programs)       Linked List:       Code – Snippet: (Singly Linked List)#include <stdio.h>#includ...
Data Structure in C (Lab. Programs)}int set_argument(void){     printf("nt------------------------------------------------...
Data Structure in C (Lab. Programs)                   scanf("%d", &i_item);                   printf("ntPlease Enter the i...
Data Structure in C (Lab. Programs)              }       }}int del_option(void){    printf("ntt---------------------------...
Data Structure in C (Lab. Programs)                  (*(ll_node + i_count)).i_next_idx = (*(ll_node+ i_nxt_idx)).i_next_id...
Data Structure in C (Lab. Programs)                      (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val;    ...
Data Structure in C (Lab. Programs)      }      else           return 0;}int disp_node(void){     int i_count = 0;     int...
Data Structure in C (Lab. Programs)struct node{       int i_prev_idx;       int i_val;       int i_next_idx;}*ll_node;int ...
Data Structure in C (Lab. Programs)       return i_check;}int select_choice(void){     {          printf("ntTo    Push Ite...
Data Structure in C (Lab. Programs)                      {                       if (i_check == 0)                        ...
Data Structure in C (Lab. Programs)       scanf("%d", &i_choice);       printf("ntt---------------------------");       if...
Data Structure in C (Lab. Programs)                      (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val;    ...
Data Structure in C (Lab. Programs)                         int i_nxt_idx = (*(ll_node +i_count)).i_next_idx;             ...
Data Structure in C (Lab. Programs)           }           return 1;     }     else            return 0;}int disp_node(void...
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
Upcoming SlideShare
Loading in...5
×

Data Structure in C (Lab Programs)

3,848

Published on

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.

Published in: Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,848
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
438
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Transcript of "Data Structure in C (Lab Programs)"

  1. 1. 2013Saket Kr. PathakSoftware Developer3D GraphicsData Structure in C (Lab. Programs)Programs are complete in best of my knowledge with zero compilation error in IDE BloodshedDev-C++. These can be easily portable to any versions of Visual Studio or Qt. If you need anyguidance please let me know via comments and Always Enjoy Programming.
  2. 2. Data Structure in C (Lab. Programs) Content (Programs List)S. No. Subject Date Sign Remark1. WAP in C for following Sorting Methods  Bubble Sort  Merge Sort  Insertion Sort  Selection Sort  Quick Sort2. WAP in C for following Searching Methods  Linear Search  Binary Search3. WAP in C for array implementation of  Stack  Queue  Circular Queue  Linked List4. WAP in C using dynamic memory allocation of  Stack  Queue  Circular Queue  Linked List5. WAP in C for implementation of Binary Tree.6. WAP in C for Tree Traversal  Pre-Order  In-Order  Post-Order7. WAP in C for Graph Traversal  Breadth First Search  Depth First Search8. WAP in C for Minimum Cost Spanning Tree9. WAP in C for Shortest Path ProblemSaket Kr. Pathak Page 2
  3. 3. Data Structure in C (Lab. Programs) 1. WAP in C for following Sorting Methods  Bubble Sort  Merge Sort  Insertion Sort  Selection Sort  Quick SortProgram: 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: O (n) time Average case: O (n2) time Worst case: O (n2) time 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("nnn"); printf("tttWAP of Bubble sort."); printf("nnn"); 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);Saket Kr. Pathak Page 3
  4. 4. Data Structure in C (Lab. Programs) printf("nnResult: n"); disp_elem(ip_store, i_store_size); printf("nnn"); system("pause"); return 0;}int* get_elem(int i_size){ int* ip_store = malloc(sizeof(int) * i_size); int i_count = 0; int i_temp_size = i_size; while (i_temp_size) { printf("nEnter item for index - %d : ", i_count); scanf("%d", (ip_store+i_count)); i_count++; i_temp_size--; } disp_elem(ip_store, i_size); return ip_store;}int* bubble_sort(int i_store[], int i_size){ int i_temp; int i_count_0, i_count_1; printf("nnSwapping Steps: n"); for (i_count_0 = (i_size - 1); i_count_0 > 0; --i_count_0) { for (i_count_1 = 1; i_count_1 <= i_count_0;++i_count_1) { if (i_store[i_count_1 - 1] > i_store[i_count_1]) { i_temp = i_store[i_count_1 - 1]; i_store[i_count_1 - 1] = i_store[i_count_1]; i_store[i_count_1] = i_temp; disp_elem(i_store, i_size); } }Saket Kr. Pathak Page 4
  5. 5. Data Structure in C (Lab. Programs) } return i_store;}void disp_elem(int i_store[], int i_size){ printf("n-------------------------------------------------------n"); printf("Elements stored in order: "); int i_ndx = 0; while (i_size) { printf("| %d |",i_store[i_ndx]); i_ndx++; i_size--; } printf("n-------------------------------------------------------");} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Merge Sort: Algorithm – [1] If the input sequence has fewer than two elements, return. [2] Partition the input sequence into two halves. [3] Sort the two subsequences using the same algorithm. [4] Merge the two sorted subsequences to form the output sequence. Time Complexity – Best case: O (n * log (n)) time Average case: O (n * log (n)) time Worst case: O (n * log (n)) time Code – Snippet:#include<stdio.h>#include<stdbool.h>#define ARRAY_SIZE 1024Saket Kr. Pathak Page 5
  6. 6. Data Structure in C (Lab. Programs)int i_size;int* get_elements(void);void disp_elements(int*);void divide_(int* i_store, int i_low, int i_high, bool b_flag);void conquer_(int* ip_storage, int i_low, int i_mid, inti_high);void combine_(int* storage, int* temp_arr_1, int* temp_arr_2,int i_low, int i_height);int main(){ printf("nnn"); printf("tttWAP of Merge sort."); printf("nnn"); int* i_store; i_store = get_elements(); divide_(i_store, 0, i_size-1, 0); disp_elements(i_store); getch(); return 0;}void divide_(int* i_store, int i_low, int i_high, bool b_flag){ printf("nDivide low(%d) | high(%d) | flag(%d): ", i_low,i_high, b_flag); int i_count; for (i_count = i_low; i_count <= i_high; ++i_count) { printf("%d", *(i_store + i_count)); } int i_mid = (i_low + i_high) / 2; if (i_low < i_high) { divide_(i_store, i_low, i_mid, 1); divide_(i_store, i_mid + 1, i_high, 0); conquer_(i_store, i_low, i_mid, i_high); }}Saket Kr. Pathak Page 6
  7. 7. Data Structure in C (Lab. Programs)void conquer_(int* ip_storage, int i_low, int i_mid, int i_high){ int* ip_temp_store_1 = malloc(sizeof(int) * ARRAY_SIZE); int* ip_temp_store_2 = malloc(sizeof(int) * ARRAY_SIZE); int n1,n2,i,j,k; n1 = i_mid - i_low + 1; n2 = i_high - i_mid; printf("ntMerge low(%d) | mid(%d) | high(%d): ", i_low,i_mid, i_high); printf("ntip_temp_store_1: "); for(i=0; i<n1; i++) { ip_temp_store_1[i] = ip_storage[i_low+i]; printf("%d", i, ip_temp_store_1[i]); } printf("ntip_temp_store_2: "); for(j=0; j<n2; j++) { ip_temp_store_2[j] = ip_storage[i_mid+j+1]; printf("%d", j, ip_temp_store_2[j]); } // To mark the end of each temporary array ip_temp_store_1[i] = 10000000; ip_temp_store_2[j] = 10000000; combine_(ip_storage, ip_temp_store_1, ip_temp_store_2,i_low, i_high);}void combine_(int* storage, int* temp_arr_1, int* temp_arr_2,int i_low, int i_height){ int i=0; int j=0; int k; printf("nResult: "); for (k=i_low; k<=i_height; k++) { if (temp_arr_1[i] <= temp_arr_2[j]) storage[k] = temp_arr_1[i++]; elseSaket Kr. Pathak Page 7
  8. 8. Data Structure in C (Lab. Programs) storage[k]=temp_arr_2[j++]; printf("%d", storage[k]); } printf("n");}void disp_elements(int* i_storage){ printf("nnn"); int i_count; printf("Elements: "); for (i_count = 0; i_count < i_size; ++i_count) { printf("%d",*(i_storage + i_count)); } printf("nnn");}int* get_elements(void){ int i_count; printf("Enter the size of array: "); scanf("%d",&i_size); int* i_storage = (int*) malloc(sizeof(int) * i_size); for(i_count = 0; i_count < i_size; i_count++) { printf("Enter the element at pos[%d]: ", i_count); scanf("%d",(i_storage + i_count)); } return i_storage;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Insertion Sort: Algorithm –Saket Kr. Pathak Page 8
  9. 9. Data Structure in C (Lab. Programs) [1] Start with the result as the first element of the input. [2] Loop over the input until it is empty, "removing" the first remaining (leftmost) element. [3] Compare the removed element against the current result, starting from the highest (rightmost) element, and working left towards the lowest element. [4] If the removed input element is lower than the current result element, copy that value into the following element to make room for the new element below, and repeat with the next lowest result element. [5] Otherwise, the new element is in the correct location; save it in the cell left by copying the last examined result up, and start again from (2) with the next input element. Time Complexity – Best case: O (n) time Average case: O (n2) time Worst case: O (n2) time Code – Snippet:#include <stdio.h>int* insertion_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("nnn"); printf("tttWAP of Insertion sort."); printf("nnn"); 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 = insertion_sort(ip_store, i_store_size); disp_elem(ip_store, i_store_size); printf("nnn"); system("pause"); return 0;}int* get_elem(int i_size)Saket Kr. Pathak Page 9
  10. 10. Data Structure in C (Lab. Programs){ 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* insertion_sort(int i_store[], int i_size){ int i_temp; int i_count_0, i_count_1, i_count_2; for (i_count_0 = 1; i_count_0 <= (i_size-1); ++i_count_0) { for (i_count_1 = 0; i_count_1 < i_count_0; ++i_count_1) { if (i_store[i_count_1] > i_store[i_count_0]) { i_temp = i_store[i_count_1] ; i_store[i_count_1] = i_store[i_count_0] ; for (i_count_2 = i_count_0; i_count_2 >i_count_1; i_count_2--) i_store[i_count_2] = i_store[i_count_2- 1] ; i_store[i_count_2 + 1] = i_temp ; } } } return i_store;}void disp_elem(int i_store[], int i_size){ printf("nDisplaying Elements of store: "); int i_ndx = 0; while (i_size) { printf("%d", i_store[i_ndx]);Saket Kr. Pathak Page 10
  11. 11. Data Structure in C (Lab. Programs) i_ndx++; i_size--; }} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~ Selection Sort: Algorithm – [1] Get a hand of unsorted cards/numbers. [2] Set a marker for the sorted section after the first card of the hand. [3] Repeat steps 4 through 6 until the unsorted section is empty. [4] Select the first unsorted card. [5] Swap this card to the left until it arrives at the correct sorted position. [6] Advance the marker to the right one card. [7] Stop Time Complexity – Best case: O (n2) time Average case: O (n2) time Worst case: O (n2) time Code – Snippet:#include <stdio.h>int* selection_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("nnn"); printf("tttWAP of Selection sort."); printf("nnn"); 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 = selection_sort(ip_store, i_store_size);Saket Kr. Pathak Page 11
  12. 12. Data Structure in C (Lab. Programs) disp_elem(ip_store, i_store_size); printf("nnn"); 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
  13. 13. 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: O (n * log (n)) time Average case: O (n * log (n)) time Worst case: O (n2) time 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("nnn"); printf("tttWAP of Quick sort."); printf("nnn"); 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
  14. 14. Data Structure in C (Lab. Programs) printf("nnn"); 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
  15. 15. Data Structure in C (Lab. Programs) i_store[i_begin] = i_store[i_finish]; i_store[i_finish] = i_temp; } } i_temp = i_store[i_pivot]; i_store[i_pivot] = i_store[i_finish]; i_store[i_finish] = i_temp; quick_sort(i_store, i_start, i_finish - 1); quick_sort(i_store, i_finish + 1, i_end); } return i_store;}void disp_elem(int i_store[], int i_size){ printf("nDisplaying Elements of store: "); int i_ndx = 0; while (i_size) { printf("%d", i_store[i_ndx]); i_ndx++; i_size--; }}~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 15
  16. 16. Data Structure in C (Lab. Programs) 2. WAP in C for following Searching Methods  Linear Search  Binary SearchProgram: Linear Search: Algorithm – [1] Input: Array D of Business objects, phone number key. [2] Output: first index where key’s phone number matches D, or -1 if not found Time Complexity – Best case: O (1) time Average case: O (n) time Worst case: O (n) time Code – Snippet:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <math.h>void linear_search(int* i_storage, int i_srch_item, inti_num_item){ int i_count; bool b_flag = false; for (i_count = 0; i_count < i_num_item; ++i_count) { if (i_storage[i_count] == i_srch_item) { b_flag = true; printf("Item (%d) is found at position: %d",i_srch_item, i_count); break; } } if (!b_flag)Saket Kr. Pathak Page 16
  17. 17. Data Structure in C (Lab. Programs) printf("Item not found.");}void get_input(int* i_storage, int i_num_item){ printf("Your Storage Items are: nt"); int i_loop_count; for(i_loop_count = 0; i_loop_count < i_num_item;++i_loop_count) { printf("%d, ", i_storage[i_loop_count]); } printf("nnn"); int i_srch_item; printf("Please Enter the item (number) to search: "); scanf("%d", &i_srch_item); printf("nnn"); linear_search(i_storage, i_srch_item, i_num_item);}void set_argument(void){ printf("WAP for Linear - Search."); printf("nLimitation: nt-> Items are restrickted withinteger number.nt-> Starting index of storage is 0."); printf("nnn"); int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; get_input(i_storage, (sizeof(i_storage)/sizeof(int)));}int main(){ set_argument(); printf("nnn"); getch(); return 0;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 17
  18. 18. Data Structure in C (Lab. Programs) Binary Search: Algorithm – [1] Get the middle element; [2] If the middle element equals to the searched value, the algorithm stops; [3] Otherwise, two cases are possible:  Searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.  Searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element. Time Complexity – Best case: O (1) time Average case: O (log (n)) time Worst case: O (n) time Code – Snippet:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdbool.h>#include <math.h>bool check_item_order(int* i_storage){ //Function to check the Item - List is in Sorted order. return true;}void recursive_bin_search(int* i_storage, int i_srch_item, inti_low, int i_mid, int i_hi){ if(i_low < i_hi) { if(i_storage[i_mid] == i_srch_item) {printf("Item (%d) is found at position: %d", i_srch_item,i_mid); } else if(i_storage[i_mid] > i_srch_item) { i_hi = i_mid; i_mid = (i_low + i_hi)/2;Saket Kr. Pathak Page 18
  19. 19. Data Structure in C (Lab. Programs)recursive_bin_search(i_storage, i_srch_item, i_low, i_mid,i_hi); } else if(i_storage[i_mid] < i_srch_item) { i_low = i_mid; i_mid = (i_low + i_hi)/2;recursive_bin_search(i_storage, i_srch_item, i_low, i_mid,i_hi); } }}void recursive_binary_search(int* i_storage, int i_num_item){ bool b_check = check_item_order(i_storage); printf("Your Storage Items are: nt"); int i_loop_count; for(i_loop_count = 0; i_loop_count < i_num_item;++i_loop_count) { printf("%d, ", i_storage[i_loop_count]); } printf("nnn"); int i_srch_item; printf("Please Enter the item (number) to search: "); scanf("%d", &i_srch_item); printf("nnn"); int i_low = 0; int i_hi = i_num_item; int i_mid = (i_low + i_hi)/2; if(b_check) recursive_bin_search(i_storage, i_srch_item, i_low,i_mid, i_hi); else printf("nItems are not in Order.n");}void binary_search(void)Saket Kr. Pathak Page 19
  20. 20. Data Structure in C (Lab. Programs){ printf("WAP for Binary - Search.");printf("nLimitation: nt-> Items are restrickted with integernumber.nt-> Starting index of storage is 0."); printf("nnn"); int i_storage[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; recursive_binary_search(i_storage,(sizeof(i_storage)/sizeof(int)));}int main(){ binary_search(); printf("nnn"); getch(); return 0;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 20
  21. 21. Data Structure in C (Lab. Programs) 3. WAP in C for array implementation of  Stack  Queue  Circular Queue  Linked ListProgram: Stack: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define STACK_SIZE 1024int i_top = -1;int stack[STACK_SIZE];int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); 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_top == (STACK_SIZE - 1)) { printf("ntStack Overflow."); return 0; } elseSaket Kr. Pathak Page 21
  22. 22. Data Structure in C (Lab. Programs) { stack[++i_top] = i_item; printf("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if(i_top == -1) { printf("ntStack is Underflow."); return 0; } else { bool b_flag = false; int i_count; for(i_count = 0; i_count <= i_top; ++i_count) { if((stack[i_count] == i_item)&&(!b_flag)) { stack[i_count] = stack[i_count+1]; b_flag = true; } else if(b_flag) { stack[i_count] = stack[i_count+1]; } } i_top = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){ if(i_top == -1) { printf("ntStack is Empty."); return 0; } else {Saket Kr. Pathak Page 22
  23. 23. Data Structure in C (Lab. Programs) int i_count; printf("ntElements of Stack are:"); for(i_count = 0; i_count <= i_top; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,stack[i_count]); } return 1; }}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Stack."); int i_item = 0; printf("ntPlease 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("ntTo Pop Item from Stack."); int i_item = 0; printf("ntPlease 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("ntTo Display Item of Stack."); int i_check = disp_item(); if(i_check == 1) return 1;Saket Kr. Pathak Page 23
  24. 24. Data Structure in C (Lab. Programs) else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Check for Exit.Saket Kr. Pathak Page 24
  25. 25. Data Structure in C (Lab. Programs) i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Queue: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define QUEUE_SIZE 1024int i_front = -1;int i_rear = -1;int queue[QUEUE_SIZE];int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Checkfor Exit. i_state = 0; } while(i_state == 1); } printf("nnn");Saket Kr. Pathak Page 25
  26. 26. Data Structure in C (Lab. Programs) getch(); return 0;}int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); 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_rear == (QUEUE_SIZE - 1)) { printf("ntQueue Overflow."); return 0; } elseSaket Kr. Pathak Page 26
  27. 27. Data Structure in C (Lab. Programs) { i_front = 0; queue[++i_rear] = i_item; printf("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if((i_front == -1)||(i_front > i_rear)) { printf("ntQueue is Underflow."); return 0; } else { bool b_flag = false; int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { if((queue[i_count] == i_item)&&(!b_flag)) { queue[i_count] = queue[i_count+1]; b_flag = true; } else if(b_flag) { queue[i_count] = queue[i_count+1]; } } i_rear = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){ if(i_front == -1) { printf("ntQueue is Empty."); return 0; } elseSaket Kr. Pathak Page 27
  28. 28. Data Structure in C (Lab. Programs) { printf("ntElements of Queue are:"); int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,queue[i_count]); } return 1; }}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Queue."); int i_item = 0; printf("ntPlease 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("ntTo Pop Item from Queue."); int i_item = 0; printf("ntPlease 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("ntTo Display Item of Queue."); int i_check = disp_item(); if(i_check == 1)Saket Kr. Pathak Page 28
  29. 29. Data Structure in C (Lab. Programs) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Circular Queue: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define QUEUE_SIZE 1024int i_front = -1;int i_rear = -1;int circular_queue[QUEUE_SIZE];int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: ");Saket Kr. Pathak Page 29
  30. 30. 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("ntQueue 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("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if(i_front == -1) { printf("ntQueue 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
  31. 31. Data Structure in C (Lab. Programs) if((circular_queue[i_count] ==i_item)&&(!b_flag)) { circular_queue[i_count] =circular_queue[i_count+1]; b_flag = true; } else if(b_flag) { circular_queue[i_count] =circular_queue[i_count+1]; } } i_rear = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){ if((i_front == -1) || (i_front == i_rear + 1)) { printf("ntQueue is Empty."); return 0; } else { printf("ntElements of Queue are:"); int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,circular_queue[i_count]); } return 1; }}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Queue.");Saket Kr. Pathak Page 31
  32. 32. Data Structure in C (Lab. Programs) int i_item = 0; printf("ntPlease 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("ntTo Pop Item from Queue."); int i_item = 0; printf("ntPlease 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("ntTo Display Item of Queue."); int i_check = disp_item(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}Saket Kr. Pathak Page 32
  33. 33. Data Structure in C (Lab. Programs)int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Linked List: Code – Snippet: (Singly Linked List)Saket Kr. Pathak Page 33
  34. 34. Data Structure in C (Lab. Programs)#include <stdio.h>#include <stdbool.h>#define LINKED_LIST_SIZE 1024int set_argument(void);int select_choice(void);int process_stack(int i_choice);int push_node(int i_item, int i_indx);int disp_node(void);int pop_node(int i_option);struct node{ int i_val; int i_next_idx;}ll_node[LINKED_LIST_SIZE];int i_ll_size = 0;int main(){ int i_check = set_argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}int set_argument(void){ printf("nt--------------------------------------------------n");Saket Kr. Pathak Page 34
  35. 35. Data Structure in C (Lab. Programs) printf("nttt Array - Singly Linked-List.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Linked-List.n"); int i_item = 0; int i_indx = 0; printf("ntPlease Enter the item: "); scanf("%d", &i_item); printf("ntPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1;Saket Kr. Pathak Page 35
  36. 36. Data Structure in C (Lab. Programs) else return 0; break; } case 2: { printf("ntTo Pop Item from Queue."); int i_check = del_option(); { if (i_check == 0) printf("nnntInvalid input."); else ; } i_check = pop_node(i_check); if(i_check == 1) return 1; else return 0; break; } case 3: { printf("ntTo Display Item of Queue."); int i_check = disp_node(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}int del_option(void){Saket Kr. Pathak Page 36
  37. 37. Data Structure in C (Lab. Programs) printf("ntt---------------------------"); { printf("nttBy Item: tt(Press) 1"); printf("nttBy Reference: tt(Press) 2"); } int i_choice; printf("nnttPlease Enter Your Choice: "); scanf("%d", &i_choice); printf("ntt---------------------------"); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int pop_node(int i_option){ if (i_option == 1) { int i_del_item; printf("ntItem to delete: "); scanf("%d", &i_del_item); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size< i_ll_size; ) { if ((ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count].i_val == i_del_item) { int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } else if (b_flag) {Saket Kr. Pathak Page 37
  38. 38. Data Structure in C (Lab. Programs) int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = ll_node[i_count].i_next_idx; i_size++; } } else break; } } else if (i_option == 2) { int i_del_idx; printf("ntIndex to delete: "); scanf("%d", &i_del_idx); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1),i_size < i_ll_size; ) { if ((ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count].i_next_idx == i_del_idx) { int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } else if (b_flag) { int i_nxt_idx =ll_node[i_count].i_next_idx;Saket Kr. Pathak Page 38
  39. 39. Data Structure in C (Lab. Programs) ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = ll_node[i_count].i_next_idx; i_size++; } } else break; } } return 1;}int push_node(int i_item, int i_indx){ if (i_indx < (LINKED_LIST_SIZE-1)) { ll_node[i_ll_size].i_val = i_item; ll_node[i_ll_size].i_next_idx = i_indx; i_ll_size = i_indx; //For Last Node ll_node[i_ll_size].i_val = 100001; ll_node[i_ll_size].i_next_idx = 0; //------------- return 1; } else return 0;}int disp_node(void){ int i_count = 0; int i_size = 0; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size <i_ll_size; ) {Saket Kr. Pathak Page 39
  40. 40. Data Structure in C (Lab. Programs) if (ll_node[i_count].i_next_idx != 0) { printf("ntItem: %d", ll_node[i_count].i_val); printf("ntNext Index: %d",ll_node[i_count].i_next_idx); i_count = ll_node[i_count].i_next_idx; i_size++; printf("nt************************n"); } else break; } return 1;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Code – Snippet: (Doubly Linked List)#include <stdio.h>#include <stdbool.h>#define LINKED_LIST_SIZE 1024int set_argument(void);int select_choice(void);int process_stack(int i_choice);int push_node(int i_item, int i_indx);int disp_node(void);int pop_node(int i_option);struct node{ int i_prev_idx; int i_val; int i_next_idx;}ll_node[LINKED_LIST_SIZE];int i_ll_size = 0;int main(){ int i_check = set_argument();Saket Kr. Pathak Page 40
  41. 41. Data Structure in C (Lab. Programs) if(i_check == 0) printf("nnntInvalid input."); else { int i_state; do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}int set_argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Singly Linked-List.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: ");Saket Kr. Pathak Page 41
  42. 42. Data Structure in C (Lab. Programs) scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Linked-List.n"); int i_item = 0; int i_indx = 0; printf("ntPlease Enter the item: "); scanf("%d", &i_item); printf("ntPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1; else return 0; break; } case 2: { printf("ntTo Pop Item from Queue."); int i_check = del_option(); { if (i_check == 0) printf("nnntInvalid input."); else ; } i_check = pop_node(i_check); if(i_check == 1) return 1; else return 0; break; } case 3: {Saket Kr. Pathak Page 42
  43. 43. Data Structure in C (Lab. Programs) printf("ntTo Display Item of Queue."); int i_check = disp_node(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}int del_option(void){ printf("ntt---------------------------"); { printf("nttBy Item: tt(Press) 1"); printf("nttBy Reference: tt(Press) 2"); } int i_choice; printf("nnttPlease Enter Your Choice: "); scanf("%d", &i_choice); printf("ntt---------------------------"); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int pop_node(int i_option){ if (i_option == 1) { int i_del_item;Saket Kr. Pathak Page 43
  44. 44. Data Structure in C (Lab. Programs) printf("ntItem to delete: "); scanf("%d", &i_del_item); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size< i_ll_size; ) { if ((ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count].i_val == i_del_item) { int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_prev_idx =ll_node[i_nxt_idx].i_prev_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } else if (b_flag) { int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_prev_idx =ll_node[i_nxt_idx].i_prev_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = ll_node[i_count].i_next_idx; i_size++; } } else break; } } else if (i_option == 2)Saket Kr. Pathak Page 44
  45. 45. Data Structure in C (Lab. Programs) { int i_del_idx; printf("ntIndex to delete: "); scanf("%d", &i_del_idx); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1),i_size < i_ll_size; ) { if ((ll_node[i_count].i_next_idx != 0)&&(!b_flag)) { if (ll_node[i_count].i_next_idx == i_del_idx) { int i_nxt_idx = ll_node[i_count].i_next_idx; ll_node[i_count].i_val =ll_node[i_nxt_idx].i_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } 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_val; ll_node[i_count].i_next_idx =ll_node[i_nxt_idx].i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = ll_node[i_count].i_next_idx; i_size++; } } else break; } } return 1;Saket Kr. Pathak Page 45
  46. 46. Data Structure in C (Lab. Programs)}int push_node(int i_item, int i_indx){ if (i_indx < (LINKED_LIST_SIZE-1)) { if (i_ll_size == 0) { ll_node[i_ll_size].i_prev_idx = 0; ll_node[i_ll_size].i_val = i_item; ll_node[i_ll_size].i_next_idx = i_indx; i_ll_size = i_indx; } else { ll_node[i_ll_size].i_prev_idx =ll_node[i_ll_size].i_prev_idx; ll_node[i_ll_size].i_val = i_item; ll_node[i_ll_size].i_next_idx = i_indx; i_ll_size = i_indx; //For Last Node ll_node[i_ll_size].i_prev_idx = i_indx; ll_node[i_ll_size].i_val = 100001; ll_node[i_ll_size].i_next_idx = 0; //------------- } return 1; } else return 0;}int disp_node(void){ int i_count = 0; int i_size = 0; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size <i_ll_size; ) { if (ll_node[i_count].i_next_idx != 0) { printf("ntPrev Index: %d",ll_node[i_count].i_prev_idx); printf("ntItem: %d", ll_node[i_count].i_val);Saket Kr. Pathak Page 46
  47. 47. Data Structure in C (Lab. Programs) printf("ntNext Index: %d",ll_node[i_count].i_next_idx); i_count = ll_node[i_count].i_next_idx; i_size++; printf("nt************************n"); } else break; } return 1;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 47
  48. 48. Data Structure in C (Lab. Programs) 4. WAP in C using dynamic memory allocation of  Stack  Queue  Circular Queue  Linked ListProgram: Stack: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define STACK_SIZE 1024int i_top = -1;int *stack;int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; stack = (int*)malloc(sizeof(int) * STACK_SIZE); do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Checkfor Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}Saket Kr. Pathak Page 48
  49. 49. Data Structure in C (Lab. Programs)int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Stack."); int i_item = 0; printf("ntPlease Enter the item: "); scanf("%d", &i_item); int i_check = push_item(i_item); if(i_check == 1)Saket Kr. Pathak Page 49
  50. 50. Data Structure in C (Lab. Programs) return 1; else return 0; break; } case 2: { printf("ntTo Pop Item from Stack."); int i_item = 0; printf("ntPlease 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("ntTo Display Item of Stack."); int i_check = disp_item(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}int push_item(int i_item){ if(i_top == (STACK_SIZE - 1)) { printf("ntStack Overflow.");Saket Kr. Pathak Page 50
  51. 51. Data Structure in C (Lab. Programs) return 0; } else { *(stack + (++i_top)) = i_item; printf("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if(i_top == -1) { printf("ntStack is Underflow."); return 0; } else { bool b_flag = false; int i_count; for(i_count = 0; i_count <= i_top; ++i_count) { if((*(stack + i_count) == i_item)&&(!b_flag)) { *(stack + i_count) = *(stack + (i_count+1)); b_flag = true; } else if(b_flag) { *(stack + i_count) = *(stack + (i_count+1)); } } i_top = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){ if(i_top == -1) { printf("ntStack is Empty."); return 0;Saket Kr. Pathak Page 51
  52. 52. Data Structure in C (Lab. Programs) } else { int i_count; printf("ntElements of Stack are:"); for(i_count = 0; i_count <= i_top; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,*(stack + i_count)); } return 1; }} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Queue: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define QUEUE_SIZE 1024int i_front = -1;int i_rear = -1;int *queue;int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; queue = (int*)malloc(sizeof(int) * QUEUE_SIZE); do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Check for Exit.Saket Kr. Pathak Page 52
  53. 53. Data Structure in C (Lab. Programs) i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int push_item(int i_item){Saket Kr. Pathak Page 53
  54. 54. Data Structure in C (Lab. Programs) if(i_rear == (QUEUE_SIZE - 1)) { printf("ntQueue Overflow."); return 0; } else { i_front = 0; *(queue + (++i_rear)) = i_item; printf("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if((i_front == -1)||(i_front > i_rear)) { printf("ntQueue is Underflow."); return 0; } else { bool b_flag = false; int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { if((*(queue + i_count) == i_item)&&(!b_flag)) { *(queue + i_count) = *(queue + (i_count+1)); b_flag = true; } else if(b_flag) { *(queue + i_count) = *(queue + (i_count+1)); } } i_rear = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){Saket Kr. Pathak Page 54
  55. 55. Data Structure in C (Lab. Programs) if(i_front == -1) { printf("ntQueue is Empty."); return 0; } else { printf("ntElements of Queue are:"); int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,*(queue + i_count)); } return 1; }}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Queue."); int i_item = 0; printf("ntPlease 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("ntTo Pop Item from Queue."); int i_item = 0; printf("ntPlease Enter the item: "); scanf("%d", &i_item); int i_check = pop_item(i_item); if(i_check == 1) return 1; else return 0; break;Saket Kr. Pathak Page 55
  56. 56. Data Structure in C (Lab. Programs) } case 3: { printf("ntTo Display Item of Queue."); int i_check = disp_item(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Circular Queue: Code – Snippet:#include <stdio.h>#include <stdbool.h>#define QUEUE_SIZE 1024int i_front = -1;int i_rear = -1;int *circular_queue;int main(){ int i_check = set_Argument(); if(i_check == 0) printf("nnntInvalid input.");Saket Kr. Pathak Page 56
  57. 57. Data Structure in C (Lab. Programs) else { int i_state; circular_queue = (int*)malloc(sizeof(int) *QUEUE_SIZE); do { i_state = process_stack(i_check); i_check = set_Argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}int set_Argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Container.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: ");Saket Kr. Pathak Page 57
  58. 58. 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("ntQueue 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("ntItem - %d, has successfully pushed intoStack.", i_item); return 1; }}int pop_item(int i_item){ if(i_front == -1) { printf("ntQueue 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 58
  59. 59. Data Structure in C (Lab. Programs) if((*(circular_queue + i_count) ==i_item)&&(!b_flag)) { *(circular_queue + i_count) =*(circular_queue + (i_count+1)); b_flag = true; } else if(b_flag) { *(circular_queue + i_count) =*(circular_queue + (i_count+1)); } } i_rear = (i_count - 2); //Substracting: 2 = (additional loop increment + 1 deleteditem) return 1; }}int disp_item(void){ if((i_front == -1) || (i_front == i_rear + 1)) { printf("ntQueue is Empty."); return 0; } else { printf("ntElements of Queue are:"); int i_count; for(i_count = i_front; i_count <= i_rear; ++i_count) { printf("ntIndex: %d | Item: %d", i_count,*(circular_queue + i_count)); } return 1; }}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Queue.");Saket Kr. Pathak Page 59
  60. 60. Data Structure in C (Lab. Programs) int i_item = 0; printf("ntPlease 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("ntTo Pop Item from Queue."); int i_item = 0; printf("ntPlease 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("ntTo Display Item of Queue."); int i_check = disp_item(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 60
  61. 61. Data Structure in C (Lab. Programs) Linked List: Code – Snippet: (Singly Linked List)#include <stdio.h>#include <stdbool.h>#define LINKED_LIST_SIZE 1024int set_argument(void);int select_choice(void);int process_stack(int i_choice);int push_node(int i_item, int i_indx);int disp_node(void);int pop_node(int i_option);struct node{ int i_val; int i_next_idx;}*ll_node;int i_ll_size = 0;int main(){ int i_check = set_argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; ll_node = (struct node*)malloc(sizeof(int) *LINKED_LIST_SIZE); do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;Saket Kr. Pathak Page 61
  62. 62. Data Structure in C (Lab. Programs)}int set_argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Singly Linked-List.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ; return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Linked-List.n"); int i_item = 0; int i_indx = 0; printf("ntPlease Enter the item: ");Saket Kr. Pathak Page 62
  63. 63. Data Structure in C (Lab. Programs) scanf("%d", &i_item); printf("ntPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1; else return 0; break; } case 2: { printf("ntTo Pop Item from Queue."); int i_check = del_option(); { if (i_check == 0) printf("nnntInvalid input."); else ; } i_check = pop_node(i_check); if(i_check == 1) return 1; else return 0; break; } case 3: { printf("ntTo Display Item of Queue."); int i_check = disp_node(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break;Saket Kr. Pathak Page 63
  64. 64. Data Structure in C (Lab. Programs) } }}int del_option(void){ printf("ntt---------------------------"); { printf("nttBy Item: tt(Press) 1"); printf("nttBy Reference: tt(Press) 2"); } int i_choice; printf("nnttPlease Enter Your Choice: "); scanf("%d", &i_choice); printf("ntt---------------------------"); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int pop_node(int i_option){ if (i_option == 1) { int i_del_item; printf("ntItem to delete: "); scanf("%d", &i_del_item); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size< i_ll_size; ) { if (((*(ll_node + i_count)).i_next_idx !=0)&&(!b_flag)) { if ((*(ll_node + i_count)).i_val == i_del_item) { int i_nxt_idx = (*(ll_node +i_count)).i_next_idx; (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val;Saket Kr. Pathak Page 64
  65. 65. Data Structure in C (Lab. Programs) (*(ll_node + i_count)).i_next_idx = (*(ll_node+ i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } 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_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = (*(ll_node + i_count)).i_next_idx; i_size++; } } else break; } } else if (i_option == 2) { int i_del_idx; printf("ntIndex to delete: "); scanf("%d", &i_del_idx); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1),i_size < i_ll_size; ) { if (((*(ll_node + i_count)).i_next_idx !=0)&&(!b_flag)) { if ((*(ll_node + i_count)).i_next_idx ==i_del_idx) { int i_nxt_idx = (*(ll_node +i_count)).i_next_idx;Saket Kr. Pathak Page 65
  66. 66. Data Structure in C (Lab. Programs) (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } 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_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = (*(ll_node + i_count)).i_next_idx; i_size++; } } else break; } } return 1;}int push_node(int i_item, int i_indx){ if (i_indx < (LINKED_LIST_SIZE-1)) { (*(ll_node + i_ll_size)).i_val = i_item; (*(ll_node + i_ll_size)).i_next_idx = i_indx; i_ll_size = i_indx; //For Last Node (*(ll_node + i_ll_size)).i_val = 100001; (*(ll_node + i_ll_size)).i_next_idx = 0; //------------- return 1;Saket Kr. Pathak Page 66
  67. 67. Data Structure in C (Lab. Programs) } else return 0;}int disp_node(void){ int i_count = 0; int i_size = 0; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size <i_ll_size; ) { if ((*(ll_node + i_count)).i_next_idx != 0) { printf("ntItem: %d", (*(ll_node +i_count)).i_val); printf("ntNext Index: %d", (*(ll_node +i_count)).i_next_idx); i_count = (*(ll_node + i_count)).i_next_idx; i_size++; printf("nt************************n"); } else break; } return 1;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~ Code – Snippet: (Doubly Linked List)#include <stdio.h>#include <stdbool.h>#define LINKED_LIST_SIZE 1024int set_argument(void);int select_choice(void);int process_stack(int i_choice);int push_node(int i_item, int i_indx);int disp_node(void);int pop_node(int i_option);Saket Kr. Pathak Page 67
  68. 68. Data Structure in C (Lab. Programs)struct node{ int i_prev_idx; int i_val; int i_next_idx;}*ll_node;int i_ll_size = 0;int main(){ int i_check = set_argument(); if(i_check == 0) printf("nnntInvalid input."); else { int i_state; ll_node = (struct node*)malloc(sizeof(int) *LINKED_LIST_SIZE); do { i_state = process_stack(i_check); i_check = set_argument(); if(i_check == 4) //Check for Exit. i_state = 0; } while(i_state == 1); } printf("nnn"); getch(); return 0;}int set_argument(void){ printf("nt--------------------------------------------------n"); printf("nttt Array - Singly Linked-List.n"); printf("nt--------------------------------------------------nn"); int i_check = select_choice(); if(i_check == 0) printf("nnntInvalid input."); else ;Saket Kr. Pathak Page 68
  69. 69. Data Structure in C (Lab. Programs) return i_check;}int select_choice(void){ { printf("ntTo Push Item: tt(Press) 1"); printf("ntTo Pop Item: tt(Press) 2"); printf("ntTo Display Item: t(Press) 3"); printf("ntTo Exit: tt(Press) 4"); } int i_choice; printf("nntPlease Enter Your Choice: "); scanf("%d", &i_choice); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int process_stack(int i_choice){ switch(i_choice) { case 1: { printf("ntTo Push Item into Linked-List.n"); int i_item = 0; int i_indx = 0; printf("ntPlease Enter the item: "); scanf("%d", &i_item); printf("ntPlease Enter the index: "); scanf("%d", &i_indx); int i_check = push_node(i_item, i_indx); if(i_check == 1) return 1; else return 0; break; } case 2: { printf("ntTo Pop Item from Queue."); int i_check = del_option();Saket Kr. Pathak Page 69
  70. 70. Data Structure in C (Lab. Programs) { if (i_check == 0) printf("nnntInvalid input."); else ; } i_check = pop_node(i_check); if(i_check == 1) return 1; else return 0; break; } case 3: { printf("ntTo Display Item of Queue."); int i_check = disp_node(); if(i_check == 1) return 1; else return 0; break; } case 4: { printf("ntTo Exit."); return 0; break; } default: { return 0; break; } }}int del_option(void){ printf("ntt---------------------------"); { printf("nttBy Item: tt(Press) 1"); printf("nttBy Reference: tt(Press) 2"); } int i_choice; printf("nnttPlease Enter Your Choice: ");Saket Kr. Pathak Page 70
  71. 71. Data Structure in C (Lab. Programs) scanf("%d", &i_choice); printf("ntt---------------------------"); if((i_choice > 0) && (i_choice < 5)) return i_choice; else return 0;}int pop_node(int i_option){ if (i_option == 1) { int i_del_item; printf("ntItem to delete: "); scanf("%d", &i_del_item); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size< i_ll_size; ) { if (((*(ll_node + i_count)).i_next_idx !=0)&&(!b_flag)) { if ((*(ll_node + i_count)).i_val == i_del_item) { int i_nxt_idx = (*(ll_node +i_count)).i_next_idx; (*(ll_node + i_count)).i_prev_idx = (*(ll_node+ i_nxt_idx)).i_prev_idx; (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val; (*(ll_node + i_count)).i_next_idx = (*(ll_node+ i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } else if (b_flag) { int i_nxt_idx = (*(ll_node +i_count)).i_next_idx; (*(ll_node + i_count)).i_prev_idx =(*(ll_node + i_nxt_idx)).i_prev_idx;Saket Kr. Pathak Page 71
  72. 72. Data Structure in C (Lab. Programs) (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = (*(ll_node + i_count)).i_next_idx; i_size++; } } else break; } } else if (i_option == 2) { int i_del_idx; printf("ntIndex to delete: "); scanf("%d", &i_del_idx); int i_count = 0; int i_size = 0; bool b_flag = false; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1),i_size < i_ll_size; ) { if (((*(ll_node + i_count)).i_next_idx !=0)&&(!b_flag)) { if ((*(ll_node + i_count)).i_next_idx ==i_del_idx) { int i_nxt_idx = (*(ll_node +i_count)).i_next_idx; (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; b_flag = true; } else if (b_flag) {Saket Kr. Pathak Page 72
  73. 73. Data Structure in C (Lab. Programs) int i_nxt_idx = (*(ll_node +i_count)).i_next_idx; (*(ll_node + i_count)).i_val = (*(ll_node +i_nxt_idx)).i_val; (*(ll_node + i_count)).i_next_idx =(*(ll_node + i_nxt_idx)).i_next_idx; i_count = i_nxt_idx; i_size++; } else { i_count = (*(ll_node + i_count)).i_next_idx; i_size++; } } else break; } } return 1;}int push_node(int i_item, int i_indx){ if (i_indx < (LINKED_LIST_SIZE-1)) { if (i_ll_size == 0) { (*(ll_node + i_ll_size)).i_prev_idx = 0; (*(ll_node + i_ll_size)).i_val = i_item; (*(ll_node + i_ll_size)).i_next_idx = i_indx; i_ll_size = i_indx; } else { (*(ll_node + i_ll_size)).i_prev_idx = (*(ll_node +i_ll_size)).i_prev_idx; (*(ll_node + i_ll_size)).i_val = i_item; (*(ll_node + i_ll_size)).i_next_idx = i_indx; i_ll_size = i_indx; //For Last Node (*(ll_node + i_ll_size)).i_prev_idx = i_indx; (*(ll_node + i_ll_size)).i_val = 100001; (*(ll_node + i_ll_size)).i_next_idx = 0; //-------------Saket Kr. Pathak Page 73
  74. 74. Data Structure in C (Lab. Programs) } return 1; } else return 0;}int disp_node(void){ int i_count = 0; int i_size = 0; for (i_count = 0; i_count < (LINKED_LIST_SIZE-1), i_size <i_ll_size; ) { if (ll_node[i_count].i_next_idx != 0) { printf("ntPrev Index: %d", (*(ll_node +i_count)).i_prev_idx); printf("ntItem: %d", (*(ll_node +i_count)).i_val); printf("ntNext Index: %d", (*(ll_node +i_count)).i_next_idx); i_count = (*(ll_node + i_count)).i_next_idx; i_size++; printf("nt************************n"); } else break; } return 1;} ~~~~~~~~~~~~~~~~~~~~~************~~~~~~~~~~~~~~~~~~~~~Saket Kr. Pathak Page 74

×