gashink_t

саод (задние 2,3,4 лаба 4)

Mar 4th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node *next;
  8.   };
  9.  
  10. typedef struct Node *Node;
  11.  
  12. Node Create();
  13. void List(Node *A);
  14. void Append(Node *A, int d);
  15. void Remove(Node *A);
  16. int GetHead(Node A);
  17. int GetTail(Node A);
  18. int isEmpty(Node A);
  19. int Poisk(Node A,int c);
  20. Node Del(Node head);
  21. int Palindrom(Node *head,int c);
  22.  
  23. void main() {
  24.     int i, n, c;
  25.     Node *head = NULL;
  26.     printf("How many elements to enter?: ");
  27.     scanf("%d", &n);
  28.     c=n;
  29.     printf("Enter the value: ");
  30.     for (i = 0; i < c; i++) {
  31.         if (i==0)
  32.             head = Create();
  33.         else {
  34.             scanf("%d", &n);
  35.             Append(&head, n);
  36.         }
  37.     }
  38.     List(head);
  39.     printf("\nAdd in begining: ");
  40.     scanf("%d", &n);
  41.     Append(&head, n);
  42.     List(head);
  43.     printf("\nDelete(Remove): ");
  44.     Remove(&head);
  45.     List(head);
  46.     n=GetHead(head);
  47.     printf("\nGetHead: %d\n",n);
  48.     n=GetTail(head);
  49.     printf("\nGetTail: %d\n",n);
  50.     if (isEmpty(head)==0)
  51.         printf("\nList empty\n");
  52.     else
  53.         printf("\nList not empty\n");
  54.     printf("\nSearch, Enter element): ");
  55.     scanf("%d", &n);
  56.     n = Poisk(head,n);
  57.     printf("Element: %d\n",n);
  58.     if(Palindrom(&head,c)==0)
  59.         printf("\nNot palindrom\n");
  60.     else
  61.         printf("\nPalindrom\n");
  62.     printf("\nDel: ");
  63.     head = Del(head);
  64.     List(head);
  65.     free(head);
  66. }
  67.  
  68. Node Create() {
  69.     Node newNode = (Node * ) malloc(sizeof(Node));
  70.     scanf("%d",&newNode->data);
  71.     newNode->next = NULL;
  72.     return (newNode);
  73. }
  74.  
  75. void List(Node *A) {
  76.   Node tmp;
  77.   tmp = A;
  78.   do {
  79.     printf("%d ", tmp->data);
  80.     tmp = tmp->next;
  81.   } while (tmp != NULL);
  82.   printf("\n");
  83. }
  84.  
  85. void Append(Node *A, int d) {
  86.   Node q = malloc(sizeof *q);
  87.   assert(q != NULL);
  88.   q->data = d;
  89.   q->next = *A;
  90.   *A = q;
  91. }
  92.  
  93. void Remove(Node *A) {
  94.   assert(*A != NULL);
  95.   Node a = (*A)->next;
  96.   free(*A);
  97.   *A = a;
  98. }
  99.  
  100. int GetHead(Node A) {
  101.   assert(A != NULL);
  102.   return A->data;
  103. }
  104.  
  105. int GetTail(Node A) {
  106.   assert(A!=NULL);
  107.   while(A->next!=NULL) {
  108.     A=A->next;
  109.     if(A->next==NULL) {
  110.      return A->data;
  111.     }
  112.   }
  113. }
  114.  
  115. int isEmpty(Node A){
  116.     if(A==NULL) return 0;
  117.     else return 1;
  118. }
  119.  
  120. int Poisk(Node A,int c){
  121.     int i = 0;
  122.     while (i < c && A->next) {
  123.         A = A->next;
  124.         i++;
  125.     }
  126.     return A->data;
  127. }
  128.  
  129. Node Del(Node head) {
  130.     if (head == NULL) return;
  131.     Node t = head;
  132.     while (t != NULL) {
  133.         Node p = t;
  134.         while (p->next != NULL) {
  135.             if (p->next->data == t->data) {
  136.                 p->next = p->next->next;
  137.             } else{
  138.               p = p->next;
  139.             }
  140.         }
  141.         t = t->next;
  142.     }
  143.     return head;
  144. }
  145.  
  146. int Palindrom(Node *head,int c) {
  147.     if(c%2==0)  {
  148.         Node a = *head;
  149.         const int z=c/2;
  150.         int p=0;
  151.         int m[z];
  152.         for(int i=0;i<c/2;i++) {
  153.             m[p]=a->data;
  154.             a=a->next;
  155.             p++;
  156.         }
  157.         int t=0;
  158.         for(p=p-1;p>=0;p--) {
  159.             if(a->data==m[p]) t++;
  160.             else return 0;
  161.             a=a->next;
  162.         }
  163.         if (t==c/2) return 1;
  164.         else return 0;
  165.     }
  166.     else {
  167.         Node a = *head;
  168.         const int z=c/2;
  169.         int p=0;
  170.         int m[z];
  171.         for(int i=0;i<c/2;i++) {
  172.             m[p]=a->data;
  173.             a=a->next;
  174.             p++;
  175.             if(i==c/2-1)a=a->next;
  176.         }
  177.         int t=0;
  178.         for(p=p-1;p>=0;p--) {
  179.             if(a->data==m[p]) t++;
  180.             else return 0;
  181.             a=a->next;
  182.        
  183.         }
  184.         if (t==c/2) return 1;
  185.         else return 0;
  186.     }
  187. }
Add Comment
Please, Sign In to add comment