add linked lists and stack implementations from my old repos

repos deleted after uploading here
c
EmaMaker 2020-12-13 18:48:09 +01:00
parent 91594a088d
commit ab6e612fa6
5 changed files with 327 additions and 0 deletions

BIN
LinkedLists/linkedlists Executable file

Binary file not shown.

177
LinkedLists/linkedlists.c Normal file
View File

@ -0,0 +1,177 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "linkedlists.h"
//Add value and index to the first value
void initLLI(lli* l, int v) {
l->index = 0;
l->value = v;
l->next = NULL;
l->prev = NULL;
// printf("Puzzette\n");
}
//Appends new value as last one in the list
void pushToLLI(lli* l, int v){
lli* l1 = l;
//get to the last "index", although we can't really speak about indeces
while(l1->next != NULL){
l1 = l1->next;
}
//now create new struct and "append" to the others
lli* lnew = malloc(sizeof(lli));
l1->next = lnew;
lnew->prev = l1;
lnew->next = NULL;
lnew->value = v;
lnew->index = l1->index +1;
}
//Deletes last value
void popFromLLI(lli* l){
lli* l1 = l;
//get to the last "index", although we can't really speak about indeces
while(l1->next != NULL){
l1 = l1->next;
}
lli* l2 = l1->prev;
l2->next = NULL;
free(l1);
}
//Adds the value at a certain index, pushing the other values towards the end
void addAtIndexLLI(lli* l, int i, int v){
if(i > sizeOfLLI(l)){
printf("Index %d too high for list with size %d\n", i, sizeOfLLI(l));
return;
}
if(i == sizeOfLLI(l)){
pushToLLI(l, v);
return;
}
//get to the index
lli* l1 = l;
while(l1->next->index != i){
l1 = l1->next;
}
lli* l0 = l1->prev;
lli* lnew = malloc(sizeof(lli));
lnew->index = i;
lnew->prev = l0;
lnew->next = l1;
lnew->value=v;
l0->next = lnew;
l1->prev = lnew;
//now fix the indices or there will be problems with size
while(l1 != NULL){
l1->index = l1->prev->index +1;
l1 = l1->next;
}
}
//Remove the value at a certain index
void removeAtIndexLLI(lli* l, int i){
if(i >= sizeOfLLI(l)){
printf("Index %d too high for list with size %d\n", i, sizeOfLLI(l));
return;
}
//get to the index
lli* l1 = l;
while(l1->index != i){
l1 = l1->next;
}
lli* l0 = l1->prev;
lli* l2 = l1->next;
free(l1);
l0->next = l2;
l2->prev = l0;
//now fix the indices or there will be problems with size
while(l2 != NULL){
l2->index = l2->prev->index +1;
l2 = l2->next;
}
}
//Prints whole list
void printLLI(lli* l, bool b){
lli* l1 = l;
printf("(index)value\n");
while(l1 != NULL){
printf("(%d)%d\t", l1->index, l1->value);
l1 = l1->next;
}
printf("\n");
if(b) printf("Size: %d\n", sizeOfLLI(l));
}
int sizeOfLLI(lli* l){
lli* l1 = l;
//get to the last "index", although we can't really speak about indeces
while(l1->next != NULL){
l1 = l1->next;
}
return l1->index + 1;
}
int valueAtIndexLLI(lli* l, int i){
if(i >= sizeOfLLI(l)){
printf("Index %d too high for list with size %d\n", i, sizeOfLLI(l));
return NULL;
}
//get to the index
lli* l1 = l;
while(l1->index != i){
l1 = l1->next;
}
return l1->value;
}
void reverseLLI(lli* l){
lli* ll = l;
//get to the last "index", although we can't really speak about indeces
while(ll->next != NULL){
ll = ll->next;
}
lli* lp = ll->prev;
lli* ln = ll;
ln->prev = NULL;
initLLI(ln, ll->value);
while(lp != NULL){
pushToLLI(ln, lp->value);
lp=lp->prev;
}
free(lp);
*l = *ln;
}

19
LinkedLists/linkedlists.h Normal file
View File

@ -0,0 +1,19 @@
#include <stdbool.h>
typedef struct linkedlistI{
struct linkedlistI *next;
struct linkedlistI *prev;
int index;
int value;
} lli;
void initLLI(lli*, int v);
void pushToLLI(lli*, int v);
void popFromLLI(lli*);
void printLLI(lli*, bool);
void addAtIndexLLI(lli*, int i, int v);
void removeAtIndexLLI(lli*, int i);
void reverseLLI(lli*);
int valueAtIndexLLI(lli*, int i);
int sizeOfLLI(lli*);

65
LinkedLists/main.c Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlists.h"
//Init the linked list. Basically you are creating only the first element
lli list;
lli* plist = &list;
int main(){
//Here's the real init
initLLI(plist, 5);
//Add a bunch of values
pushToLLI(plist, 312);
pushToLLI(plist, 3142);
pushToLLI(plist, 3115);
pushToLLI(plist, 762);
pushToLLI(plist, 99);
pushToLLI(plist, 544);
pushToLLI(plist, 66);
//Print
printf("List created and values pushed\n");
printLLI(plist, true);
//Pop last value
popFromLLI(plist);
//Print Again
printf("\nPopped last value\n");
printLLI(plist, true);
//Remove and index 3
removeAtIndexLLI(plist, 3);
//Remove and inexistent index
removeAtIndexLLI(plist, 8);
//Print Again
printf("\nRemoved element at index 3. Tried to remove element at index 8 but list was too small\n");
printLLI(plist, true);
//Add but with error
addAtIndexLLI(plist, 7, 75);
//Add like if pushing
addAtIndexLLI(plist, 6, 74);
//Add in the middle
addAtIndexLLI(plist, 3, 32);
//Print Again
printf("\nAdded values at defined indeces. First one was threated like if it was pushed at the end of the list. Second one couldn't be added because the list is too small and last one was added ad defind index\n");
printLLI(plist, true);
//Reverse
reverseLLI(plist);
//Print Again
printf("\nReversed the list\n");
printLLI(plist, true);
}

66
Stack/stack.c Normal file
View File

@ -0,0 +1,66 @@
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 5
void push(int);
void pop();
void print();
int get();
typedef struct stack{
int values[STACK_SIZE];
int cv;
} bufferStack;
bufferStack stack;
bufferStack* pstack = &stack;
int main(){
pstack->cv = 0;
print();
for(int i = 0; i < STACK_SIZE; i++){
push(i);
// print();
printf("%d\n", get());
}
for(int i = 0; i < STACK_SIZE; i++){
// print();
pop();
printf("%d\n", get());
}
}
void push(int v){
if(pstack->cv < STACK_SIZE && pstack->cv >= 0){
pstack->values[pstack->cv] = v;
pstack->cv++;
// if(pstack->cv >= STACK_SIZE) pstack->cv--;
}
}
void pop(){
if(pstack->cv >= 0){
pstack->cv--;
pstack->values[pstack->cv] = 0;
}
}
int get(){
return pstack->values[pstack->cv-1];
}
void print(){
for(int i = 0; i < pstack->cv; i++){
printf("%d\t", pstack->values[i]);
}
printf("\n");
}