#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct listitem
{
int data;
struct listitem* next;
};
typedef struct listitem Listitem;
struct list
{
Listitem* head;
};
typedef struct list List;
#define FIRST_ITEM_INDEX 1
void insertAt(List*, int index, int val);
void removeAllByVal(List*, int val);
void removeFrom(List*, int index);
bool isListEmpty(List*);
Listitem* getItemFrom(List*, int index);
void printList(List*);
void printMenu();
void clear();
int main()
{
List* list = (List*)malloc(sizeof(List));
char* action = (char*)malloc(sizeof(char)*256);
int index;
int val;
for(;;) {
printf("\n");
printMenu();
scanf("%s", action);
printf("\n");
if (0 == strcmp("1", action)) {
printf("pozycja: ");
scanf("%d", &index);
printf("wartosc: ");
scanf("%d", &val);
insertAt(list, index, val);
} else if (0 == strcmp("2", action)) {
printf("wartosc: ");
scanf("%d", &val);
removeAllByVal(list, val);
} else if (0 == strcmp("3", action)) {
printf("pozycja: ");
scanf("%d", &index);
removeFrom(list, index);
} else if (0 == strcmp("4", action)) {
printList(list);
} else if (0 == strcmp("5", action)) {
clear();
}
}
return 0;
}
bool isListEmpty(List* list)
{
return (NULL == list->head);
}
Listitem* getItemFrom(List* list, int index)
{
Listitem* item = list->head;
if (index < FIRST_ITEM_INDEX) {
return NULL;
}
int i;
for (i = FIRST_ITEM_INDEX + 1; i <= index; i++) {
if (NULL == item) {
break;
} else {
item = item->next;
}
}
return item;
}
void insertAt(List* list, int index, int val)
{
Listitem *previousItem;
previousItem = getItemFrom(list, index - 1);
Listitem *newItem = (Listitem*)malloc(sizeof(Listitem));
newItem->data = val;
if (FIRST_ITEM_INDEX == index) {
if (!isListEmpty(list)) {
newItem->next = list->head;
}
list->head = newItem;
} else if (NULL != previousItem) {
newItem->next = previousItem->next;
previousItem->next = newItem;
}
}
void removeFrom(List* list, int index)
{
if (isListEmpty(list)) {
return;
}
Listitem *previousItem;
previousItem = getItemFrom(list, index - 1);
Listitem *tmp;
if (FIRST_ITEM_INDEX == index) {
tmp = list->head;
list->head = tmp->next;
free(tmp);
} else if (NULL != previousItem && NULL != previousItem->next) {
tmp = previousItem->next;
previousItem->next = tmp->next;
free(tmp);
}
}
void removeAllByVal(List* list, int val)
{
Listitem* currentItem = list->head;
Listitem* nextItem;
while(NULL != currentItem) {
nextItem = currentItem->next;
if (val == currentItem->data) {
list->head = nextItem;
free(currentItem);
currentItem = list->head;
continue;
}
if (NULL != nextItem && val == nextItem->data) {
currentItem->next = nextItem->next;
free(nextItem);
}
currentItem = currentItem->next;
}
}
void printList(List* list)
{
int i;
Listitem* c;
for(i = FIRST_ITEM_INDEX, c = list->head; NULL != c; c = c->next, i++) {
printf(“pozycja = %d\twartosc = %d\n”, i, c->data);
}
}
void printMenu()
{
printf(“1) Wstaw element na zadana pozycje\n”);
printf(“2) Usun wszystkie elementy o podanej wartosci\n”);
printf(“3) Usun element z zadanej pozycji\n”);
printf(“4) Wyswietl liste\n”);
printf(“5) Czysc ekran\n”);
printf("Wybierz akcję (cyfra): ");
}
void clear()
{
int l = 24;
while(–l) {
puts("");
}
}