gashink_t

cсаод (1задание 4 лабы)

Mar 4th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 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.  
  20. void main() {
  21.   int i, n, c;
  22.   Node *head = NULL;
  23.   printf("How many elements to enter?: ");
  24.   scanf("%d", &n);
  25.   c=n;
  26.   printf("Enter the value: ");
  27.   for (i = 0; i < c; i++) {
  28.       if (i==0)
  29.         head = Create();
  30.       else
  31.       {
  32.           scanf("%d", &n);
  33.           Append(&head, n);
  34.       }
  35.   }
  36.   List(head);
  37.   printf("\nAdd in begining: ");
  38.   scanf("%d", &n);
  39.   Append(&head, n);
  40.   List(head);
  41.   printf("\nDelete: ");
  42.   Remove(&head);
  43.   List(head);
  44.   n=GetHead(head);
  45.   printf("\nGetHead: %d\n",n);
  46.   n=GetTail(head);
  47.   printf("\nGetTail: %d\n",n);
  48.   if (isEmpty(head)==0)
  49.     printf("\nList empty\n");
  50.   else
  51.     printf("\nList not empty\n");
  52.   free(head);
  53. }
  54.  
  55. Node Create() {
  56.     Node newNode = (Node * ) malloc(sizeof(Node));
  57.     scanf("%d",&newNode->data);
  58.     newNode->next = NULL;
  59.     return (newNode);
  60. }
  61.  
  62. void List(Node *A) {
  63.   Node tmp;
  64.   tmp = A;
  65.   do {
  66.     printf("%d ", tmp->data);
  67.     tmp = tmp->next;
  68.   } while (tmp != NULL);
  69.   printf("\n");
  70. }
  71.  
  72. void Append(Node *A, int d) {
  73.   Node q = malloc(sizeof *q);
  74.   assert(q != NULL);
  75.   q->data = d;
  76.   q->next = *A;
  77.   *A = q;
  78. }
  79.  
  80. void Remove(Node *A) {
  81.   assert(*A != NULL);
  82.   Node a = (*A)->next;
  83.   free(*A);
  84.   *A = a;
  85. }
  86.  
  87. int GetHead(Node A) {
  88.   assert(A != NULL);
  89.   return A->data;
  90. }
  91.  
  92. int GetTail(Node A) {
  93.   assert(A!=NULL);
  94.   while(A->next!=NULL)
  95.   {
  96.     A=A->next;
  97.     if(A->next==NULL)
  98.     {
  99.      return A->data;
  100.     }
  101.   }
  102. }
  103.  
  104. int isEmpty(Node A){
  105.     if(A==NULL) return 0;
  106.     else return 1;
  107. }
Add Comment
Please, Sign In to add comment