I tried to create several functions for linked lists. Here are all of them, but i have problem with function delete_All and delete.
delete_All need to delete all elements with given value, and delete only first.
The first mistake that i cannt find is free(_) (marked in code as "HERE!!!") and also endless program work in delete_All when we put in head of Linked List and at the same time in the next element value which needs to be removed.
For example (1,1,2,3,4,) cannt remove first pair "1". delete and some other functions cannt use free function.
#include <stdlib.h>
#include <stdio.h>
struct uzel {
int n;
struct uzel *next;
};
struct uzel *
initializef(struct uzel *head)
{
static int c = 0;
int a;
printf("uzel[%d] = ", c);
scanf("%d", &a);
if (a == 0) {
head->next = NULL;
}
else {
c++;
head->n = a;
head->next = malloc(sizeof(struct uzel));
initializef(head->next);
}
return head;
}
void
gprint(struct uzel *head)
{
printf("(");
while (head->next != NULL) {
printf("%d ", head->n);
head = head->next;
}
printf(")\n");
}
struct uzel *
delete(struct uzel *head, int val)
{
if (head->n == val) {
struct uzel *newhead = head;
newhead = head->next;
struct uzel *del = head;
// free(del);//HERE!!!!!!!!!
return newhead;
}
struct uzel *right = head;
struct uzel *left = right;
while (left->next) {
right = left->next;
if (right->next != NULL) {
if (right->n == val) {
struct uzel *del = right;
left->next = right->next;
free(del);
}
} // but here is ok!!!
left = left->next;
}
return head;
}
struct uzel *
delete_all(struct uzel *head, int val)
{
struct uzel *newhead = head;
while (newhead->n == val) {
newhead = head->next;
// struct uzel* del=head;//here!!!!!!!!!!!!!!
// free(del);
if ((newhead->n) == NULL) {
printf("No more elements to work with,ERROR");
return 0;
}
}
struct uzel *right = newhead;
struct uzel *left = newhead;
while (left->next) {
right = right->next;
if (right->n == val) {
struct uzel *tmp = right;
left->next = right->next;
free(tmp);
}
else {
left = left->next;
}
}
return newhead;
}
void
addAfter(int info, int after_what, struct uzel *head)
{
struct uzel *left = head;
struct uzel *right = head;
while ((left->n) != after_what) {
right = right->next;
left = left->next;
}
right = right->next;
struct uzel *newelem = malloc(sizeof(struct uzel));
newelem->n = info;
newelem->next = right;
left->next = newelem;
}
void
addAfterALL(int info, int after_what, struct uzel *head)
{
struct uzel *left = head;
struct uzel *right = head;
while ((right->next)) {
while (((left->n) != after_what)) {
right = right->next;
left = left->next;
}
right = right->next;
struct uzel *newelem = malloc(sizeof(struct uzel));
newelem->n = info;
newelem->next = right;
left->next = newelem;
}
}
int
main()
{
int a, b;
struct uzel head2;
struct uzel *mainhead1 = initializef(&head2);
gprint(mainhead1);
printf("Enter a number to delete: ");
scanf("%d", &a);
printf("Delete all %d ? If yes enter 2 if no enter 1: ", a);
scanf("%d", &b);
if (b == 2) {
struct uzel *mainhead = delete_all(mainhead1, a);
gprint(mainhead);
}
else {
struct uzel *mainhead = delete(mainhead1, a);
gprint(mainhead);
}
printf("Enter after what number u want to insert smth: ");
int r;
scanf("%d", &r);
printf("Enter what number u want to insert: ");
int u;
scanf("%d", &u);
printf("After all numbers %d ? If yes enter 1 If no enter 2:", r);
int g;
scanf("%d", &g);
if (g == 2) {
addAfter(u, r, mainhead1);
gprint(mainhead1);
}
if (g == 1) {
addAfterALL(u, r, mainhead1);
gprint(mainhead1);
}
}