#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "c.h"
void readfromkeyboard(Element_t *item)
{
if(!item)
{
fprintf(stderr, "Nullpointer");
exit(-1);
}
fprintf(stderr, "Waehle Song:");
fgets(item->songtitle, 255, stdin);
*strrchr(item->songtitle, '\n')=0;
fprintf(stderr, "Waehle interpreter:");
fgets(item->interpreter, 255, stdin);
*strrchr(item->interpreter, '\n')=0;
}
Element_t *allocateElement()
{
Element_t *elem = malloc(sizeof(Element_t));
if (elem == 0)
{
fprintf(stderr, "Error:␣no␣memory␣available!\n");
return 0;
}
readfromkeyboard(elem);
elem->next = 0;
return elem;
}
void freeList(Element_t **list)
{
Element_t *next;
Element_t *list2=*list;
while (list2 != 0)
{
next = list2->next;
free(list2);
list2=next;
}
list2=0;
}
Element_t *insertLast(Element_t *list)
{
Element_t *elem = allocateElement();
if (list != 0)
/* es wurde eine Liste uebergeben */
{
Element_t *end = find_end(list);
/* Ende finden */
end->next = elem;
/* neues Element an das Ende anfuegen
*/
}
else
list = elem;
/* elem ist neue Liste */
return list;
}
Element_t *find_end(Element_t *list)
{
while (list->next != 0)
list = list->next;
return list;
}
void print_singleElement(Element_t *element)
{
printf("%s-%s\n", element->songtitle, element->interpreter);
}
void print_entireList(Element_t *list)
{
printf("-------Liste-------\n");
long i=0;
while(list)
{
i++;
printf("%ld. ", i);
print_singleElement(list);
list = list->next;
}
printf("\nendd-------Liste-------");
}
/*void print_list(Element_t *list)
{
printf("----------␣Single␣Linked␣List␣---------\n");
while (list != 0)
{
printf("%ld\n", list->value);
list = list->next;
}
printf("---------------------------------------\n");
}
*/
void print_list_reverse(Element_t *list)
{
if(list->next!=0)
print_list_reverse(list->next);
print_singleElement(list);
//list=list->next;
}
Element_t *elementAt(Element_t *list, long index)
{
Element_t *l =list;
long i;
for(i=0; i<index-1; i++)
l=l->next;
return l;
}
long elementsCount(Element_t *list)
{
if(list==0)
return 0;
long i=1;
while(list->next)
{
list=list->next;
i++;
}
return i;
}
Element_t *removeFirst(Element_t *list)
{
Element_t *begin = list;
while (list != 0)
{
if (list->next != 0)
/* Das gesuchte Element ist erreicht */
{
begin = list->next;
/* Sichern des Rests der liste, falls */
/* das erste Element geloescht wird */
free(list);
/* Element freigeben */
return begin;
}
else
/* nicht gefunden */
{
list = list->next;
}
}
fprintf(stderr, "Error:␣element␣not␣found!\n");
return begin;
}
void insertAt(Element_t *list, long insertAfter)
{
Element_t *elem;
if (list != 0)
{
list = elementAt(list,insertAfter);
if (list != 0)
{
elem = allocateElement(); /* neues Element anlegen */
if (elem == 0)
{
fprintf(stderr, "Error:␣no␣memory␣allocated␣for␣push_front!\n");
// return list;
}
elem->next = list->next;
/* Nachfolger von list als Nachfolger */
/*
von elem setzen */
list->next = elem;
/* elem als Nachfolger von list festlegen */
}
else
fprintf(stderr, "Element␣not␣found\n");
}
}
void print_list_random(Element_t *list)
{
if( list->next)
{
if(rand()<266666)
{
print_list_random(list->next);
fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
}
else{
fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
print_list_random(list->next);}
}
else{
fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
}
}