Skip to content

Instantly share code, notes, and snippets.

@Edmartt
Created December 27, 2023 08:30
Show Gist options
  • Save Edmartt/3065955a19a66762949fdf1dc9db8687 to your computer and use it in GitHub Desktop.
Save Edmartt/3065955a19a66762949fdf1dc9db8687 to your computer and use it in GitHub Desktop.
Linked List in C
#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