Advertisement
Jorge_Lugo97

Listas Ligadas

Nov 7th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.95 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct Node {
  4. int data;
  5. struct Node* next;
  6. };
  7.  
  8. int main()
  9. {
  10.  
  11. struct Node* A1 = NULL;
  12. struct Node* A2 = NULL;
  13. struct Node* A3 = NULL;
  14. struct Node* A4 = NULL;
  15. struct Node* p = NULL;
  16.  
  17. A1 = (struct Node*)malloc(sizeof(struct Node));
  18. A2 = (struct Node*)malloc(sizeof(struct Node));
  19. A3 = (struct Node*)malloc(sizeof(struct Node));
  20. A4 = (struct Node*)malloc(sizeof(struct Node));
  21.  
  22. A1->data=1;
  23. A1->next=A2;
  24. A2->data=2;
  25. A2->next=A3;
  26. A3->data=3;
  27. A3->next=NULL;
  28.  
  29.     p=A1;
  30.     while (p!=NULL){
  31.         printf("%d ", p->data);
  32.         p=p->next;
  33. }
  34. // Inserta 4
  35. A4->data=4;
  36. A4->next=A1->next;
  37. A1->next=A4;
  38.  
  39.     p=A1;
  40.         printf("\n");
  41.     while (p!=NULL){
  42.         printf("%d ", p->data);
  43.         p=p->next;
  44.     }
  45.    
  46. // ELimia A2
  47.  
  48. A4->next=A4->next->next;
  49. free(A2);
  50.  
  51.     p=A1;
  52.         printf("\n");
  53.     while (p!=NULL){
  54.         printf("%d ", p->data);
  55.         p=p->next;
  56.     }
  57. }
  58. **********************************************************************************************************************************
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <assert.h>
  62. #define STACK_EMPTY (0)
  63.  
  64. struct elt {
  65. struct elt *next;
  66. int value;
  67. };
  68.  
  69. typedef struct elt *Stack;
  70.  
  71. void stackPush(Stack *s, char value)
  72. {
  73. struct elt *e;
  74. e = malloc(sizeof(struct elt));
  75. assert(e);
  76. e->value = value;
  77. e->next = *s;
  78. *s = e;
  79. }
  80.  
  81. int stackEmpty(const Stack *s)
  82. {
  83. return (*s == 0);
  84. }
  85.  
  86. int stackPop(Stack *s)
  87. {
  88. char ret [20];
  89. struct elt *e;
  90. assert(!stackEmpty(s));
  91. ret = (*s)->value;
  92. /* patch out first element */
  93. e = *s;
  94. *s = e->next;
  95. free(e);
  96. return ret;
  97. }
  98.  
  99. void stackPrint(const Stack *s)
  100. {
  101. struct elt *e;
  102. for(e = *s; e != 0; e = e->next)
  103. {
  104. printf("%d ", e->value);
  105. }
  106.  
  107. putchar('\n');
  108. }
  109.  
  110. int main(int argc, char **argv)
  111. {
  112. int i;
  113. Stack s;
  114. s = STACK_EMPTY;
  115. for(i = 0; i < 5; i++)
  116. {
  117. printf("push %d\n", i);
  118. stackPush(&s, i);
  119. stackPrint(&s);
  120. }
  121.  
  122. while(!stackEmpty(&s)) {
  123. printf("pop gets %d\n", stackPop(&s));
  124. stackPrint(&s);
  125. }
  126.  
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement