gashink_t

алгоритмом удаление структуры из любого места стека

Feb 19th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct stack {      
  5.     int number,data;
  6.     struct stack *next;
  7. };
  8.  
  9. struct stack *create(struct stack *, int, int);
  10. void del(struct stack** ,int);
  11. void print(struct stack *p);
  12.  
  13. int main() {      
  14.     int i, n, nd;
  15.     struct stack *head;    
  16.     head = NULL;
  17.     scanf("%d", &n);        
  18.     for (i=0; i <= n; i++) {            
  19.         head = create(head,i,i+1);            
  20.         printf("%d)%d\n", i+1,head->data);
  21.     }      
  22.     printf("\ncacuyu ctrocu udalit?");
  23.     scanf("%d", &nd);
  24.     del(&head, nd);  
  25.     print(head);
  26.     free(head);
  27. }
  28.  
  29. struct stack *create(struct stack *head, int x, int num) {        
  30.     struct stack *element;
  31.     element = (struct stack *)malloc(sizeof(struct stack));
  32.     element->next = head;        
  33.     element->data = x;  
  34.     element->number = num;
  35.     return element;
  36. }
  37.  
  38. void print(struct stack *p){
  39.     while (p != NULL) {            
  40.         printf("%d-->", p->data);
  41.         p = p->next;
  42.     }
  43. }
  44.  
  45. void del(struct stack** p,int n){
  46.     if ((*p)->number == n) {        
  47.         struct stack* t = *p;
  48.         *p = (*p)->next;
  49.         free(t);
  50.         return;
  51.     }
  52.     else {
  53.         del(&(*p)->next,n);
  54.     }
  55. }
Add Comment
Please, Sign In to add comment