forked from zenathark/MIT4011
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tercerparcial.c
78 lines (73 loc) · 1.6 KB
/
tercerparcial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <iso646.h>
#include <stdbool.h>
#include <math.h>
#include <memory.h>
#include <stdlib.h>
typedef struct tCell {
int payload;
void* next;
}Cell;
Cell* agregar (Cell *root, int numero){
Cell *nuevo_nodo;
nuevo_nodo = (Cell*)malloc(sizeof(Cell));
nuevo_nodo->payload= numero;
nuevo_nodo->next= NULL;
Cell *cursor = root;
while (cursor->next != NULL){
cursor = (Cell*)cursor->next;
}
cursor->next = (void*)nuevo_nodo;
return root;
}
void imprimeLista (Cell *root){
printf ("x");
while ((root = ((Cell*)root->next )) !=NULL){
printf ("-->%d ", root->payload);
printf("\n");
}
}
Cell *pop(Cell *root){
Cell *cursor = root;
if(root->next == NULL) return root;
while(((Cell*)cursor->next)->next != NULL){
cursor = (Cell*)cursor->next;
}
free(cursor->next);
cursor->next = NULL;
return root;
}
Cell *erase( Cell *root){
while(root->next != NULL)
pop(root);
return root;
}
int menu(){
int opcion;
printf ("Menu: \n 0: Agrega nodo \n 1: Borrar ultimo numero \n 2: Imprime lista \n 3: Salir \n Selecciona opción:");
scanf ("%d", &opcion);
return opcion;
}
int main(int argc, char* argv[]){
Cell root;
int nuevo_numero;
root.payload = -1;
root.next= NULL;
int opcion = menu();
while( opcion != 3){
if(opcion ==0){
printf ("Dame un numero:");
scanf ("%d", &nuevo_numero);
agregar(&root, nuevo_numero);
printf("\n");
}else if (opcion ==1){
pop (&root);
}else if (opcion ==2){
imprimeLista(&root);
}
printf("\n");
opcion= menu();
}
erase(&root);
return 0;
}