Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <stdlib.h>
- #define NMAX 1001
- void checkAlloc(const void *const p) {
- if (p == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- }
- typedef struct Node {
- char text[NMAX];
- struct Node *prev, *next;
- } Node;
- Node *newNode(const char s[NMAX]) {
- Node *node = malloc(sizeof(Node));
- checkAlloc(node);
- node->next = node->prev = NULL;
- strcpy(node->text, s);
- return node;
- }
- typedef struct {
- Node *first, *last;
- } List;
- void initList(List *list) {
- list->first = list->last = NULL;
- }
- void printList(const List *const list) {
- for (Node *it = list->first; it; it = it->next) {
- fputs(it->text, stdout);
- }
- fputc('\n', stdout);
- }
- void freeList(List *list) {
- Node *next;
- for (Node *it = list->first; it; it = next) {
- next = it->next;
- free(it);
- }
- }
- void addEnd(List *list, const char text[NMAX]) {
- Node *node = newNode(text);
- if (!list->last) {
- list->first = list->last = node;
- return;
- }
- list->last->next = node;
- node->prev = list->last;
- list->last = node;
- }
- void moveLastAtStart(List *list) {
- if (!list->first || list->first == list->last) {
- return;
- }
- Node *last = list->last;
- list->last->prev->next = NULL;
- last->prev = NULL;
- last->next = list->first;
- list->first = last;
- }
- int main() {
- List list;
- initList(&list);
- int n;
- scanf("%d", &n);
- getchar();
- static char s[NMAX];
- for (int i = 0; i < n; i++) {
- if (fgets(s, NMAX, stdin) == NULL) {
- perror("");
- exit(1);
- }
- addEnd(&list, s);
- }
- moveLastAtStart(&list);
- printList(&list);
- freeList(&list);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement