• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

C linked list

No3x

Mitglied
Hi! Ich hab ein paar Probleme mite meiner linked list. Das verketten klappt nicht so ganz, ich weiß nicht genau wie ich das erste item verlinken muss. Pointer sind auch eher neu, dafür diese Übung.

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

typedef struct item {
    struct item *next;
    int id;
    
} item;

typedef struct List {
    item* first;
} List;

/* Add item to list */
void addElement(List* list, int id) {
    /* get first item */
    item* p = list->first;
    
    /* go to the end of the list */
    while (p != NULL) {
        p = malloc(sizeof(item));
        p = p->next;
    }
    
    /* create new item */
    p = malloc(sizeof(item));
    p->next = NULL;
    p->id = id;
    
    /* if this is the first item we'll recognize that */
    if(list->first == NULL) {
        list->first = p;
    }
    
    printf("added item with id %d at %i\n", id, &p);
    printf("now are %d elements in the list\n", countItemsfromList(list)); 
}

/* Print all items */
void printList(List* list) {
    /* get first item */
    item* p = list->first;
    
    /* go to the end of the list */
    while (p != NULL) {
        /* print the id of the curren item */
        printf("%i\n",     p->id);
        p = p->next;
    }
}

/* get an specific item from list per key */
int getItemfromList(List* list, int cid) {
    /* get first item */
    item* p = list->first;
    
    /* go to the end of the list */
    while (p != NULL) {
        /* if the key applies to these we'll return the key */
        if (p->id == cid) {
            return p->id;
        }
        p = p->next;
    }
    return 0;
}

/* returns the number of items in the list */
int countItemsfromList(List* list) {
    /* get first item */
    item* p = list->first;
    int count = 1;
    
    /* go to the end of the list */
    while (p != NULL) {
        p = p->next;
        count++;
    }
    return count;
}

int main (int argc, const char * argv[]) {
    int result = 0;
    struct List *liste = (List*)malloc(sizeof(List));    

    addElement(liste, 1);    
    addElement(liste, 2);
    addElement(liste, 3);


    result = getItemfromList(liste, 3);
    printf("ID ist : %d", result);
    printList(liste);

}

Im Grunde genommen ist erst einmal nur die addElement Funktion von Interesse.
first verweist auf das erste Element (in diesem fallmit der id 1), jedoch bleibt p->next leer, da mir das nächste item ja noch nicht bekannt ist... Da komm ich gerade nicht zurecht.
Kann mir jemand helfen?

Erledigt.
Code:
#include <stdio.h>
#include <stdlib.h>

typedef struct item {
  struct item *next;
  int id;
} item;

typedef struct List {
  item* head;
} List;

/* Add item to list */
void addElement(List* list, int id) {
  
  /* create new item */
  item* newitem = malloc(sizeof(item));
  newitem->next = NULL;
  newitem->id = id;
  
  /* find the end */
  if(list->head == NULL) {
    list->head = newitem;
    printf("added head item with id %d at %p\n", newitem->id, newitem);
  } else {
    item* last = list->head;
    /* go to the end of the list */
    while (last->next != NULL) {
      last = last->next;
    }
    last->next = newitem;
    printf("added item with id %d at %p - my previous is %p\n", newitem->id,  last->next,last);
  }
  printf("now are %d elements in the list\n", countItemsfromList(list)); 
}

/* returns the number of items in the list */
int countItemsfromList(List* list) {
  /* get head item */
  item* last = list->head;
  int count = 0;
  
  /* go to the end of the list */
  while (last != NULL) {
    last = last->next;
    count++;
  }
  return count;
}

int main (int argc, const char * argv[]) {
  
  struct List *liste = (List*)malloc(sizeof(List));  
  liste->head = NULL;
  
  addElement(liste, 1);  
  addElement(liste, 2);
  addElement(liste, 3);
  
  free(liste);
  return 0;
}
 
Zuletzt bearbeitet:
Werbung:
Zurück
Oben