This commit is contained in:
lukas 2020-04-28 21:25:03 +02:00
commit c3513b192b
3 changed files with 258 additions and 0 deletions

129
double_chained_functions.c Executable file
View File

@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdlib.h>
#include "double_chained_functions.h"
messdaten* insertFirst (messdaten* firstelem, messdaten* newelement)
{
if (firstelem != NULL)
{
newelement->next = firstelem;
newelement->prev = firstelem->prev;
firstelem->prev = newelement;
}
else
{
newelement->next = NULL;
newelement->prev = newelement;
}
return (newelement);
}
messdaten* insertLast (messdaten* firstelem, messdaten* newelement)
{
if (firstelem != NULL)
{
newelement->prev = firstelem->prev;
firstelem->prev = newelement;
newelement->prev->next = newelement;
newelement->next = NULL;
}
else
{
firstelem = insertFirst (firstelem, newelement);
}
return (firstelem);
}
messdaten* insertPos (messdaten* firstelem, messdaten* newelement, int pos)
{
int i = 0;
messdaten* temp = firstelem;
if (pos == 0)
{
firstelem = insertFirst (firstelem, newelement);
}
else
{
while (temp->next != NULL && i < (pos - 2))
{
temp = temp->next;
i++;
}
if (temp->next != NULL)
{
newelement->next = temp->next;
temp->next = newelement;
newelement->prev = temp->next->prev;
temp->next->prev = newelement;
}
else
{
firstelem = insertLast (firstelem, newelement);
fprintf (stderr,"llfunct:insertPos->Warning Position is not existing. Element is added to the end of the list!!\n");
}
}
return (firstelem);
}
messdaten* deleteFirst (messdaten* firstelem)
{
messdaten* n = firstelem;
firstelem = n->next;
firstelem->prev = n->prev;
free (n);
return (firstelem);
}
messdaten* deleteLast (messdaten* firstelem)
{
messdaten* temp = firstelem->prev;
firstelem->prev = firstelem->prev->prev;
free ((firstelem->prev->next));
firstelem->prev->next = NULL;
return (firstelem);
}
messdaten* deletePos (messdaten* firstelem, int pos)
{
int i = 0;
messdaten* temp = firstelem;
messdaten* tempsec;
while (i < (pos - 2))
{
temp = temp->next;
i++;
}
tempsec = temp->next->next;
tempsec->prev = temp;
free (temp->next);
temp->next = tempsec;
return (firstelem);
}
messdaten* deleteAll (messdaten* firstelem)
{
messdaten* temp;
while (firstelem->next != NULL)
{
temp = firstelem;
firstelem = temp->next;
free (temp);
}
return (firstelem);
}

74
double_chained_functions.h Executable file
View File

@ -0,0 +1,74 @@
/*
* double_chained_functions.h
*
* Created on: 14.11.2017
* Author: lukas
*/
#ifndef DOUBLE_CHAINED_FUNCTIONS_H_
#define DOUBLE_CHAINED_FUNCTIONS_H_
typedef struct mydata
{
char sensorname[10];
float messwert;
} mydata;
typedef struct messdaten
{
struct messdaten * next;
struct messdaten * prev;
struct mydata data;
} messdaten;
/* functon to insert the first element
* in: firstelem; newelement
* out: newelement
* changed: firstelem
*/
messdaten* insertFirst (messdaten* firstelem, messdaten* newelement);
/* functon to insert the last element
* in: firstelem; newelement
* out: newelement
* changed: lastelement
*/
messdaten* insertLast (messdaten* firstelem, messdaten* newelement);
/* functon to insert the element on a specific position
* in: firstelem; newelement; position
* out: newelement
* changed: one more item in chained list...
*/
messdaten* insertPos (messdaten* firstelem, messdaten* newelement, int pos);
/* functon to delete the first element
* in: firstelem;
* out: newelement
* changed: firstelem
*/
messdaten*
deleteFirst (messdaten* firstelem);
/* functon to delete the last element
* in: firstelem
* out: newelement
* changed: lastelement
*/
messdaten* deleteLast (messdaten* firstelem);
/* functon to delete the element of a specific element
* in: firstelem; newelement
* out: newelement
* changed: firstelem
*/
messdaten* deletePos (messdaten* firstelem, int pos);
/* functon to delete all the list elements
* in: firstelem
* out: newelement
* changed: the whole list
*/
messdaten* deleteAll (messdaten* firstelem);
#endif /* DOUBLE_CHAINED_FUNCTIONS_H_ */

55
double_chained_list.c Executable file
View File

@ -0,0 +1,55 @@
/*
============================================================================
Name : double_chained_list.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include "double_chained_functions.h"
int main (void)
{
messdaten * head;
messdaten * erste;
messdaten * zweite;
messdaten * dritte;
head = malloc (sizeof(messdaten));
erste = malloc (sizeof(messdaten));
zweite = malloc (sizeof(messdaten));
dritte = malloc (sizeof(messdaten));
if (head == NULL || erste == NULL || zweite == NULL || dritte == NULL)
{
return EXIT_FAILURE;
}
head = insertLast (head, dritte);
head = insertPos (head, erste, 1);
head = insertPos (head, zweite, 2);
erste->data.messwert = 42;
zweite->data.messwert = 2;
dritte->data.messwert = 3;
printf ("Messwert von head: >%f<\n", head->data.messwert);
printf ("Messwert von head next: >%f<\n", head->next->data.messwert);
printf ("Messwert von head next next: >%f<\n",
head->next->next->data.messwert);
printf ("Messwert von head next next: >%f<\n\n",head->next->next->next->data.messwert);
printf ("Messwert von head >%f<\n", head->data.messwert);
printf ("Messwert von erste: >%f<\n", erste->data.messwert);
printf ("Messwert von zweite: >%f<\n", zweite->data.messwert);
printf ("Messwert von zweite: >%f<\n", dritte->data.messwert);
deleteAll (head);
return EXIT_SUCCESS;
}