Implementación de Estructuras de Datos en C
Implementación de Estructuras de Datos en C
cola_array.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX 4
5
6 int cola[MAX];
7 int FRONT = -1;
8 int REAR =-1;
9
10 void insertar(){
11 int elemento;
12 if(REAR == MAX-1){
13 printf("\nCola Overflow\n");
14 }
15 else{
16 if (FRONT == -1)
17 FRONT =0;
18 printf("Ingrese el elemento a insertar a la cola: \n");
19 scanf("%d", &elemento);
20 REAR++;
21 cola[REAR] = elemento;
22 }
23 }
24
25 void borrar(){
26 if(FRONT == -1 || FRONT > REAR){
27 printf("\nCola Underflow\n");
28 return;
29 }
30 printf("\nEl elemento borrado en la cola es %d \n", cola[FRONT]);
31 FRONT++;
32 }
33
34 void mostrarCola(){
35 if(FRONT == -1 || FRONT > REAR){
36 printf("\nCola Undeflow\n");
37 return;
38 }
39 printf("\nLos elementos en la cola son: \n\n");
40 for(int i= FRONT; i<= REAR; i++){
41 printf(" %d <-", cola[i]);
42 if(i == REAR){
43 printf(" Termina la cola\n");
44 }
45 }
46 }
47
48 int main() {
localhost:58625/92d5c1c8-7892-47a6-a480-11d5c02b29c3/ 1/2
13/3/25, 8:11 p.m. cola_array.c
49 int choice;
50
51 while (1) {
52 printf("\nMenu de opciones:\n");
53 printf("1. Agregar elemento\n");
54 printf("2. Eliminar elemento\n");
55 printf("3. Mostrar elementos de la pila\n");
56 printf("4. Salir\n");
57 printf("Ingrese su opcion: ");
58 scanf("%d", &choice);
59
60 switch (choice) {
61 case 1:
62 insertar();
63 break;
64 case 2:
65 borrar();
66 break;
67 case 3:
68 mostrarCola();
69 break;
70 case 4:
71 exit(0);
72 default:
73 printf("Opción no válida. Intente de nuevo.\n");
74 }
75 }
76
77 return 0;
78 }
79
80
localhost:58625/92d5c1c8-7892-47a6-a480-11d5c02b29c3/ 2/2
13/3/25, 8:10 p.m. Cola_enlazada.c
Cola_enlazada.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 //Se define la estructura del nodo
4 struct Node
5 {
6 int valor;
7 struct Node* next;
8 };
9
10 //Funcion que nos permita crear nodos
11 struct Node* createNode(const int Valor){
12 struct Node*newNode= (struct Node*)malloc(sizeof(struct Node));//aqui se pide la memoria
a la computadora
13
14 if(newNode == NULL){ //Si no es posible crear el nodo
15 printf("Fallo alojamiento de memoria");
16 exit(1); //terminar el programa
17 }
18 newNode->valor = Valor;
19 newNode->next = NULL;
20 return newNode;
21
22 }
23
24 void insertEnd(struct Node**head,int Valor){
25 struct Node*newNode= createNode(Valor);
26 if(*head == NULL){ //si no hay elementos en la cola
27 *head=newNode;//agrega el elemento a la cola
28 return;
29 }
30 //Si ya hay almenos 1 elemento en la cola
31 struct Node *temp= *head; //Variable temporal
32 while (temp->next != NULL) //Se recorre la lista hasta llegar al ultimo elemento
33 {
34 temp = temp->next;
35 }
36 temp->next = newNode; //agruega el elemento al final de la cola
37
38 }
39
40
41 void deleteBegin(struct Node** head) {
42 if (*head == NULL) { //Si ya no hay elementos en la lista
43 printf("Cola Underflow");
44 return; //terminar
45 }
46 struct Node* temp = *head;
localhost:58625/9a647eea-778d-4b13-99af-f2dcbb1f922b/ 1/3
13/3/25, 8:10 p.m. Cola_enlazada.c
47 *head = (*head)->next; //el elemento siguiente pasa a tomar la posisision anterior a este
elemento
48 free(temp); //borra de la momoria el elemento actual
49 }
50
51
52 void displayLiSt(struct Node*head){
53 struct Node* temp= head;
54 while (temp != NULL)
55 {
56 printf("%d <- ", temp->valor);
57 temp = temp->next;
58 }
59 if (temp == NULL)
60 {
61 printf("Termina la cola");
62 }
63
64
65 }
66
67 void freeList(struct Node*head){
68 struct Node* temp;
69 while (head != NULL) //comenzamos a recorrer la cola hasta el final
70 {
71 temp = head;
72 head = head->next;
73 free(temp); //borra de la memoria el elemento de la cola
74 }
75
76
77 }
78
79
80 int main(){
81 struct Node*head = NULL;
82 int valor,opcion;
83 do{
84 printf("\nMenu:\n");
85 printf("1. Enqueue(Insertar elemento)\n");
86 printf("2. Mostrar lista\n");
87 printf("3. Dequeue (Borrar elemento)\n");
88 printf("4. Terminar programa\n");
89 printf("Porfavor seleccione una opcion\n");
90 scanf("%d", &opcion);
91 switch (opcion)
92 {
93 case 1:
94 printf("Por favor ingrese un valor\n");
95 scanf("%d",&valor);
localhost:58625/9a647eea-778d-4b13-99af-f2dcbb1f922b/ 2/3
13/3/25, 8:10 p.m. Cola_enlazada.c
96 insertEnd(&head,valor);
97 break;
98 case 2:
99 displayLiSt(head);
100 break;
101 case 3:
102 deleteBegin(&head);
103 break;
104 case 4:
105 printf("Saliendo del programa...");
106 break;
107 default:
108 printf("Opción no válida. Intente de nuevo.\n");
109 }
110
111 }while(opcion != 4);
112 freeList(head);
113 return 0;
114
115 }
116
117
localhost:58625/9a647eea-778d-4b13-99af-f2dcbb1f922b/ 3/3
13/3/25, 8:10 p.m. pila_array.c
pila_array.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX 4 // Definimos el tamaño máximo de la pila
5
6 int stack[MAX]; // Declaramos el arreglo que representará la pila
7 int top = -1; // Inicializamos la cima de la pila
8
9 // Función para verificar si la pila está vacía
10 int isEmpty() {
11 return top == -1;
12 }
13
14 // Función para verificar si la pila está llena
15 int isFull() {
16 return top == MAX - 1;
17 }
18
19 // Función para agregar un elemento a la pila (Push)
20 void push(int data) {
21 if (isFull()) {
22 printf("La pila está llena. No se puede agregar más elementos.\n");
23 return;
24 }
25 stack[++top] = data;
26 printf("Elemento %d agregado a la pila.\n", data);
27 }
28
29 // Función para eliminar un elemento de la pila (Pop)
30 void pop() {
31 if (isEmpty()) {
32 printf("La pila está vacía. No se puede eliminar elementos.\n");
33 return;
34 }
35 printf("\nElemento %d eliminado de la pila.\n", stack[top--]);
36 }
37
38 // Función para mostrar los elementos de la pila
39 void display() {
40 if (isEmpty()) {
41 printf("La pila está vacía.\n");
42 return;
43 }
44 printf("Elementos en la pila: \n");
45 for (int i = top; i>-1 ; i--) {
46 printf(" %d\n", stack[i]);
47 }
48 printf("\n");
localhost:58500/1492bbb7-4af7-4516-b7a9-c1a4d393b583/ 1/3
13/3/25, 8:10 p.m. pila_array.c
49 }
50
51
52 void realizarPeticionesPo
p(){
53 int numPeticiones;
54 printf("Cuantos elementos desea borrar?\n");
55 scanf("%d",&numPeticiones);
56
57 for(int i =0;i<numPeticiones;i++){
58 if(isEmpty()){
59 printf("\nStack Underflow\n");
60 return;
61 }
62 pop();
63 }
64
65 }
66
67 void completarPila(){
68 int data;
69 while(!isFull()){
70 printf("Ingrese el elemento a agregar: ");
71 scanf("%d", &data);
72 push(data);
73 }
74
75 }
76
77
78
79
80
81 // Función principal con el menú de opciones
82 int main() {
83 int choice, data;
84
85 while (1) {
86 printf("\nMenú de opciones:\n");
87 printf("1. Agregar elemento (Push)\n");
88 printf("2. Eliminar elemento (Pop)\n");
89 printf("3. Mostrar elementos de la pila\n");
90 printf("4. Llenar la pila\n");
91 printf("5. Borrar 'n' elementos de la pila\n");
92 printf("6. Salir\n");
93 printf("Ingrese su opcion: ");
94 scanf("%d", &choice);
95
96 switch (choice) {
97 case 1:
98 printf("Ingrese el elemento a agregar: ");
localhost:58500/1492bbb7-4af7-4516-b7a9-c1a4d393b583/ 2/3
13/3/25, 8:10 p.m. pila_array.c
99 scanf("%d", &data);
100 push(data);
101 break;
102 case 2:
103 pop();
104 break;
105 case 3:
106 display();
107 break;
108 case 4:
109 completarPila();
110 break;
111 case 5:
112 realizarPeticionesPo
p();
113 break;
114 case 6:
115 exit(0);
116 default:
117 printf("Opción no válida. Intente de nuevo.\n");
118 }
119 }
120
121 return 0;
122 }
123
124
125
localhost:58500/1492bbb7-4af7-4516-b7a9-c1a4d393b583/ 3/3
13/3/25, 8:10 p.m. pila_lista.c
pila_lista.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #define MAX 4
5 struct Node
6 {
7 int valor;
8 struct Node* next;
9 };
10
11 int isFull(struct Node*head);
12 int tamLista(struct Node*head);
13
14 struct Node* createNode(const int Valor){
15 struct Node*newNode= (struct Node*)malloc(sizeof(struct Node));//aqui se pide la memoria
a la computadora
16
17 if(newNode == NULL){
18 printf("Fallo alojamiento de memoria");
19 exit(1);
20 }
21 newNode->valor = Valor;
22 newNode->next = NULL;
23 return newNode;
24
25 }
26
27 int isFull(struct Node*head)
28 {
29 int numelementos = tamLista(head);
30 return (numelementos >= MAX);
31 }
32
33 int isEmpty(struct Node*head)
34 {
35 int numelementos = tamLista(head);
36 return (numelementos ==0);
37 }
38
39 void insertEnd(struct Node**head,int Valor){
40 struct Node*newNode= createNode(Valor);
41 if(isFull(*head)){
42 printf("Stack Overflow");
43 return;
44 }
45
46 if(*head == NULL){
47 *head=newNode;
localhost:58500/159ec9ae-34a4-4afc-b29e-ff9243042cbc/ 1/5
13/3/25, 8:10 p.m. pila_lista.c
48 return;
49 }
50 struct Node *temp= *head;
51 while (temp->next != NULL)
52 {
53 temp = temp->next;
54 }
55 temp->next = newNode;
56
57 }
58
59
60 void deleteEnd(struct Node** head) {
61 if (*head == NULL) {
62 printf("Stack Underflow\n");
63 return;
64 }
65
66 // Caso 1: Solo hay un nodo en la lista
67 if ((*head)->next == NULL) {
68 printf("Elemento borrado: %d\n", (*head)->valor);
69 free(*head);
70 *head = NULL; // La lista queda vacía
71 return;
72 }
73
74 // Caso 2: Hay más de un nodo en la lista
75 struct Node* temp = *head;
76 struct Node* prev = NULL;
77
78 // Avanzar hasta el último nodo
79 while (temp->next != NULL) {
80 prev = temp;
81 temp = temp->next;
82 }
83
84 // Imprimir el valor del nodo que se va a borrar
85 printf("Elemento borrado: %d\n", temp->valor);
86
87 // Desconectar el último nodo de la lista
88 prev->next = NULL;
89
90 // Liberar la memoria del último nodo
91 free(temp);
92 }
93
94 void displayLiSt(struct Node* head) {
95 // Caso base: si la lista está vacía, no hacemos nada
96 if (head == NULL) {
97 return;
localhost:58500/159ec9ae-34a4-4afc-b29e-ff9243042cbc/ 2/5
13/3/25, 8:10 p.m. pila_lista.c
98 }
99
100 // Llamada recursiva para avanzar al siguiente nodo
101 displayLiSt(head->next);
102
103 // Imprimimos el valor del nodo actual (esto se ejecuta después de la recursión)
104 printf("\nValor del elemento: %d\n", head->valor);
105 }
106
107 void freeList(struct Node*head){
108 struct Node* temp;
109 while (head != NULL)
110 {
111 temp = head;
112 head = head->next;
113 free(temp);
114 }
115
116 }
117
118 int tamLista(struct Node*head){
119 int posicion=0;
120 struct Node*temp =head;
121 while (temp!=NULL)
122 {
123 temp = temp->next;
124 posicion++;
125 }
126 return posicion;
127
128 }
129
130
131
132 void llenarPila(struct Node** head) {
133 int valor;
134 while (!isFull(*head)) {
135 printf("Agregar un valor: ");
136 scanf("%d", &valor);
137 insertEnd(head, valor);
138 }
139 }
140
141 void borrarElementos(struct Node**head, int iteraciones){
142 for(int i=0; i<iteraciones;i++){
143 deleteEnd(head);
144 }
145 }
146
147
localhost:58500/159ec9ae-34a4-4afc-b29e-ff9243042cbc/ 3/5
13/3/25, 8:10 p.m. pila_lista.c
localhost:58500/159ec9ae-34a4-4afc-b29e-ff9243042cbc/ 4/5
13/3/25, 8:10 p.m. pila_lista.c
198
199
200
201
202
203
204
localhost:58500/159ec9ae-34a4-4afc-b29e-ff9243042cbc/ 5/5
13/3/25, 8:09 p.m. lista_circula.c
lista_circula.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct Node
5 {
6 int valor;
7 struct Node* next;
8 };
9
10 struct Node* createNode(const int Valor){
11 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // aqui se pide la
memoria a la computadora
12
13 if (newNode == NULL){
14 printf("Fallo alojamiento de memoria");
15 exit(1);
16 }
17 newNode->valor = Valor;
18 newNode->next = newNode; // En una lista circular, el nuevo nodo apunta a sí mismo
19 return newNode;
20 }
21
22 void insertBegin(struct Node** head, int Valor){
23 struct Node* newNode = createNode(Valor);
24 if (*head == NULL) {
25 *head = newNode;
26 return;
27 }
28
29 struct Node* temp = *head;
30 while (temp->next != *head) {
31 temp = temp->next;
32 }
33
34 newNode->next = *head;
35 temp->next = newNode;
36 *head = newNode;
37 }
38
39 void insertEnd(struct Node** head, int Valor){
40 struct Node* newNode = createNode(Valor);
41 if (*head == NULL) {
42 *head = newNode;
43 return;
44 }
45
46 struct Node* temp = *head;
47 while (temp->next != *head) {
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 1/6
13/3/25, 8:09 p.m. lista_circula.c
48 temp = temp->next;
49 }
50
51 temp->next = newNode;
52 newNode->next = *head;
53 }
54
55 void deletePos(struct Node** head, int pos){
56 if (*head == NULL) {
57 printf("Lista vacía\n");
58 return;
59 }
60
61 struct Node* temp = *head;
62 struct Node* prev = NULL;
63 int posicion = 0;
64
65 while (temp->next != *head && posicion != pos) {
66 prev = temp;
67 temp = temp->next;
68 posicion++;
69 }
70
71 if (posicion != pos) {
72 printf("Posición no encontrada\n");
73 return;
74 }
75
76 if (prev == NULL) {
77 struct Node* last = *head;
78 while (last->next != *head) {
79 last = last->next;
80 }
81 *head = temp->next;
82 last->next = *head;
83 } else {
84 prev->next = temp->next;
85 }
86
87 free(temp);
88 }
89
90 void deleteEnd(struct Node** head){
91 if (*head == NULL) {
92 printf("No hay lista que eliminar\n");
93 return;
94 }
95
96 struct Node* temp = *head;
97 struct Node* prev = NULL;
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 2/6
13/3/25, 8:09 p.m. lista_circula.c
98
99 if (temp->next == *head) {
100 free(*head);
101 *head = NULL;
102 return;
103 }
104
105 while (temp->next != *head) {
106 prev = temp;
107 temp = temp->next;
108 }
109
110 prev->next = *head;
111 free(temp);
112 }
113
114 int tamLista(struct Node* head){
115 if (head == NULL) return 0;
116
117 int posicion = 1;
118 struct Node* temp = head;
119
120 while (temp->next != head) {
121 temp = temp->next;
122 posicion++;
123 }
124
125 return posicion;
126 }
127
128 void insertAnyPos(struct Node** head, int pos, int Valor){
129 if (pos == 0) { // Caso especial: Insertar al inicio
130 insertBegin(head, Valor);
131 return;
132 }
133
134 struct Node* temp = *head;
135 int posicion = 0;
136
137 while (temp->next != *head && posicion < pos - 1) { // Busca la posición anterior a `pos`
138 temp = temp->next;
139 posicion++;
140 }
141
142 if (posicion != pos - 1) {
143 printf("Posición fuera de rango\n");
144 return;
145 }
146
147 struct Node* newNode = createNode(Valor);
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 3/6
13/3/25, 8:09 p.m. lista_circula.c
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 4/6
13/3/25, 8:09 p.m. lista_circula.c
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 5/6
13/3/25, 8:09 p.m. lista_circula.c
248 default:
249 printf("Opción no válida. Intente de nuevo.\n");
250 }
251 } while (opcion != 9);
252 freeList(head);
253 return 0;
254 }
255
localhost:58307/33101858-3216-4b91-a4af-0bef5542d423/ 6/6
13/3/25, 8:09 p.m. Lista_enlazada_doble.c
Lista_enlazada_doble.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct Node {
5 int valor;
6 struct Node* next;
7 struct Node* prev;
8 };
9
10 struct Node* createNode(const int Valor) {
11 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
12
13 if (newNode == NULL) {
14 printf("Fallo alojamiento de memoria");
15 exit(1);
16 }
17 newNode->valor = Valor;
18 newNode->next = NULL;
19 newNode->prev = NULL;
20 return newNode;
21 }
22
23 void insertBegin(struct Node** head, int Valor) {
24 struct Node* newNode = createNode(Valor);
25 newNode->next = *head;
26 if (*head != NULL) {
27 (*head)->prev = newNode;
28 }
29 *head = newNode;
30 }
31
32 void insertEnd(struct Node** head, int Valor) {
33 struct Node* newNode = createNode(Valor);
34 if (*head == NULL) {
35 *head = newNode;
36 return;
37 }
38 struct Node *temp = *head;
39 while (temp->next != NULL) {
40 temp = temp->next;
41 }
42 temp->next = newNode;
43 newNode->prev = temp;
44 }
45
46 void deletePos(struct Node** head, int pos) {
47 struct Node* temp = *head;
48 int posicion = 0;
localhost:58307/02898a9a-887b-428d-a18b-4895d539e3cc/ 1/5
13/3/25, 8:09 p.m. Lista_enlazada_doble.c
localhost:58307/02898a9a-887b-428d-a18b-4895d539e3cc/ 2/5
13/3/25, 8:09 p.m. Lista_enlazada_doble.c
99 }
100 int tamaniolista = tamLista(*head);
101 if (tamaniolista < pos) {
102 for (int i = tamaniolista; i < pos; i++) {
103 insertEnd(head, 0);
104 }
105 }
106 struct Node* temp = *head;
107 int posicion = 0;
108 while (temp != NULL && posicion < pos - 1) { // Busca la posición anterior a `pos`
109 temp = temp->next;
110 posicion++;
111 }
112 if (temp == NULL) {
113 printf("Posición fuera de rango\n");
114 return;
115 }
116 struct Node* newNode = createNode(Valor);
117 newNode->next = temp->next;
118 newNode->prev = temp;
119 if (temp->next != NULL) {
120 temp->next->prev = newNode;
121 }
122 temp->next = newNode;
123 }
124
125 void displayLiSt(struct Node* head) {
126 struct Node* temp = head;
127 while (temp != NULL) {
128 if(temp->prev == NULL){
129 printf("%d -> ", temp->valor);
130 temp = temp->next;
131 continue;
132 }
133 printf("<- %d -> ", temp->valor);
134 temp = temp->next;
135 }
136 if (temp == NULL) {
137 printf("NULL");
138 }
139 }
140
141 void freeList(struct Node* head) {
142 struct Node* temp;
143 while (head != NULL) {
144 temp = head;
145 head = head->next;
146 free(temp);
147 }
148 }
localhost:58307/02898a9a-887b-428d-a18b-4895d539e3cc/ 3/5
13/3/25, 8:09 p.m. Lista_enlazada_doble.c
149
150 void crearLista(struct Node** head, int tamanioLista) {
151 if (tamanioLista <= 0) {
152 printf("El tamanio de la lista debe contener al menos 1 elemento");
153 return;
154 }
155 for (int i = 0; i < tamanioLista; i++) {
156 insertBegin(head, 0);
157 }
158 }
159
160 int main() {
161 struct Node* head = NULL;
162 int valor, opcion, posicion, tam;
163 do {
164 printf("\nMenu:\n");
165 printf("1. Insertar Valor\n");
166 printf("2. Mostrar lista\n");
167 printf("3. Insertar al inicio\n");
168 printf("4. Insertar en una posicion especifica\n");
169 printf("5. Insertar al final\n");
170 printf("6. Borrar inicio\n");
171 printf("7. Borrar final\n");
172 printf("8. Borrar una posicion especifica\n");
173 printf("9. Terminar programa\n");
174 printf("Por favor seleccione una opcion\n");
175 scanf("%d", &opcion);
176 switch (opcion) {
177 case 1:
178 //printf("Por favor ingrese el tamanio de la lista\n");
179 //scanf("%d", &tam);
180 //crearLista(&head, tam);
181 printf("Por favor ingrese un valor\n");
182 scanf("%d", &valor);
183 insertBegin(&head, valor);
184 break;
185 case 2:
186 displayLiSt(head);
187 break;
188 case 3:
189 printf("Por favor ingrese un valor\n");
190 scanf("%d", &valor);
191 insertBegin(&head, valor);
192 break;
193 case 4:
194 printf("Por favor ingrese la posicion a insertar\n");
195 scanf("%d", &posicion);
196 printf("Por favor ingrese un valor\n");
197 scanf("%d", &valor);
198 insertAnyPos(&head, posicion, valor);
localhost:58307/02898a9a-887b-428d-a18b-4895d539e3cc/ 4/5
13/3/25, 8:09 p.m. Lista_enlazada_doble.c
199 break;
200 case 5:
201 printf("Por favor ingrese un valor\n");
202 scanf("%d", &valor);
203 insertEnd(&head, valor);
204 break;
205 case 6:
206 deletePos(&head, 0);
207 break;
208 case 7:
209 deleteEnd(&head);
210 break;
211 case 8:
212 printf("Por favor ingrese la posicion a eliminar\n");
213 scanf("%d", &posicion);
214 deletePos(&head, posicion);
215 break;
216 case 9:
217 printf("Saliendo del programa...");
218 break;
219 default:
220 printf("Opción no válida. Intente de nuevo.\n");
221 }
222 } while (opcion != 9);
223 freeList(head);
224 return 0;
225 }
226
localhost:58307/02898a9a-887b-428d-a18b-4895d539e3cc/ 5/5
13/3/25, 8:09 p.m. lista_simple.c
lista_simple.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 struct Node
5 {
6 int valor;
7 struct Node* next;
8 };
9
10 struct Node* createNode(const int Valor){
11 struct Node*newNode= (struct Node*)malloc(sizeof(struct Node));
12
13 if(newNode == NULL){
14 printf("Fallo alojamiento de memoria");
15 exit(1);
16 }
17 newNode->valor = Valor;
18 newNode->next = NULL;
19 return newNode;
20
21 }
22
23 void insertBegin(struct Node** head, int Valor){
24 struct Node* newNode = createNode(Valor);
25 newNode->next =*head;
26 *head=newNode;
27 }
28
29 void insertEnd(struct Node**head,int Valor){
30 struct Node*newNode= createNode(Valor);
31 if(*head == NULL){
32 *head=newNode;
33 return;
34 }
35 struct Node *temp= *head;
36 while (temp->next != NULL)
37 {
38 temp = temp->next;
39 }
40 temp->next = newNode;
41
42 }
43
44 void deletePos(struct Node**head, int pos){
45 struct Node*temp = *head;
46 struct Node*prev = NULL;
47 int posicion =0;
48 while (temp != NULL && posicion != pos)
localhost:58307/9259a6fa-5be5-4bd5-9871-f04587f90072/ 1/5
13/3/25, 8:09 p.m. lista_simple.c
49 {
50 prev= temp;
51 temp= temp->next;
52 posicion++;
53 }
54 if (temp ==NULL)
55 {
56 printf("Posicion no encontrada\n");
57 return;
58 }
59 if (prev == NULL)
60 {
61 *head = temp->next;
62 }
63 else{
64 prev->next = temp->next;
65 }
66 free(temp);
67
68 }
69
70 void deleteEnd(struct Node**head){
71 if(*head == NULL){
72 printf("No hay lista que eliminar");
73 return;
74 }
75 if ((*head)->next ==NULL)
76 {
77 free(*head);
78 *head = NULL;
79 return;
80 }
81
82
83 struct Node*temp = *head;
84 struct Node*prev = NULL;
85 while (temp->next != NULL)
86 {
87 prev = temp;
88 temp = temp->next;
89 }
90 prev->next = NULL;
91 free(temp);
92
93 }
94 int tamLista(struct Node*head){
95 int posicion=0;
96 struct Node*temp =head;
97 while (temp!=NULL)
98 {
localhost:58307/9259a6fa-5be5-4bd5-9871-f04587f90072/ 2/5
13/3/25, 8:09 p.m. lista_simple.c
99 temp = temp->next;
100 posicion++;
101 }
102 return posicion;
103
104 }
105
106 void insertAnyPos(struct Node**head, int pos, int Valor){
107 if (pos == 0 ) { // Caso especial: Insertar al inicio
108 insertBegin(head, Valor);
109 return;
110 }
111 int tamaniolista = tamLista(*head);
112 if(tamaniolista < pos){
113 for(int i=tamaniolista;i<pos;i++){
114 insertEnd(head,0);
115 }
116 }
117 struct Node* temp = *head;
118
119 int posicion = 0;
120
121
122 while (temp != NULL && posicion < pos - 1) { // Busca la posición anterior a `pos`
123 temp = temp->next;
124 posicion++;
125 }
126
127 if (temp == NULL) {
128 printf("Posición fuera de rango\n");
129 return;
130 }
131
132 struct Node* newNode = createNode(Valor);
133 newNode->next = temp->next;
134 temp->next = newNode;
135
136 }
137
138
139 void displayLiSt(struct Node*head){
140 struct Node* temp= head;
141 while (temp != NULL)
142 {
143 printf("%d -> ", temp->valor);
144 temp = temp->next;
145 }
146 if (temp == NULL)
147 {
148 printf("NULL");
localhost:58307/9259a6fa-5be5-4bd5-9871-f04587f90072/ 3/5
13/3/25, 8:09 p.m. lista_simple.c
149 }
150
151
152 }
153
154 void freeList(struct Node*head){
155 struct Node* temp;
156 while (head != NULL)
157 {
158 temp = head;
159 head = head->next;
160 free(temp);
161 }
162
163
164 }
165
166 void crearLista(struct Node**head,int tamanioLista){
167 if(tamanioLista <=0){
168 printf("El tamanio de la lista debe contener almenos 1 elemento");
169 return;
170 }
171 for(int i =0;i<tamanioLista;i++){
172 insertBegin(head,0);
173 }
174 }
175
176
177 int main(){
178 struct Node*head = NULL;
179 int valor,opcion,posicion,tam;
180 do{
181 printf("\nMenu:\n");
182 printf("1. Crear lista\n");
183 printf("2. Mostrar lista\n");
184 printf("3. Insertar al inicio\n");
185 printf("4. Insertar en una posicion especifica\n");
186 printf("5. Insertar al final\n");
187 printf("6. Borrar inicio\n");
188 printf("7. Borrar final\n");
189 printf("8. Borrar una posicion especifica\n");
190 printf("9. Terminar programa\n");
191 printf("Porfavor seleccione una opcion\n");
192 scanf("%d", &opcion);
193 switch (opcion)
194 {
195 case 1:
196 printf("Por favor ingrese el tamanio de la lista\n");
197 scanf("%d",&tam);
198 crearLista(&head,tam);
localhost:58307/9259a6fa-5be5-4bd5-9871-f04587f90072/ 4/5
13/3/25, 8:09 p.m. lista_simple.c
199 break;
200 case 2:
201 displayLiSt(head);
202 break;
203 case 3:
204 printf("Por favor ingrese un valor\n");
205 scanf("%d",&valor);
206 insertBegin(&head,valor);
207 break;
208 case 4:
209 printf("Por favor ingrese la posicion a insertar\n");
210 scanf("%d",&posicion);
211 printf("Por favor ingrese un valor\n");
212 scanf("%d",&valor);
213 insertAnyPos(&head,posicion,valor);
214 break;
215 case 5:
216 printf("Por favor ingrese un valor\n");
217 scanf("%d",&valor);
218 insertEnd(&head,valor);
219 break;
220 case 6:
221 deletePos(&head,0);
222 break;
223 case 7:
224 deleteEnd(&head);
225 break;
226 case 8:
227 printf("Por favor ingrese la posicion a eliminar\n");
228 scanf("%d",&posicion);
229 deletePos(&head, posicion);
230 break;
231 case 9:
232 printf("Saliendo del programa...");
233 break;
234 default:
235 printf("Opción no válida. Intente de nuevo.\n");
236 }
237
238
239
240 }while(opcion != 9);
241 freeList(head);
242 return 0;
243
244 }
245
246
localhost:58307/9259a6fa-5be5-4bd5-9871-f04587f90072/ 5/5
13/3/25, 8:08 p.m. binary_search.c
binary_search.c
1 #include <stdio.h>
2
3 // Función de búsqueda binaria recursiva
4 int binarySearch(int array[], int element, int start, int end) {
5 if (end < start) {
6 return -1; // Elemento no encontrado
7 }
8
9 int middle = (start + end) / 2;
10
11 if (array[middle] == element) {
12 return middle; // Elemento encontrado
13 } else if (element < array[middle]) {
14 return binarySearch(array, element, start, middle - 1);
15 } else {
16 return binarySearch(array, element, middle + 1, end);
17 }
18 }
19
20 int main() {
21 int unsortedArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
22 int size = sizeof(unsortedArray) / sizeof(unsortedArray[0]);
23
24 int index1 = binarySearch(unsortedArray, 2, 0, size - 1);
25 int index2 = binarySearch(unsortedArray, 22, 0, size - 1);
26
27 printf("indice de 2: %d\n", index1); // Output: Índice de 2: 1
28 printf("22 no encontrado: %d\n", index2); // Output: 22 no encontrado: -1
29
30 return 0;
31 }
localhost:58086/3407545e-d1c2-43a4-84e6-728cf23f0484/ 1/1
13/3/25, 8:08 p.m. bubble_sort.c
bubble_sort.c
1 #include <stdio.h>
2
3 void bubbleSort(int array[], int size) {
4 for (int i = 0; i < size; i++) {
5 for (int j = 0; j < size - 1; j++) {
6 if (array[j] > array[j + 1]) {
7 int temp = array[j];
8 array[j] = array[j + 1];
9 array[j + 1] = temp;
10 }
11 }
12 }
13 }
14
15 //imprimir el array
16 void printArray(int array[], int size) {
17 for (int i = 0; i < size; i++) {
18 printf("%d ", array[i]);
19 }
20 printf("\n");
21 }
22
23 int main() {
24 int array[] = {4, 3, 2, 1};
25 int size = sizeof(array) / sizeof(array[0]);
26
27 bubbleSort(array, size);
28 printArray(array, size);
29
30 return 0;
31 }
localhost:58086/211b6d71-edcd-420d-8c6e-93cd59f6c886/ 1/1
13/3/25, 8:08 p.m. selected_food.c
selected_food.c
1 #include <stdio.h>
2
3 void selectedFood(const char* food[], int size) {
4 for (int i = 0; i < size; i++) {
5 printf("%s %s\n", food[i], food[i]);
6 }
7 }
8
9 int main() {
10 const char* food[] = {"Palomitas", "Hamburguesa", "Dona", "Refresco"};
11 int size = sizeof(food) / sizeof(food[0]);
12
13 selectedFood(food, size);
14
15 return 0;
16 }
localhost:58086/073909e4-2881-44ac-b5c4-125c817c54a5/ 1/1
13/3/25, 8:08 p.m. find_by_indx.c
find_by_indx.c
1 #include <stdio.h>
2
3 const char* findByIndex(const char* food[],int index) {
4 return food[index];
5 }
6
7 int main() {
8 const char* foodArray[] = {"Palomitas", "Hamburguesa", "Dona", "Refresco"};
9
10 printf("%s\n", findByIndex(foodArray,2));
11
12 return 0;
13 }
localhost:58086/974b17b2-67c8-49e0-b330-8ae89db975ac/ 1/1
13/3/25, 8:07 p.m. cucloWhile_contarNum.c
cucloWhile_contarNum.c
1 #include <time.h>
2 #include <stdio.h>
3
4 int main(void){
5 int num,i,sum;
6 printf("Ingrese un numero positivo: ");
7 scanf("%d",&num);
8 //Se captura el tiempo inicial
9 clock_t begin = clock();
10 //ejecuta hasta que se cumpla la condicion
11 i=0;
12 while (i<=num)
13 {
14 sum= sum+i;
15 i++;
16 }
17
18 //se captura el tiempo final de ejecucion
19 clock_t end = clock();
20 double time_spent = (double)(end-begin)/CLOCKS_PER_SEC;
21 //muestra suma total
22 printf("\n La SUMA de los primeros %d numeros es: %d",num,sum);
23
24 printf("\n Tiempo de ejecucion: %f\n", time_spent);
25 getchar();
26 }
localhost:57828/5026ddb3-566f-462d-adcd-7dd708acd092/ 1/1
13/3/25, 8:06 p.m. cicloWhile_cadenaInvertida.c
cicloWhile_cadenaInvertida.c
1 #include <string.h>
2 #include <stdio.h>
3
4 int main(){
5 char str1[50], temp; //declara e inicializa el tam del array
6 int i=0,j=0;
7 printf("Ingrese una cadena a ser invertide: ");
8 scanf("%s", str1);
9 j=strlen(str1)-1; //obtiene la longitud de la cadena
10 //uso del ciclo WHILE para definir la condicion
11 while(i<j){
12 //uso de la vle TEMP para almacenar los caracteres de str1
13 temp = str1[j];
14 str1[j] = str1[i];
15 str1[i] = temp;
16 i++; //incrementa i en 1
17 j--; //disminuye j en 1
18 }
19 printf("La cadena en orden inverso es: %s", str1);
20 return 0;
21 }
22
localhost:57828/a0035751-fa71-465a-b1fe-cb7ff40e1a7f/ 1/1
13/3/25, 8:06 p.m. cicloFor_cadenaInvertida.c
cicloFor_cadenaInvertida.c
1 #include <string.h>
2 #include <stdio.h>
3
4 void main(){
5 char str[40], temp; //define el tamanio del array
6
7 int i, left,right,len;
8 printf("\n Muestra una cadena en reversa en C: \n");
9 printf("\n------------------------------------------\n");
10 printf("\nIngrese una cadena para ordenar en reversa\n");
11 scanf("%s",&str);
12 len = strlen(str);//Obtiene la longitud de la cadena
13 left =0;
14 right = len-1; //establece el indice derecho len-1
15 //uso del ciclo for para almacenar la cadena invertida
16 for(i=left; i<right;i++){
17 temp= str[i];
18 str[i] = str[right];
19 str[right]= temp;
20 right--;
21 }
22 printf("La cadena insvertida en orden es: %s", str);
23 getchar();
24 }
25
localhost:57828/71576514-e717-4e23-b7af-b3d707af554e/ 1/1
13/3/25, 8:06 p.m. funcRecurcion_cadena_invertida.c
funcRecurcion_cadena_invertida.c
1 #include <string.h>
2 #include <stdio.h>
3 //uso de la funcion recurcion
4 void revstr(char *str1){
5 //declara variable static
6 static int i,len,temp;
7 len = strlen(str1);
8 if(i<len/2){
9 //variable temp para almacenar la cadena temporalmente
10 temp=str1[i];
11 str1[i] = str1[len-i-1];
12 str1[len-i-1] = temp;
13 i++;
14 revstr(str1); //llamadas recursivas a la funcionrevstr
15 }
16
17 }
18 int main(){
19 char str1[50]; //tamanio de char str
20 printf("Ingrese la cadena: ");
21 gets(str1); //uso de funcion gets para tomar la cadena
22 printf("\n Antes de invertir la cadena: %s", str1);
23 //llamando la funcion revstr
24 revstr(str1);
25 printf("Despues de reversa, la cedena; %s", str1);
26
27 }
28
localhost:57828/382c07ff-437a-4c2b-b916-c745e9701c58/ 1/1
13/3/25, 8:05 p.m. apuntadore_cadena_invertida.c
apuntadore_cadena_invertida.c
1 #include <string.h>
2 #include <stdio.h>
3 int str_len(char*st);
4 void revstr(char*st);
5 int main(){
6 char st[50];
7 printf("Ingrese una cadena a invertir: ");
8 scanf("%s",st);
9 revstr(st);
10 printf("La cadena invertida es: %s", st);
11 return 0;
12 }
13
14 void revstr(char*st){
15 int len, i;
16 char *start,*end,temp;
17 len = str_len(st);
18 start = st;
19 end = st;
20 for(i=0; i<len-1;i++)
21 end++;
22
23 for(i=0; i<len/2;i++){
24 temp=*end; //mov eax, [edi] ;edi =end
25 //mov ebx, [esi] ;esi = start
26 *end= *start; //mov [edi], ebx
27 *start= temp; //mov [esi], eax
28 start++; //inc esi
29 end--; //dec edi
30 }
31
32 }
33
34 int str_len(char *ptr){
35 int i =0;
36 while(*(ptr+i)!= '\0')
37 i++;
38 return i;
39 }
localhost:57828/13b59dd6-e004-49de-83e0-9e45108a41a3/ 1/1