Skip to content

Instantly share code, notes, and snippets.

@retraigo
Last active September 21, 2022 16:05
Show Gist options
  • Save retraigo/4193f60e72d4f8288a4e52244dd25524 to your computer and use it in GitHub Desktop.
Save retraigo/4193f60e72d4f8288a4e52244dd25524 to your computer and use it in GitHub Desktop.
Linked list
// 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);
}
// 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);
}
}
// 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);
}
// 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