Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define LINE_SIZE 128
- struct node {
- char *data;
- struct node* next;
- };
- struct node* push(struct node* head, char *data) {
- struct node* tmp;
- if ((tmp = (struct node*)malloc(sizeof(struct node))) == NULL) {
- printf("memory error\n");
- exit(-1);
- }
- tmp->data = data;
- tmp->next = head; //head = NULL -> tmp->next=NULL
- head = tmp;
- return head;
- }
- void display(struct node* head) {
- struct node* p;
- while (head != NULL) {
- p = head;
- printf("%s\n", head->data);
- head = head->next;
- free(p->data);
- free(p);
- }
- }
- int main() {
- struct node* head = NULL;
- char c;
- char *str;
- char buf[LINE_SIZE];
- int i = 0;
- while ((c = getchar()) != EOF) {
- if ((c <= 'Z' && c >= 'A') || (c <= 'z' && c >= 'a')) {
- buf[i] = c;
- i++;
- }
- if (c == '\n' || c == ' ' || c == '\t') {
- buf[i] = '\0';
- if ((str = (char *)malloc(i + 1)) == NULL) {
- printf("memory error");
- exit(-1);
- }
- strcpy(str, buf);
- head = push(head, str);
- i = 0;
- }
- }
- display(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement