Last active
September 21, 2022 16:05
-
-
Save retraigo/4193f60e72d4f8288a4e52244dd25524 to your computer and use it in GitHub Desktop.
Linked list
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
// Doubl Linked-List | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Element { | |
char value; | |
struct Element *next; | |
struct Element *previous; | |
}; | |
struct Element *first; | |
void insert_end(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
newItem->previous = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
while (temp->next != NULL) { | |
temp = temp->next; | |
} | |
temp->next = newItem; | |
newItem->previous = temp; | |
} | |
} | |
void insert_beginning(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
newItem->previous = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
first = newItem; | |
first->next = temp; | |
temp->previous = first; | |
} | |
} | |
void insert_custom(char itemToInsert, int position) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} | |
struct Element *temp = first; | |
for (int i = 0; i < position && temp->next != NULL; ++i) { | |
temp = temp->next; | |
} | |
newItem->next = temp->next; | |
temp->next = newItem; | |
newItem->next->previous = newItem; | |
newItem->previous = temp; | |
} | |
void delete_end() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
while (temp->next->next != NULL) { | |
temp = temp->next; | |
} | |
struct Element *last = temp->next; | |
temp->next = NULL; | |
free(last); | |
} | |
} | |
void delete_beginning() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
first = first->next; | |
first->previous = NULL; | |
free(temp); | |
} | |
} | |
void delete_custom(int position) { | |
if (first == NULL) | |
return; | |
struct Element *temp = first; | |
for (int i = 0; i < (position - 1) && temp->next->next != NULL; ++i) { | |
temp = temp->next; | |
} | |
struct Element *last = temp->next; | |
temp->next = last->next; | |
temp->next->previous = temp; | |
free(last); | |
} |
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
// Kinda messed up this queue. Enqueues at the head and dequeues at the tail. | |
// Queue implemented with Linked-List | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Element { | |
char value; | |
struct Element *next; | |
}; | |
struct Element *first; | |
void enqueue(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
first = newItem; | |
first->next = temp; | |
} | |
} | |
void dequeue() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
while (temp->next->next != NULL) { | |
temp = temp->next; | |
} | |
struct Element *last = temp->next; | |
temp->next = NULL; | |
free(last); | |
} | |
} |
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
// Single Linked-List | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Element { | |
char value; | |
struct Element *next; | |
}; | |
struct Element *first; | |
void insert_end(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
while (temp->next != NULL) { | |
temp = temp->next; | |
} | |
temp->next = newItem; | |
} | |
} | |
void insert_beginning(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
first = newItem; | |
first->next = temp; | |
} | |
} | |
void insert_custom(char itemToInsert, int position) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} | |
struct Element *temp = first; | |
for (int i = 0; i < position && temp->next != NULL; ++i) { | |
temp = temp->next; | |
} | |
newItem->next = temp->next; | |
temp->next = newItem; | |
} | |
void delete_end() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
while (temp->next->next != NULL) { | |
temp = temp->next; | |
} | |
struct Element *last = temp->next; | |
temp->next = NULL; | |
free(last); | |
} | |
} | |
void delete_beginning() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
first = first->next; | |
free(temp); | |
} | |
} | |
void delete_custom(int position) { | |
if (first == NULL) | |
return; | |
struct Element *temp = first; | |
for (int i = 0; i < (position - 1) && temp->next->next != NULL; ++i) { | |
temp = temp->next; | |
} | |
struct Element *last = temp->next; | |
temp->next = last->next; | |
free(last); | |
} |
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
// Stack implemented with Linked List | |
#include <stdio.h> | |
#include <stdlib.h> | |
struct Element { | |
char value; | |
struct Element *next; | |
}; | |
struct Element *first; | |
void push(char itemToInsert) { | |
struct Element *newItem = (struct Element *)malloc(sizeof(struct Element)); | |
newItem->value = itemToInsert; | |
newItem->next = NULL; | |
if (first == NULL) { | |
first = newItem; | |
} else { | |
struct Element *temp = first; | |
first = newItem; | |
first->next = temp; | |
} | |
} | |
void pop() { | |
if (first == NULL) | |
return; | |
else { | |
struct Element *temp = first; | |
first = first->next; | |
free(temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment