I have a problem with my double conected list I have to add 2 functions. First which adds element on specified position (if position does not exsist it has to add an element at the end of list), example:
Elements: 1 2 3
Add on position (we start numbering from 0): 1 element: 21
Elements after adding: 1 21 2 3
Second which deletes element on specified position (if position does not exsist it has to do nothing), example:
Elements: 1 2 3
Delete position: 1
Elements after deleting: 1 3
Here's what I've tried to write, the problem is that my program is crashing when I try to add or delete element to/from list. Thank you for your time and help!
#include <stdio.h>
#include <stdlib.h>
struct ellist2c {
int x;
struct ellist2c * right;
struct ellist2c * left;
};
struct list2c {
struct ellist2c * begining;
struct ellist2c * end;
int position;
};
void push(int newdata, struct list2c * list) {
struct ellist2c * newEl = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
newEl -> x = newdata;
newEl -> left= NULL;
if (list -> position == 0) {
list -> begining= newEl;
list -> end= newEl;
newEl -> right= NULL;
} else {
newEl -> right= list -> end;
list -> end-> left= newEl;
list -> end= newEl;
}
list -> position++;
}
void pushonposition(int newData, int poz, struct list2c * list){
struct ellist2c * sk = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
sk -> x = newData;
struct ellist2c * el = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
el = list -> begining;
if (poz == 0) {
el -> x = newData;
if (list -> begining == NULL) {
el -> right = NULL;
el -> left = NULL;
list -> begining = el;
list -> end = el;
} else {
el -> left = NULL;
list -> begining -> left = el;
el -> right = list -> begining;
list -> begining = el;
}
list -> position++;
return;
}
if (poz >= list -> position - 1) {
el -> x = newData;
if (list -> begining == NULL) {
el -> right = NULL;
el -> left = NULL;
list -> begining = el;
list -> end = el;
} else {
el -> right = NULL;
el -> left = list -> end;
list -> end -> right = el;
list -> end = el;
}
list -> position++;
return;
}
for (int i = 0; i < poz; i++) {
el = el -> right;
}
sk -> left = el -> left;
struct ellist2c * before = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
before = list -> begining;
for (int i = 0; i < poz - 1; i++) {
before = before -> right;
}
sk -> right = before -> right;
before -> right = sk;
sk -> right -> left = sk;
}
void popposition(struct list2c * list, int index) {
struct ellist2c * el = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
if (list -> begining == NULL) {
return;
}
if (index == 0) {
if (list -> begining == NULL) {
return;
}
if (list -> begining == list -> end) {
el = list -> end;
free(el);
list -> end = NULL;
list -> begining = NULL;
printf("Empty list");
} else {
el = list -> end;
list -> end = el -> left;
free(el);
list -> end -> right = NULL;
}
list -> position--;
return;
}
el = list -> begining;
for (int i = 0; i < index; i++) {
el = el -> right;
if (el == NULL) {
return;
}
}
if (el != list -> end && el != list -> begining) {
el -> right -> left = el -> left;
el -> left -> right = el -> right;
free(el);
return;
}
if (el == list -> end) {
list -> end = el -> left;
list -> end -> right = NULL;
free(el);
return;
}
if (el == list -> begining) {
list -> begining = el -> right;
list -> begining -> left = NULL;
free(el);
return;
}}
void print(struct list2c * list) {
struct ellist2c * el = (struct ellist2c * ) malloc(sizeof(struct ellist2c ));
el = list -> begining;
while (el) {
printf("%d\n", el -> x);
el = el -> left;
}
}
int main(){
struct list2c * list = (struct list2c * ) malloc(sizeof(struct list2c ));
list -> begining= NULL;
list -> end = NULL;
list -> position = 0;
push(5, list);
push(6, list);
push(7, list);
pushonposition(1, 1, list);
pushonposition(1, 1, list);
popposition(1, list);
print(list);
return (0);
}