Skip to content
Snippets Groups Projects
Commit 8443b92f authored by root's avatar root
Browse files

ex2

parent 516ffc5c
No related branches found
No related tags found
No related merge requests found
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct _element {
int data;
struct _element *next;
} element;
void cl_push(element* current, element* new_elem, int data){
new_elem->data = data;
new_elem->next = NULL;
current->next = new_elem;
}
void cl_insert_at(element* head, element* new_elem, int val, uint index){
element* current = head;
for (uint i = 0; i < index; i++)
{
element* next = current->next;
if (next == NULL){
return;
}
current = next;
}
element* next = current->next;
current->next = new_elem;
new_elem->next = next;
new_elem->data = val;
}
void cl_remove_at(element* head, uint index){
element* current = head;
for (uint i = 0; i < index; i++)
{
element* next = current->next;
if (next == NULL){
return;
}
current = next;
}
element* next = current->next;
element* n_next = next->next;
current->data = next->data;
current->next = n_next;
}
int main(){ int main(){
element first_elem = {.data = 8, .next = NULL};
element* head = &first_elem;
element second;
element third;
element fourth;
cl_push(&first_elem, &second, -1);
cl_push(&second, &third, 15);
cl_push(&third, &fourth, 12);
element newOne;
cl_insert_at(head, &newOne, 99, 1);
cl_remove_at(head, 2);
element* index = head;
while (index != NULL){
printf("Data: %d\n", index->data);
index = index->next;
}
return 0; return 0;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment