Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct _strList {
  6.     char *str;
  7.     struct _strList *next;
  8. } strList;
  9.  
  10. typedef struct _List {
  11.     strList *begin;
  12.     strList *end;
  13. } List;
  14.  
  15. strList *list_insert(List *lst, char *s) {
  16.     strList *new_cell = (strList*) malloc(sizeof(strList));
  17.     new_cell->str = strdup(s);
  18.     new_cell->next = lst->begin;
  19.  
  20.     lst->begin = new_cell;
  21.     if(list->end == NULL)
  22.         lst->end = new_cell;
  23.  
  24.     return lst;
  25. }
  26.  
  27. strList *list_append(strList *lst, char *s) {
  28.     strList *new_cell = (strList*) malloc(sizeof(strList));
  29.     new_cell->str = strdup(s);
  30.     new_cell->next = NULL;
  31.  
  32.     if (lst) {
  33.         strList *aux;
  34.         for ( aux = lst ; aux->next ; aux = aux->next ) {}
  35.         aux->next = new_cell;
  36.     } else {
  37.         lst = new_cell;
  38.     }
  39.     return lst;
  40. }
  41.  
  42. void show_list(List *lst) {
  43.     strList *aux = lst->begin;
  44.  
  45.     for( ; aux ; aux = aux->next){
  46.         printf("%s\n", aux->str);
  47.     }
  48. }
  49.  
  50. int main() {
  51.     List *lst = (List*) malloc(sizeof(List));
  52.     lst->begin = lst->end = NULL;
  53.  
  54.     lst = list_insert(lst, "Ola");
  55.     lst = list_insert(lst, "Bah");
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement