Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "./list.h"
- int main(int argc, char *argv[]) {
- int arr[] = {1,2,3,4};
- List *list = createList();
- int i = 0;
- for(i = 0; i < 4; i++) {
- appendList(list, arr[i]);
- }
- return 0;
- }
- List *createList() {
- List *list = malloc(sizeof(List));
- if(list == NULL) {
- return NULL;
- }
- list->head = malloc(sizeof(Node));
- list->tail = malloc(sizeof(Node));
- if(list->head == NULL || list->tail == NULL) {
- free(list);
- return NULL;
- }
- list->size = 0;
- return list;
- }
- void appendList(List *list, int num) {
- if(list->head->value == 0) {
- list->head->value = num;
- list->tail->value = num;
- return;
- }
- Node *current = calloc(1, sizeof(Node));
- current = list->head;
- while(current->next != NULL) {
- current = current->next;
- }
- current->next = calloc(1, sizeof(Node));
- if(current->next == NULL) {
- free(current->next);
- printf("Failed to allocate memory");
- exit(1);
- }
- current->next->value = num;
- list->size += 1;
- list->tail = current->next;
- }
- #ifndef List_h
- #define List_h
- #include <stdlib.h>
- typedef struct node {
- int value;
- struct node *next;
- } Node;
- typedef struct {
- Node *head;
- Node *tail;
- int size;
- } List;
- List *createList();
- void appendList(List *, int num);
- Node *removeList(List *);
- void printList(List *);
- #endif
Add Comment
Please, Sign In to add comment