Use this code however you want.
Source code of linked.h:
#ifndef LINKED_H
#define LINKED_H
#include <stdlib.h>
#include <stdio.h>
typedef struct Node {
int val;
struct Node *next;
} Node;
typedef struct {
Node *head;
} Linked_List;
Node *create_node(int);
Linked_List *create_linked_list();
void linked_list_add_at_head(Linked_List *, int);
void linked_list_add_at_tail(Linked_List *, int);
void linked_list_add_at_index(Linked_List *, int, int);
void linked_list_delete_at_index(Linked_List *, int);
void linked_list_free(Linked_List *);
int linked_list_get(Linked_List *, int);
void display(Linked_List *);
#endif
Source code of linked.c:
#include "linked.h"
#include <stdio.h>
#include <stdlib.h>
Node *create_node(int val){
Node *node = (Node*)calloc(1, sizeof(Node));
node->next = NULL;
node->val = val;
return node;
}
Linked_List *create_linked_list(){
Linked_List *list = (Linked_List*)calloc(1, sizeof(Linked_List));
list->head = NULL;
return list;
}
void linked_list_add_at_head(Linked_List *list, int val){
if(NULL == list->head)
list->head = create_node(val);
else{
Node *temp = list->head;
list->head = create_node(val);
list->head->next = temp;
}
}
void linked_list_add_at_tail(Linked_List *list, int val){
Node *cur;
if(!list->head)
linked_list_add_at_head(list, val);
else{
cur = list->head;
while(NULL != cur->next)
cur = cur->next;
cur->next = create_node(val);
}
}
void linked_list_add_at_index(Linked_List *list, int index, int val){
Node *cur = list->head;
Node *temp;
if(!index)
linked_list_add_at_head(list, val);
else if(index && cur){
for(int i = 1; i < index; i++){
if(cur->next)
cur = cur->next;
else
return;
}
temp = cur->next;
cur->next = create_node(val);
cur = cur->next;
cur->next = temp;
}
}
void linked_list_delete_at_index(Linked_List *list, int index){
Node *cur = list->head;
Node *delete_node;
if(!index && cur->next){
list->head = cur->next;
free(cur);
}
else if(!index && !cur->next)
list->head = NULL;
else if(cur->next){
for(int i = 1; i < index; i++){
if(cur->next->next)
cur = cur->next;
else
return;
}
delete_node = cur->next;
cur->next = delete_node->next;
free(delete_node);
}
}
void linked_list_free(Linked_List *list){
Node *temp = list->head;
Node *next;
if(temp && temp->next){
next = temp->next;
free(temp);
while(next->next){
temp = next;
next = next->next;
free(temp);
}
free(next);
}
else if(temp)
free(temp);
}
int linked_list_get(Linked_List *list, int index){
Node *cur = list->head;
if(!index && cur)
return cur->val;
else if(!index && !cur)
return -1;
else{
for(int i = 0; i < index; i++){
if(cur->next)
cur = cur->next;
else
return -1;
}
return cur->val;
}
}
void display(Linked_List *list){
Node *cur = list->head;
printf("Linked list: ");
if(NULL == list->head){
printf("NULL\n");
return;
}
while(NULL != cur){
printf("%d ", cur->val);
cur = cur->next;
}
printf("\n");
}
Let`s test this.
source code of list.c:
#include <stdlib.h>
#include <stdio.h>
#include "linked.h"
int main(){
Linked_List *list = create_linked_list();
for(int i = 0; i < 10; i++)
linked_list_add_at_tail(list, i*3);
linked_list_add_at_index(list, 2, 2);
display(list);
linked_list_free(list);
return 0;
}
We need to compile this:
gcc list.c linked.c -o list.exe
list.exe
>> Linked list: 0 3 2 6 9 12 15 18 21 24 27.