Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct Node {
- int inf;
- struct Node *nxt;
- } Node;
- Node* newNode(int inf, Node *lst) {
- Node *node = malloc(sizeof(Node));
- if (!node) {
- perror("");
- exit(-1);
- }
- node->inf = inf;
- node->nxt = lst;
- return node;
- }
- void freeLst(Node *fi) {
- Node *aux = NULL;
- while (fi) {
- aux = fi->nxt;
- free(fi);
- fi = aux;
- }
- }
- void printLst(Node *node) {
- for (; node != NULL; node = node->nxt) {
- printf("%d ", node->inf);
- }
- puts("");
- }
- Node *revLst(Node *fi) {
- Node *prev = NULL, *curr = fi, *nxt = NULL;
- while (curr != NULL) {
- nxt = curr->nxt;
- curr->nxt = prev;
- prev = curr;
- curr = nxt;
- }
- return prev;
- }
- int main(void) {
- Node *fi = NULL;
- int n;
- scanf("%d", &n);
- for (int x, i = 0; i < n; i++) {
- scanf("%d", &x);
- fi = newNode(x, fi);//add to start
- }
- fi = revLst(fi);
- printLst(fi);
- freeLst(fi);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement