cba_guru

Data Structures

Jun 1st, 2018
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.18 KB | None | 0 0
  1. //WAP a Program to create an array.
  2.  
  3. #include<stdio.h>
  4. #include<conio.h>
  5.  
  6. main()
  7. {
  8.     int a[5];
  9.     int i;
  10.    
  11.     printf("Enter the elements of array:");
  12.     for(i=0; i<5; i++)
  13.     scanf("\n%d", &a[i]);
  14.    
  15.     //Display Array list
  16.     printf("The inserted array elemets are:");
  17.     for(i=0; i<5; i++)
  18.     printf("\n%d", a[i]);
  19.     getch();
  20. }
  21. ---------------------------------------
  22. //Insertion in array
  23.  
  24. #include<stdio.h>
  25. #include<conio.h>
  26.  
  27. void main()
  28. {
  29.     int a[100], i, n, v;
  30.     char ch;
  31.    
  32.     printf("Enter the size of array: ");
  33.     scanf("\n%d", &n);
  34.    
  35.     printf("\nEnter the Elements of Array: \n");
  36.     for(i=0; i<n; i++)
  37.     scanf("\n%d", &a[i]);
  38.    
  39.     printf("\nThe Elements of Array are:");
  40.     for(i=0; i<n; i++)
  41.     printf("\n%d", a[i]);
  42.    
  43.     back:
  44.         printf("\nDo you want to add more? If Yes type y or Y and Press Enter.");
  45.         fflush(stdin);
  46.         scanf("%c", &ch);
  47.         if (ch=='y' || ch=='Y')
  48.         {
  49.             printf("Enter Data: ");
  50.             scanf("%d", &v);
  51.             n++;
  52.             a[n-1]=v;
  53.             printf("\nThe Elements of Array are:");
  54.             for(i=0; i<n; i++)
  55.             {
  56.                 printf("\n%d", a[i]);
  57.             }
  58.             goto back;
  59.         }
  60.     getch();
  61. }
  62. ---------------------------------------
  63. //Insertion at nth position in array
  64.  
  65. #include<stdio.h>
  66. #include<conio.h>
  67.  
  68. void main()
  69. {
  70.     int a[100], i, n, v, p;
  71.     char ch;
  72.    
  73.     printf("Enter the size of array: ");
  74.     scanf("\n%d", &n);
  75.    
  76.     printf("\nEnter the Elements of Array: \n");
  77.     for(i=0; i<n; i++)
  78.     scanf("\n%d", &a[i]);
  79.    
  80.     printf("\nThe Elements of Array are:");
  81.     for(i=0; i<n; i++)
  82.     printf("\n%d", a[i]);
  83.    
  84.    
  85.     printf("\nDo you want to add more? If Yes type y or Y and Press Enter.");
  86.     fflush(stdin);
  87.     scanf("%c", &ch);
  88.     if (ch=='y' || ch=='Y')
  89.     {
  90.        printf("Enter the location where you wish to insert an element\n");
  91.        scanf("%d", &p);
  92.      
  93.        printf("Enter the value to insert\n");
  94.        scanf("%d", &v);
  95.      
  96.        for (i=n-1; i>= p-1; i--)
  97.        a[i+1] = a[i];
  98.        a[p-1]=v;
  99.      
  100.        printf("Resultant array is\n");
  101.        for (i= 0; i<=n; i++)
  102.        printf("%d\n", a[i]);
  103.     }
  104.     getch();
  105. }
  106. ---------------------------------------
  107. //Deletion in array at nth position
  108.  
  109. #include<stdio.h>
  110. #include<conio.h>
  111.  
  112. void main()
  113. {
  114.     int a[100], i, n, v, p;
  115.    
  116.     printf("Enter the size of array: ");
  117.     scanf("\n%d", &n);
  118.    
  119.     printf("\nEnter the Elements of Array: \n");
  120.     for(i=0; i<n; i++)
  121.     scanf("\n%d", &a[i]);
  122.    
  123.     printf("\nThe Elements of Array are:");
  124.     for(i=0; i<n; i++)
  125.     printf("\n%d", a[i]);
  126.    
  127.     printf("\nEnter the location where you wish to delete element");
  128.     scanf("%d", &p);
  129.    
  130.     if (p>=n+1)
  131.     printf("Deletion not possible.\n");
  132.    
  133.     else
  134.     {
  135.         for(i=p-1; i<n-1; i++)
  136.         a[i] = a[i+1];
  137.        
  138.         for(i=0; i<n-1; i++)
  139.         printf("%d\n", a[i]);
  140.     }
  141.     getch();
  142. }
  143. ---------------------------------------
  144. //Reverse of an array.
  145.  
  146. #include<stdio.h>
  147. #include<conio.h>
  148.  
  149. void main()
  150. {
  151.     int a[100], i, n;
  152.    
  153.     printf("Enter the size of array: ");
  154.     scanf("\n%d", &n);
  155.    
  156.     printf("\nEnter the Elements of Array: \n");
  157.     for(i=0; i<n; i++)
  158.     scanf("\n%d", &a[i]);
  159.    
  160.     //Display Reverse Array list
  161.     printf("The Reverse array elemets are:");
  162.     for(i=n-1; i>=0; i--)
  163.     {
  164.         printf("\n%d", a[i]);
  165.     }
  166.     getch();
  167. }
  168. ---------------------------------------
  169. //Merging of two arrays
  170.  
  171. #include<stdio.h>
  172. #include<conio.h>
  173.  
  174. int main()
  175. {
  176.     int a1[25], a2[25], a3[50], i, m, n, temp;
  177.    
  178.     printf("Enter the size of Array1: ");
  179.     scanf("\n%d", &m);
  180.     printf("\nEnter the Array1 Elements: \n");
  181.     for(i=0; i<m; i++)
  182.     {
  183.         scanf("%d", &a1[i]);
  184.     }
  185.    
  186.     printf("\nEnter the size of Array2: ");
  187.     scanf("\n%d", &n);
  188.     printf("\nEnter the Array2 Elements: \n");
  189.     for(i=0; i<n; i++)
  190.     {
  191.         scanf("%d", &a2[i]);
  192.     }
  193.    
  194.     printf("\nThe Elements of Array1:");
  195.     for(i=0; i<m; i++)
  196.     {
  197.         printf("\n%d", a1[i]);
  198.     }
  199.    
  200.     printf("\nThe Elements of Array2:");
  201.     for(i=0; i<n; i++)
  202.     {
  203.         printf("\n%d", a2[i]);
  204.     }
  205.    
  206.     int size = m+n;
  207.     a3[size];
  208.    
  209.     back:
  210.         printf("\nPress 1 if you want to merge Array 1 with Array 2\n");
  211.         printf("Press 2 if you want to merge Array 2 with Array 1\n");
  212.         fflush(stdin);
  213.         scanf("\n%d", &temp);
  214.        
  215.         if(temp==1)
  216.         {
  217.             for(i=0; i<m; i++)
  218.             a3[i]=a1[i];
  219.            
  220.             for(i=m; i<size; i++)
  221.             a3[i]=a2[i-m];
  222.            
  223.             printf("\nArray1 + Array2 is: \n");
  224.             for(i=0; i<size; i++)
  225.             printf("\n%d", a3[i]);
  226.            
  227.             return 0;
  228.         }
  229.        
  230.         else if(temp==2)
  231.         {
  232.             for(i=0; i<n; i++)
  233.             a3[i]=a2[i];
  234.            
  235.             for(i=n; i<size; i++)
  236.             a3[i]=a1[i-n];
  237.            
  238.             printf("\nArray2 + Array1 is: \n");
  239.             for(i=0; i<size; i++)
  240.             printf("\n%d", a3[i]);
  241.            
  242.             return 0;
  243.         }
  244.         else
  245.         {
  246.             printf("Entered wrong choice\n");
  247.             goto back;
  248.         }
  249. }
  250. ---------------------------------------
  251. #include <stdio.h>
  252. #include <malloc.h>
  253. #include <stdlib.h>
  254.  
  255. struct node {
  256.   int value;
  257.   struct node *next;
  258. };
  259.  
  260. void insert();
  261. void display();
  262.  
  263. typedef struct node DATA_NODE;
  264.  
  265. DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
  266. int data;
  267.  
  268. int main()
  269. {
  270.   int option = 0;
  271.   printf("Singly Linked List Example - All Operations\n");
  272.  
  273.   while (option < 3)
  274.   {
  275.     printf("\nOptions\n");
  276.     printf("1 : Insert into Linked List \n");
  277.     printf("2 : Display from Linked List \n");
  278.     printf("Others : Exit()\n");
  279.     printf("Enter your option:");
  280.     scanf("%d", &option);
  281.    
  282. switch (option)
  283. {
  284.       case 1:
  285.         insert();
  286.         break;
  287.       case 2:
  288.         display();
  289.         break;
  290.       default:
  291.         break;
  292.     }
  293.   }
  294.  
  295.   return 0;
  296. }
  297.  
  298. void insert()
  299. {
  300.   printf("\nEnter Element for Insert Linked List : \n");
  301.   scanf("%d", &data);
  302.  
  303.   temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
  304.  
  305.   temp_node->value = data;
  306.  
  307.   if (first_node == 0)
  308. {
  309.     first_node = temp_node;
  310.   } else {
  311.     head_node->next = temp_node;
  312.   }
  313.   temp_node->next = 0;
  314.   head_node = temp_node;
  315.  
  316. }
  317.  
  318. void display() {
  319.   int count = 0;
  320.   temp_node = first_node;
  321.   printf("\nDisplay Linked List : \n");
  322.   while (temp_node != 0) {
  323.     printf("# %d # ", temp_node->value);
  324.     count++;
  325.     temp_node = temp_node -> next;
  326.   }
  327.   printf("\nNo Of Items In Linked List : %d\n", count);
  328. }
  329.  
  330.  
  331. ---------------------------------------
  332. #include<stdio.h>
  333. #include<conio.h>
  334.  
  335. void main()
  336. {
  337.     int a[50], top, pos, i, n, x;
  338.     char ch;
  339.    
  340.     printf("Enter the size: ");
  341.     scanf("%d", &n);
  342.     printf("Enter the Elements: \n");
  343.     top=n;
  344.     for(i=0; i<top; i++)
  345.     {
  346.         scanf("%d", &a[i]);
  347.     }
  348.    
  349.     //The Elements in the Stack
  350.     printf("Elements in the Stack: \n");
  351.     for(i=top-1; i>=0; i--)
  352.     {
  353.         printf("%d \n", a[i]);
  354.     }
  355.    
  356.     back:
  357.         fflush(stdin);
  358.         printf("Press d to delete an Element or i to insert an Element.");
  359.         scanf("%c", &ch);
  360.         if(ch=='d' || ch=='D')
  361.         {
  362.             printf("Enter the position you want to delete an element:\n");
  363.             scanf("%d", &pos);
  364.             if (pos-1==top-1);
  365.             top--;
  366.             printf("The updated elements in stack are:\n");
  367.             for(i=top-1; i>=0; i--);
  368.         }
  369.        
  370.        
  371.    
  372. }
  373. ---------------------------------------
  374. #include<stdio.h>
  375.  
  376. main(){
  377. int top,n,pos,i,x;
  378. int a[100];
  379. char ch;
  380. printf("Enter the size:\n");
  381. scanf("%d",&n);
  382. printf("Enter %d elements:\n",n);
  383. top=n;
  384. for(i=0;i<top;i++){
  385.     scanf("%d",&a[i]);
  386. }
  387.  
  388. printf("The elements in stack are:\n");
  389. for(i=top-1;i>=0;i--){
  390.     printf("%d\n",a[i]);
  391. }
  392.  
  393. back:
  394. fflush(stdin);
  395. printf("Press d if you want to delete an element or i to insert an element:\n");
  396. scanf("%c",&ch);
  397. if(ch=='d' || ch=='D'){
  398.     printf("Enter the position you want to delete an element:\n");
  399.     scanf("%d",&pos);
  400.     if(pos-1==top-1){
  401.         top--;
  402.         printf("The updated elements in stack are:\n");
  403.         for(i=top-1;i>=0;i--){
  404.             printf("%d\n",a[i]);
  405.         }
  406.         goto back;
  407.     }
  408.     else{
  409.         printf("Cann't remove the element at this position.\n");
  410.         goto back;
  411.     }
  412. }
  413. else if(ch=='i' || ch=='I'){
  414.         printf("Enter the element you want to add:\n");
  415.         scanf("%d",&x);
  416.         if(top>=n){
  417.             printf("Overflow\n");
  418.             goto back;
  419.     }
  420.         else{
  421.             top++;
  422.             a[top-1]=x;
  423.             printf("The updated elements in stack are:\n");
  424.             for(i=top-1;i>=0;i--){
  425.                 printf("%d\n",a[i]);
  426.             }
  427.             goto back;
  428.         }
  429. }
  430. else{
  431.     return 0;
  432. }
  433. }
  434. ---------------------------------------
  435.  
  436. ---------------------------------------
Add Comment
Please, Sign In to add comment