Initial commit. Able to add int to the beginning and end of the list, as well as traverse the list.

This commit is contained in:
amazing-username
2019-07-19 00:33:27 -04:00
commit 1867267b06
4 changed files with 87 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "klist.h"
void insert_at_begin(struct node **list, void *val)
{
struct node *tmp = (struct node*)malloc(sizeof(struct node));
void *tmp_val = malloc(sizeof(val));
memcpy(tmp_val, val, sizeof(val));
if ((*list) == NULL) {
(*list) = tmp;
(*list)->data = tmp_val;
(*list)->next = NULL;
return;
}
tmp->data = tmp_val;
tmp->next = *list;
*list = tmp;
}
void insert_at_end(struct node **list, void *val)
{
struct node *tmp = (struct node*)malloc(sizeof(struct node));
void *tmp_val = malloc(sizeof(val));
memcpy(tmp_val, val, sizeof(val));
if ((*list) == NULL) {
(*list) = tmp;
(*list)->data = tmp_val;
(*list)->next = NULL;
return;
}
tmp = *list;
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = (struct node*)malloc(sizeof(struct node));
tmp->next->data = tmp_val;
tmp->next->next = NULL;
}
void traverse(struct node **list)
{
if (*list == NULL)
return;
struct node *tmp;
do
{
printf("node value %d\n", *(int*)(*list)->data);
tmp = *list;
*list = (*list)->next;
}
while(tmp->next != NULL);
}