Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #define SIZE 100
  6.  
  7. struct node
  8. {
  9.     char info[SIZE];
  10.     struct node *next, *prev;
  11. };
  12.  
  13.  
  14.  
  15. void create(struct node **lista,char data[])
  16. {
  17.     struct node *temp;
  18.     temp = (struct node *)malloc(sizeof(struct node));
  19.  
  20.     if (*lista == NULL)
  21.     {
  22.         strcpy(temp->info,data);
  23.         temp->next=NULL;
  24.         temp->prev=NULL;
  25.         *lista=temp;
  26.     }
  27. }
  28.  
  29. void print_list_in_both_directions(struct node *list_pointer)
  30. {
  31.     struct node *backward_pointer = NULL;
  32.  
  33.     puts("");
  34.     puts("normal direction:");
  35.     while (list_pointer)
  36.     {
  37.         backward_pointer = list_pointer;
  38.         printf("%s", list_pointer->info);
  39.         list_pointer = list_pointer->next;
  40.     }
  41.     puts("");
  42.     puts("backward direction:");
  43.     while (backward_pointer)
  44.     {
  45.         printf("%s", backward_pointer->info);
  46.         backward_pointer = backward_pointer->prev;
  47.     }
  48. }
  49.  
  50. void insert_end(struct node **lista,char data[])
  51. {
  52.     struct node *ptr, *tempnode;
  53.     ptr = *lista;
  54.  
  55.     if(*lista==NULL)
  56.     {
  57.         create(lista,data);
  58.     }
  59.     else
  60.     {
  61.         while(ptr->next!=NULL)
  62.             ptr=ptr->next;
  63.  
  64.         tempnode=(struct node *)malloc(sizeof(struct node));
  65.         strcpy(tempnode->info,data);
  66.         tempnode->next=NULL;
  67.         tempnode->prev=ptr;
  68.         ptr->next=tempnode;
  69.     }
  70. }
  71.  
  72. int main()
  73. {
  74.  
  75.     struct node *lista=NULL;
  76.     char data[SIZE];
  77.     char str[]="koniec\n";
  78.  
  79.  
  80.     do
  81.     {
  82.         printf("wpisz slowo ");
  83.         fgets(data, SIZE, stdin);
  84.         insert_end(&lista,data);
  85.  
  86.     }
  87.     while(strcmp(data,str)!=0);
  88.  
  89.     print_list_in_both_directions(lista);
  90.  
  91.     return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement