Created
May 29, 2018 05:28
-
-
Save iamaamir/d25ee7a979252dfa6e16dc7f255736c4 to your computer and use it in GitHub Desktop.
basic implementation of linkedlist in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
// const | |
#define AtBeginning 1 | |
#define AtEnd 0 | |
struct Node | |
{ | |
int data; | |
struct Node *next; | |
}; | |
//get a random number | |
int once = 1; | |
int randomValue(){ | |
if (once){ | |
srand(time(NULL)); // should only be called once | |
once = !once; | |
} | |
return rand(); | |
} | |
struct Node* creatNewNode(){ | |
return (struct Node*) malloc(sizeof(struct Node)); | |
} | |
void printList(struct Node* list){ | |
while(list != NULL){ | |
printf("%d\n", list->data); | |
list = list->next; | |
} | |
} | |
void insertAtBeginning(struct Node** head, int data){ | |
struct Node* new_node = creatNewNode(); | |
new_node->data = data; | |
/* Make next of new node as head */ | |
new_node->next = (*head); | |
/* move the head to point to the new node */ | |
(*head) = new_node; | |
} | |
void insertAtEnd(struct Node** head, int data){ | |
struct Node* new_node = creatNewNode(); | |
new_node->data = data; | |
struct Node* head_ref = *head; | |
/*new_node is going to be the last node, | |
so make next of it as NULL*/ | |
new_node->next = NULL; | |
//what if the list is empty | |
if (head_ref == NULL) | |
{ | |
//we are alone so we are the head | |
*head = new_node; | |
} | |
// take out the last one | |
while(head_ref->next !=NULL) head_ref = head_ref->next; | |
//grab your seat | |
head_ref->next = new_node; | |
} | |
void insertAfter(struct Node** node, int data, int where){ | |
struct Node* node_ref = *node; | |
// what if the given is the last node | |
if (node == NULL) | |
{ | |
// insertAtEnd(node, data); | |
return; | |
} | |
struct Node* new_node = creatNewNode(); | |
new_node->data = data; | |
// Make next of new node as next of prev node | |
new_node->next = node_ref->next; | |
//grab the seat | |
node_ref->next = new_node; | |
} | |
void insert(int where, struct Node** head, int data){ | |
// 1 - Insert at Beginning | |
if (where == AtBeginning) insertAtBeginning(head, data); | |
// 0 - Insert at End | |
if (where == AtEnd) insertAtEnd(head, data); | |
//else inset at the given positoin | |
} | |
int main() | |
{ | |
struct Node* head = creatNewNode(); | |
struct Node* second = creatNewNode(); | |
head->data = 1; | |
head->next = second; | |
second->data = 2; | |
second->next = NULL; | |
printf("original\n"); | |
printList(head); | |
insert(AtBeginning, &head, randomValue()); | |
printf("AtBeginning\n"); | |
printList(head); | |
insert(AtEnd, &head, randomValue()); | |
printf("AtEnd\n"); | |
printList(head); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment