Created
December 27, 2023 08:30
-
-
Save Edmartt/3065955a19a66762949fdf1dc9db8687 to your computer and use it in GitHub Desktop.
Linked List in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct Node{ | |
int data; | |
struct Node* next; | |
}; | |
typedef struct Node node; | |
void addElement(node** start, int data){ | |
node* newNode = (node*)malloc(sizeof(node)); | |
newNode->data = data; | |
newNode->next = NULL; | |
if (*start == NULL){ | |
*start = newNode; | |
}else { | |
node* current = *start; | |
while(current->next !=NULL){ | |
current = current->next; | |
} | |
current->next = newNode; | |
} | |
} | |
void showElements(node* start){ | |
node* current = start; | |
while (current != NULL) { | |
printf("%d -> ", current->data); | |
current = current->next; | |
} | |
printf("NULL\n"); | |
} | |
int main(){ | |
node* list = NULL; | |
addElement(&list, 5); | |
addElement(&list, 7); | |
addElement(&list, 10); | |
showElements(list); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment