fork download
  1. //Implementation file//
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. struct list
  6. {
  7. char *item;
  8. struct list* next;
  9. };
  10. typedef struct list LIST; //aliasing the structure//
  11. LIST *create() //constrcutor//
  12. {
  13. LIST *L = (LIST*)malloc(sizeof (LIST));
  14. L->next = 0;
  15. return L;
  16. }
  17. LIST* insert(LIST* L, char *newitem)
  18. {
  19. L->item=(char*)malloc(sizeof(char)*strlen(newitem));
  20. strcpy(L->item, newitem);
  21. LIST *newL = (LIST*)malloc(sizeof (LIST));
  22. newL->next = 0;
  23. L->next = newL;
  24. return newL;
  25. }
  26. void display(LIST *L)
  27. {
  28. int i;
  29. for(i=0; L->next != 0;L = L->next) // Assumes there's at least 1 item
  30. {
  31. printf("Elements are: %s\n", L->item);
  32. }
  33. }
  34. int main ()
  35. {
  36. LIST *startElement;
  37. startElement=create();
  38. LIST *lastElement = insert(startElement, "America");
  39. lastElement = insert(lastElement, "Brazil");
  40. display(startElement);
  41. // Free all memory.. exercise :)
  42. return 0;
  43. }
Success #stdin #stdout 0s 2424KB
stdin
Standard input is empty
stdout
Elements are: America
Elements are: Brazil