Advertisement
Guest User

list.c

a guest
Oct 31st, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct element{
  5.   struct element *prev;
  6.   struct element *next;
  7.   char *text;
  8.  
  9. };
  10.   typedef struct element linked_list;
  11.  
  12.  
  13. linked_list empty_linked_list(){
  14.  
  15.   linked_list kopf  = {NULL,NULL,NULL};//malloc(sizeof(linked_list));
  16.   return kopf;
  17. }
  18.  
  19. int append(linked_list *liste,char* text){
  20.  
  21.  linked_list *ele = malloc(sizeof(linked_list));
  22.   if(ele != NULL){
  23.     ele->text = text;
  24.  
  25.     if(liste->next == NULL){//liste = leer
  26.       liste->prev = ele;
  27.       liste->next = ele;
  28.       //return 1;
  29.     }else{
  30.       liste->prev = ele; //letzte element
  31.       ele->prev = liste;//letzte element in der Liste
  32.       //return 11;
  33.     }
  34.     return 1;
  35.   }else{
  36.     return 0;
  37.   }
  38. }
  39.  
  40. void worka(char* t){
  41.   printf("%s\n",t);
  42. }
  43.  
  44. void visit_all(linked_list *liste, void (*work)(char* t)){
  45.  
  46.   while(liste->next != NULL){
  47.     work(liste->text);
  48.     //printf("%s\n",liste->text);
  49.     liste = liste->next;
  50.   }
  51.  
  52. }
  53.  
  54. int main(){
  55.  
  56.   linked_list li = empty_linked_list();
  57.  
  58.   char *bla = "hallo";
  59.   char *bla2 = "Welt";
  60.  
  61.   printf("%d\n", append(&li,bla));
  62.   printf("%d\n", append(&li,bla));
  63.   // printf("%d\n", insert(&li,bla2,2));
  64.    visit_all(&li,&worka);
  65.  
  66.   return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement