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:
@@ -0,0 +1,16 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.10)
|
||||||
|
|
||||||
|
project(klist)
|
||||||
|
|
||||||
|
set (INCLUDE
|
||||||
|
include/klist.h
|
||||||
|
include/types.h
|
||||||
|
)
|
||||||
|
|
||||||
|
set (SOURCES
|
||||||
|
src/klist.c
|
||||||
|
)
|
||||||
|
|
||||||
|
include_directories(${CMAKE_CURRENT_LIST_DIR}/include/)
|
||||||
|
|
||||||
|
add_library(klist STATIC ${SOURCES} ${INCLUDE})
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#include "types.h"
|
||||||
|
|
||||||
|
void insert_at_begin(struct node**, void*);
|
||||||
|
void insert_at_end(struct node**, void*);
|
||||||
|
void traverse(struct node**);
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
struct node
|
||||||
|
{
|
||||||
|
void *data;
|
||||||
|
struct node *next;
|
||||||
|
};
|
||||||
+60
@@ -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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user