Skip to content

Instantly share code, notes, and snippets.

@cs-fedy
Last active September 17, 2020 20:41
Show Gist options
  • Save cs-fedy/e6fbdc6435e1b1feade519c404edd53b to your computer and use it in GitHub Desktop.
Save cs-fedy/e6fbdc6435e1b1feade519c404edd53b to your computer and use it in GitHub Desktop.
k linked list implementation in C.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "kll.h"
// Function on linked list:
LinkedList * createList() {
LinkedList *ll ;
ll = malloc(sizeof(LinkedList)) ;
ll -> head = NULL ;
ll -> tail = NULL ;
return ll ;
}
unsigned isEmpty(LinkedList * ll) {
return (ll -> head == NULL) && (ll -> tail == NULL);
}
void insertAfter(Node * nd, int key) {
struct Node * temp_nd;
temp_nd = malloc(sizeof(Node));
temp_nd -> key = key;
temp_nd -> next = nd -> next;
nd -> next = temp_nd;
}
void insertBefore(Node * nd, int key) {
struct Node * temp_nd;
temp_nd = malloc(sizeof(Node));
*temp_nd = *nd;
nd -> key = key;
nd -> next = temp_nd;
}
Node * findNode(Node * nd, int key) {
while (nd && nd -> key != key) {
nd = nd -> next;
}
return nd;
}
void show(Node * nd) {
printf("%d -- ", nd -> key);
}
void traverse(Node * nd, void(*callback)(Node *)) {
while (nd) {
(*callback)(nd);
nd = nd -> next;
}
printf("\n");
}
void removeSuccessor(Node * nd) {
Node * temp_nd;
temp_nd = nd -> next;
nd -> next = temp_nd -> next;
free(temp_nd);
}
void removeNode(Node * nd) {
Node * temp_nd;
assert(nd -> next);
temp_nd = nd -> next;
*nd = *temp_nd;
free(temp_nd);
}
// Function on k linked list:
KLinkedList * createKList(int linkedListsNumber) {
KLinkedList * kll = NULL;
kll = malloc(sizeof(KLinkedList));
kll -> size = linkedListsNumber;
kll -> lists = malloc(linkedListsNumber * sizeof(LinkedList));
int index;
for (index = 0; index < linkedListsNumber; index++) {
kll -> lists [index] = createList();
}
return kll;
}
void insertFirst(int index, KLinkedList * kll, int key) {
Node * nd = malloc(sizeof(Node));
nd -> key = key;
nd -> next = kll -> lists[index] -> head;
if (isEmpty(kll -> lists[index]))
kll -> lists[index] -> tail = nd;
kll -> lists[index] -> head = nd;
}
void insertLast(int index, KLinkedList * kll, int key) {
if (isEmpty(kll -> lists[index]))
insertFirst(index, kll, key);
else {
Node * nd = malloc(sizeof(Node));
nd -> key = key;
nd -> next = NULL;
kll -> lists[index] -> tail -> next = nd;
kll -> lists[index] -> tail = nd;
}
}
void removeHead(int index, KLinkedList * kll) {
Node * nd;
nd = kll -> lists[index] -> head;
kll -> lists[index] -> head = nd -> next;
free(nd);
if (kll -> lists[index] -> head == NULL)
kll -> lists[index] -> tail = NULL;
}
void removeTail(int index, KLinkedList * kll) {
Node * nd;
if (kll -> lists[index] -> head == kll -> lists[index] -> tail)
removeHead(index, kll);
else {
nd = kll -> lists[index] -> head;
while(nd ->next != kll -> lists[index] -> tail)
nd = nd -> next;
nd -> next = NULL;
free(kll -> lists[index] -> tail);
kll -> lists[index] -> tail = nd;
}
}
int main() {
return 0;
}
// Node of a linked list.
struct Node {
int key;
struct Node * next; // Pointer to next node in LL.
};
struct LinkedList {
struct Node * head;
struct Node * tail;
};
struct KLinkedList {
int size;
struct LinkedList ** lists;
};
typedef struct Node Node;
typedef struct LinkedList LinkedList;
typedef struct KLinkedList KLinkedList;
// Helper Functions Prototype:
void show(Node * nd); // Higher order function to be used in traverse: show node's key.
// Function Prototype or method on linked list:
LinkedList * createList(void); // Initiate a linked list.
unsigned isEmpty(LinkedList * ll); // Check if a linked list is empty or no.
void insertAfter(Node * nd, int key); // Add a node after a given node.
void insertBefore(Node * nd, int key); // Add a node before a given node.
Node * findNode(Node * nd, int key); // Check if a given key exist or no.
void traverse(Node * nd, void (*callback)(Node * nd)); // Traverse the linked list starting from a given Node.
void removeSuccessor(Node * nd); // Remove the successor of a Node.
void removeNode(Node * nd); // Remove Node from the list.
// Function Prototype or method on k linked list:
KLinkedList * createKList(int linkedListsNumber); // create k linked list.
void insertFirst(int index, KLinkedList * kll, int key); // Add a node in the beginning of list X.
void insertLast(int index, KLinkedList * kll, int key); // Add a node in the end of list X.
void removeHead(int index, KLinkedList * kll); // Remove a node from the beginning of list X.
void removeTail(int index, KLinkedList * kll); // Remove a node from the end of list X.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment