Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- typedef struct Node {
- int data;
- struct Node *nxt;
- } Node;
- Node* newNode(int data, Node* nxt) {
- Node* node = (Node*)malloc(sizeof(Node));
- if (node == NULL) {
- perror("");
- exit(EXIT_FAILURE);
- }
- node->data = data;
- node->nxt = nxt;
- return node;
- }
- void addToStart(Node** head, int data) {
- Node* node = newNode(data, *head);
- *head = node;
- }
- void printList(Node* head) {
- for (Node* node = head; node; node = node->nxt) {
- printf("%d ", node->data);
- }
- puts("");
- }
- void reverse(Node** head) {
- Node* prev = NULL;
- Node* curr = *head;
- Node* nxt = NULL;
- while (curr) {
- nxt = curr->nxt;
- curr->nxt = prev;
- prev = curr;
- curr = nxt;
- }
- *head = prev;
- }
- int main(void) {
- Node* head = NULL;
- addToStart(&head, 1);
- addToStart(&head, 2);
- addToStart(&head, 3);
- reverse(&head);
- printList(head);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement