Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node{
  5.     int value;
  6.     struct node *next;
  7. }Node;
  8.  
  9. typedef struct vector{
  10.     Node *front;
  11.     Node *back;
  12. }Vector;
  13.  
  14. Node *newElement(int value){
  15.     Node *elem = (Node*)malloc(sizeof(Node));
  16.     elem->next = NULL;
  17.     elem->value = value;
  18.     return elem;
  19. }
  20.  
  21. Vector *createEmptyVector(){
  22.     Vector *vec = (Vector*)malloc(sizeof(Vector));
  23.     vec->front = vec->back = NULL;
  24.     return vec;
  25. }
  26.  
  27. void enVector(Vector *vec, int value){
  28.     Node *elem = newElement(value);
  29.     if(vec->back == NULL){
  30.         vec->back = vec->front = elem;
  31.         return;
  32.     }
  33.     vec->back->next = elem;
  34.     vec->back = elem;
  35. }
  36.  
  37. Node *deVector(Vector *vec){
  38.     if(vec->front == NULL){
  39.         return NULL;
  40.     }
  41.     Node *temporary = vec->front;
  42.     vec->front = vec->front->next;
  43.     if(vec->front == NULL){
  44.         vec->back = NULL;
  45.     }
  46.     return temporary;
  47. }
  48.  
  49.  
  50. void printVector(Vector *vec){
  51.     Node *temp = vec->front;
  52.     if(temp == NULL){
  53.         printf("\nEmpty\n");
  54.         return;
  55.     }
  56.     while(temp != NULL){
  57.         printf("%d ", temp->value);
  58.         temp = temp->next;
  59.     }
  60. }
  61.  
  62. int checkEmpty(Vector *vec){
  63.     if(vec->front == NULL){
  64.         return 1;
  65.     }else return 0;
  66. }
  67.  
  68. int getFirst(Vector *vec){
  69.     return vec->front == NULL ? -1 : vec->front->value;
  70. }
  71.  
  72. int getLast(Vector *vec){
  73.     return vec->back == NULL ? -1 : vec->back->value;
  74. }
  75.  
  76. int getVectorLength(Vector *vec){
  77.     if(vec->front == NULL){
  78.         return 0;
  79.     }
  80.     int i = 0;
  81.     Node *tmp = vec->front;
  82.     while(tmp != NULL){
  83.         i++;
  84.         tmp = tmp->next;
  85.     }
  86.     return i;
  87. }
  88.  
  89. int getElement(Vector *vec, int at){
  90.     if(vec->front == NULL){
  91.         printf("\nEmpty\n");
  92.         return -1;
  93.     }
  94.     int i = 1;
  95.     Node *temp = vec->front;
  96.     while(at != i){
  97.         temp = temp->next;
  98.         if(temp == NULL){
  99.             printf("\nNo such index in vector\n");
  100.             return -1;
  101.         }
  102.         i++;
  103.     }
  104.     return temp->value;
  105. }
  106.  
  107. void removeElementAt(Vector *vec, int at){
  108.     if(at == 1){    deVector(vec);  return;}
  109.     if(vec->front == NULL){
  110.         printf("\nEmpty\n");
  111.         return;
  112.     }
  113.     int i = 1;
  114.     Node *temp = vec->front;
  115.     while(at-1 != i){
  116.         temp = temp->next;
  117.         if(temp == NULL){
  118.             printf("\nNo such index in vector\n");
  119.             return;
  120.         }
  121.         i++;
  122.     }
  123.     if(temp->next != NULL){
  124.         temp->next = temp->next->next;
  125.     }else{
  126.         printf("\nNo such index in vector\n");
  127.     }
  128. }
  129.  
  130. void insertElementAt(Vector *vec, int value, int at){
  131.     if(at == 1){
  132.         Node *tmp = (Node*)malloc(sizeof(Node));
  133.         tmp->next = vec->front;
  134.         tmp->value = value;
  135.         vec->front = tmp;
  136.         return;
  137.     }
  138.     if(vec->front == NULL){
  139.         printf("\nEmpty\n");
  140.         return;
  141.     }
  142.     int i = 1;
  143.     Node *temp = vec->front;
  144.     while(at-1 != i){
  145.         temp = temp->next;
  146.         if(temp == NULL){
  147.             printf("\nNo such index in vector\n");
  148.             return;
  149.         }
  150.         i++;
  151.     }
  152.     Node *tnp = (Node*)malloc(sizeof(Node));
  153.     tnp->value = value;
  154.     tnp->next = temp->next;
  155.     temp->next = tnp;
  156. }
  157.  
  158. void printOptions(){
  159.     printf("\nPress 0 to exit");
  160.     printf("\nPress 1 to create an empty Vector");
  161.     printf("\nPress 2 to check if the Vector is empty");
  162.     printf("\nPress 3 to push back a new element to the Vector");
  163.     printf("\nPress 4 to print the elements");
  164.     printf("\nPress 5 to remove first element");
  165.     printf("\nPress 6 get first element value");
  166.     printf("\nPress 7 to get Vector length");
  167.     printf("\nPress 8 to remove element at chosen point");
  168.     printf("\nPress 9 to get an element value at chosen point");
  169.     printf("\nPress 10 to insert an element at chosen index");
  170.     printf("\nOption: ");
  171. }
  172.  
  173. int main()
  174. {
  175.     int action;
  176.     int brk = 0;
  177.     int value;
  178.     int otheInt;
  179.     char newElementMsg[] = "\nValue of the new element: ";
  180.     Vector *vecto;
  181.     Node *nod;
  182.     while(!brk){
  183.         printOptions();
  184.         scanf("%d", &action);
  185.         switch(action){
  186.             case 0:
  187.                 brk=1;
  188.                 break;
  189.             case 1:
  190.                 vecto = createEmptyVector();
  191.                 break;
  192.             case 2:
  193.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  194.                 printf("\t%d\n", checkEmpty(vecto));
  195.                 break;
  196.             case 3:
  197.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  198.                 printf("%s", newElementMsg);
  199.                 scanf("%d", &value);
  200.                 enVector(vecto, value);
  201.                 break;
  202.             case 4:
  203.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  204.                 printVector(vecto);
  205.                 break;
  206.             case 5:
  207.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  208.                 //if(nod = deVector(vecto) != NULL)
  209.                     printf("\nElement deVectord: %d\n", deVector(vecto)->value);
  210.                 break;
  211.             case 6:
  212.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  213.                 printf("First element: %d", getFirst(vecto));
  214.                 break;
  215.             case 7:
  216.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  217.                 printf("Vector length: %d", getVectorLength(vecto));
  218.                 break;
  219.             case 8:
  220.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  221.                 printf("%s", "\nwhich to remove\n");
  222.                 scanf("%d", &value);
  223.                 removeElementAt(vecto, value);
  224.                 break;
  225.             case 9:
  226.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  227.                 printf("%s", "\nwhich to get\n");
  228.                 scanf("%d", &value);
  229.                 getElement(vecto, value);
  230.                 break;
  231.             case 10:
  232.                 if(vecto == NULL){    printf("FIRST CREATE Vector!\n"); continue;}
  233.                 printf("%s", "\nwhere to insert\n");
  234.                 scanf("%d", &otheInt);
  235.                 printf("%s", "\nwhat value\n");
  236.                 scanf("%d", &value);
  237.                 insertElementAt(vecto, value, otheInt);
  238.                 break;
  239.             default:
  240.                 break;
  241.             }
  242.     }
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement