Hi C Profis



  • Hi c profis!
    jung habe ein problem habe hier ein quellcode und wollte das gerne mit einer weiteren funktion erweitern doch mir fehlt das nötige wissen würde mich freuen wenn mir jemand helfen könnte!

    quellcode:

    #ifdef HAVE_CONFIG_H
    #include <config.h>
    #endif

    #include <stdio.h>
    #include <stdlib.h>

    struct nlist{
    struct nlist *next;
    char *name;
    };

    struct nlist *start=NULL;

    int lookup(char *s)
    {
    struct nlist *np;
    int i;

    for(np = start,i=1; np != NULL;np = np->next,i++){
    if(strcmp(s, np->name) == 0)
    return(i);
    }

    return(0);
    }

    void inclus(char* name)
    {
    struct nlist *np, *last;
    unsigned hashval;

    for(np = start; np != NULL; np = np->next){
    if(strcmp(name, np->name) == 0)
    break;
    }
    if(np == NULL){
    if(start == NULL){
    np = (struct nlist 😉 malloc(sizeof(*np));
    if (np == NULL || (np->name = strdup(name)) == NULL)
    return(NULL);
    np->next = NULL;
    start = np;
    }
    else{
    for(last = start; last->next != NULL;last = last->next)
    ;
    np = (struct nlist 😉 malloc(sizeof(*np));
    if (np == NULL || (np->name = strdup(name)) == NULL)
    return(NULL);
    np->next = NULL;
    last->next = np;
    }
    }

    }

    void main(void)
    {
    int befehl,ende,position;
    char eingabe[20];

    do{

    printf("Geben Sie einen Befehl ein 1 = Eintrag erstellen; 2 = Eintrag
    suchen :");
    scanf("%d",&befehl);

    printf("Geben Sie einen Namen ein :");
    scanf("%s",eingabe);

    if(befehl == 1)
    inclus(eingabe);

    if(befehl == 2){
    position = lookup(eingabe);
    if(position == 0)
    printf("\nElement nicht gefunden\n");
    else
    printf("\nElement an Position %d\n",position);
    }

    printf("Programm weiterlaufen lassen = 0 :");
    scanf("%d",&ende);

    }while(ende == 0);
    }

    hier soll bitte eine dritte funktion eingebaut werden der ermöglicht einen eintrag zu löschen.?



  • Wir sind hier kein Code Liefer Forum! Bitte stell nur Fragen nach Konkreten Problemen und such dir niemanden, der deine Aufgaben löst! 😡 :o

    Außerdem solltest du vielleicht Code-Tags benutzen :o

    Hier hast du ein paar Tipps

    du setzt das next von der struct vor der zu löschenden struct auf das next der zu löschenden struct, dann gibst du den Speicher der zu löschenden struct frei


Anmelden zum Antworten