2 GPT Promp Refs

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

Trabalho de listas ligadas - busca em nó na lista

#include <stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

int search(struct Node* head, int x) {


struct Node* current = head;
while (current != NULL) {
if (current->data == x)
return 1;
current = current->next;
}
return 0;
}

void printList(struct Node* node) {


while (node != NULL) {
printf("%d -> ", node->data);
node = node->next;
}
printf("NULL\n");
}

int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;

head = (struct Node*)malloc(sizeof(struct Node));


second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));

head->data = 1;
head->next = second;

second->data = 2;
second->next = third;

third->data = 3;
third->next = NULL;

int item_to_find = 2;
if (search(head, item_to_find))
printf("Element %d is present in the list\n", item_to_find);
else
printf("Element %d is not present in the list\n", item_to_find);

printList(head);

return 0;
}

You might also like