Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define LINE_SIZE 128
  6.  
  7. struct node {
  8.     char *data;
  9.     struct node* next;
  10. };
  11.  
  12. struct node* push(struct node* head, char *data) {
  13.     struct node* tmp;
  14.     if ((tmp = (struct node*)malloc(sizeof(struct node))) == NULL) {
  15.         printf("memory error\n");
  16.         exit(-1);
  17.     }
  18.     tmp->data = data;
  19.     tmp->next = head; //head = NULL -> tmp->next=NULL
  20.     head = tmp;
  21.     return head;
  22. }
  23.  
  24. void display(struct node* head) {
  25.     struct node* p;
  26.  
  27.     while (head != NULL) {
  28.         p = head;
  29.         printf("%s\n", head->data);
  30.         head = head->next;
  31.         free(p->data);
  32.         free(p);
  33.     }
  34. }
  35.  
  36. int main() {
  37.     struct node* head = NULL;
  38.     char c;
  39.     char *str;
  40.     char buf[LINE_SIZE];
  41.     int i = 0;
  42.     while ((c = getchar()) != EOF) {
  43.         if ((c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a')) {
  44.             buf[i] = c;
  45.             i++;
  46.         }
  47.         if (c == '\n' || c == ' ' || c == '\t') {
  48.             buf[i] = '\0';
  49.             if ((str = (char *)malloc(i + 1)) == NULL) {
  50.                 printf("memory error");
  51.                 exit(-1);
  52.             }
  53.             strcpy(str, buf);
  54.             head = push(head, str);
  55.             i = 0;
  56.         }
  57.     }
  58.     display(head);
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement