Guest User

Untitled

a guest
Nov 29th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.97 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. typedef struct Node{
  7.     int number;
  8.     int summa;
  9.     char data[11];
  10.     char fio[21];
  11.     struct Node * prev;
  12.     struct Node * next;
  13. } NODE;
  14.  
  15. typedef NODE * PNODE;
  16.  
  17.  
  18. PNODE Add_list(PNODE);
  19. void menu(PNODE);
  20. void Print(PNODE);
  21. void sort(PNODE);
  22. void Add_nachalo(void);
  23. void Delete(void);
  24.  
  25. PNODE head = NULL;
  26. PNODE lost = NULL;
  27.  
  28. int main(int argc, char *argv[]) {
  29.     menu(head);
  30.     return 0;
  31.  
  32. }
  33.  
  34.  
  35. void menu(PNODE head) {
  36.     int k;
  37.     printf("\n1. Add konez in list.");
  38.     printf("\n2. Add nachalo in list.");
  39.     printf("\n3. Print a list.");
  40.     printf("\n4. Sort a list.");
  41.     printf("\n5. Delete data.");
  42.     printf("\n6. Exit.\n\t");
  43.     do {
  44.         scanf("%i", &k);
  45.         switch (k) {
  46.             case 1 :
  47.                 head = Add_list(head);
  48.                 menu(head);
  49.             case 2 :
  50.                 Add_nachalo();
  51.                 menu(head);
  52.             case 3 :
  53.                 Print(head);
  54.                 menu(head);
  55.             case 4 :
  56.                 sort(head);
  57.                 Print(head);
  58.                 menu(head);
  59.             case 5 :
  60.                 Delete();
  61.                 Print(head);
  62.                 menu(head);
  63.             case 6 :
  64.                 exit(0);
  65.         }
  66.     } while (k);
  67. }
  68.  
  69.  
  70. PNODE Add_list(PNODE h){
  71.     int i=1;
  72.     do{
  73.     PNODE p = (PNODE)malloc(sizeof(struct Node));
  74.     PNODE r;
  75.     if (h == NULL) {
  76.         printf ("                                  New record\n\n");
  77.         printf("\nNew FIO: ");
  78.         fflush(stdin);
  79.         gets(p->fio, 21);
  80.         if (p->fio[0] == '\0') return h;
  81.         printf("Waybill number: ");
  82.         scanf("%d", &p->number);
  83.         printf("New date: ");
  84.         fflush(stdin);
  85.         gets(p->data, 11);
  86.         printf("New summa: ");
  87.         scanf("%i", &p->summa);
  88.         p->next = NULL;
  89.         p->prev = NULL;
  90.         h = p;
  91.     } else {
  92.         r = (PNODE)malloc(sizeof(struct Node));
  93.         r = h;
  94.         while (r->next != NULL) r = r->next;
  95.             printf ("                                   New record\n\n");
  96.             printf("\nNew FIO: ");
  97.             fflush(stdin);
  98.             gets(p->fio, 21);
  99.             if (p->fio[0] == '\0') return h;
  100.             printf("New number: ");
  101.             scanf("%d", &p->number);
  102.             printf("New date: ");
  103.             fflush(stdin);
  104.             gets(p->data, 11);
  105.             printf("New summa: ");
  106.             scanf("%i", &p->summa);
  107.             r->next = p;
  108.             p->prev = r;
  109.             p->next = NULL;
  110.     }
  111.     }while(i==1);
  112.     return h;
  113. }
  114.  
  115.  
  116. void Add_nachalo(void){
  117.     PNODE g;
  118.     g = NULL;
  119.  
  120.     if(!lost) head = Add_list(head);
  121.     g = (PNODE)malloc(sizeof(struct Node));
  122.     if(!g) return ;
  123.     g->next = lost;
  124.     g->prev = lost->prev;
  125.     lost->prev = g;
  126.     if(g->prev){
  127.         g->prev->next =g;
  128.     }else{
  129.         head = g;
  130.             printf ("                                   New record\n\n");
  131.             printf("\nNew FIO: ");
  132.             fflush(stdin);
  133.             gets(g->fio, 21);
  134.             printf("New number: ");
  135.             scanf("%d", &g->number);
  136.             printf("New date: ");
  137.             fflush(stdin);
  138.             gets(g->data, 11);
  139.             printf("New summa: ");
  140.             scanf("%i", &g->summa);
  141.     }
  142.     return;
  143. }
  144.  
  145.  
  146. void Print(PNODE h) {    
  147.     PNODE CurNode = h;
  148.     int k = 0;
  149.     if (CurNode == NULL) return;
  150.     printf ("                                  Print a list\n");
  151.     printf ("----------------------------------------------------------------------------\n");
  152.     printf ("| Number |    Data    |    Summa    |               FIO                    |\n");
  153.     printf ("----------------------------------------------------------------------------\n");
  154.     while (CurNode != NULL) {
  155.         if (k++) printf("\n");
  156.         printf("|%8i", CurNode->number);
  157.         printf("|%12s|", CurNode->data);
  158.         printf("%13i|", CurNode->summa);
  159.         printf("%38s|\n", CurNode->fio);
  160.         CurNode = CurNode->next;
  161.     printf("----------------------------------------------------------------------------");
  162.     }
  163.     return;
  164. }
  165.  
  166.  
  167. void sort(PNODE start) {
  168.     PNODE tmp;
  169.     PNODE a;
  170.     int _number;
  171.     int _summa;
  172.     char _data[11];
  173.     char _fio[21];
  174.     int flag = 1;
  175.     if (start == NULL) return;
  176.     while (flag == 1) {
  177.         tmp = start;
  178.         a = tmp->next;
  179.         flag = 0;
  180.         while (a) {
  181.             if ((tmp->number) > (a->number)) {
  182.                 _number = tmp->number;
  183.                 tmp->number = a->number;
  184.                 a->number = _number;
  185.                 _summa = tmp->summa;
  186.                 tmp->summa = a->summa;
  187.                 a->summa = _summa;
  188.                 strcpy_s(_data, 11, tmp->data);
  189.                 strcpy_s(tmp->data, 11, a->data);
  190.                 strcpy_s(a->data, 11, _data);
  191.                 strcpy_s(_fio, 21, tmp->fio);
  192.                 strcpy_s(tmp->fio, 21, a->fio);
  193.                 strcpy_s(a->fio, 21, _fio);
  194.                 flag = 1;
  195.             }
  196.             tmp = tmp->next;
  197.             a = a->next;
  198.         }
  199.     }
  200. }
  201.  
  202. void Delete(void)
  203. {
  204.     PNODE g, l, j;
  205.     g = NULL;
  206.     g = head;
  207.     j = (PNODE)malloc(sizeof(struct Node));
  208.     _clrscr();
  209.     getchar();
  210.     printf ("Enter date: \n");
  211.     gets(j->data);
  212.     if(g == NULL){
  213.         printf("Spisok pust.\n"); return;
  214.     }
  215.         if(j->data == g->data){
  216.                     if(g == head){
  217.                         if (head->next == NULL){
  218.                             head = NULL;
  219.                             lost = NULL;
  220.                         }else{
  221.                             head = g->next;
  222.                             head->prev = NULL;
  223.                         }
  224.                     }else{
  225.                         l = g->next;
  226.                         g = g->prev;
  227.                         g->next = l;
  228.                         if(l != NULL)
  229.                     l->prev = g;
  230.                         if(l == NULL)
  231.                     lost = g ;
  232.                     }
  233.                 }
  234.           g = g->next;
  235.     free(j);
  236.     Print(head);
  237. }
Advertisement
Add Comment
Please, Sign In to add comment